diff --git a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestContent.java b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestContent.java index 0edda44319..fde789afd3 100644 --- a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestContent.java +++ b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestContent.java @@ -16,6 +16,9 @@ package net.fabricmc.fabric.test.datagen; +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.Blocks; @@ -32,6 +35,7 @@ import net.minecraft.util.Identifier; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.registry.DynamicRegistries; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; @@ -46,6 +50,14 @@ public class DataGeneratorTestContent implements ModInitializer { public static final RegistryKey SIMPLE_ITEM_GROUP = RegistryKey.of(RegistryKeys.ITEM_GROUP, new Identifier(MOD_ID, "simple")); + public static final RegistryKey> TEST_DATAGEN_DYNAMIC_REGISTRY_KEY = + RegistryKey.ofRegistry(new Identifier("fabric", "test_datagen_dynamic")); + public static final RegistryKey TEST_DYNAMIC_REGISTRY_ITEM_KEY = RegistryKey.of( + TEST_DATAGEN_DYNAMIC_REGISTRY_KEY, + new Identifier(MOD_ID, "tiny_potato") + ); + public static final TestDatagenObject TEST_DYNAMIC_REGISTRY_ITEM = new TestDatagenObject(":tiny_potato:"); + @Override public void onInitialize() { SIMPLE_BLOCK = createBlock("simple_block", true, AbstractBlock.Settings.create()); @@ -60,6 +72,8 @@ public void onInitialize() { .icon(() -> new ItemStack(Items.DIAMOND_PICKAXE)) .displayName(Text.translatable("fabric-data-gen-api-v1-testmod.simple_item_group")) .build()); + + DynamicRegistries.register(TEST_DATAGEN_DYNAMIC_REGISTRY_KEY, TestDatagenObject.CODEC); } private static Block createBlock(String name, boolean hasItem, AbstractBlock.Settings settings) { @@ -72,4 +86,10 @@ private static Block createBlock(String name, boolean hasItem, AbstractBlock.Set return block; } + + public record TestDatagenObject(String value) { + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.STRING.fieldOf("value").forGetter(TestDatagenObject::value) + ).apply(instance, TestDatagenObject::new)); + } } diff --git a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java index e990661288..e889511c59 100644 --- a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java +++ b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java @@ -23,6 +23,8 @@ import static net.fabricmc.fabric.test.datagen.DataGeneratorTestContent.MOD_ID; import static net.fabricmc.fabric.test.datagen.DataGeneratorTestContent.SIMPLE_BLOCK; import static net.fabricmc.fabric.test.datagen.DataGeneratorTestContent.SIMPLE_ITEM_GROUP; +import static net.fabricmc.fabric.test.datagen.DataGeneratorTestContent.TEST_DYNAMIC_REGISTRY_ITEM; +import static net.fabricmc.fabric.test.datagen.DataGeneratorTestContent.TEST_DYNAMIC_REGISTRY_ITEM_KEY; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -55,6 +57,7 @@ import net.minecraft.loot.provider.number.ConstantLootNumberProvider; import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.book.RecipeCategory; +import net.minecraft.registry.RegistryBuilder; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.RegistryWrapper; import net.minecraft.registry.tag.BlockTags; @@ -71,6 +74,7 @@ import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; import net.fabricmc.fabric.api.datagen.v1.provider.FabricAdvancementProvider; import net.fabricmc.fabric.api.datagen.v1.provider.FabricBlockLootTableProvider; +import net.fabricmc.fabric.api.datagen.v1.provider.FabricDynamicRegistryProvider; import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider; import net.fabricmc.fabric.api.datagen.v1.provider.FabricModelProvider; import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider; @@ -97,6 +101,7 @@ public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) { pack.addProvider(TestBarterLootTableProvider::new); pack.addProvider(ExistingEnglishLangProvider::new); pack.addProvider(JapaneseLangProvider::new); + pack.addProvider(TestDynamicRegistryProvider::new); TestBlockTagProvider blockTagProvider = pack.addProvider(TestBlockTagProvider::new); pack.addProvider((output, registries) -> new TestItemTagProvider(output, registries, blockTagProvider)); @@ -114,6 +119,14 @@ public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) { } } + @Override + public void buildRegistry(RegistryBuilder registryBuilder) { + registryBuilder.addRegistry( + DataGeneratorTestContent.TEST_DATAGEN_DYNAMIC_REGISTRY_KEY, + (registerable) -> { } + ); + } + private static class TestRecipeProvider extends FabricRecipeProvider { private TestRecipeProvider(FabricDataOutput output) { super(output); @@ -364,4 +377,24 @@ public void accept(BiConsumer consumer) { ); } } + + /** + * Tests generating files for a custom dynamic registry. + * Note that Biome API testmod provides the test for vanilla dynamic registries. + */ + private static class TestDynamicRegistryProvider extends FabricDynamicRegistryProvider { + TestDynamicRegistryProvider(FabricDataOutput output, CompletableFuture registriesFuture) { + super(output, registriesFuture); + } + + @Override + protected void configure(RegistryWrapper.WrapperLookup registries, Entries entries) { + entries.add(TEST_DYNAMIC_REGISTRY_ITEM_KEY, TEST_DYNAMIC_REGISTRY_ITEM); + } + + @Override + public String getName() { + return "Test Dynamic Registry"; + } + } }