diff --git a/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch b/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch index 95960faac4..54150877c3 100644 --- a/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch +++ b/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch @@ -77,19 +77,11 @@ if (mobeffect != null) { MobEffectInstance mobeffectinstance = new MobEffectInstance(mobeffect, p_105130_.m_133625_(), p_105130_.m_133624_(), p_105130_.m_133627_(), p_105130_.m_133626_(), p_105130_.m_133628_()); mobeffectinstance.m_19562_(p_105130_.m_133619_()); -@@ -1228,11 +_,14 @@ - public void m_5859_(ClientboundUpdateTagsPacket p_105134_) { - PacketUtils.m_131363_(p_105134_, this, this.f_104888_); - TagContainer tagcontainer = TagContainer.m_144449_(this.f_104903_, p_105134_.m_179482_()); -- Multimap>, ResourceLocation> multimap = StaticTags.m_13283_(tagcontainer); -+ boolean vanillaConnection = net.minecraftforge.fmllegacy.network.NetworkHooks.isVanillaConnection(f_104885_); -+ Multimap>, ResourceLocation> multimap = vanillaConnection ? StaticTags.m_13283_(net.minecraftforge.common.ForgeTagHandler.withNoCustom(tagcontainer)) : StaticTags.validateVanillaTags(tagcontainer);//Forge: If we are connecting to vanilla validate all tags to properly validate custom tags the client may "require", and if we are connecting to forge only validate the vanilla tag types as the custom tag types get synced in a separate packet so may still arrive - if (!multimap.isEmpty()) { +@@ -1233,6 +_,7 @@ f_104883_.warn("Incomplete server tags, disconnecting. Missing: {}", (Object)multimap); this.f_104885_.m_129507_(new TranslatableComponent("multiplayer.disconnect.missing_tags")); } else { -+ net.minecraftforge.common.ForgeTagHandler.resetCachedTagCollections(true, vanillaConnection); -+ tagcontainer = TagContainer.reinjectOptionalTags(tagcontainer); ++ tagcontainer = net.minecraftforge.common.ForgeTagHandler.reinjectOptionalTags(tagcontainer); this.f_104895_ = tagcontainer; if (!this.f_104885_.m_129531_()) { tagcontainer.m_13431_(); diff --git a/patches/minecraft/net/minecraft/data/tags/GameEventTagsProvider.java.patch b/patches/minecraft/net/minecraft/data/tags/GameEventTagsProvider.java.patch new file mode 100644 index 0000000000..e5f564a96e --- /dev/null +++ b/patches/minecraft/net/minecraft/data/tags/GameEventTagsProvider.java.patch @@ -0,0 +1,15 @@ +--- a/net/minecraft/data/tags/GameEventTagsProvider.java ++++ b/net/minecraft/data/tags/GameEventTagsProvider.java +@@ -8,8 +_,12 @@ + import net.minecraft.world.level.gameevent.GameEvent; + + public class GameEventTagsProvider extends TagsProvider { ++ @Deprecated + public GameEventTagsProvider(DataGenerator p_176826_) { + super(p_176826_, Registry.f_175412_); ++ } ++ public GameEventTagsProvider(DataGenerator p_126517_, String modId, @javax.annotation.Nullable net.minecraftforge.common.data.ExistingFileHelper existingFileHelper) { ++ super(p_126517_, Registry.f_175412_, modId, existingFileHelper); + } + + protected void m_6577_() { diff --git a/patches/minecraft/net/minecraft/server/ServerResources.java.patch b/patches/minecraft/net/minecraft/server/ServerResources.java.patch index a44594cf23..f20067c7f9 100644 --- a/patches/minecraft/net/minecraft/server/ServerResources.java.patch +++ b/patches/minecraft/net/minecraft/server/ServerResources.java.patch @@ -8,11 +8,3 @@ } public ServerFunctionLibrary m_136157_() { -@@ -94,6 +_,7 @@ - - public void m_136179_() { - this.f_136148_.m_13480_().m_13431_(); -+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.TagsUpdatedEvent.CustomTagTypes(f_136148_.m_13480_())); - } - - public void close() { diff --git a/patches/minecraft/net/minecraft/server/players/PlayerList.java.patch b/patches/minecraft/net/minecraft/server/players/PlayerList.java.patch index 581e8765f3..9759b40553 100644 --- a/patches/minecraft/net/minecraft/server/players/PlayerList.java.patch +++ b/patches/minecraft/net/minecraft/server/players/PlayerList.java.patch @@ -16,17 +16,14 @@ GameRules gamerules = serverlevel1.m_46469_(); boolean flag = gamerules.m_46207_(GameRules.f_46156_); boolean flag1 = gamerules.m_46207_(GameRules.f_46145_); -@@ -158,8 +_,10 @@ +@@ -158,6 +_,7 @@ servergamepacketlistenerimpl.m_141995_(new ClientboundChangeDifficultyPacket(leveldata.m_5472_(), leveldata.m_5474_())); servergamepacketlistenerimpl.m_141995_(new ClientboundPlayerAbilitiesPacket(p_11263_.m_150110_())); servergamepacketlistenerimpl.m_141995_(new ClientboundSetCarriedItemPacket(p_11263_.m_150109_().f_35977_)); + net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.OnDatapackSyncEvent(this, p_11263_)); servergamepacketlistenerimpl.m_141995_(new ClientboundUpdateRecipesPacket(this.f_11195_.m_129894_().m_44051_())); servergamepacketlistenerimpl.m_141995_(new ClientboundUpdateTagsPacket(this.f_11195_.m_129895_().m_144442_(this.f_11206_))); -+ net.minecraftforge.fmllegacy.network.NetworkHooks.syncCustomTagTypes(p_11263_, this.f_11195_.m_129895_()); this.m_11289_(p_11263_); - p_11263_.m_8951_().m_12850_(); - p_11263_.m_8952_().m_12789_(p_11263_); @@ -174,7 +_,7 @@ this.m_11264_(mutablecomponent.m_130940_(ChatFormatting.YELLOW), ChatType.SYSTEM, Util.f_137441_); @@ -113,16 +110,14 @@ } @Nullable -@@ -785,7 +_,9 @@ +@@ -785,6 +_,7 @@ playeradvancements.m_135981_(this.f_11195_.m_129889_()); } + net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.OnDatapackSyncEvent(this, null)); this.m_11268_(new ClientboundUpdateTagsPacket(this.f_11195_.m_129895_().m_144442_(this.f_11206_))); -+ net.minecraftforge.fmllegacy.network.NetworkHooks.syncCustomTagTypes(this.f_11195_.m_129895_()); ClientboundUpdateRecipesPacket clientboundupdaterecipespacket = new ClientboundUpdateRecipesPacket(this.f_11195_.m_129894_().m_44051_()); - for(ServerPlayer serverplayer : this.f_11196_) { @@ -797,5 +_,13 @@ public boolean m_11316_() { diff --git a/patches/minecraft/net/minecraft/tags/GameEventTags.java.patch b/patches/minecraft/net/minecraft/tags/GameEventTags.java.patch new file mode 100644 index 0000000000..214cfa4661 --- /dev/null +++ b/patches/minecraft/net/minecraft/tags/GameEventTags.java.patch @@ -0,0 +1,17 @@ +--- a/net/minecraft/tags/GameEventTags.java ++++ b/net/minecraft/tags/GameEventTags.java +@@ -12,6 +_,14 @@ + return f_144301_.m_13244_(p_144308_); + } + ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(net.minecraft.resources.ResourceLocation name) { ++ return createOptional(name, null); ++ } ++ ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(net.minecraft.resources.ResourceLocation name, @javax.annotation.Nullable java.util.Set> defaults) { ++ return f_144301_.createOptional(name, defaults); ++ } ++ + public static TagCollection m_144306_() { + return f_144301_.m_13246_(); + } diff --git a/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch b/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch index 328c434efa..d9e7568822 100644 --- a/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch +++ b/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch @@ -4,11 +4,11 @@ private final String f_144327_; private TagCollection f_13227_ = TagCollection.m_13410_(); private final List> f_13228_ = Lists.newArrayList(); -+ private static java.util.Map>> toAdd = com.google.common.collect.Maps.newHashMap(); ++ @Nullable private static java.util.Map>> toAdd = com.google.common.collect.Maps.newHashMap(); public StaticTagHelper(ResourceKey> p_144329_, String p_144330_) { this.f_144326_ = p_144329_; -@@ -24,9 +_,46 @@ +@@ -24,9 +_,60 @@ } public Tag.Named m_13244_(String p_13245_) { @@ -55,13 +55,8 @@ + namedtag.m_13260_(f_13227_::m_13404_); + this.f_13228_.add(namedtag); + return namedtag; - } - - public void m_13232_() { -@@ -47,13 +_,31 @@ - }); - } - ++ } ++ + public TagCollection reinjectOptionalTags(TagCollection tagCollection) { + java.util.Map> currentTags = tagCollection.m_5643_(); + java.util.Map> missingOptionals = this.f_13228_.stream().filter(e -> e instanceof OptionalNamedTag && !currentTags.containsKey(e.m_6979_())).collect(Collectors.toMap(Wrapper::m_6979_, namedTag -> { @@ -74,16 +69,11 @@ + return TagCollection.m_13396_(missingOptionals); + } + return tagCollection; -+ } -+ - public TagCollection m_13246_() { - return this.f_13227_; } -+ public List> getWrappers() { -+ return this.f_13228_; -+ } -+ + public void m_13232_() { +@@ -53,7 +_,7 @@ + public Set m_13247_(TagContainer p_13248_) { TagCollection tagcollection = p_13248_.m_144452_(this.f_144326_); - Set set = this.f_13228_.stream().map(StaticTagHelper.Wrapper::m_6979_).collect(Collectors.toSet()); @@ -108,7 +98,7 @@ + + @Override + public String toString() { -+ return "NamedTag[" + m_6979_().toString() + ']'; ++ return "NamedTag[" + m_6979_() + ']'; + } + @Override public boolean equals(Object o) { return (o == this) || (o instanceof Tag.Named && java.util.Objects.equals(this.m_6979_(), ((Tag.Named)o).m_6979_())); } + @Override public int hashCode() { return m_6979_().hashCode(); } @@ -138,7 +128,7 @@ + + @Override + public String toString() { -+ return "OptionalNamedTag[" + m_6979_().toString() + ']'; ++ return "OptionalNamedTag[" + m_6979_() + ']'; } } } diff --git a/patches/minecraft/net/minecraft/tags/StaticTags.java.patch b/patches/minecraft/net/minecraft/tags/StaticTags.java.patch index e24b15db25..b921050664 100644 --- a/patches/minecraft/net/minecraft/tags/StaticTags.java.patch +++ b/patches/minecraft/net/minecraft/tags/StaticTags.java.patch @@ -21,42 +21,20 @@ return statictaghelper; } } -@@ -55,11 +_,29 @@ - - private static void m_144356_() { - Set> set = m_144355_().stream().map(StaticTagHelper::m_144338_).collect(Collectors.toSet()); -- if (!Sets.difference(f_144340_, set).isEmpty()) { -+ if (!Sets.difference(set, f_144340_).isEmpty()) { // Forge: Fix bug with extra tag collections crashing - throw new IllegalStateException("Missing helper registrations"); - } +@@ -54,10 +_,16 @@ } + private static void m_144356_() { ++ if (true) { m_144355_(); return; } //FORGE this check is unnecessary as the 2 collections are patched back into a map above. Still need to 'clinit' them though. + Set> set = m_144355_().stream().map(StaticTagHelper::m_144338_).collect(Collectors.toSet()); + if (!Sets.difference(f_144340_, set).isEmpty()) { + throw new IllegalStateException("Missing helper registrations"); + } ++ } ++ + @javax.annotation.Nullable + public static StaticTagHelper get(ResourceLocation rl) { + return HELPER_MAP.get(ResourceKey.m_135788_(rl)); -+ } -+ -+ public static Multimap>, ResourceLocation> validateVanillaTags(TagContainer tagCollectionSupplier) { -+ Multimap>, ResourceLocation> multimap = HashMultimap.create(); -+ f_13264_.forEach((tagHelper) -> { -+ if (!net.minecraftforge.common.ForgeTagHandler.getCustomTagTypeNames().contains(tagHelper.m_144338_().m_135782_())) -+ multimap.putAll(tagHelper.m_144338_(), tagHelper.m_13247_(tagCollectionSupplier)); -+ }); -+ return multimap; -+ } -+ -+ public static void fetchCustomTagTypes(TagContainer tagCollectionSupplier) { -+ net.minecraftforge.common.ForgeTagHandler.getCustomTagTypeNames().forEach(tagRegistry -> HELPER_MAP.get(ResourceKey.m_135788_(tagRegistry)).m_13242_(tagCollectionSupplier)); -+ } -+ + } + public static void m_144349_(Consumer> p_144350_) { - f_13264_.forEach(p_144350_); - } -@@ -70,6 +_,7 @@ - f_13264_.forEach((p_144344_) -> { - p_144344_.m_144336_(tagcontainer$builder); - }); -+ net.minecraftforge.common.ForgeTagHandler.populateTagCollectionManager(); - return tagcontainer$builder.m_144485_(); - } - } diff --git a/patches/minecraft/net/minecraft/tags/TagContainer.java.patch b/patches/minecraft/net/minecraft/tags/TagContainer.java.patch index df79f35fb5..31f0465c8a 100644 --- a/patches/minecraft/net/minecraft/tags/TagContainer.java.patch +++ b/patches/minecraft/net/minecraft/tags/TagContainer.java.patch @@ -1,33 +1,18 @@ --- a/net/minecraft/tags/TagContainer.java +++ b/net/minecraft/tags/TagContainer.java -@@ -15,7 +_,7 @@ - import org.apache.logging.log4j.LogManager; - import org.apache.logging.log4j.Logger; - --public class TagContainer { -+public class TagContainer implements net.minecraftforge.common.extensions.IForgeTagContainer { - static final Logger f_144432_ = LogManager.getLogger(); - public static final TagContainer f_13420_ = new TagContainer(ImmutableMap.of()); - public final Map>, TagCollection> f_144433_; @@ -74,6 +_,7 @@ public void m_13431_() { StaticTags.m_13269_(this); Blocks.m_50758_(); -+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.TagsUpdatedEvent.VanillaTagTypes(this)); ++ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.TagsUpdatedEvent(this)); } public Map>, TagCollection.NetworkPayload> m_144442_(final RegistryAccess p_144443_) { -@@ -108,6 +_,13 @@ - f_144432_.error("Unknown registry {}", (Object)p_144447_); - } - -+ } -+ -+ public static TagContainer reinjectOptionalTags(TagContainer tagCollectionSupplier) { -+ TagContainer.Builder builder = new TagContainer.Builder(); -+ //noinspection unchecked,rawtypes -+ StaticTags.m_144349_(h -> builder.m_144486_(h.m_144338_(), h.reinjectOptionalTags(tagCollectionSupplier.m_144452_((ResourceKey) h.m_144338_())))); -+ return builder.m_144485_(); - } - - public static class Builder { +@@ -81,6 +_,7 @@ + this.m_144436_(new TagContainer.CollectionConsumer() { + public void m_142574_(ResourceKey> p_144481_, TagCollection p_144482_) { + Optional> optional = p_144443_.m_6632_(p_144481_); ++ optional = net.minecraftforge.common.ForgeTagHandler.getWrapperRegistry(p_144481_, optional); + if (optional.isPresent()) { + map.put(p_144481_, p_144482_.m_144411_(optional.get())); + } else { diff --git a/patches/minecraft/net/minecraft/tags/TagManager.java.patch b/patches/minecraft/net/minecraft/tags/TagManager.java.patch index 3da79ce227..a31d15fc54 100644 --- a/patches/minecraft/net/minecraft/tags/TagManager.java.patch +++ b/patches/minecraft/net/minecraft/tags/TagManager.java.patch @@ -8,34 +8,20 @@ private TagContainer f_13478_ = TagContainer.f_13420_; public TagManager(RegistryAccess p_144572_) { -@@ -40,15 +_,16 @@ - } - - }); -- return CompletableFuture.allOf(list.stream().map((p_144591_) -> { -- return p_144591_.f_144598_; -- }).toArray((p_144574_) -> { -- return new CompletableFuture[p_144574_]; -- })).thenCompose(p_13482_::m_6769_).thenAcceptAsync((p_144594_) -> { -+ CompletableFuture> customTagTypeResults = net.minecraftforge.common.ForgeTagHandler.getCustomTagTypeReloadResults(p_13483_, p_13486_, customTagTypeReaders); -+ List> futures = Lists.newArrayList(); -+ list.stream().map(li -> li.f_144598_).forEach(futures::add); -+ futures.add(customTagTypeResults); -+ return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).thenCompose(p_13482_::m_6769_).thenAcceptAsync((p_144594_) -> { - TagContainer.Builder tagcontainer$builder = new TagContainer.Builder(); - list.forEach((p_144586_) -> { - p_144586_.m_144602_(tagcontainer$builder); - }); -+ net.minecraftforge.common.ForgeTagHandler.updateCustomTagTypes(customTagTypeResults.join()); - TagContainer tagcontainer = tagcontainer$builder.m_144485_(); - Multimap>, ResourceLocation> multimap = StaticTags.m_13283_(tagcontainer); - if (!multimap.isEmpty()) { -@@ -56,6 +_,8 @@ +@@ -56,6 +_,7 @@ return p_144596_.getKey() + ":" + p_144596_.getValue(); }).sorted().collect(Collectors.joining(","))); } else { -+ tagcontainer = TagContainer.reinjectOptionalTags(tagcontainer); -+ net.minecraftforge.common.ForgeTagHandler.reinjectOptionalTagsCustomTypes(); ++ tagcontainer = net.minecraftforge.common.ForgeTagHandler.reinjectOptionalTags(tagcontainer); SerializationTags.m_13202_(tagcontainer); this.f_13478_ = tagcontainer; } +@@ -73,6 +_,8 @@ + }, p_144577_); + return new TagManager.LoaderInfo<>(p_144578_, completablefuture); + } else { ++ if (net.minecraftforge.common.ForgeTagHandler.getCustomTagTypeNames().contains(p_144578_.m_144338_().m_135782_())) ++ return new TagManager.LoaderInfo<>(p_144578_, CompletableFuture.supplyAsync(() -> ((TagLoader) customTagTypeReaders.get(p_144578_.m_144338_().m_135782_())).m_144544_(p_144576_), p_144577_)); + f_144568_.warn("Can't find registry for {}", (Object)p_144578_.m_144338_()); + return null; + } diff --git a/patches/minecraft/net/minecraft/world/item/alchemy/Potion.java.patch b/patches/minecraft/net/minecraft/world/item/alchemy/Potion.java.patch index 663d62a4e4..7daa9b779e 100644 --- a/patches/minecraft/net/minecraft/world/item/alchemy/Potion.java.patch +++ b/patches/minecraft/net/minecraft/world/item/alchemy/Potion.java.patch @@ -6,7 +6,7 @@ -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.SerializationTags.m_13199_().m_144452_(Registry.f_122905_)); ++ private final net.minecraftforge.common.util.ReverseTagWrapper reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.SerializationTags.m_13199_().m_144452_(Registry.f_122905_)); private final String f_43481_; private final ImmutableList f_43482_; diff --git a/patches/minecraft/net/minecraft/world/item/enchantment/Enchantment.java.patch b/patches/minecraft/net/minecraft/world/item/enchantment/Enchantment.java.patch index 3ab09a4642..23736553fd 100644 --- a/patches/minecraft/net/minecraft/world/item/enchantment/Enchantment.java.patch +++ b/patches/minecraft/net/minecraft/world/item/enchantment/Enchantment.java.patch @@ -6,7 +6,7 @@ -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.SerializationTags.m_13199_().getCustomTypeCollection(net.minecraftforge.registries.ForgeRegistries.ENCHANTMENTS)); ++ private final net.minecraftforge.common.util.ReverseTagWrapper reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.SerializationTags.m_13199_().m_144452_(net.minecraftforge.registries.ForgeRegistries.Keys.ENCHANTMENTS)); private final EquipmentSlot[] f_44671_; private final Enchantment.Rarity f_44674_; public final EnchantmentCategory f_44672_; diff --git a/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntityType.java.patch b/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntityType.java.patch index 90b1585f61..6283a0ce74 100644 --- a/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntityType.java.patch +++ b/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntityType.java.patch @@ -13,7 +13,7 @@ private final BlockEntityType.BlockEntitySupplier f_58914_; private final Set f_58915_; private final Type f_58916_; -+ private final net.minecraftforge.common.util.ReverseTagWrapper> reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.SerializationTags.m_13199_().getCustomTypeCollection(net.minecraftforge.registries.ForgeRegistries.BLOCK_ENTITIES)); ++ private final net.minecraftforge.common.util.ReverseTagWrapper> reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, () -> net.minecraft.tags.SerializationTags.m_13199_().m_144452_(net.minecraftforge.registries.ForgeRegistries.Keys.BLOCK_ENTITY_TYPES)); @Nullable public static ResourceLocation m_58954_(BlockEntityType p_58955_) { diff --git a/patches/minecraft/net/minecraft/world/level/gameevent/GameEvent.java.patch b/patches/minecraft/net/minecraft/world/level/gameevent/GameEvent.java.patch new file mode 100644 index 0000000000..7d5780881c --- /dev/null +++ b/patches/minecraft/net/minecraft/world/level/gameevent/GameEvent.java.patch @@ -0,0 +1,18 @@ +--- a/net/minecraft/world/level/gameevent/GameEvent.java ++++ b/net/minecraft/world/level/gameevent/GameEvent.java +@@ -51,10 +_,15 @@ + public static final int f_157788_ = 16; + private final String f_157789_; + private final int f_157790_; ++ private final net.minecraftforge.common.util.ReverseTagWrapper reverseTags = new net.minecraftforge.common.util.ReverseTagWrapper<>(this, net.minecraft.tags.GameEventTags::m_144306_); + + public GameEvent(String p_157819_, int p_157820_) { + this.f_157789_ = p_157819_; + this.f_157790_ = p_157820_; ++ } ++ ++ public java.util.Set getTags() { ++ return reverseTags.getTagNames(); + } + + public String m_157821_() { 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/block_entity_types/storage.json similarity index 59% rename from src/generated_test/resources/data/custom_tag_types_test/tags/tile_entity_types/storage.json rename to src/generated_test/resources/data/custom_tag_types_test/tags/block_entity_types/storage.json index f17ea32d3e..7915067f6b 100644 --- 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/block_entity_types/storage.json @@ -3,6 +3,7 @@ "values": [ "minecraft:barrel", "minecraft:chest", - "minecraft:ender_chest" + "minecraft:ender_chest", + "minecraft:trapped_chest" ] } \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java index d2cbecba7a..5e83693b90 100644 --- a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java @@ -113,7 +113,7 @@ public class ForgeInternalHandler } @SubscribeEvent - public synchronized void tagsUpdated(TagsUpdatedEvent.VanillaTagTypes event) + public synchronized void tagsUpdated(TagsUpdatedEvent event) { ForgeHooks.updateBurns(); } diff --git a/src/main/java/net/minecraftforge/common/ForgeTagHandler.java b/src/main/java/net/minecraftforge/common/ForgeTagHandler.java index a385513ee6..3f75d4ba3d 100644 --- a/src/main/java/net/minecraftforge/common/ForgeTagHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeTagHandler.java @@ -21,31 +21,25 @@ package net.minecraftforge.common; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; import java.util.function.Supplier; -import java.util.stream.Collectors; import javax.annotation.Nullable; -import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.tags.Tag; -import net.minecraft.tags.Tag.Named; -import net.minecraft.tags.TagCollection; -import net.minecraft.tags.TagContainer; -import net.minecraft.tags.TagLoader; + +import com.mojang.serialization.Lifecycle; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; import net.minecraft.tags.StaticTagHelper; import net.minecraft.tags.StaticTags; +import net.minecraft.tags.Tag.Named; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagContainer; +import net.minecraft.tags.TagLoader; import net.minecraftforge.common.Tags.IOptionalNamedTag; -import net.minecraftforge.common.extensions.IForgeTagContainer; -import net.minecraftforge.fmllegacy.network.FMLPlayMessages.SyncCustomTagTypes; import net.minecraftforge.registries.ForgeRegistry; +import net.minecraftforge.registries.GameData; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.IForgeRegistryEntry; import net.minecraftforge.registries.RegistryManager; @@ -55,20 +49,19 @@ import org.apache.logging.log4j.Logger; public class ForgeTagHandler { private static final Logger LOGGER = LogManager.getLogger(); - private static Map> customTagTypes = Collections.emptyMap(); private static Set customTagTypeNames = Collections.emptySet(); private static boolean tagTypesSet = false; @Nullable - private static > StaticTagHelper getTagRegistry(IForgeRegistry registry) + private static > StaticTagHelper getTagHelper(IForgeRegistry registry) { return (StaticTagHelper) StaticTags.get(registry.getRegistryName()); } private static void validateRegistrySupportsTags(IForgeRegistry registry) { - //Note: We also check against getTagRegistry in case someone decides to use the helpers for tag creation for types supported by vanilla - if (getTagRegistry(registry) == null && (!(registry instanceof ForgeRegistry) || ((ForgeRegistry) registry).getTagFolder() == null)) + //Note: We also check against getTagHelper in case someone decides to use the helpers for tag creation for types supported by vanilla + if (getTagHelper(registry) == null && (!(registry instanceof ForgeRegistry) || ((ForgeRegistry) registry).getTagFolder() == null)) { throw new IllegalArgumentException("Registry " + registry.getRegistryName() + " does not support tag types."); } @@ -88,7 +81,7 @@ public class ForgeTagHandler validateRegistrySupportsTags(registry); if (tagTypesSet) { - StaticTagHelper tagRegistry = getTagRegistry(registry); + StaticTagHelper tagRegistry = getTagHelper(registry); if (tagRegistry == null) throw new IllegalArgumentException("Registry " + registry.getRegistryName() + " does not support tag types."); return tagRegistry.bind(name.toString()); } @@ -124,7 +117,7 @@ public class ForgeTagHandler validateRegistrySupportsTags(registry); if (tagTypesSet) { - StaticTagHelper tagRegistry = getTagRegistry(registry); + StaticTagHelper tagRegistry = getTagHelper(registry); if (tagRegistry == null) throw new IllegalArgumentException("Registry " + registry.getRegistryName() + " does not support tag types."); return tagRegistry.createOptional(name, defaults); } @@ -197,27 +190,17 @@ public class ForgeTagHandler return customTagTypeNames; } - /** - * Gets a map of registry name to tag collection for all custom tag types. - * - * @apiNote Prefer interacting with this via the current {@link IForgeTagContainer} and using one of the forge extension getCustomTypeCollection methods - */ - public static Map> getCustomTagTypes() - { - return customTagTypes; - } - /** * Sets the set containing the resource locations representing the registry name of each forge registry that supports custom tag types. * * @apiNote Internal: Calling this manually WILL cause a crash to occur as it can only be called once, and is done so by * forge after all registries have been initialized. */ - public static void setCustomTagTypes(Set customTagTypes) + public static void setCustomTagTypes(Set customTagTypesNames) { if (tagTypesSet) throw new RuntimeException("Custom tag types have already been set, this method should only be called by forge, and after registries are initialized"); tagTypesSet = true; - customTagTypeNames = ImmutableSet.copyOf(customTagTypes); + customTagTypeNames = ImmutableSet.copyOf(customTagTypesNames); //Add the static references for custom tag types to the proper tag registries // Note: If this ends up being a hotspot due to lots of mods having lots of statically registered tags // that get loaded/registered before the new registry event is fired/processed everywhere then this @@ -228,7 +211,7 @@ public class ForgeTagHandler /** * Creates a map for custom tag type to tag reader * - * @apiNote Internal: For use by NetworkTagManager + * @apiNote Internal: For use by TagManager */ public static Map> createCustomTagTypeReaders() { @@ -246,166 +229,35 @@ public class ForgeTagHandler } /** - * Resets the cached collections for the various custom tag types. + * Wraps the forge registry if it supports tags into the internal registry for use in serialization + * + * @apiNote Internal: For use in TagContainer + */ + @SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "unchecked"}) + public static Optional> getWrapperRegistry(ResourceKey> key, Optional> vanillaReg) + { + if (vanillaReg.isPresent()) + return vanillaReg; + + ForgeRegistry reg = RegistryManager.ACTIVE.getRegistry(key.location()); + if (reg == null || reg.getTagFolder() == null) + return Optional.empty(); + + if (reg.getDefaultKey() == null) + return Optional.of((Registry) GameData.getWrapper(reg.getRegistryKey(), Lifecycle.stable())); + return Optional.of((Registry) GameData.getWrapper(reg.getRegistryKey(), Lifecycle.stable(), "ignored")); + } + + /** + * Helper to reinject missing optional tags. * * @apiNote Internal */ - public static void resetCachedTagCollections(boolean makeEmpty, boolean withOptional) + public static TagContainer reinjectOptionalTags(TagContainer tagCollectionSupplier) { - ImmutableMap.Builder> builder = ImmutableMap.builder(); - for (ResourceLocation registryName : customTagTypeNames) - { - StaticTagHelper tagRegistry = StaticTags.get(registryName); - if (tagRegistry != null) - { - if (makeEmpty) - { - if (withOptional) - builder.put(registryName, tagRegistry.reinjectOptionalTags(TagCollection.of(Collections.emptyMap()))); - else - builder.put(registryName, TagCollection.of(Collections.emptyMap())); - } - else - { - builder.put(registryName, TagCollection.of(tagRegistry.getWrappers().stream().distinct().collect(Collectors.toMap(Named::getName, namedTag -> namedTag)))); - } - } - } - customTagTypes = builder.build(); - } - - /** - * Used to ensure that all custom tag types have a defaulted collection when vanilla is initializing a defaulted TagCollectionManager - * - * @apiNote Internal: For use by TagCollectionManager - */ - public static void populateTagCollectionManager() - { - //Default the tag collections - resetCachedTagCollections(false, false); - if (!customTagTypes.isEmpty()) - { - LOGGER.debug("Populated the TagCollectionManager with {} extra types", customTagTypes.size()); - } - } - - /** - * Updates the custom tag types' tags from reloading via NetworkTagManager - * - * @apiNote Internal: For use by NetworkTagManager - */ - public static void updateCustomTagTypes(List tagCollectionReaders) - { - ImmutableMap.Builder> builder = ImmutableMap.builder(); - for (TagCollectionReaderInfo info : tagCollectionReaders) - { - builder.put(info.tagType, info.reader.build(info.tagBuilders)); - } - customTagTypes = builder.build(); - } - - /** - * Updates the custom tag types' tags from packet - * - * @apiNote Internal - */ - public static void updateCustomTagTypes(SyncCustomTagTypes packet) - { - customTagTypes = packet.getCustomTagTypes(); - reinjectOptionalTagsCustomTypes(); - } - - /** - * Gets the completable future containing the reload results for all custom tag types. - * - * @apiNote Internal: For use by NetworkTagManager - */ - public static CompletableFuture> getCustomTagTypeReloadResults(ResourceManager resourceManager, Executor backgroundExecutor, Map> readers) - { - CompletableFuture> customResults = CompletableFuture.completedFuture(new ArrayList<>()); - for (Entry> entry : readers.entrySet()) - { - CompletableFuture> collectionFuture = - CompletableFuture.supplyAsync(() -> entry.getValue().load(resourceManager), backgroundExecutor); - customResults = customResults.thenCombine(collectionFuture, (results, result) -> { - results.add(new TagCollectionReaderInfo(entry.getKey(), entry.getValue(), result)); - return results; - }); - } - return customResults; - } - - /** - * Add all the missing optional tags back into the custom tag types tag collections - * - * @apiNote Internal - */ - public static void reinjectOptionalTagsCustomTypes() - { - ImmutableMap.Builder> builder = ImmutableMap.builder(); - for (Entry> entry : customTagTypes.entrySet()) - { - ResourceLocation registry = entry.getKey(); - StaticTagHelper tagRegistry = StaticTags.get(registry); - TagCollection tagCollection = entry.getValue(); - builder.put(registry, tagRegistry == null ? tagCollection : tagRegistry.reinjectOptionalTags((TagCollection) tagCollection)); - } - customTagTypes = builder.build(); - } - - /** - * Gets an {@link IForgeTagContainer} with empty custom tag type collections to allow for checking if the client is requiring any tags of custom tag types. - * - * @apiNote Internal: For use with validating missing tags when connecting to a vanilla server - */ - public static TagContainer withNoCustom(TagContainer tagCollectionSupplier) - { - ImmutableMap.Builder> builder = ImmutableMap.builder(); - for (ResourceLocation registryName : customTagTypeNames) - { - StaticTagHelper tagRegistry = StaticTags.get(registryName); - if (tagRegistry != null) - { - builder.put(registryName, TagCollection.of(Collections.emptyMap())); - } - } - return withSpecificCustom(tagCollectionSupplier, builder.build()); - } - - /** - * Gets an {@link IForgeTagContainer} with specific custom tag types for testing if any tags are missing. - * - * @apiNote Internal - */ - public static TagContainer withSpecificCustom(TagContainer tagCollectionSupplier, Map> customTagTypes) - { - return new TagContainer(tagCollectionSupplier.collections) - { - @Override - public Map> getCustomTagTypes() - { - return customTagTypes; - } - }; - } - - /** - * Helper storage class for keeping track of various data for all custom tag types in the NetworkTagReader to make the code easier to read. - * - * @apiNote Internal: For use by NetworkTagManager - */ - public static class TagCollectionReaderInfo - { - - private final ResourceLocation tagType; - private final TagLoader reader; - private final Map tagBuilders; - - private TagCollectionReaderInfo(ResourceLocation tagType, TagLoader reader, Map tagBuilders) - { - this.tagType = tagType; - this.reader = reader; - this.tagBuilders = tagBuilders; - } + TagContainer.Builder builder = new TagContainer.Builder(); + //noinspection unchecked,rawtypes + StaticTags.visitHelpers(h -> builder.add(h.getKey(), h.reinjectOptionalTags(tagCollectionSupplier.getOrEmpty((ResourceKey) h.getKey())))); + return builder.build(); } } \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java b/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java index 16e84bec1b..207e12b387 100644 --- a/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java +++ b/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java @@ -36,6 +36,7 @@ import net.minecraftforge.registries.IForgeRegistryEntry; public abstract class ForgeRegistryTagsProvider> extends TagsProvider { + //TODO add game event tags here when they become a forge registry //Special handling for vanilla tag types in case someone decides to use the ForgeRegistryTagsProvider instead of one of the vanilla subtypes private static final Map, String> vanillaTypes = ImmutableMap., String>builder() .put(ForgeRegistries.BLOCKS, "blocks") diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeTagContainer.java b/src/main/java/net/minecraftforge/common/extensions/IForgeTagContainer.java deleted file mode 100644 index de0d36e094..0000000000 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeTagContainer.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Minecraft Forge - * Copyright (c) 2016-2021. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation version 2.1 - * of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -package net.minecraftforge.common.extensions; - -import java.util.Map; -import net.minecraft.tags.TagCollection; -import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.common.ForgeTagHandler; -import net.minecraftforge.registries.IForgeRegistry; -import net.minecraftforge.registries.IForgeRegistryEntry; - -public interface IForgeTagContainer -{ - /** - * Gets the tag map of registry names to tag collections for the various custom tag types. - * - * @apiNote Prefer using one of the getCustomTypeCollection methods - */ - default Map> getCustomTagTypes() - { - return ForgeTagHandler.getCustomTagTypes(); - } - - /** - * Gets the {@link TagCollection} for a forge registry with the given name, or throws an exception if the registry doesn't support custom tag types. - * @param regName Name of the forge registry - * @return The tag collection - */ - default TagCollection getCustomTypeCollection(ResourceLocation regName) - { - if (!ForgeTagHandler.getCustomTagTypeNames().contains(regName)) throw new IllegalArgumentException("Registry " + regName + ", does not support custom tag types"); - return getCustomTagTypes().get(regName); - } - - /** - * Gets the {@link TagCollection} for a forge registry, or throws an exception if the registry doesn't support custom tag types. - * @param reg Forge registry - * @return The tag collection - */ - default > TagCollection getCustomTypeCollection(IForgeRegistry reg) - { - return (TagCollection) getCustomTypeCollection(reg.getRegistryName()); - } -} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/event/TagsUpdatedEvent.java b/src/main/java/net/minecraftforge/event/TagsUpdatedEvent.java index 05043d80c5..2b783bd29a 100644 --- a/src/main/java/net/minecraftforge/event/TagsUpdatedEvent.java +++ b/src/main/java/net/minecraftforge/event/TagsUpdatedEvent.java @@ -20,11 +20,14 @@ package net.minecraftforge.event; import net.minecraft.tags.TagContainer; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.Event; /** - * Fired on the client when {@link ITagCollectionSupplier} has all of its tags synced from the server to the client (just after a client has connected). - * Fired on the server when {@link ITagCollectionSupplier} has read all tags from disk (during a data reload). + * Fired on the client when : + * {@link TagContainer} has all of its tags synced from the server to the client (just after a client has connected). + * The integrated server is about to be created see {@link net.minecraft.client.Minecraft#doLoadLevel} + * Fired on the server when {@link TagContainer} has read all tags from disk, during initial load and after the reload command is used * This event is fired on the {@link MinecraftForge#EVENT_BUS} * On the client, this event fires on the Client Thread. * On the server, this event may be fired on the Server Thread, or an async reloader thread. @@ -45,26 +48,4 @@ public class TagsUpdatedEvent extends Event { return manager; } - - /** - * Fired after the Vanilla Tag types have been processed - */ - public static class VanillaTagTypes extends TagsUpdatedEvent - { - public VanillaTagTypes(TagContainer manager) - { - super(manager); - } - } - - /** - * Fired after any custom tag types have been processed - */ - public static class CustomTagTypes extends TagsUpdatedEvent - { - public CustomTagTypes(TagContainer manager) - { - super(manager); - } - } } diff --git a/src/main/java/net/minecraftforge/fmllegacy/network/FMLPlayMessages.java b/src/main/java/net/minecraftforge/fmllegacy/network/FMLPlayMessages.java index 2f3000e389..8e9006d18b 100644 --- a/src/main/java/net/minecraftforge/fmllegacy/network/FMLPlayMessages.java +++ b/src/main/java/net/minecraftforge/fmllegacy/network/FMLPlayMessages.java @@ -19,49 +19,27 @@ package net.minecraftforge.fmllegacy.network; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; import io.netty.buffer.Unpooled; -import java.util.List; -import java.util.Map; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.inventory.MenuAccess; import net.minecraft.client.gui.screens.MenuScreens; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.multiplayer.ClientLevel; -import net.minecraft.resources.ResourceKey; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.MenuType; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.tags.Tag; -import net.minecraft.tags.TagCollection; -import net.minecraft.tags.TagContainer; -import net.minecraft.tags.StaticTags; -import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Mth; import net.minecraft.world.phys.Vec3; -import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.level.Level; -import net.minecraftforge.common.ForgeTagHandler; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.TagsUpdatedEvent; import net.minecraftforge.fmllegacy.LogicalSidedProvider; import net.minecraft.core.Registry; import net.minecraft.network.chat.Component; import net.minecraftforge.fmllegacy.common.registry.IEntityAdditionalSpawnData; - import java.util.Optional; import java.util.UUID; import java.util.function.Supplier; -import net.minecraftforge.registries.IForgeRegistry; -import net.minecraftforge.registries.IForgeRegistryEntry; -import net.minecraftforge.registries.RegistryManager; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; public class FMLPlayMessages { @@ -326,113 +304,4 @@ public class FMLPlayMessages return additionalData; } } - - public static class SyncCustomTagTypes - { - private static final Logger LOGGER = LogManager.getLogger(); - private final Map> customTagTypeCollections; - - SyncCustomTagTypes(Map> customTagTypeCollections) - { - this.customTagTypeCollections = customTagTypeCollections; - } - - public Map> getCustomTagTypes() - { - return customTagTypeCollections; - } - - public static void encode(SyncCustomTagTypes msg, FriendlyByteBuf buf) - { - buf.writeVarInt(msg.customTagTypeCollections.size()); - msg.customTagTypeCollections.forEach((registryName, modded) -> forgeTagCollectionWrite(buf, registryName, modded.getAllTags())); - } - - private static void forgeTagCollectionWrite(FriendlyByteBuf buf, ResourceLocation registryName, Map> tags) - { - buf.writeResourceLocation(registryName); - buf.writeVarInt(tags.size()); - tags.forEach((name, tag) -> { - buf.writeResourceLocation(name); - List elements = tag.getValues(); - buf.writeVarInt(elements.size()); - for (T element : elements) - { - buf.writeResourceLocation(((IForgeRegistryEntry) element).getRegistryName()); - } - }); - } - - public static SyncCustomTagTypes decode(FriendlyByteBuf buf) - { - ImmutableMap.Builder> builder = ImmutableMap.builder(); - int size = buf.readVarInt(); - for (int i = 0; i < size; i++) - { - ResourceLocation regName = buf.readResourceLocation(); - IForgeRegistry registry = RegistryManager.ACTIVE.getRegistry(regName); - if (registry != null) - { - builder.put(regName, readTagCollection(buf, registry)); - } - } - return new SyncCustomTagTypes(builder.build()); - } - - private static > TagCollection readTagCollection(FriendlyByteBuf buf, IForgeRegistry registry) - { - Map> tags = Maps.newHashMap(); - int totalTags = buf.readVarInt(); - for (int i = 0; i < totalTags; i++) - { - ImmutableSet.Builder elementBuilder = ImmutableSet.builder(); - ResourceLocation name = buf.readResourceLocation(); - int totalElements = buf.readVarInt(); - for (int j = 0; j < totalElements; j++) - { - T element = registry.getValue(buf.readResourceLocation()); - if (element != null) - { - elementBuilder.add(element); - } - } - tags.put(name, Tag.fromSet(elementBuilder.build())); - } - return TagCollection.of(tags); - } - - public static void handle(SyncCustomTagTypes msg, Supplier ctx) - { - ctx.get().enqueueWork(() -> { - if (Minecraft.getInstance().level != null) - { - TagContainer tagCollectionSupplier = Minecraft.getInstance().level.getTagManager(); - //Validate that all the tags exist using the tag type collections from the packet - // We mimic vanilla in that we validate before updating the actual stored tags so that it can gracefully fallback - // to the last working set of tags - //Note: We gracefully ignore any tag types the server may have that we don't as they won't be in our tag registry - // so they won't be validated - //Override and use the tags from the packet to test for validation before we actually set them - Multimap>, ResourceLocation> missingTags = StaticTags.getAllMissingTags(ForgeTagHandler.withSpecificCustom(tagCollectionSupplier, msg.customTagTypeCollections)); - if (missingTags.isEmpty()) - { - //If we have no missing tags, update the custom tag types - ForgeTagHandler.updateCustomTagTypes(msg); - if (!ctx.get().getNetworkManager().isMemoryConnection()) - { - //And if everything hasn't already been set due to being in single player - // Fetch and update the custom tag types. We skip vanilla tag types as they have already been fetched - // And fire an event that the custom tag types have been updated - StaticTags.fetchCustomTagTypes(tagCollectionSupplier); - MinecraftForge.EVENT_BUS.post(new TagsUpdatedEvent.CustomTagTypes(tagCollectionSupplier)); - } - } else { - LOGGER.warn("Incomplete server tags, disconnecting. Missing: {}", missingTags); - ctx.get().getNetworkManager().disconnect(new TranslatableComponent("multiplayer.disconnect.missing_tags")); - } - } - }); - ctx.get().setPacketHandled(true); - } - } } diff --git a/src/main/java/net/minecraftforge/fmllegacy/network/NetworkHooks.java b/src/main/java/net/minecraftforge/fmllegacy/network/NetworkHooks.java index 35eea6b19e..f4c5649095 100644 --- a/src/main/java/net/minecraftforge/fmllegacy/network/NetworkHooks.java +++ b/src/main/java/net/minecraftforge/fmllegacy/network/NetworkHooks.java @@ -32,7 +32,6 @@ import io.netty.channel.ChannelHandlerContext; import net.minecraft.tags.TagCollection; import net.minecraft.tags.TagContainer; import net.minecraft.network.chat.TextComponent; -import net.minecraftforge.common.extensions.IForgeTagContainer; import net.minecraftforge.fml.util.thread.EffectiveSide; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -234,33 +233,6 @@ public class NetworkHooks MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, c)); } - /** - * Syncs the custom tag types attached to a {@link IForgeTagContainer} to all connected players. - * @param tagCollectionSupplier The tag collection supplier containing the custom tags - */ - public static void syncCustomTagTypes(TagContainer tagCollectionSupplier) - { - Map> customTagTypes = tagCollectionSupplier.getCustomTagTypes(); - if (!customTagTypes.isEmpty()) - { - FMLNetworkConstants.playChannel.send(PacketDistributor.ALL.noArg(), new FMLPlayMessages.SyncCustomTagTypes(customTagTypes)); - } - } - - /** - * Syncs the custom tag types attached to a {@link IForgeTagContainer} to the given player. - * @param player The player to sync the custom tags to. - * @param tagCollectionSupplier The tag collection supplier containing the custom tags - */ - public static void syncCustomTagTypes(ServerPlayer player, TagContainer tagCollectionSupplier) - { - Map> customTagTypes = tagCollectionSupplier.getCustomTagTypes(); - if (!customTagTypes.isEmpty()) - { - FMLNetworkConstants.playChannel.sendTo(new FMLPlayMessages.SyncCustomTagTypes(customTagTypes), player.connection.getConnection(), NetworkDirection.PLAY_TO_CLIENT); - } - } - @Nullable public static FMLConnectionData getConnectionData(Connection mgr) { diff --git a/src/main/java/net/minecraftforge/fmllegacy/network/NetworkInitialization.java b/src/main/java/net/minecraftforge/fmllegacy/network/NetworkInitialization.java index 80f51475b6..708ca4f6db 100644 --- a/src/main/java/net/minecraftforge/fmllegacy/network/NetworkInitialization.java +++ b/src/main/java/net/minecraftforge/fmllegacy/network/NetworkInitialization.java @@ -97,19 +97,6 @@ class NetworkInitialization { consumer(FMLPlayMessages.OpenContainer::handle). add(); - //TODO Dimensions.. -// playChannel.messageBuilder(FMLPlayMessages.DimensionInfoMessage.class, 2) -// .decoder(FMLPlayMessages.DimensionInfoMessage::decode) -// .encoder(FMLPlayMessages.DimensionInfoMessage::encode) -// .consumer(FMLPlayMessages.DimensionInfoMessage::handle) -// .add(); - - playChannel.messageBuilder(FMLPlayMessages.SyncCustomTagTypes.class, 3). - decoder(FMLPlayMessages.SyncCustomTagTypes::decode). - encoder(FMLPlayMessages.SyncCustomTagTypes::encode). - consumer(FMLPlayMessages.SyncCustomTagTypes::handle). - add(); - return playChannel; } diff --git a/src/main/java/net/minecraftforge/network/VanillaConnectionNetworkFilter.java b/src/main/java/net/minecraftforge/network/VanillaConnectionNetworkFilter.java index efea67c32c..4c9dab6164 100644 --- a/src/main/java/net/minecraftforge/network/VanillaConnectionNetworkFilter.java +++ b/src/main/java/net/minecraftforge/network/VanillaConnectionNetworkFilter.java @@ -21,18 +21,25 @@ package net.minecraftforge.network; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.function.BiConsumer; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import io.netty.channel.ChannelHandler; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.synchronization.ArgumentTypes; +import net.minecraft.core.Registry; import net.minecraft.network.protocol.Packet; import net.minecraft.network.Connection; import net.minecraft.network.protocol.game.ClientboundCommandsPacket; import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket; +import net.minecraft.network.protocol.game.ClientboundUpdateTagsPacket; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagCollection; +import net.minecraftforge.common.ForgeTagHandler; import net.minecraftforge.fmllegacy.network.NetworkHooks; import net.minecraftforge.registries.ForgeRegistries; @@ -58,6 +65,7 @@ public class VanillaConnectionNetworkFilter extends VanillaPacketFilter ImmutableMap.>, BiConsumer, List>>>builder() .put(handler(ClientboundUpdateAttributesPacket.class, VanillaConnectionNetworkFilter::filterEntityProperties)) .put(handler(ClientboundCommandsPacket.class, VanillaConnectionNetworkFilter::filterCommandList)) + .put(handler(ClientboundUpdateTagsPacket.class, VanillaConnectionNetworkFilter::filterCustomTagTypes)) .build() ); } @@ -99,4 +107,15 @@ public class VanillaConnectionNetworkFilter extends VanillaPacketFilter }); return new ClientboundCommandsPacket(newRoot); } + + /** + * Filters out custom tag types that the vanilla client won't recognize. + * It prevents a rare error from logging and reduces the packet size + */ + private static ClientboundUpdateTagsPacket filterCustomTagTypes(ClientboundUpdateTagsPacket packet) { + Map>, TagCollection.NetworkPayload> tags = packet.getTags() + .entrySet().stream().filter(e -> !ForgeTagHandler.getCustomTagTypeNames().contains(e.getKey().location())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + return new ClientboundUpdateTagsPacket(tags); + } } diff --git a/src/main/java/net/minecraftforge/registries/GameData.java b/src/main/java/net/minecraftforge/registries/GameData.java index 7dbdddd144..f0040e66f8 100644 --- a/src/main/java/net/minecraftforge/registries/GameData.java +++ b/src/main/java/net/minecraftforge/registries/GameData.java @@ -404,7 +404,7 @@ public class GameData { LOGGER.debug(REGISTRIES, "Registering custom tag type for: {}", registryName); customTagTypes.add(registryName); - StaticTags.create(ResourceKey.createRegistryKey(registryName), entry.getValue().getTagFolder()); + StaticTags.create(ResourceKey.createRegistryKey(registryName), "tags/" + entry.getValue().getTagFolder()); } } ForgeTagHandler.setCustomTagTypes(customTagTypes); diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 16b067c419..a1783cdd54 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -205,6 +205,7 @@ public net.minecraft.tags.Tag$BuilderEntry (Lnet/minecraft/tags/Tag$Entry; public net.minecraft.tags.BlockTags m_13116_(Ljava/lang/String;)Lnet/minecraft/tags/Tag$Named; # bind public net.minecraft.tags.EntityTypeTags m_13127_(Ljava/lang/String;)Lnet/minecraft/tags/Tag$Named; # bind public net.minecraft.tags.FluidTags m_13134_(Ljava/lang/String;)Lnet/minecraft/tags/Tag$Named; # bind +public net.minecraft.tags.GameEventTags m_144307_(Ljava/lang/String;)Lnet/minecraft/tags/Tag$Named; # bind public net.minecraft.tags.ItemTags m_13194_(Ljava/lang/String;)Lnet/minecraft/tags/Tag$Named; # bind protected net.minecraft.tags.StaticTagHelper$Wrapper f_13251_ # tag public net.minecraft.tags.TagContainer (Ljava/util/Map;)V # constructor diff --git a/src/main/resources/forge.exc b/src/main/resources/forge.exc index 9e3d3282ca..7ec7f00bb6 100644 --- a/src/main/resources/forge.exc +++ b/src/main/resources/forge.exc @@ -16,6 +16,7 @@ net/minecraft/client/resources/model/WeightedBakedModel.getQuads(Lnet/minecraft/ net/minecraft/data/tags/BlockTagsProvider.(Lnet/minecraft/data/DataGenerator;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;)V=|p_126511_,modId,existingFileHelper net/minecraft/data/tags/EntityTypeTagsProvider.(Lnet/minecraft/data/DataGenerator;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;)V=|p_126517_,modId,existingFileHelper net/minecraft/data/tags/FluidTagsProvider.(Lnet/minecraft/data/DataGenerator;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;)V=|p_126523_,modId,existingFileHelper +net/minecraft/data/tags/GameEventTagsProvider.(Lnet/minecraft/data/DataGenerator;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;)V=|p_126517_,modId,existingFileHelper net/minecraft/data/tags/ItemTagsProvider.(Lnet/minecraft/data/DataGenerator;Lnet/minecraft/data/tags/BlockTagsProvider;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;)V=|p_126530_,p_126531_,modId,existingFileHelper net/minecraft/data/tags/TagsProvider.(Lnet/minecraft/data/DataGenerator;Lnet/minecraft/core/Registry;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;)V=|p_126546_,p_126547_,modId,existingFileHelper net/minecraft/data/tags/TagsProvider.(Lnet/minecraft/data/DataGenerator;Lnet/minecraft/core/Registry;Ljava/lang/String;Lnet/minecraftforge/common/data/ExistingFileHelper;Ljava/lang/String;)V=|p_126546_,p_126547_,modId,existingFileHelper,folder diff --git a/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java b/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java index 4974c028b8..c4cb01e9a9 100644 --- a/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java +++ b/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java @@ -19,12 +19,12 @@ package net.minecraftforge.debug.misc; -import com.google.common.collect.Sets; import java.util.Set; import java.util.function.Supplier; import javax.annotation.Nullable; import net.minecraft.core.Registry; import net.minecraft.data.DataGenerator; +import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.Enchantments; @@ -68,10 +68,11 @@ public class CustomTagTypesTest private static final ResourceLocation customRegistryName = new ResourceLocation(MODID, "custom_type_registry"); private static final DeferredRegister CUSTOMS = DeferredRegister.create(Custom.class, MODID); private static final RegistryObject CUSTOM = CUSTOMS.register("custom", Custom::new); + private static final ResourceKey> CUSTOM_KEY = ResourceKey.createRegistryKey(customRegistryName); private static final Supplier> CUSTOM_REG = CUSTOMS.makeRegistry(customRegistryName.getPath(), () -> new RegistryBuilder().tagFolder(MODID + "/custom_types")); - private static final Tag.Named TESTS = ForgeTagHandler.createOptionalTag(customRegistryName, new ResourceLocation(MODID, "tests"), Sets.newHashSet(CUSTOM)); - private static final Tag.Named OPTIONAL_TEST = ItemTags.createOptional(new ResourceLocation(MODID, "optional_test"), Sets.newHashSet(() -> Items.BONE)); + private static final Tag.Named TESTS = ForgeTagHandler.createOptionalTag(customRegistryName, new ResourceLocation(MODID, "tests"), Set.of(CUSTOM)); + private static final Tag.Named OPTIONAL_TEST = ItemTags.createOptional(new ResourceLocation(MODID, "optional_test"), Set.of(() -> Items.BONE)); private static final Tag.Named FIRE = ForgeTagHandler.createOptionalTag(ForgeRegistries.ENCHANTMENTS, new ResourceLocation(MODID, "fire")); private static final Tag.Named DAMAGE = ForgeTagHandler.createOptionalTag(ForgeRegistries.POTIONS, new ResourceLocation(MODID, "damage")); private static final Tag.Named> STORAGE = ForgeTagHandler.createOptionalTag(ForgeRegistries.BLOCK_ENTITIES, new ResourceLocation(MODID, "storage")); @@ -103,6 +104,7 @@ public class CustomTagTypesTest if (!itemStack.isEmpty()) { LOGGER.info("{} {} {}", Items.BONE.getTags(), OPTIONAL_TEST.getValues().size(), SerializationTags.getInstance().getOrEmpty(Registry.ITEM_REGISTRY).getTag(new ResourceLocation(MODID, "optional_test"))); + LOGGER.info("{} {}", CUSTOM.get().getTags(), TESTS.getValues().size()); EnchantmentHelper.getEnchantments(itemStack).forEach((enchantment, level) -> logTagsIfPresent(enchantment.getTags())); if (itemStack.getItem() instanceof PotionItem) logTagsIfPresent(PotionUtils.getPotion(itemStack).getTags()); BlockEntity blockEntity = event.getWorld().getBlockEntity(event.getPos()); @@ -120,17 +122,12 @@ public class CustomTagTypesTest public static class Custom extends ForgeRegistryEntry { - private final ReverseTagWrapper reverseTags = new ReverseTagWrapper<>(this, () -> SerializationTags.getInstance().getCustomTypeCollection(CUSTOM_REG.get())); + private final ReverseTagWrapper reverseTags = new ReverseTagWrapper<>(this, () -> SerializationTags.getInstance().getOrEmpty(CUSTOM_KEY)); public Set getTags() { return reverseTags.getTagNames(); } - - public boolean isIn(Tag tag) - { - return tag.contains(this); - } } public static class CustomRegistryTags extends ForgeRegistryTagsProvider @@ -203,13 +200,13 @@ public class CustomTagTypesTest @Override protected void addTags() { - tag(STORAGE).add(BlockEntityType.BARREL, BlockEntityType.CHEST, BlockEntityType.ENDER_CHEST); + tag(STORAGE).add(BlockEntityType.BARREL, BlockEntityType.CHEST, BlockEntityType.ENDER_CHEST, BlockEntityType.TRAPPED_CHEST); } @Override public String getName() { - return "Tile Entity Type Tags"; + return "Block Entity Type Tags"; } } }