Skip to content

Commit

Permalink
Fix data generation for custom dynamic registry (#3216)
Browse files Browse the repository at this point in the history
* Fix datagen for custom dynamic registry

* Test datagen for custom dynamic registry

* Check if the dynamic registry is registered with Fabric API

* Cleanup testmod

* Don't apply to registries not added using the fabric api.

---------

Co-authored-by: modmuss50 <[email protected]>
Signed-off-by: modmuss50 <[email protected]>
  • Loading branch information
apple502j and modmuss50 committed Jul 25, 2023
1 parent 3a1ea2f commit 26f09d4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.event.registry.DynamicRegistries;
import net.fabricmc.fabric.impl.registry.sync.DynamicRegistriesImpl;

/**
* A provider to help with data-generation of dynamic registry objects,
Expand Down Expand Up @@ -220,7 +221,9 @@ public CompletableFuture<?> run(DataWriter writer) {

private <T> CompletableFuture<?> writeRegistryEntries(DataWriter writer, RegistryOps<JsonElement> ops, RegistryEntries<T> entries) {
final RegistryKey<? extends Registry<T>> registry = entries.registry;
final DataOutput.PathResolver pathResolver = output.getResolver(DataOutput.OutputType.DATA_PACK, registry.getValue().getPath());
final boolean shouldOmitNamespace = registry.getValue().getNamespace().equals(Identifier.DEFAULT_NAMESPACE) || !DynamicRegistriesImpl.FABRIC_DYNAMIC_REGISTRY_KEYS.contains(registry);
final String directoryName = shouldOmitNamespace ? registry.getValue().getPath() : registry.getValue().getNamespace() + "/" + registry.getValue().getPath();
final DataOutput.PathResolver pathResolver = output.getResolver(DataOutput.OutputType.DATA_PACK, directoryName);
final List<CompletableFuture<?>> futures = new ArrayList<>();

for (Map.Entry<RegistryKey<T>, T> entry : entries.entries.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +32,10 @@
import net.minecraft.util.Identifier;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;

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;

Expand All @@ -48,6 +53,13 @@ public class DataGeneratorTestContent implements ModInitializer {
.displayName(Text.translatable("fabric-data-gen-api-v1-testmod.simple_item_group"))
.build();

public static final RegistryKey<Registry<TestDatagenObject>> TEST_DATAGEN_DYNAMIC_REGISTRY_KEY =
RegistryKey.ofRegistry(new Identifier("fabric", "test_datagen_dynamic"));
public static final RegistryKey<TestDatagenObject> TEST_DYNAMIC_REGISTRY_ITEM_KEY = RegistryKey.of(
TEST_DATAGEN_DYNAMIC_REGISTRY_KEY,
new Identifier(MOD_ID, "tiny_potato")
);

@Override
public void onInitialize() {
SIMPLE_BLOCK = createBlock("simple_block", true, AbstractBlock.Settings.of(Material.STONE));
Expand All @@ -57,6 +69,7 @@ public void onInitialize() {
BLOCK_THAT_DROPS_NOTHING = createBlock("block_that_drops_nothing", false, AbstractBlock.Settings.of(Material.STONE).dropsNothing());

ItemGroupEvents.modifyEntriesEvent(SIMPLE_ITEM_GROUP).register(entries -> entries.add(SIMPLE_BLOCK));
DynamicRegistries.register(TEST_DATAGEN_DYNAMIC_REGISTRY_KEY, TestDatagenObject.CODEC);
}

private static Block createBlock(String name, boolean hasItem, AbstractBlock.Settings settings) {
Expand All @@ -69,4 +82,10 @@ private static Block createBlock(String name, boolean hasItem, AbstractBlock.Set

return block;
}

public record TestDatagenObject(String value) {
public static final Codec<TestDatagenObject> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("value").forGetter(TestDatagenObject::value)
).apply(instance, TestDatagenObject::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_DATAGEN_DYNAMIC_REGISTRY_KEY;
import static net.fabricmc.fabric.test.datagen.DataGeneratorTestContent.TEST_DYNAMIC_REGISTRY_ITEM_KEY;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -55,6 +57,8 @@
import net.minecraft.loot.provider.number.ConstantLootNumberProvider;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.book.RecipeCategory;
import net.minecraft.registry.Registerable;
import net.minecraft.registry.RegistryBuilder;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.tag.BlockTags;
Expand All @@ -71,6 +75,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;
Expand All @@ -97,6 +102,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));
Expand All @@ -114,6 +120,18 @@ public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
}
}

@Override
public void buildRegistry(RegistryBuilder registryBuilder) {
registryBuilder.addRegistry(
TEST_DATAGEN_DYNAMIC_REGISTRY_KEY,
this::bootstrapTestDatagenRegistry
);
}

private void bootstrapTestDatagenRegistry(Registerable<DataGeneratorTestContent.TestDatagenObject> registerable) {
registerable.register(TEST_DYNAMIC_REGISTRY_ITEM_KEY, new DataGeneratorTestContent.TestDatagenObject(":tiny_potato:"));
}

private static class TestRecipeProvider extends FabricRecipeProvider {
private TestRecipeProvider(FabricDataOutput output) {
super(output);
Expand Down Expand Up @@ -364,4 +382,24 @@ public void accept(BiConsumer<Identifier, LootTable.Builder> 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<RegistryWrapper.WrapperLookup> registriesFuture) {
super(output, registriesFuture);
}

@Override
protected void configure(RegistryWrapper.WrapperLookup registries, Entries entries) {
entries.add(registries.getWrapperOrThrow(TEST_DATAGEN_DYNAMIC_REGISTRY_KEY), TEST_DYNAMIC_REGISTRY_ITEM_KEY);
}

@Override
public String getName() {
return "Test Dynamic Registry";
}
}
}

0 comments on commit 26f09d4

Please sign in to comment.