diff --git a/src/main/java/ru/bclib/api/tag/TagAPI.java b/src/main/java/ru/bclib/api/tag/TagAPI.java index 7396a7cb..03b7b3f6 100644 --- a/src/main/java/ru/bclib/api/tag/TagAPI.java +++ b/src/main/java/ru/bclib/api/tag/TagAPI.java @@ -1,8 +1,10 @@ package ru.bclib.api.tag; import com.google.common.collect.Maps; -import com.google.common.collect.Sets; + +import net.minecraft.core.DefaultedRegistry; import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.tags.Tag; @@ -14,17 +16,37 @@ import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorPreset; + import ru.bclib.api.biomes.BiomeAPI; import ru.bclib.mixin.common.DiggerItemAccessor; import java.util.Map; import java.util.Set; -import java.util.function.BiConsumer; +import java.util.function.Function; public class TagAPI { - private static final Map> TAGS_BLOCK = Maps.newConcurrentMap(); - private static final Map> TAGS_ITEM = Maps.newConcurrentMap(); - private static final Map> TAGS_BIOME = Maps.newConcurrentMap(); + private static final Map> TYPES = Maps.newHashMap(); + + public static TagType.RegistryBacked BLOCKS = registerType(Registry.BLOCK); + public static TagType.RegistryBacked ITEMS = registerType(Registry.ITEM); + public static TagType.Simple BIOMES = registerType(Registry.BIOME_REGISTRY, "tags/worldgen/biome", b->BiomeAPI.getBiomeID(b)); + + private static TagType.RegistryBacked registerType(DefaultedRegistry registry) { + TagType type = new TagType.RegistryBacked<>(registry); + return (TagType.RegistryBacked)TYPES.computeIfAbsent(type.directory, (dir)->type); + } + + public static TagType.Simple registerType(ResourceKey> registry, String directory, Function locationProvider) { + return (TagType.Simple)TYPES.computeIfAbsent(directory, (dir)->new TagType.Simple<>(registry, dir, locationProvider)); + } + + public static TagType.UnTyped registerType(ResourceKey> registry, String directory) { + return (TagType.UnTyped)TYPES.computeIfAbsent(directory, (dir)->new TagType.UnTyped<>(registry, dir)); + } + + + /** * Get or create {@link TagKey}. @@ -33,6 +55,7 @@ public class TagAPI { * @param id - {@link ResourceLocation} tag id. * @return {@link TagKey}. */ + @Deprecated(forRemoval = true) public static TagKey makeTag(Registry registry, TagLocation id) { return registry .getTagNames() @@ -49,7 +72,7 @@ public class TagAPI { * @return {@link Block} {@link TagKey}. */ public static TagKey makeBiomeTag(String modID, String name) { - return TagKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(modID, name)); + return BIOMES.makeTag(new ResourceLocation(modID, name)); } @@ -61,7 +84,7 @@ public class TagAPI { * @return {@link Block} {@link TagKey}. */ public static TagKey makeBlockTag(String modID, String name) { - return makeTag(Registry.BLOCK, new TagLocation<>(modID, name)); + return BLOCKS.makeTag(new ResourceLocation(modID, name)); } /** @@ -71,7 +94,7 @@ public class TagAPI { * @return {@link Block} {@link TagKey}. */ public static TagKey makeBlockTag(ResourceLocation id) { - return makeTag(Registry.BLOCK, new TagLocation<>(id)); + return BLOCKS.makeTag(id); } /** @@ -82,7 +105,7 @@ public class TagAPI { * @return {@link Item} {@link TagKey}. */ public static TagKey makeItemTag(String modID, String name) { - return makeTag(Registry.ITEM, new TagLocation<>(modID, name)); + return ITEMS.makeTag(new ResourceLocation(modID, name)); } /** @@ -92,7 +115,7 @@ public class TagAPI { * @return {@link Item} {@link TagKey}. */ public static TagKey makeItemTag(ResourceLocation id) { - return makeTag(Registry.ITEM, new TagLocation<>(id)); + return ITEMS.makeTag(id); } /** @@ -103,7 +126,7 @@ public class TagAPI { * @see Fabric Wiki (Tags) */ public static TagKey makeCommonBlockTag(String name) { - return makeTag(Registry.BLOCK, new TagLocation<>("c", name)); + return BLOCKS.makeCommonTag(name); } /** @@ -114,7 +137,7 @@ public class TagAPI { * @see Fabric Wiki (Tags) */ public static TagKey makeCommonItemTag(String name) { - return makeTag(Registry.ITEM, new TagLocation<>("c", name)); + return ITEMS.makeCommonTag(name); } /** @@ -134,10 +157,9 @@ public class TagAPI { * @param biome The {@link Biome} to add tag. */ @SafeVarargs + @Deprecated(forRemoval = true) public static void addBiomeTags(Biome biome, TagLocation... tagIDs) { - for (TagLocation tagID : tagIDs) { - addBiomeTagUntyped(tagID, biome); - } + BIOMES.add(biome, tagIDs); } /** @@ -145,8 +167,10 @@ public class TagAPI { * @param tagID {@link TagLocation} tag ID. * @param biomes array of {@link Biome} to add into tag. */ + + @Deprecated(forRemoval = true) public static void addBiomeTag(TagLocation tagID, Biome... biomes) { - addBiomeTagUntyped(tagID, biomes); + BIOMES.add(tagID, biomes); } /** @@ -155,21 +179,7 @@ public class TagAPI { * @param biomes array of {@link Biome} to add into tag. */ public static void addBiomeTag(TagKey tagID, Biome... biomes) { - addBiomeTagUntyped(tagID.location(), biomes); - } - /** - * Adds one Tag to multiple Biomes. - * @param tagID {@link ResourceLocation} tag ID. - * @param biomes array of {@link Biome} to add into tag. - */ - protected static void addBiomeTagUntyped(ResourceLocation tagID, Biome... biomes) { - Set set = TAGS_BIOME.computeIfAbsent(tagID, k -> Sets.newHashSet()); - for (Biome biome : biomes) { - ResourceLocation id = BiomeAPI.getBiomeID(biome); - if (id != null) { - set.add(id); - } - } + BIOMES.add(tagID, biomes); } @@ -179,10 +189,9 @@ public class TagAPI { * @param block The {@link Block} to add tag. */ @SafeVarargs + @Deprecated(forRemoval = true) public static void addBlockTags(Block block, TagLocation... tagIDs) { - for (TagLocation tagID : tagIDs) { - addBlockTagUntyped(tagID, block); - } + BLOCKS.add(block, tagIDs); } /** @@ -190,8 +199,9 @@ public class TagAPI { * @param tagID {@link TagLocation} tag ID. * @param blocks array of {@link Block} to add into tag. */ + @Deprecated(forRemoval = true) public static void addBlockTag(TagLocation tagID, Block... blocks) { - addBlockTagUntyped(tagID, blocks); + BLOCKS.add(tagID, blocks); } /** @@ -200,22 +210,7 @@ public class TagAPI { * @param blocks array of {@link Block} to add into tag. */ public static void addBlockTag(TagKey tagID, Block... blocks) { - addBlockTagUntyped(tagID.location(), blocks); - } - - /** - * Adds one Tag to multiple Blocks. - * @param tagID {@link ResourceLocation} tag ID. - * @param blocks array of {@link Block} to add into tag. - */ - protected static void addBlockTagUntyped(ResourceLocation tagID, Block... blocks) { - Set set = TAGS_BLOCK.computeIfAbsent(tagID, k -> Sets.newHashSet()); - for (Block block : blocks) { - ResourceLocation id = Registry.BLOCK.getKey(block); - if (id != Registry.BLOCK.getDefaultKey()) { - set.add(id); - } - } + BLOCKS.add(tagID, blocks); } /** @@ -223,11 +218,10 @@ public class TagAPI { * @param tagIDs array of {@link TagLocation} tag IDs. * @param item The {@link Item} to add tag. */ + @Deprecated(forRemoval = true) @SafeVarargs public static void addItemTags(ItemLike item, TagLocation... tagIDs) { - for (TagLocation tagID : tagIDs) { - addItemTagUntyped(tagID, item); - } + ITEMS.add(item.asItem(), tagIDs); } /** @@ -235,8 +229,21 @@ public class TagAPI { * @param tagID {@link TagLocation} tag ID. * @param items array of {@link ItemLike} to add into tag. */ + @Deprecated(forRemoval = true) + public static void addItemTag(TagLocation tagID, Item... items) { + ITEMS.add(tagID, items); + } + + /** + * Adds one Tag to multiple Items. + * @param tagID {@link TagLocation} tag ID. + * @param items array of {@link ItemLike} to add into tag. + */ + @Deprecated(forRemoval = true) public static void addItemTag(TagLocation tagID, ItemLike... items) { - addItemTagUntyped(tagID, items); + for(ItemLike i : items) { + ITEMS.add(i.asItem(), tagID); + } } /** @@ -245,23 +252,19 @@ public class TagAPI { * @param items array of {@link ItemLike} to add into tag. */ public static void addItemTag(TagKey tagID, ItemLike... items) { - addItemTagUntyped(tagID.location(), items); - } - - /** - * Adds one Tag to multiple Items. - * @param tagID {@link ResourceLocation} tag ID. - * @param items array of {@link ItemLike} to add into tag. - */ - protected static void addItemTagUntyped(ResourceLocation tagID, ItemLike... items) { - Set set = TAGS_ITEM.computeIfAbsent(tagID, k -> Sets.newHashSet()); - for (ItemLike item : items) { - ResourceLocation id = Registry.ITEM.getKey(item.asItem()); - if (id != Registry.ITEM.getDefaultKey()) { - set.add(id); - } + for(ItemLike i : items){ + ITEMS.add(i.asItem(), tagID); } } + + /** + * Adds one Tag to multiple Items. + * @param tagID {@link TagKey} tag ID. + * @param items array of {@link ItemLike} to add into tag. + */ + public static void addItemTag(TagKey tagID, Item... items) { + ITEMS.add(tagID, items); + } /** * Automatically called in {@link net.minecraft.tags.TagLoader#loadAndBuild(ResourceManager)}. @@ -274,17 +277,23 @@ public class TagAPI { * @return The {@code tagsMap} Parameter. */ public static Map apply(String directory, Map tagsMap) { - final BiConsumer> consumer; - consumer = (id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids); - if ("tags/blocks".equals(directory)) { - TAGS_BLOCK.forEach(consumer); - } - else if ("tags/items".equals(directory)) { - TAGS_ITEM.forEach(consumer); - } - else if ("tags/worldgen/biome".equals(directory)) { - TAGS_BIOME.forEach(consumer); + TagType type = TYPES.get(directory); + if (type!=null){ + type.forEach((id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids)); } + +// final BiConsumer> consumer; +// consumer = (id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids); +// +// if ("tags/blocks".equals(directory)) { +// TAGS_BLOCK.forEach(consumer); +// } +// else if ("tags/items".equals(directory)) { +// TAGS_ITEM.forEach(consumer); +// } +// else if ("tags/worldgen/biome".equals(directory)) { +// TAGS_BIOME.forEach(consumer); +// } return tagsMap; } diff --git a/src/main/java/ru/bclib/api/tag/TagType.java b/src/main/java/ru/bclib/api/tag/TagType.java new file mode 100644 index 00000000..8ab76b96 --- /dev/null +++ b/src/main/java/ru/bclib/api/tag/TagType.java @@ -0,0 +1,180 @@ +package ru.bclib.api.tag; + +import net.minecraft.core.DefaultedRegistry; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.tags.TagManager; +import net.minecraft.world.level.biome.Biome; + +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class TagType { + public static class RegistryBacked extends Simple{ + private final DefaultedRegistry registry; + + RegistryBacked(DefaultedRegistry registry) { + super( + registry.key(), + TagManager.getTagDir(registry.key()), + (T element) -> { + ResourceLocation id = registry.getKey(element); + if (id != registry.getDefaultKey()) { + return id; + } + return null; + } + ); + this.registry = registry; + } + + @Override + public TagKey makeTag(ResourceLocation id) { + return registry + .getTagNames() + .filter(tagKey -> tagKey.location().equals(id)) + .findAny() + .orElse(TagKey.create(registry.key(), id)); + } + } + + public static class Simple extends TagType { + Simple(ResourceKey> registry, + String directory, + Function locationProvider) { + super(registry, directory, locationProvider); + } + + public void add(TagKey tagID, T... elements) { + super.add(tagID, elements); + } + + public void add(T element, TagKey... tagIDs) { + super.add(element, tagIDs); + } + + @Deprecated(forRemoval = true) + public void add(ResourceLocation tagID, T... elements) { + super.add(tagID, elements); + } + + @Deprecated(forRemoval = true) + public void add(T element, ResourceLocation... tagIDs) { + super.add(element, tagIDs); + } + } + + public static class UnTyped extends TagType { + UnTyped(ResourceKey> registry, + String directory) { + super(registry, directory, (t)->{throw new RuntimeException("Using Untyped TagType with Type-Dependant access. ");}); + } + } + public final String directory; + private final Map> tags = Maps.newConcurrentMap(); + public final ResourceKey> registryKey; + private final Function locationProvider; + + private TagType(ResourceKey> registry, + String directory, + Function locationProvider) { + this.registryKey = registry; + this.directory = directory; + this.locationProvider = locationProvider; + } + + public Set getSetForTag(ResourceLocation tagID) { + return tags.computeIfAbsent(tagID, k -> Sets.newHashSet()); + } + + public Set getSetForTag(TagKey tag) { + return getSetForTag(tag.location()); + } + + + /** + * Get or create a {@link TagKey}. + * + * @param id - {@link ResourceLocation} of the tag; + * @return the corresponding TagKey {@link TagKey}. + */ + public TagKey makeTag(ResourceLocation id) { + return TagKey.create(registryKey, id); + } + + /** + * Get or create a common {@link TagKey} (namespace is 'c'). + * + * @param name - The name of the Tag; + * @return the corresponding TagKey {@link TagKey}. + * @see Fabric Wiki (Tags) + */ + public TagKey makeCommonTag(String name) { + return TagKey.create(registryKey, new ResourceLocation("c", name)); + } + + public void addUntyped(TagKey tagID, ResourceLocation... elements) { + Set set = getSetForTag(tagID); + for (ResourceLocation id : elements) { + if (id != null) { + set.add(id); + } + } + } + + public void addUntyped(ResourceLocation element, TagKey... tagIDs) { + for (TagKey tagID : tagIDs) { + addUntyped(tagID, element); + } + } + + /** + * Adds one Tag to multiple Elements. + * @param tagID {@link TagKey< Biome >} tag ID. + * @param elements array of Elements to add into tag. + */ + protected void add(TagKey tagID, T... elements) { + Set set = getSetForTag(tagID); + for (T element : elements) { + ResourceLocation id = locationProvider.apply(element); + if (id != null) { + set.add(id); + } + } + } + + protected void add(T element, TagKey... tagIDs) { + for (TagKey tagID : tagIDs) { + add(tagID, element); + } + } + + @Deprecated(forRemoval = true) + protected void add(ResourceLocation tagID, T... elements) { + Set set = getSetForTag(tagID); + for (T element : elements) { + ResourceLocation id = locationProvider.apply(element); + if (id != null) { + set.add(id); + } + } + } + + @Deprecated(forRemoval = true) + protected void add(T element, ResourceLocation... tagIDs) { + for (ResourceLocation tagID : tagIDs) { + add(tagID, element); + } + } + + public void forEach(BiConsumer> consumer) { + tags.forEach(consumer); + } +}