From e5e948ef4f084d668bb020af428a799e9f05c335 Mon Sep 17 00:00:00 2001 From: paulevsGitch Date: Tue, 20 Jul 2021 04:54:41 +0300 Subject: [PATCH] Moved TagHelper inside TagAPI --- src/main/java/ru/bclib/api/TagAPI.java | 151 ++++++++++++++++-- .../ru/bclib/mixin/common/AnvilMenuMixin.java | 1 - .../ru/bclib/mixin/common/TagLoaderMixin.java | 4 +- .../java/ru/bclib/registry/ItemsRegistry.java | 12 +- src/main/java/ru/bclib/util/TagHelper.java | 138 ---------------- 5 files changed, 146 insertions(+), 160 deletions(-) delete mode 100644 src/main/java/ru/bclib/util/TagHelper.java diff --git a/src/main/java/ru/bclib/api/TagAPI.java b/src/main/java/ru/bclib/api/TagAPI.java index 6ca55627..622c4834 100644 --- a/src/main/java/ru/bclib/api/TagAPI.java +++ b/src/main/java/ru/bclib/api/TagAPI.java @@ -1,7 +1,11 @@ package ru.bclib.api; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import net.fabricmc.fabric.api.tag.TagRegistry; +import net.minecraft.core.Registry; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.tags.BlockTags; import net.minecraft.tags.ItemTags; import net.minecraft.tags.Tag; @@ -9,14 +13,19 @@ import net.minecraft.tags.Tag.Named; import net.minecraft.tags.TagCollection; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; +import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import ru.bclib.BCLib; -import ru.bclib.util.TagHelper; +import java.util.Map; +import java.util.Set; import java.util.function.Supplier; public class TagAPI { + private static final Map> TAGS_BLOCK = Maps.newConcurrentMap(); + private static final Map> TAGS_ITEM = Maps.newConcurrentMap(); + // Block Tags public static final Tag.Named BOOKSHELVES = makeCommonBlockTag("bookshelves"); public static final Tag.Named GEN_TERRAIN = makeBlockTag(BCLib.MOD_ID, "gen_terrain"); @@ -114,8 +123,8 @@ public class TagAPI { * @param block - {@link Block}. */ public static void addNetherGround(Block block) { - TagHelper.addTag(NETHER_GROUND, block); - TagHelper.addTag(GEN_TERRAIN, block); + addTag(NETHER_GROUND, block); + addTag(GEN_TERRAIN, block); } /** @@ -124,21 +133,137 @@ public class TagAPI { * @param block - {@link Block}. */ public static void addEndGround(Block block) { - TagHelper.addTag(GEN_TERRAIN, block); - TagHelper.addTag(END_GROUND, block); + addTag(GEN_TERRAIN, block); + addTag(END_GROUND, block); } /** * Initializes basic tags. Should be called only in BCLib main class. */ public static void init() { - TagHelper.addTag(BOOKSHELVES, Blocks.BOOKSHELF); - TagHelper.addTag(GEN_TERRAIN, Blocks.END_STONE, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL); - TagHelper.addTag(NETHER_GROUND, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL); - TagHelper.addTag(END_GROUND, Blocks.END_STONE); - TagHelper.addTag(BLOCK_CHEST, Blocks.CHEST); - TagHelper.addTag(ITEM_CHEST, Items.CHEST); - TagHelper.addTag(IRON_INGOTS, Items.IRON_INGOT); - TagHelper.addTag(FURNACES, Blocks.FURNACE); + addTag(BOOKSHELVES, Blocks.BOOKSHELF); + addTag(GEN_TERRAIN, Blocks.END_STONE, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL); + addTag(NETHER_GROUND, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL); + addTag(END_GROUND, Blocks.END_STONE); + addTag(BLOCK_CHEST, Blocks.CHEST); + addTag(ITEM_CHEST, Items.CHEST); + addTag(IRON_INGOTS, Items.IRON_INGOT); + addTag(FURNACES, Blocks.FURNACE); + } + + /** + * Adds one Tag to multiple Blocks. + *

+ * Example: + *

{@code  Tag.Named DIMENSION_STONE = makeBlockTag("mymod", "dim_stone");
+	 * addTag(DIMENSION_STONE, Blocks.END_STONE, Blocks.NETHERRACK);}
+ *

+ * The call will reserve the Tag. The Tag is added to the blocks once + * {@link #apply(String, Map)} was executed. + * + * @param tag The new Tag + * @param blocks One or more blocks that should receive the Tag. + */ + public static void addTag(Tag.Named tag, Block... blocks) { + ResourceLocation tagID = tag.getName(); + 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); + } + } + } + + /** + * Adds one Tag to multiple Items. + *

+ * Example: + *

{@code  Tag.Named METALS = makeBlockTag("mymod", "metals");
+	 * addTag(METALS, Items.IRON_INGOT, Items.GOLD_INGOT, Items.COPPER_INGOT);}
+ *

+ * The call will reserve the Tag. The Tag is added to the items once + * {@link #apply(String, Map)} was executed. + * + * @param tag The new Tag + * @param items One or more item that should receive the Tag. + */ + public static void addTag(Tag.Named tag, ItemLike... items) { + ResourceLocation tagID = tag.getName(); + 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); + } + } + } + + /** + * Adds multiple Tags to one Item. + *

+ * The call will reserve the Tags. The Tags are added to the Item once + * * {@link #apply(String, Map)} was executed. + * + * @param item The Item that will receive all Tags + * @param tags One or more Tags + */ + @SafeVarargs + public static void addTags(ItemLike item, Tag.Named... tags) { + for (Tag.Named tag : tags) { + addTag(tag, item); + } + } + + /** + * Adds multiple Tags to one Block. + *

+ * The call will reserve the Tags. The Tags are added to the Block once + * * {@link #apply(String, Map)} was executed. + * + * @param block The Block that will receive all Tags + * @param tags One or more Tags + */ + @SafeVarargs + public static void addTags(Block block, Tag.Named... tags) { + for (Tag.Named tag : tags) { + addTag(tag, block); + } + } + + /** + * Adds all {@code ids} to the {@code builder}. + * + * @param builder + * @param ids + * @return The Builder passed as {@code builder}. + */ + public static Tag.Builder apply(Tag.Builder builder, Set ids) { + ids.forEach(value -> builder.addElement(value, "Better End Code")); + return builder; + } + + /** + * Automatically called in {@link net.minecraft.tags.TagLoader#loadAndBuild(ResourceManager)}. + *

+ * In most cases there is no need to call this Method manually. + * + * @param directory The name of the Tag-directory. Should be either "tags/blocks" or + * "tags/items". + * @param tagsMap The map that will hold the registered Tags + * @return The {@code tagsMap} Parameter. + */ + public static Map apply(String directory, Map tagsMap) { + Map> endTags = null; + if ("tags/blocks".equals(directory)) { + endTags = TAGS_BLOCK; + } + else if ("tags/items".equals(directory)) { + endTags = TAGS_ITEM; + } + if (endTags != null) { + endTags.forEach((id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids)); + } + return tagsMap; } } diff --git a/src/main/java/ru/bclib/mixin/common/AnvilMenuMixin.java b/src/main/java/ru/bclib/mixin/common/AnvilMenuMixin.java index 9417f9b0..a8391228 100644 --- a/src/main/java/ru/bclib/mixin/common/AnvilMenuMixin.java +++ b/src/main/java/ru/bclib/mixin/common/AnvilMenuMixin.java @@ -9,7 +9,6 @@ import net.minecraft.world.inventory.DataSlot; import net.minecraft.world.inventory.ItemCombinerMenu; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.AnvilBlock; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; diff --git a/src/main/java/ru/bclib/mixin/common/TagLoaderMixin.java b/src/main/java/ru/bclib/mixin/common/TagLoaderMixin.java index 0fa5be52..4d89c067 100644 --- a/src/main/java/ru/bclib/mixin/common/TagLoaderMixin.java +++ b/src/main/java/ru/bclib/mixin/common/TagLoaderMixin.java @@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyArg; -import ru.bclib.util.TagHelper; +import ru.bclib.api.TagAPI; import java.util.Map; @@ -18,6 +18,6 @@ public class TagLoaderMixin { @ModifyArg(method = "loadAndBuild", at = @At(value = "INVOKE", target = "Lnet/minecraft/tags/TagLoader;build(Ljava/util/Map;)Lnet/minecraft/tags/TagCollection;")) public Map be_modifyTags(Map tagsMap) { - return TagHelper.apply(directory, tagsMap); + return TagAPI.apply(directory, tagsMap); } } diff --git a/src/main/java/ru/bclib/registry/ItemsRegistry.java b/src/main/java/ru/bclib/registry/ItemsRegistry.java index 2f491b49..dde6333b 100644 --- a/src/main/java/ru/bclib/registry/ItemsRegistry.java +++ b/src/main/java/ru/bclib/registry/ItemsRegistry.java @@ -27,7 +27,7 @@ import ru.bclib.items.ModelProviderItem; import ru.bclib.items.tool.BaseAxeItem; import ru.bclib.items.tool.BaseHoeItem; import ru.bclib.items.tool.BasePickaxeItem; -import ru.bclib.util.TagHelper; +import ru.bclib.api.TagAPI; public abstract class ItemsRegistry extends BaseRegistry { @@ -54,19 +54,19 @@ public abstract class ItemsRegistry extends BaseRegistry { registerItem(id, item, BaseRegistry.getModItems(id.getNamespace())); if (item instanceof ShovelItem) { - TagHelper.addTag((Tag.Named) FabricToolTags.SHOVELS, item); + TagAPI.addTag((Tag.Named) FabricToolTags.SHOVELS, item); } else if (item instanceof SwordItem) { - TagHelper.addTag((Tag.Named) FabricToolTags.SWORDS, item); + TagAPI.addTag((Tag.Named) FabricToolTags.SWORDS, item); } else if (item instanceof BasePickaxeItem) { - TagHelper.addTag((Tag.Named) FabricToolTags.PICKAXES, item); + TagAPI.addTag((Tag.Named) FabricToolTags.PICKAXES, item); } else if (item instanceof BaseAxeItem) { - TagHelper.addTag((Tag.Named) FabricToolTags.AXES, item); + TagAPI.addTag((Tag.Named) FabricToolTags.AXES, item); } else if (item instanceof BaseHoeItem) { - TagHelper.addTag((Tag.Named) FabricToolTags.HOES, item); + TagAPI.addTag((Tag.Named) FabricToolTags.HOES, item); } return item; diff --git a/src/main/java/ru/bclib/util/TagHelper.java b/src/main/java/ru/bclib/util/TagHelper.java deleted file mode 100644 index d21df3ff..00000000 --- a/src/main/java/ru/bclib/util/TagHelper.java +++ /dev/null @@ -1,138 +0,0 @@ -package ru.bclib.util; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.tags.Tag; -import net.minecraft.world.item.Item; -import net.minecraft.world.level.ItemLike; -import net.minecraft.world.level.block.Block; - -import java.util.Map; -import java.util.Set; - -/** - * Utility functions to manage Minecraft Tags - */ -public class TagHelper { - private static final Map> TAGS_BLOCK = Maps.newConcurrentMap(); - private static final Map> TAGS_ITEM = Maps.newConcurrentMap(); - - /** - * Adds one Tag to multiple Blocks. - *

- * Example: - *

{@code  Tag.Named DIMENSION_STONE = makeBlockTag("mymod", "dim_stone");
-	 * TagHelper.addTag(DIMENSION_STONE, Blocks.END_STONE, Blocks.NETHERRACK);}
- *

- * The call will reserve the Tag. The Tag is added to the blocks once - * {@link #apply(String, Map)} was executed. - * - * @param tag The new Tag - * @param blocks One or more blocks that should receive the Tag. - */ - public static void addTag(Tag.Named tag, Block... blocks) { - ResourceLocation tagID = tag.getName(); - 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); - } - } - } - - /** - * Adds one Tag to multiple Items. - *

- * Example: - *

{@code  Tag.Named METALS = makeBlockTag("mymod", "metals");
-	 * TagHelper.addTag(METALS, Items.IRON_INGOT, Items.GOLD_INGOT, Items.COPPER_INGOT);}
- *

- * The call will reserve the Tag. The Tag is added to the items once - * {@link #apply(String, Map)} was executed. - * - * @param tag The new Tag - * @param items One or more item that should receive the Tag. - */ - public static void addTag(Tag.Named tag, ItemLike... items) { - ResourceLocation tagID = tag.getName(); - 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); - } - } - } - - /** - * Adds multiple Tags to one Item. - *

- * The call will reserve the Tags. The Tags are added to the Item once - * * {@link #apply(String, Map)} was executed. - * - * @param item The Item that will receive all Tags - * @param tags One or more Tags - */ - @SafeVarargs - public static void addTags(ItemLike item, Tag.Named... tags) { - for (Tag.Named tag : tags) { - addTag(tag, item); - } - } - - /** - * Adds multiple Tags to one Block. - *

- * The call will reserve the Tags. The Tags are added to the Block once - * * {@link #apply(String, Map)} was executed. - * - * @param block The Block that will receive all Tags - * @param tags One or more Tags - */ - @SafeVarargs - public static void addTags(Block block, Tag.Named... tags) { - for (Tag.Named tag : tags) { - addTag(tag, block); - } - } - - /** - * Adds all {@code ids} to the {@code builder}. - * - * @param builder - * @param ids - * @return The Builder passed as {@code builder}. - */ - public static Tag.Builder apply(Tag.Builder builder, Set ids) { - ids.forEach(value -> builder.addElement(value, "Better End Code")); - return builder; - } - - /** - * Automatically called in {@link net.minecraft.tags.TagLoader#loadAndBuild(ResourceManager)}. - *

- * In most cases there is no need to call this Method manually. - * - * @param directory The name of the Tag-directory. Should be either "tags/blocks" or - * "tags/items". - * @param tagsMap The map that will hold the registered Tags - * @return The {@code tagsMap} Parameter. - */ - public static Map apply(String directory, Map tagsMap) { - Map> endTags = null; - if ("tags/blocks".equals(directory)) { - endTags = TAGS_BLOCK; - } - else if ("tags/items".equals(directory)) { - endTags = TAGS_ITEM; - } - if (endTags != null) { - endTags.forEach((id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids)); - } - return tagsMap; - } -}