Skip to content

Commit

Permalink
Port fabric-data-generation-api-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jun 23, 2024
1 parent 3bffb18 commit a0167ea
Show file tree
Hide file tree
Showing 20 changed files with 189 additions and 306 deletions.
8 changes: 8 additions & 0 deletions buildSrc/src/main/kotlin/ffapi.neo-entrypoint.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ abstract class GenerateForgeModEntrypoint : DefaultTask() {
public $className(net.neoforged.bus.api.IEventBus bus) {
$testEnvSetup$entrypointInitializers
${addGametests(modMetadata)}
${addDatagen(modMetadata)}
}
}
""".trimIndent()
Expand All @@ -133,6 +134,13 @@ abstract class GenerateForgeModEntrypoint : DefaultTask() {
});"""
}

private fun addDatagen(modMetadata: LoaderModMetadata): String {
val entrypoints = modMetadata.getEntrypoints("fabric-datagen").map(EntrypointMetadata::getValue).takeIf { it.isNotEmpty() } ?: return ""
return entrypoints.joinToString(separator = "\n ") {
"net.fabricmc.fabric.impl.datagen.FabricDataGenHelper.registerDatagenEntrypoint(MOD_ID, new $it());"
}
}

private fun packageNameForEntryPoint(modid: String): String {
val uniqueName = projectNamePattern.find(modid)?.groups?.get(1)?.value
?: throw RuntimeException("Unable to determine generated package name for mod $modid")
Expand Down
21 changes: 5 additions & 16 deletions fabric-data-generation-api-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,14 @@ loom {

runs {
datagen {
inherit testmodServer
data()
name "Data Generation"
vmArg "-Dfabric-api.datagen"
vmArg "-Dfabric-api.datagen.output-dir=${file("src/testmod/generated")}"
vmArg "-Dfabric-api.datagen.strict-validation"

programArgs "--mod", "fabric_data_generation_api_v1", "--all", "--output", file("src/testmod/generated").absolutePath

ideConfigGenerated = true
runDir "build/datagen"
}
datagenClient {
client()
name "Data Generation Client"
vmArg "-Dfabric-api.datagen"
vmArg "-Dfabric-api.datagen.output-dir=${file("src/testmod/generated")}"
vmArg "-Dfabric-api.datagen.strict-validation"

ideConfigGenerated = true
runDir "build/datagen"
source sourceSets.testmodClient
source sourceSets.testmod
}
}
}
Expand All @@ -73,7 +62,7 @@ import java.util.zip.ZipFile

task generateAccessWidener() {
doLast {
File inputJar = loom.namedMinecraftProvider.parentMinecraftProvider.commonJar.path.toFile()
File inputJar = loom.namedMinecraftProvider.parentMinecraftProvider.mergedJar.path.toFile()
String accessWidener = "accessWidener\tv2\tnamed\n"
accessWidener += "\n"
accessWidener += "# DO NOT EDIT BY HAND! This file is generated automatically.\n"
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

import com.mojang.datafixers.util.Pair;
import net.fabricmc.fabric.impl.datagen.DataGeneratorExtension;
import org.jetbrains.annotations.ApiStatus;

import net.minecraft.SharedConstants;
Expand All @@ -36,14 +38,16 @@
* An extension to vanilla's {@link DataGenerator} providing mod specific data, and helper functions.
*/
public final class FabricDataGenerator extends DataGenerator {
private final DataGenerator parent;
private final ModContainer modContainer;
private final boolean strictValidation;
private final FabricDataOutput fabricOutput;
private final CompletableFuture<HolderLookup.Provider> registriesFuture;

@ApiStatus.Internal
public FabricDataGenerator(Path output, ModContainer mod, boolean strictValidation, CompletableFuture<HolderLookup.Provider> registriesFuture) {
public FabricDataGenerator(DataGenerator parent, Path output, ModContainer mod, boolean strictValidation, CompletableFuture<HolderLookup.Provider> registriesFuture) {
super(output, SharedConstants.getCurrentVersion(), true);
this.parent = parent;
this.modContainer = Objects.requireNonNull(mod);
this.strictValidation = strictValidation;
this.fabricOutput = new FabricDataOutput(mod, output, strictValidation);
Expand All @@ -54,7 +58,8 @@ public FabricDataGenerator(Path output, ModContainer mod, boolean strictValidati
* Create a default {@link Pack} instance for generating a mod's data.
*/
public Pack createPack() {
return new Pack(true, modContainer.getMetadata().getName(), this.fabricOutput);
DataGenerator.PackGenerator parentPack = ((DataGeneratorExtension) this.parent).createPack(modContainer.getMetadata().getName(), this.fabricOutput);
return new Pack(parentPack);
}

/**
Expand All @@ -66,8 +71,8 @@ public Pack createPack() {
* in the identifier.
*/
public Pack createBuiltinResourcePack(ResourceLocation id) {
Path path = this.vanillaPackOutput.getOutputFolder().resolve("resourcepacks").resolve(id.getPath());
return new Pack(true, id.toString(), new FabricDataOutput(modContainer, path, strictValidation));
Pair<DataGenerator.PackGenerator, Path> parentPack = ((DataGeneratorExtension) this.parent).createBuiltinResourcePack(true, id, modContainer, strictValidation);
return new Pack(parentPack.getFirst());
}

/**
Expand Down Expand Up @@ -133,8 +138,11 @@ public DataGenerator.PackGenerator getBuiltinDatapack(boolean shouldRun, String
* Represents a pack of generated data (i.e. data pack or resource pack). Providers are added to a pack.
*/
public final class Pack extends DataGenerator.PackGenerator {
private Pack(boolean shouldRun, String name, FabricDataOutput output) {
super(shouldRun, name, output);
private final DataGenerator.PackGenerator parent;

private Pack(DataGenerator.PackGenerator parent) {
super(false, null, null);
this.parent = parent;
}

/**
Expand All @@ -143,7 +151,7 @@ private Pack(boolean shouldRun, String name, FabricDataOutput output) {
* @return the {@link DataProvider}
*/
public <T extends DataProvider> T addProvider(Factory<T> factory) {
return super.addProvider(output -> factory.create((FabricDataOutput) output));
return this.parent.addProvider(output -> factory.create((FabricDataOutput) output));
}

/**
Expand All @@ -153,7 +161,12 @@ public <T extends DataProvider> T addProvider(Factory<T> factory) {
* @return the {@link DataProvider}
*/
public <T extends DataProvider> T addProvider(RegistryDependentFactory<T> factory) {
return super.addProvider(output -> factory.create((FabricDataOutput) output, registriesFuture));
return this.parent.addProvider(output -> factory.create((FabricDataOutput) output, registriesFuture));
}

@Override
public <T extends DataProvider> T addProvider(DataProvider.Factory<T> factory) {
return this.parent.addProvider(factory);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import net.neoforged.neoforge.common.conditions.ICondition;
import org.jetbrains.annotations.Nullable;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementHolder;
Expand Down Expand Up @@ -71,9 +72,9 @@ protected RecipeOutput withConditions(RecipeOutput exporter, ResourceCondition..
Preconditions.checkArgument(conditions.length > 0, "Must add at least one condition.");
return new RecipeOutput() {
@Override
public void accept(ResourceLocation identifier, Recipe<?> recipe, @Nullable AdvancementHolder advancementEntry) {
public void accept(ResourceLocation identifier, Recipe<?> recipe, @Nullable AdvancementHolder advancementEntry, ICondition... recipeConditions) {
FabricDataGenHelper.addConditions(recipe, conditions);
exporter.accept(identifier, recipe, advancementEntry);
exporter.accept(identifier, recipe, advancementEntry, recipeConditions);
}

@Override
Expand All @@ -89,7 +90,7 @@ public CompletableFuture<?> run(CachedOutput writer, HolderLookup.Provider wrapp
List<CompletableFuture<?>> list = new ArrayList<>();
buildRecipes(new RecipeOutput() {
@Override
public void accept(ResourceLocation recipeId, Recipe<?> recipe, @Nullable AdvancementHolder advancement) {
public void accept(ResourceLocation recipeId, Recipe<?> recipe, @Nullable AdvancementHolder advancement, ICondition... recipeConditions) {
ResourceLocation identifier = getRecipeIdentifier(recipeId);

if (!generatedRecipes.add(identifier)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public final class FabricTagBuilder extends TagAppender<T> {
private final TagsProvider.TagAppender<T> parent;

private FabricTagBuilder(TagAppender<T> parent) {
super(parent.builder);
super(parent.builder, "fabric");
this.parent = parent;
}

Expand All @@ -245,7 +245,7 @@ private FabricTagBuilder(TagAppender<T> parent) {
* @return the {@link FabricTagBuilder} instance
*/
public FabricTagBuilder setReplace(boolean replace) {
((net.fabricmc.fabric.impl.datagen.FabricTagBuilder) builder).fabric_setReplace(replace);
replace(replace);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@

package net.fabricmc.fabric.impl.datagen;

public interface FabricTagBuilder {
void fabric_setReplace(boolean replace);
import com.mojang.datafixers.util.Pair;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;

boolean fabric_isReplaced();
import java.nio.file.Path;

public interface DataGeneratorExtension {
DataGenerator.PackGenerator createPack(String name, PackOutput output);

Pair<DataGenerator.PackGenerator, Path> createBuiltinResourcePack(boolean shouldRun, ResourceLocation packName, ModContainer modInfo, boolean strictValidation);
}
Loading

0 comments on commit a0167ea

Please sign in to comment.