From ef1e899589d1cebc33648956dc41da22aff6aa09 Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 4 Dec 2022 20:06:53 +0100 Subject: [PATCH] Generalized Tag-Provider --- .../bclib/api/v3/datagen/TagDataProvider.java | 63 ++++++++++ .../datagen/bclib/tests/TestBiomes.java | 111 ++++++++---------- 2 files changed, 113 insertions(+), 61 deletions(-) create mode 100644 src/main/java/org/betterx/bclib/api/v3/datagen/TagDataProvider.java diff --git a/src/main/java/org/betterx/bclib/api/v3/datagen/TagDataProvider.java b/src/main/java/org/betterx/bclib/api/v3/datagen/TagDataProvider.java new file mode 100644 index 00000000..0be1374e --- /dev/null +++ b/src/main/java/org/betterx/bclib/api/v3/datagen/TagDataProvider.java @@ -0,0 +1,63 @@ +package org.betterx.bclib.api.v3.datagen; + +import org.betterx.worlds.together.tag.v3.TagRegistry; + +import net.minecraft.core.HolderLookup; +import net.minecraft.resources.ResourceLocation; + +import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; +import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import org.jetbrains.annotations.Nullable; + +public class TagDataProvider extends FabricTagProvider { + @Nullable + protected final List modIDs; + + protected final TagRegistry tagRegistry; + + /** + * Constructs a new {@link FabricTagProvider} with the default computed path. + * + *

Common implementations of this class are provided. + * + * @param tagRegistry + * @param modIDs List of ModIDs that are allowed to inlcude data. All Resources in the namespace of the + * mod will be written to the tag. If null all elements get written, and empty list will + * write nothing + * @param output the {@link FabricDataOutput} instance + * @param registriesFuture the backing registry for the tag type + */ + public TagDataProvider( + TagRegistry tagRegistry, + @Nullable List modIDs, + FabricDataOutput output, + CompletableFuture registriesFuture + ) { + super(output, tagRegistry.registryKey, registriesFuture); + this.tagRegistry = tagRegistry; + this.modIDs = modIDs; + } + + protected boolean shouldAdd(ResourceLocation loc) { + return modIDs == null || modIDs.contains(loc.getNamespace()); + } + + @Override + protected void addTags(HolderLookup.Provider arg) { + tagRegistry.forEachTag((tag, locs, tags) -> { + final FabricTagProvider.FabricTagBuilder builder = getOrCreateTagBuilder(tag); + + boolean modTag = shouldAdd(tag.location()); + locs.stream() + .filter(loc -> modTag || this.shouldAdd(loc)) + .forEach(builder::add); + + tags.stream() + .filter(tagKey -> modTag || this.shouldAdd(tagKey.location())) + .forEach(builder::addTag); + }); + } +} diff --git a/src/main/java/org/betterx/datagen/bclib/tests/TestBiomes.java b/src/main/java/org/betterx/datagen/bclib/tests/TestBiomes.java index 6ccd4b9d..bf4286b4 100644 --- a/src/main/java/org/betterx/datagen/bclib/tests/TestBiomes.java +++ b/src/main/java/org/betterx/datagen/bclib/tests/TestBiomes.java @@ -4,11 +4,12 @@ import org.betterx.bclib.BCLib; import org.betterx.bclib.api.v2.levelgen.biomes.BCLBiome; import org.betterx.bclib.api.v2.levelgen.biomes.BCLBiomeBuilder; import org.betterx.bclib.api.v2.levelgen.biomes.BCLBiomeContainer; +import org.betterx.bclib.api.v3.datagen.TagDataProvider; import org.betterx.datagen.bclib.BCLibDatagen; +import org.betterx.worlds.together.WorldsTogether; import org.betterx.worlds.together.tag.v3.TagManager; import net.minecraft.core.HolderLookup; -import net.minecraft.core.registries.Registries; import net.minecraft.data.worldgen.BootstapContext; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.block.Blocks; @@ -16,9 +17,10 @@ import net.minecraft.world.level.block.Blocks; import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider; +import java.util.List; import java.util.concurrent.CompletableFuture; -public class TestBiomes extends FabricTagProvider { +public class TestBiomes extends TagDataProvider { static BCLBiomeContainer THE_YELLOW = BCLBiomeBuilder .start(BCLib.makeID("the_yellow")) .precipitation(Biome.Precipitation.NONE) @@ -49,6 +51,46 @@ public class TestBiomes extends FabricTagProvider { .endLandBiome() .build(); + static BCLBiomeContainer THE_GRAY = BCLBiomeBuilder + .start(BCLib.makeID("the_gray")) + .precipitation(Biome.Precipitation.NONE) + .temperature(1.0f) + .wetness(1.0f) + .fogColor(0xFFFFFF) + .waterColor(0x777777) + .waterFogColor(0xFFFFFF) + .skyColor(0xAAAAAA) + .addNetherClimateParamater(-1, 1) + .surface(Blocks.GRAY_CONCRETE) + .endVoidBiome() + .build(); + static BCLBiomeContainer THE_ORANGE = BCLBiomeBuilder + .start(BCLib.makeID("the_orange")) + .precipitation(Biome.Precipitation.NONE) + .temperature(1.0f) + .wetness(1.0f) + .fogColor(0xFF7700) + .waterColor(0x773300) + .waterFogColor(0xFF7700) + .skyColor(0xAA7700) + .addNetherClimateParamater(-1, 1.1f) + .surface(Blocks.ORANGE_CONCRETE) + .netherBiome() + .build(); + static BCLBiomeContainer THE_PURPLE = BCLBiomeBuilder + .start(BCLib.makeID("the_purple")) + .precipitation(Biome.Precipitation.NONE) + .temperature(1.0f) + .wetness(1.0f) + .fogColor(0xFF00FF) + .waterColor(0x770077) + .waterFogColor(0xFF00FF) + .skyColor(0xAA00AA) + .addNetherClimateParamater(-1.1f, 1) + .surface(Blocks.PURPLE_CONCRETE) + .netherBiome() + .build(); + /** * Constructs a new {@link FabricTagProvider} with the default computed path. * @@ -61,71 +103,18 @@ public class TestBiomes extends FabricTagProvider { FabricDataOutput output, CompletableFuture registriesFuture ) { - super(output, Registries.BIOME, registriesFuture); + super(TagManager.BIOMES, List.of(BCLib.MOD_ID, WorldsTogether.MOD_ID), output, registriesFuture); } public static void bootstrap(BootstapContext bootstrapContext) { - BCLib.LOGGER.info("Bootstrap Biomes"); if (BCLibDatagen.ADD_TESTS && BCLib.isDevEnvironment()) { + BCLib.LOGGER.info("Bootstrap Biomes"); + THE_YELLOW = THE_YELLOW.register(bootstrapContext); THE_BLUE = THE_BLUE.register(bootstrapContext); - - BCLBiome theGray = BCLBiomeBuilder - .start(BCLib.makeID("the_gray")) - .precipitation(Biome.Precipitation.NONE) - .temperature(1.0f) - .wetness(1.0f) - .fogColor(0xFFFFFF) - .waterColor(0x777777) - .waterFogColor(0xFFFFFF) - .skyColor(0xAAAAAA) - .addNetherClimateParamater(-1, 1) - .surface(Blocks.GRAY_CONCRETE) - .endVoidBiome() - .build() - .register(bootstrapContext).biome(); - - BCLBiome theOrange = BCLBiomeBuilder - .start(BCLib.makeID("the_orange")) - .precipitation(Biome.Precipitation.NONE) - .temperature(1.0f) - .wetness(1.0f) - .fogColor(0xFF7700) - .waterColor(0x773300) - .waterFogColor(0xFF7700) - .skyColor(0xAA7700) - .addNetherClimateParamater(-1, 1.1f) - .surface(Blocks.ORANGE_CONCRETE) - .netherBiome() - .build() - .register(bootstrapContext).biome(); - - BCLBiome thePurple = BCLBiomeBuilder - .start(BCLib.makeID("the_purple")) - .precipitation(Biome.Precipitation.NONE) - .temperature(1.0f) - .wetness(1.0f) - .fogColor(0xFF00FF) - .waterColor(0x770077) - .waterFogColor(0xFF00FF) - .skyColor(0xAA00AA) - .addNetherClimateParamater(-1.1f, 1) - .surface(Blocks.PURPLE_CONCRETE) - .netherBiome() - .build() - .register(bootstrapContext).biome(); + THE_GRAY = THE_GRAY.register(bootstrapContext); + THE_ORANGE = THE_ORANGE.register(bootstrapContext); + THE_PURPLE = THE_PURPLE.register(bootstrapContext); } } - - @Override - protected void addTags(HolderLookup.Provider arg) { - TagManager.BIOMES.forEachTag((tag, locs, tags) -> { - final FabricTagProvider.FabricTagBuilder builder = getOrCreateTagBuilder(tag); - boolean modTag = tag.location().getNamespace().equals(BCLib.MOD_ID); - locs.stream().filter(l -> modTag || l.getNamespace().equals(BCLib.MOD_ID)).forEach(builder::add); - tags.stream() - .filter(t -> modTag || t.location().getNamespace().equals(BCLib.MOD_ID)) - .forEach(builder::addTag); - }); - } }