From b1659300e0921202b422d63e5831a90ee2dd50a9 Mon Sep 17 00:00:00 2001 From: Richard Freimer Date: Thu, 15 Oct 2020 15:45:25 -0400 Subject: [PATCH] Adds tag support for Enchantments, Potions, and TileEntityTypes (#7379) --- .../enchantment/Enchantment.java.patch | 22 +++- .../net/minecraft/potion/Potion.java.patch | 18 ++- .../tileentity/TileEntityType.java.patch | 23 ++++ src/generated_test/resources/.cache/cache | 3 + .../tags/enchantments/fire.json | 7 ++ .../tags/potions/damage.json | 7 ++ .../tags/tile_entity_types/storage.json | 8 ++ .../minecraftforge/registries/GameData.java | 6 +- .../debug/misc/CustomTagTypesTest.java | 107 +++++++++++++++++- 9 files changed, 188 insertions(+), 13 deletions(-) create mode 100644 src/generated_test/resources/data/custom_tag_types_test/tags/enchantments/fire.json create mode 100644 src/generated_test/resources/data/custom_tag_types_test/tags/potions/damage.json create mode 100644 src/generated_test/resources/data/custom_tag_types_test/tags/tile_entity_types/storage.json diff --git a/patches/minecraft/net/minecraft/enchantment/Enchantment.java.patch b/patches/minecraft/net/minecraft/enchantment/Enchantment.java.patch index 2832b21ab..6806ca242 100644 --- a/patches/minecraft/net/minecraft/enchantment/Enchantment.java.patch +++ b/patches/minecraft/net/minecraft/enchantment/Enchantment.java.patch @@ -1,15 +1,31 @@ --- a/net/minecraft/enchantment/Enchantment.java +++ b/net/minecraft/enchantment/Enchantment.java -@@ -18,7 +18,7 @@ +@@ -18,7 +18,8 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; -public abstract class Enchantment { +public abstract class Enchantment extends net.minecraftforge.registries.ForgeRegistryEntry { ++ private final net.minecraftforge.common.util.ReverseTagWrapper reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.TagCollectionManager.func_242178_a().getCustomTypeCollection(net.minecraftforge.registries.ForgeRegistries.ENCHANTMENTS)); private final EquipmentSlotType[] field_185263_a; private final Enchantment.Rarity field_77333_a; public final EnchantmentType field_77351_y; -@@ -114,7 +114,7 @@ +@@ -37,6 +38,14 @@ + this.field_185263_a = p_i46731_3_; + } + ++ public java.util.Set getTags() { ++ return reverseTags.getTagNames(); ++ } ++ ++ public boolean isIn(net.minecraft.tags.ITag tag) { ++ return tag.func_230235_a_(this); ++ } ++ + public Map func_222181_a(LivingEntity p_222181_1_) { + Map map = Maps.newEnumMap(EquipmentSlotType.class); + +@@ -114,7 +123,7 @@ } public boolean func_92089_a(ItemStack p_92089_1_) { @@ -18,7 +34,7 @@ } public void func_151368_a(LivingEntity p_151368_1_, Entity p_151368_2_, int p_151368_3_) { -@@ -139,6 +139,24 @@ +@@ -139,6 +148,24 @@ return true; } diff --git a/patches/minecraft/net/minecraft/potion/Potion.java.patch b/patches/minecraft/net/minecraft/potion/Potion.java.patch index 019651479..5d6d26f8c 100644 --- a/patches/minecraft/net/minecraft/potion/Potion.java.patch +++ b/patches/minecraft/net/minecraft/potion/Potion.java.patch @@ -1,11 +1,27 @@ --- a/net/minecraft/potion/Potion.java +++ b/net/minecraft/potion/Potion.java -@@ -6,7 +6,7 @@ +@@ -6,7 +6,8 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.registry.Registry; -public class Potion { +public class Potion extends net.minecraftforge.registries.ForgeRegistryEntry { ++ private final net.minecraftforge.common.util.ReverseTagWrapper reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.TagCollectionManager.func_242178_a().getCustomTypeCollection(net.minecraftforge.registries.ForgeRegistries.POTION_TYPES)); private final String field_185179_d; private final ImmutableList field_185180_e; +@@ -23,6 +24,14 @@ + this.field_185180_e = ImmutableList.copyOf(p_i46740_2_); + } + ++ public java.util.Set getTags() { ++ return reverseTags.getTagNames(); ++ } ++ ++ public boolean isIn(net.minecraft.tags.ITag tag) { ++ return tag.func_230235_a_(this); ++ } ++ + public String func_185174_b(String p_185174_1_) { + return p_185174_1_ + (this.field_185179_d == null ? Registry.field_212621_j.func_177774_c(this).func_110623_a() : this.field_185179_d); + } diff --git a/patches/minecraft/net/minecraft/tileentity/TileEntityType.java.patch b/patches/minecraft/net/minecraft/tileentity/TileEntityType.java.patch index f310a7d70..c78f4e228 100644 --- a/patches/minecraft/net/minecraft/tileentity/TileEntityType.java.patch +++ b/patches/minecraft/net/minecraft/tileentity/TileEntityType.java.patch @@ -9,3 +9,26 @@ private static final Logger field_206866_A = LogManager.getLogger(); public static final TileEntityType field_200971_b = func_200966_a("furnace", TileEntityType.Builder.func_223042_a(FurnaceTileEntity::new, Blocks.field_150460_al)); public static final TileEntityType field_200972_c = func_200966_a("chest", TileEntityType.Builder.func_223042_a(ChestTileEntity::new, Blocks.field_150486_ae)); +@@ -54,6 +54,7 @@ + private final Supplier field_200995_z; + private final Set field_223046_I; + private final Type field_206867_C; ++ private final net.minecraftforge.common.util.ReverseTagWrapper> reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.TagCollectionManager.func_242178_a().getCustomTypeCollection(net.minecraftforge.registries.ForgeRegistries.TILE_ENTITIES)); + + @Nullable + public static ResourceLocation func_200969_a(TileEntityType p_200969_0_) { +@@ -75,6 +76,14 @@ + this.field_206867_C = p_i51497_3_; + } + ++ public java.util.Set getTags() { ++ return reverseTags.getTagNames(); ++ } ++ ++ public boolean isIn(net.minecraft.tags.ITag> tag) { ++ return tag.func_230235_a_(this); ++ } ++ + @Nullable + public T func_200968_a() { + return this.field_200995_z.get(); diff --git a/src/generated_test/resources/.cache/cache b/src/generated_test/resources/.cache/cache index 607f36b50..4a2d7fb38 100644 --- a/src/generated_test/resources/.cache/cache +++ b/src/generated_test/resources/.cache/cache @@ -68,6 +68,9 @@ d2c8e860521c5e738ed0798337f5728d610825ff assets/piston_event_test/models/block/s f51f026a75e27a0a53a76771d553c0e3385a2966 assets/piston_event_test/models/item/shiftonmove.json cf16f861eaf5815238c2278eb48bde0688cb73b7 assets/scaffolding_test/blockstates/scaffolding_method_test.json 862196702dcd5538c5ceb4ac6202e57eb5c8a97f data/custom_tag_types_test/tags/custom_tag_types_test/custom_types/tests.json +8c1969250406edfbd470cc06aca4dcccfd6ce682 data/custom_tag_types_test/tags/enchantments/fire.json +44985f1cb0df54b632187ed97a6a443455f1e51c data/custom_tag_types_test/tags/potions/damage.json +9405b72d54aad3f4377035d9707bb9947e9032a3 data/custom_tag_types_test/tags/tile_entity_types/storage.json 4fbaf6f4a3ea05cc071076e27f44ac81f9cc50e3 data/data_gen_test/advancements/conditional.json 66442c8000af9af4ba36109b3c9d09d9ffd57277 data/data_gen_test/advancements/recipes/conditional2.json ed4cbf1a3a2f5d8969f6346fdc9acdbe81d0c919 data/data_gen_test/recipes/conditional.json diff --git a/src/generated_test/resources/data/custom_tag_types_test/tags/enchantments/fire.json b/src/generated_test/resources/data/custom_tag_types_test/tags/enchantments/fire.json new file mode 100644 index 000000000..14ae0bf5c --- /dev/null +++ b/src/generated_test/resources/data/custom_tag_types_test/tags/enchantments/fire.json @@ -0,0 +1,7 @@ +{ + "replace": false, + "values": [ + "minecraft:fire_aspect", + "minecraft:flame" + ] +} \ No newline at end of file diff --git a/src/generated_test/resources/data/custom_tag_types_test/tags/potions/damage.json b/src/generated_test/resources/data/custom_tag_types_test/tags/potions/damage.json new file mode 100644 index 000000000..8ea822471 --- /dev/null +++ b/src/generated_test/resources/data/custom_tag_types_test/tags/potions/damage.json @@ -0,0 +1,7 @@ +{ + "replace": false, + "values": [ + "minecraft:harming", + "minecraft:strong_harming" + ] +} \ No newline at end of file diff --git a/src/generated_test/resources/data/custom_tag_types_test/tags/tile_entity_types/storage.json b/src/generated_test/resources/data/custom_tag_types_test/tags/tile_entity_types/storage.json new file mode 100644 index 000000000..f17ea32d3 --- /dev/null +++ b/src/generated_test/resources/data/custom_tag_types_test/tags/tile_entity_types/storage.json @@ -0,0 +1,8 @@ +{ + "replace": false, + "values": [ + "minecraft:barrel", + "minecraft:chest", + "minecraft:ender_chest" + ] +} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/registries/GameData.java b/src/main/java/net/minecraftforge/registries/GameData.java index 4490de0a2..f80beb304 100644 --- a/src/main/java/net/minecraftforge/registries/GameData.java +++ b/src/main/java/net/minecraftforge/registries/GameData.java @@ -143,10 +143,10 @@ public class GameData makeRegistry(EFFECTS, Effect.class ).legacyName("potions").create(); //makeRegistry(BIOMES, Biome.class).legacyName("biomes").create(); makeRegistry(SOUND_EVENTS, SoundEvent.class).legacyName("soundevents").create(); - makeRegistry(POTIONS, Potion.class, "empty").legacyName("potiontypes").create(); - makeRegistry(ENCHANTMENTS, Enchantment.class).legacyName("enchantments").create(); + makeRegistry(POTIONS, Potion.class, "empty").legacyName("potiontypes").tagFolder("potions").create(); + makeRegistry(ENCHANTMENTS, Enchantment.class).legacyName("enchantments").tagFolder("enchantments").create(); makeRegistry(ENTITY_TYPES, c(EntityType.class), "pig").legacyName("entities").create(); - makeRegistry(TILE_ENTITY_TYPES, c(TileEntityType.class)).disableSaving().legacyName("tileentities").create(); + makeRegistry(TILE_ENTITY_TYPES, c(TileEntityType.class)).disableSaving().legacyName("tileentities").tagFolder("tile_entity_types").create(); makeRegistry(PARTICLE_TYPES, c(ParticleType.class)).disableSaving().create(); makeRegistry(CONTAINER_TYPES, c(ContainerType.class)).disableSaving().create(); makeRegistry(PAINTING_TYPES, PaintingType.class, "kebab").create(); diff --git a/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java b/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java index 6d5669b63..ebfab953b 100644 --- a/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java +++ b/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java @@ -24,24 +24,35 @@ import java.util.Set; import java.util.function.Supplier; import javax.annotation.Nullable; import net.minecraft.data.DataGenerator; +import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.enchantment.Enchantments; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.item.PotionItem; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionUtils; +import net.minecraft.potion.Potions; import net.minecraft.tags.ITag; import net.minecraft.tags.ItemTags; import net.minecraft.tags.TagCollectionManager; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.ForgeTagHandler; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.data.ExistingFileHelper; import net.minecraftforge.common.data.ForgeRegistryTagsProvider; import net.minecraftforge.common.util.ReverseTagWrapper; -import net.minecraftforge.event.entity.ProjectileImpactEvent; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.RegistryObject; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.GatherDataEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.RegistryBuilder; @@ -60,13 +71,16 @@ public class CustomTagTypesTest () -> new RegistryBuilder().tagFolder(MODID + "/custom_types")); private static final ITag.INamedTag TESTS = ForgeTagHandler.createOptionalTag(customRegistryName, new ResourceLocation(MODID, "tests"), Sets.newHashSet(CUSTOM)); private static final ITag.INamedTag OPTIONAL_TEST = ItemTags.createOptional(new ResourceLocation(MODID, "optional_test"), Sets.newHashSet(() -> Items.BONE)); + private static final ITag.INamedTag FIRE = ForgeTagHandler.createOptionalTag(ForgeRegistries.ENCHANTMENTS, new ResourceLocation(MODID, "fire")); + private static final ITag.INamedTag DAMAGE = ForgeTagHandler.createOptionalTag(ForgeRegistries.POTION_TYPES, new ResourceLocation(MODID, "damage")); + private static final ITag.INamedTag> STORAGE = ForgeTagHandler.createOptionalTag(ForgeRegistries.TILE_ENTITIES, new ResourceLocation(MODID, "storage")); public CustomTagTypesTest() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); CUSTOMS.register(modBus); modBus.addListener(this::gatherData); - MinecraftForge.EVENT_BUS.addListener(this::projectileImpact); + MinecraftForge.EVENT_BUS.addListener(this::leftClick); } private void gatherData(GatherDataEvent event) @@ -74,13 +88,33 @@ public class CustomTagTypesTest if (event.includeServer()) { DataGenerator gen = event.getGenerator(); - gen.addProvider(new CustomRegistryTags(gen, event.getExistingFileHelper())); + ExistingFileHelper existingFileHelper = event.getExistingFileHelper(); + gen.addProvider(new CustomRegistryTags(gen, existingFileHelper)); + gen.addProvider(new EnchantmentTags(gen, existingFileHelper)); + gen.addProvider(new PotionTags(gen, existingFileHelper)); + gen.addProvider(new TileEntityTypeTags(gen, existingFileHelper)); } } - private void projectileImpact(ProjectileImpactEvent.Arrow event) + private void leftClick(PlayerInteractEvent.LeftClickBlock event) { - LOGGER.info("{} {} {}", Items.BONE.getTags(), OPTIONAL_TEST.func_230236_b_().size(), TagCollectionManager.func_242178_a().func_241836_b().get(new ResourceLocation(MODID, "optional_test"))); + ItemStack itemStack = event.getItemStack(); + if (!itemStack.isEmpty()) + { + LOGGER.info("{} {} {}", Items.BONE.getTags(), OPTIONAL_TEST.func_230236_b_().size(), TagCollectionManager.func_242178_a().func_241836_b().get(new ResourceLocation(MODID, "optional_test"))); + EnchantmentHelper.getEnchantments(itemStack).forEach((enchantment, level) -> logTagsIfPresent(enchantment.getTags())); + if (itemStack.getItem() instanceof PotionItem) logTagsIfPresent(PotionUtils.getPotionFromItem(itemStack).getTags()); + TileEntity tileEntity = event.getWorld().getTileEntity(event.getPos()); + if (tileEntity != null) logTagsIfPresent(tileEntity.getType().getTags()); + } + } + + private void logTagsIfPresent(Set tags) + { + if (!tags.isEmpty()) + { + LOGGER.info("Tags: {}", tags); + } } public static class Custom extends ForgeRegistryEntry @@ -112,8 +146,69 @@ public class CustomTagTypesTest } @Override - public String getName() { + public String getName() + { return "Custom Registry Tags"; } } + + public static class EnchantmentTags extends ForgeRegistryTagsProvider + { + public EnchantmentTags(DataGenerator gen, @Nullable ExistingFileHelper existingFileHelper) + { + super(gen, ForgeRegistries.ENCHANTMENTS, MODID, existingFileHelper); + } + + @Override + protected void registerTags() + { + func_240522_a_(FIRE).func_240534_a_(Enchantments.FIRE_ASPECT, Enchantments.FLAME); + } + + @Override + public String getName() + { + return "Enchantment Tags"; + } + } + + public static class PotionTags extends ForgeRegistryTagsProvider + { + public PotionTags(DataGenerator gen, @Nullable ExistingFileHelper existingFileHelper) + { + super(gen, ForgeRegistries.POTION_TYPES, MODID, existingFileHelper); + } + + @Override + protected void registerTags() + { + func_240522_a_(DAMAGE).func_240534_a_(Potions.HARMING, Potions.STRONG_HARMING); + } + + @Override + public String getName() + { + return "Potion Tags"; + } + } + + public static class TileEntityTypeTags extends ForgeRegistryTagsProvider> + { + public TileEntityTypeTags(DataGenerator gen, @Nullable ExistingFileHelper existingFileHelper) + { + super(gen, ForgeRegistries.TILE_ENTITIES, MODID, existingFileHelper); + } + + @Override + protected void registerTags() + { + func_240522_a_(STORAGE).func_240534_a_(TileEntityType.BARREL, TileEntityType.CHEST, TileEntityType.ENDER_CHEST); + } + + @Override + public String getName() + { + return "Tile Entity Type Tags"; + } + } }