Skip to content

Commit

Permalink
Test datagen for custom dynamic registry
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Jul 23, 2023
1 parent 515366e commit 0d968d1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
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 @@ -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;

Expand All @@ -46,6 +50,14 @@ public class DataGeneratorTestContent implements ModInitializer {

public static final RegistryKey<ItemGroup> SIMPLE_ITEM_GROUP = RegistryKey.of(RegistryKeys.ITEM_GROUP, new Identifier(MOD_ID, "simple"));

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")
);
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());
Expand All @@ -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) {
Expand All @@ -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<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_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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -364,4 +377,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(TEST_DYNAMIC_REGISTRY_ITEM_KEY, TEST_DYNAMIC_REGISTRY_ITEM);
}

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

0 comments on commit 0d968d1

Please sign in to comment.