From af75547d37037fe5f743693b8d6c75843aead58b Mon Sep 17 00:00:00 2001 From: Richard Freimer Date: Wed, 23 Sep 2020 23:12:11 -0400 Subject: [PATCH] Fix a few minor issues with custom tag types and fix OptionalNamedTags (#7316) --- .../play/ClientPlayNetHandler.java.patch | 10 +- .../resources/DataPackRegistries.java.patch | 8 ++ .../net/minecraft/tags/BlockTags.java.patch | 8 +- .../minecraft/tags/EntityTypeTags.java.patch | 8 +- .../net/minecraft/tags/FluidTags.java.patch | 8 +- .../tags/ITagCollectionSupplier.java.patch | 33 ++++++ .../net/minecraft/tags/ItemTags.java.patch | 8 +- .../tags/NetworkTagManager.java.patch | 9 ++ .../net/minecraft/tags/TagRegistry.java.patch | 50 ++++++--- src/generated_test/resources/.cache/cache | 1 - .../tags/biomes/oceans.json | 15 --- .../common/ForgeTagHandler.java | 104 +++++++++++++++++- .../data/ForgeRegistryTagsProvider.java | 6 + .../net/minecraftforge/fml/ModLoader.java | 4 +- .../fml/network/FMLPlayMessages.java | 38 +------ .../fml/network/NetworkHooks.java | 9 +- .../minecraftforge/registries/GameData.java | 2 +- .../debug/misc/CustomTagTypesTest.java | 51 +++++---- 18 files changed, 258 insertions(+), 114 deletions(-) delete mode 100644 src/generated_test/resources/data/custom_tag_types_test/tags/biomes/oceans.json diff --git a/patches/minecraft/net/minecraft/client/network/play/ClientPlayNetHandler.java.patch b/patches/minecraft/net/minecraft/client/network/play/ClientPlayNetHandler.java.patch index 02491b069..45aed706b 100644 --- a/patches/minecraft/net/minecraft/client/network/play/ClientPlayNetHandler.java.patch +++ b/patches/minecraft/net/minecraft/client/network/play/ClientPlayNetHandler.java.patch @@ -77,21 +77,23 @@ if (effect != null) { EffectInstance effectinstance = new EffectInstance(effect, p_147260_1_.func_180755_e(), p_147260_1_.func_149428_f(), p_147260_1_.func_186984_g(), p_147260_1_.func_179707_f(), p_147260_1_.func_205527_h()); effectinstance.func_100012_b(p_147260_1_.func_149429_c()); -@@ -1388,11 +1401,12 @@ +@@ -1388,11 +1401,14 @@ public void func_199723_a(STagsListPacket p_199723_1_) { PacketThreadUtil.func_218797_a(p_199723_1_, this, this.field_147299_f); ITagCollectionSupplier itagcollectionsupplier = p_199723_1_.func_199858_a(); - Multimap multimap = TagRegistryManager.func_242198_b(itagcollectionsupplier); -+ Multimap multimap = TagRegistryManager.validateVanillaTags(itagcollectionsupplier); ++ boolean vanillaConnection = net.minecraftforge.fml.network.NetworkHooks.isVanillaConnection(field_147302_e); ++ Multimap multimap = vanillaConnection ? TagRegistryManager.func_242198_b(net.minecraftforge.common.ForgeTagHandler.withNoCustom(itagcollectionsupplier)) : TagRegistryManager.validateVanillaTags(itagcollectionsupplier);//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()) { field_147301_d.warn("Incomplete server tags, disconnecting. Missing: {}", (Object)multimap); this.field_147302_e.func_150718_a(new TranslationTextComponent("multiplayer.disconnect.missing_tags")); } else { -+ net.minecraftforge.common.ForgeTagHandler.resetCachedTagCollections(); ++ net.minecraftforge.common.ForgeTagHandler.resetCachedTagCollections(true, vanillaConnection); ++ itagcollectionsupplier = ITagCollectionSupplier.reinjectOptionalTags(itagcollectionsupplier); this.field_199725_m = itagcollectionsupplier; if (!this.field_147302_e.func_150731_c()) { itagcollectionsupplier.func_242212_e(); -@@ -1883,10 +1897,12 @@ +@@ -1883,10 +1899,12 @@ int l5 = packetbuffer.readInt(); this.field_147299_f.field_184132_p.field_229018_q_.func_229022_a_(blockpos8, l3, s10, l5); } else { diff --git a/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch b/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch index 79d6306f4..786414aef 100644 --- a/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch +++ b/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch @@ -8,3 +8,11 @@ } public FunctionReloader func_240960_a_() { +@@ -81,6 +82,7 @@ + + public void func_240971_i_() { + this.field_240955_e_.func_242231_a().func_242212_e(); ++ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.TagsUpdatedEvent.CustomTagTypes(field_240955_e_.func_242231_a())); + } + + public void close() { diff --git a/patches/minecraft/net/minecraft/tags/BlockTags.java.patch b/patches/minecraft/net/minecraft/tags/BlockTags.java.patch index f11705262..850367c33 100644 --- a/patches/minecraft/net/minecraft/tags/BlockTags.java.patch +++ b/patches/minecraft/net/minecraft/tags/BlockTags.java.patch @@ -1,11 +1,15 @@ --- a/net/minecraft/tags/BlockTags.java +++ b/net/minecraft/tags/BlockTags.java -@@ -97,6 +97,10 @@ +@@ -97,6 +97,14 @@ return field_199899_c.func_232937_a_(p_199894_0_); } + public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation name) { -+ return field_199899_c.createOptional(name, () -> null); ++ return createOptional(name, null); ++ } ++ ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation name, @javax.annotation.Nullable java.util.Set> defaults) { ++ return field_199899_c.createOptional(name, defaults); + } + public static ITagCollection func_199896_a() { diff --git a/patches/minecraft/net/minecraft/tags/EntityTypeTags.java.patch b/patches/minecraft/net/minecraft/tags/EntityTypeTags.java.patch index 1b247a881..98d7515ca 100644 --- a/patches/minecraft/net/minecraft/tags/EntityTypeTags.java.patch +++ b/patches/minecraft/net/minecraft/tags/EntityTypeTags.java.patch @@ -1,11 +1,15 @@ --- a/net/minecraft/tags/EntityTypeTags.java +++ b/net/minecraft/tags/EntityTypeTags.java -@@ -16,6 +16,10 @@ +@@ -16,6 +16,14 @@ return field_219766_c.func_232937_a_(p_232896_0_); } + public static net.minecraftforge.common.Tags.IOptionalNamedTag> createOptional(ResourceLocation name) { -+ return field_219766_c.createOptional(name, () -> null); ++ return createOptional(name, null); ++ } ++ ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag> createOptional(ResourceLocation name, @javax.annotation.Nullable java.util.Set>> defaults) { ++ return field_219766_c.createOptional(name, defaults); + } + public static ITagCollection> func_219762_a() { diff --git a/patches/minecraft/net/minecraft/tags/FluidTags.java.patch b/patches/minecraft/net/minecraft/tags/FluidTags.java.patch index 937018ced..1500eac21 100644 --- a/patches/minecraft/net/minecraft/tags/FluidTags.java.patch +++ b/patches/minecraft/net/minecraft/tags/FluidTags.java.patch @@ -1,11 +1,15 @@ --- a/net/minecraft/tags/FluidTags.java +++ b/net/minecraft/tags/FluidTags.java -@@ -13,7 +13,16 @@ +@@ -13,7 +13,20 @@ return field_206961_c.func_232937_a_(p_206956_0_); } + public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation name) { -+ return field_206961_c.createOptional(name, () -> null); ++ return createOptional(name, null); ++ } ++ ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation name, @javax.annotation.Nullable java.util.Set> defaults) { ++ return field_206961_c.createOptional(name, defaults); + } + public static List> func_241280_c_() { diff --git a/patches/minecraft/net/minecraft/tags/ITagCollectionSupplier.java.patch b/patches/minecraft/net/minecraft/tags/ITagCollectionSupplier.java.patch index cb6c898e9..15b43221a 100644 --- a/patches/minecraft/net/minecraft/tags/ITagCollectionSupplier.java.patch +++ b/patches/minecraft/net/minecraft/tags/ITagCollectionSupplier.java.patch @@ -17,3 +17,36 @@ } default void func_242210_a(PacketBuffer p_242210_1_) { +@@ -58,4 +59,32 @@ + } + }; + } ++ ++ static ITagCollectionSupplier reinjectOptionalTags(ITagCollectionSupplier tagCollectionSupplier) { ++ ITagCollection blockTagCollection = BlockTags.field_199899_c.reinjectOptionalTags(tagCollectionSupplier.func_241835_a()); ++ ITagCollection itemTagCollection = ItemTags.field_199906_c.reinjectOptionalTags(tagCollectionSupplier.func_241836_b()); ++ ITagCollection fluidTagCollection = FluidTags.field_206961_c.reinjectOptionalTags(tagCollectionSupplier.func_241837_c()); ++ ITagCollection> entityTypeTagCollection = EntityTypeTags.field_219766_c.reinjectOptionalTags(tagCollectionSupplier.func_241838_d()); ++ return new ITagCollectionSupplier() { ++ @Override ++ public ITagCollection func_241835_a() { ++ return blockTagCollection; ++ } ++ ++ @Override ++ public ITagCollection func_241836_b() { ++ return itemTagCollection; ++ } ++ ++ @Override ++ public ITagCollection func_241837_c() { ++ return fluidTagCollection; ++ } ++ ++ @Override ++ public ITagCollection> func_241838_d() { ++ return entityTypeTagCollection; ++ } ++ }; ++ } + } diff --git a/patches/minecraft/net/minecraft/tags/ItemTags.java.patch b/patches/minecraft/net/minecraft/tags/ItemTags.java.patch index 50a4ebc6e..4aa8fbefc 100644 --- a/patches/minecraft/net/minecraft/tags/ItemTags.java.patch +++ b/patches/minecraft/net/minecraft/tags/ItemTags.java.patch @@ -1,11 +1,15 @@ --- a/net/minecraft/tags/ItemTags.java +++ b/net/minecraft/tags/ItemTags.java -@@ -65,6 +65,10 @@ +@@ -65,6 +65,14 @@ return field_199906_c.func_232937_a_(p_199901_0_); } + public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation name) { -+ return field_199906_c.createOptional(name, () -> null); ++ return createOptional(name, null); ++ } ++ ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation name, @javax.annotation.Nullable java.util.Set> defaults) { ++ return field_199906_c.createOptional(name, defaults); + } + public static ITagCollection func_199903_a() { diff --git a/patches/minecraft/net/minecraft/tags/NetworkTagManager.java.patch b/patches/minecraft/net/minecraft/tags/NetworkTagManager.java.patch index 7229ecbf6..d6dc3cea4 100644 --- a/patches/minecraft/net/minecraft/tags/NetworkTagManager.java.patch +++ b/patches/minecraft/net/minecraft/tags/NetworkTagManager.java.patch @@ -23,3 +23,12 @@ ITagCollectionSupplier itagcollectionsupplier = ITagCollectionSupplier.func_242209_a(itagcollection, itagcollection1, itagcollection2, itagcollection3); Multimap multimap = TagRegistryManager.func_242198_b(itagcollectionsupplier); if (!multimap.isEmpty()) { +@@ -43,6 +46,8 @@ + return p_232978_0_.getKey() + ":" + p_232978_0_.getValue(); + }).sorted().collect(Collectors.joining(","))); + } else { ++ itagcollectionsupplier = ITagCollectionSupplier.reinjectOptionalTags(itagcollectionsupplier); ++ net.minecraftforge.common.ForgeTagHandler.reinjectOptionalTagsCustomTypes(); + TagCollectionManager.func_242180_a(itagcollectionsupplier); + this.field_242230_e = itagcollectionsupplier; + } diff --git a/patches/minecraft/net/minecraft/tags/TagRegistry.java.patch b/patches/minecraft/net/minecraft/tags/TagRegistry.java.patch index 160a9d483..0fa8e2da7 100644 --- a/patches/minecraft/net/minecraft/tags/TagRegistry.java.patch +++ b/patches/minecraft/net/minecraft/tags/TagRegistry.java.patch @@ -15,7 +15,7 @@ + return add(new TagRegistry.NamedTag<>(new ResourceLocation(p_232937_1_))); + } + -+ public net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation key, @Nullable java.util.function.Supplier> defaults) { ++ public net.minecraftforge.common.Tags.IOptionalNamedTag createOptional(ResourceLocation key, @Nullable Set> defaults) { + return add(new TagRegistry.OptionalNamedTag<>(key, defaults)); + } + @@ -24,8 +24,8 @@ + return delayedAdd(tagRegistry, new TagRegistry.NamedTag<>(name)); + } + -+ /** Call via ForgeTagHandler#createOptionalTag to avoid any exceptions due to calling this after it is safe to call {@link #createOptional(ResourceLocation, java.util.function.Supplier)} */ -+ public static net.minecraftforge.common.Tags.IOptionalNamedTag createDelayedOptional(ResourceLocation tagRegistry, ResourceLocation key, @Nullable java.util.function.Supplier> defaults) { ++ /** Call via ForgeTagHandler#createOptionalTag to avoid any exceptions due to calling this after it is safe to call {@link #createOptional(ResourceLocation, Set)} */ ++ public static net.minecraftforge.common.Tags.IOptionalNamedTag createDelayedOptional(ResourceLocation tagRegistry, ResourceLocation key, @Nullable Set> defaults) { + return delayedAdd(tagRegistry, new TagRegistry.OptionalNamedTag<>(key, defaults)); + } + @@ -53,7 +53,28 @@ this.field_232931_c_.add(namedtag); return namedtag; } -@@ -56,7 +94,7 @@ +@@ -46,6 +84,20 @@ + }); + } + ++ public ITagCollection reinjectOptionalTags(ITagCollection tagCollection) { ++ java.util.Map> currentTags = tagCollection.func_241833_a(); ++ java.util.Map> missingOptionals = this.field_232931_c_.stream().filter(e -> e instanceof OptionalNamedTag && !currentTags.containsKey(e.func_230234_a_())).collect(Collectors.toMap(NamedTag::func_230234_a_, namedTag -> { ++ OptionalNamedTag optionalNamedTag = (OptionalNamedTag) namedTag; ++ optionalNamedTag.defaulted = true; ++ return optionalNamedTag.resolveDefaulted(); ++ })); ++ if (!missingOptionals.isEmpty()) { ++ missingOptionals.putAll(currentTags); ++ return ITagCollection.func_242202_a(missingOptionals); ++ } ++ return tagCollection; ++ } ++ + public ITagCollection func_232939_b_() { + return this.field_232930_b_; + } +@@ -56,7 +108,7 @@ public Set func_242189_b(ITagCollectionSupplier p_242189_1_) { ITagCollection itagcollection = this.field_242184_c.apply(p_242189_1_); @@ -62,7 +83,7 @@ ImmutableSet immutableset = ImmutableSet.copyOf(itagcollection.func_199908_a()); return Sets.difference(set, immutableset); } -@@ -93,5 +131,45 @@ +@@ -93,5 +145,40 @@ public List func_230236_b_() { return this.func_232944_c_().func_230236_b_(); } @@ -77,10 +98,10 @@ + + private static class OptionalNamedTag extends NamedTag implements net.minecraftforge.common.Tags.IOptionalNamedTag { + @Nullable -+ private final java.util.function.Supplier> defaults; -+ private boolean defaulted = true; ++ private final Set> defaults; ++ private boolean defaulted = false; + -+ private OptionalNamedTag(ResourceLocation name, @Nullable java.util.function.Supplier> defaults) { ++ private OptionalNamedTag(ResourceLocation name, @Nullable Set> defaults) { + super(name); + this.defaults = defaults; + } @@ -90,16 +111,11 @@ + return defaulted; + } + -+ @Override -+ void func_232943_a_(Function> p_232943_1_) { -+ super.func_232943_a_(p_232943_1_); -+ if (this.field_232942_b_ == null) { -+ this.defaulted = true; -+ Set defs = defaults == null ? null : defaults.get(); -+ this.field_232942_b_ = defs == null ? Tag.func_241284_a_() : Tag.func_241286_a_(defs); -+ } else { -+ this.defaulted = false; ++ Tag resolveDefaulted() { ++ if (defaults == null || defaults.isEmpty()) { ++ return Tag.func_241284_a_(); + } ++ return Tag.func_241286_a_(ImmutableSet.copyOf(defaults.stream().map(java.util.function.Supplier::get).collect(Collectors.toSet()))); + } + + @Override diff --git a/src/generated_test/resources/.cache/cache b/src/generated_test/resources/.cache/cache index 6eab62cfa..77d64f79f 100644 --- a/src/generated_test/resources/.cache/cache +++ b/src/generated_test/resources/.cache/cache @@ -67,7 +67,6 @@ a012d6d92bab1c91913bd0f2dc0492a0fce916f2 assets/minecraft/blockstates/wall_torch d2c8e860521c5e738ed0798337f5728d610825ff assets/piston_event_test/models/block/shiftonmove.json f51f026a75e27a0a53a76771d553c0e3385a2966 assets/piston_event_test/models/item/shiftonmove.json cf16f861eaf5815238c2278eb48bde0688cb73b7 assets/scaffolding_test/blockstates/scaffolding_method_test.json -42d3ce7d941eb8b80c3d541b937d0f93c9cf7c3c data/custom_tag_types_test/tags/biomes/oceans.json 862196702dcd5538c5ceb4ac6202e57eb5c8a97f data/custom_tag_types_test/tags/custom_tag_types_test/custom_types/tests.json 4fbaf6f4a3ea05cc071076e27f44ac81f9cc50e3 data/data_gen_test/advancements/conditional.json ed4cbf1a3a2f5d8969f6346fdc9acdbe81d0c919 data/data_gen_test/recipes/conditional.json diff --git a/src/generated_test/resources/data/custom_tag_types_test/tags/biomes/oceans.json b/src/generated_test/resources/data/custom_tag_types_test/tags/biomes/oceans.json deleted file mode 100644 index bb4783110..000000000 --- a/src/generated_test/resources/data/custom_tag_types_test/tags/biomes/oceans.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "replace": false, - "values": [ - "minecraft:ocean", - "minecraft:frozen_ocean", - "minecraft:deep_ocean", - "minecraft:warm_ocean", - "minecraft:lukewarm_ocean", - "minecraft:cold_ocean", - "minecraft:deep_warm_ocean", - "minecraft:deep_lukewarm_ocean", - "minecraft:deep_cold_ocean", - "minecraft:deep_frozen_ocean" - ] -} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/common/ForgeTagHandler.java b/src/main/java/net/minecraftforge/common/ForgeTagHandler.java index 5edc214b8..618ddfa50 100644 --- a/src/main/java/net/minecraftforge/common/ForgeTagHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeTagHandler.java @@ -25,6 +25,7 @@ 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; @@ -121,7 +122,7 @@ public class ForgeTagHandler * @param Type of the registry * @return An optional tag */ - public static > IOptionalNamedTag createOptionalTag(IForgeRegistry registry, ResourceLocation name, @Nullable Supplier> defaults) + public static > IOptionalNamedTag createOptionalTag(IForgeRegistry registry, ResourceLocation name, @Nullable Set> defaults) { validateRegistrySupportsTags(registry); if (tagTypesSet) @@ -180,7 +181,7 @@ public class ForgeTagHandler * @implNote This method only errors instantly if tag types have already been set, otherwise the error is delayed until after registries finish initializing * and we can validate if the custom registry really does support custom tags. */ - public static > IOptionalNamedTag createOptionalTag(ResourceLocation registryName, ResourceLocation name, @Nullable Supplier> defaults) + public static > IOptionalNamedTag createOptionalTag(ResourceLocation registryName, ResourceLocation name, @Nullable Set> defaults) { if (tagTypesSet) { @@ -220,6 +221,10 @@ public class ForgeTagHandler 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); + //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 + // potentially should end up being moved into an async processor. TagRegistry.performDelayedAdd(); } @@ -248,7 +253,7 @@ public class ForgeTagHandler * * @apiNote Internal */ - public static void resetCachedTagCollections() + public static void resetCachedTagCollections(boolean makeEmpty, boolean withOptional) { ImmutableMap.Builder> builder = ImmutableMap.builder(); for (ResourceLocation registryName : customTagTypeNames) @@ -256,7 +261,17 @@ public class ForgeTagHandler TagRegistry tagRegistry = TagRegistryManager.get(registryName); if (tagRegistry != null) { - builder.put(registryName, ITagCollection.func_242202_a(tagRegistry.func_241288_c_().stream().distinct().collect(Collectors.toMap(INamedTag::func_230234_a_, namedTag -> namedTag)))); + if (makeEmpty) + { + if (withOptional) + builder.put(registryName, tagRegistry.reinjectOptionalTags(ITagCollection.func_242202_a(Collections.emptyMap()))); + else + builder.put(registryName, ITagCollection.func_242202_a(Collections.emptyMap())); + } + else + { + builder.put(registryName, ITagCollection.func_242202_a(tagRegistry.func_241288_c_().stream().distinct().collect(Collectors.toMap(INamedTag::func_230234_a_, namedTag -> namedTag)))); + } } } customTagTypes = builder.build(); @@ -270,7 +285,7 @@ public class ForgeTagHandler public static ITagCollectionSupplier populateTagCollectionManager(ITagCollection blockTags, ITagCollection itemTags, ITagCollection fluidTags, ITagCollection> entityTypeTags) { //Default the tag collections - resetCachedTagCollections(); + resetCachedTagCollections(false, false); if (!customTagTypes.isEmpty()) { LOGGER.debug("Populated the TagCollectionManager with {} extra types", customTagTypes.size()); @@ -301,6 +316,7 @@ public class ForgeTagHandler public static void updateCustomTagTypes(SyncCustomTagTypes packet) { customTagTypes = packet.getCustomTagTypes(); + reinjectOptionalTagsCustomTypes(); } /** @@ -321,6 +337,84 @@ public class ForgeTagHandler 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(); + TagRegistry tagRegistry = TagRegistryManager.get(registry); + ITagCollection tagCollection = entry.getValue(); + builder.put(registry, tagRegistry == null ? tagCollection : tagRegistry.reinjectOptionalTags((ITagCollection) tagCollection)); + } + customTagTypes = builder.build(); + } + + /** + * Gets an {@link ITagCollectionSupplier} 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 ITagCollectionSupplier withNoCustom(ITagCollectionSupplier tagCollectionSupplier) + { + ImmutableMap.Builder> builder = ImmutableMap.builder(); + for (ResourceLocation registryName : customTagTypeNames) + { + TagRegistry tagRegistry = TagRegistryManager.get(registryName); + if (tagRegistry != null) + { + builder.put(registryName, ITagCollection.func_242202_a(Collections.emptyMap())); + } + } + return withSpecificCustom(tagCollectionSupplier, builder.build()); + } + + /** + * Gets an {@link ITagCollectionSupplier} with specific custom tag types for testing if any tags are missing. + * + * @apiNote Internal + */ + public static ITagCollectionSupplier withSpecificCustom(ITagCollectionSupplier tagCollectionSupplier, Map> customTagTypes) + { + return new ITagCollectionSupplier() + { + @Override + public ITagCollection func_241835_a() + { + return tagCollectionSupplier.func_241835_a(); + } + + @Override + public ITagCollection func_241836_b() + { + return tagCollectionSupplier.func_241836_b(); + } + + @Override + public ITagCollection func_241837_c() + { + return tagCollectionSupplier.func_241837_c(); + } + + @Override + public ITagCollection> func_241838_d() + { + return tagCollectionSupplier.func_241838_d(); + } + + @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. * diff --git a/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java b/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java index ead9df52b..d3a3fe24f 100644 --- a/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java +++ b/src/main/java/net/minecraftforge/common/data/ForgeRegistryTagsProvider.java @@ -70,4 +70,10 @@ public abstract class ForgeRegistryTagsProvider { return generator.getOutputFolder().resolve("data/" + id.getNamespace() + "/tags/" + folder + "/" + id.getPath() + ".json"); } + + @Override + protected String getTagFolder() + { + return folder; + } } diff --git a/src/main/java/net/minecraftforge/fml/ModLoader.java b/src/main/java/net/minecraftforge/fml/ModLoader.java index 459a42172..3d0d7fdcd 100644 --- a/src/main/java/net/minecraftforge/fml/ModLoader.java +++ b/src/main/java/net/minecraftforge/fml/ModLoader.java @@ -195,10 +195,10 @@ public class ModLoader dispatchAndHandleError(ModLoadingStage.CREATE_REGISTRIES, syncExecutor, parallelExecutor, periodicTask); ObjectHolderRegistry.findObjectHolders(); CapabilityManager.INSTANCE.injectCapabilities(modList.getAllScanData()); - statusConsumer.ifPresent(c->c.accept("Populating registries")); - dispatchAndHandleError(ModLoadingStage.LOAD_REGISTRIES, syncExecutor, parallelExecutor, periodicTask); statusConsumer.ifPresent(c->c.accept("Adding custom tag types")); GameData.setCustomTagTypesFromRegistries(); + statusConsumer.ifPresent(c->c.accept("Populating registries")); + dispatchAndHandleError(ModLoadingStage.LOAD_REGISTRIES, syncExecutor, parallelExecutor, periodicTask); statusConsumer.ifPresent(c->c.accept("Early mod loading complete")); } diff --git a/src/main/java/net/minecraftforge/fml/network/FMLPlayMessages.java b/src/main/java/net/minecraftforge/fml/network/FMLPlayMessages.java index 26aef4bae..02292c1e7 100644 --- a/src/main/java/net/minecraftforge/fml/network/FMLPlayMessages.java +++ b/src/main/java/net/minecraftforge/fml/network/FMLPlayMessages.java @@ -26,7 +26,6 @@ import com.google.common.collect.Multimap; import io.netty.buffer.Unpooled; import java.util.List; import java.util.Map; -import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.IHasContainer; import net.minecraft.client.gui.ScreenManager; @@ -34,10 +33,8 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.world.ClientWorld; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; -import net.minecraft.fluid.Fluid; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.ContainerType; -import net.minecraft.item.Item; import net.minecraft.network.PacketBuffer; import net.minecraft.tags.ITag; import net.minecraft.tags.ITagCollection; @@ -414,39 +411,8 @@ public class FMLPlayMessages // 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 - Multimap missingTags = TagRegistryManager.func_242198_b(new ITagCollectionSupplier() - { - @Override - public ITagCollection func_241835_a() - { - return tagCollectionSupplier.func_241835_a(); - } - - @Override - public ITagCollection func_241836_b() - { - return tagCollectionSupplier.func_241836_b(); - } - - @Override - public ITagCollection func_241837_c() - { - return tagCollectionSupplier.func_241837_c(); - } - - @Override - public ITagCollection> func_241838_d() - { - return tagCollectionSupplier.func_241838_d(); - } - - @Override - public Map> getCustomTagTypes() - { - //Override and use the tags from the packet to test for validation before we actually set them - return msg.customTagTypeCollections; - } - }); + //Override and use the tags from the packet to test for validation before we actually set them + Multimap missingTags = TagRegistryManager.func_242198_b(ForgeTagHandler.withSpecificCustom(tagCollectionSupplier, msg.customTagTypeCollections)); if (missingTags.isEmpty()) { //If we have no missing tags, update the custom tag types diff --git a/src/main/java/net/minecraftforge/fml/network/NetworkHooks.java b/src/main/java/net/minecraftforge/fml/network/NetworkHooks.java index a3adfb406..e34321410 100644 --- a/src/main/java/net/minecraftforge/fml/network/NetworkHooks.java +++ b/src/main/java/net/minecraftforge/fml/network/NetworkHooks.java @@ -119,9 +119,14 @@ public class NetworkHooks FMLNetworkConstants.playChannel.sendTo(new FMLPlayMessages.DimensionInfoMessage(player.dimension), manager, NetworkDirection.PLAY_TO_CLIENT); }*/ - public static void handleClientLoginSuccess(NetworkManager manager) { + public static boolean isVanillaConnection(NetworkManager manager) + { if (manager == null || manager.channel() == null) throw new NullPointerException("ARGH! Network Manager is null (" + manager != null ? "CHANNEL" : "MANAGER"+")" ); - if (getConnectionType(()->manager) == ConnectionType.VANILLA) { + return getConnectionType(() -> manager) == ConnectionType.VANILLA; + } + + public static void handleClientLoginSuccess(NetworkManager manager) { + if (isVanillaConnection(manager)) { LOGGER.info("Connected to a vanilla server. Catching up missing behaviour."); ConfigTracker.INSTANCE.loadDefaultServerConfigs(); } else { diff --git a/src/main/java/net/minecraftforge/registries/GameData.java b/src/main/java/net/minecraftforge/registries/GameData.java index 0706394b3..836d8d9e3 100644 --- a/src/main/java/net/minecraftforge/registries/GameData.java +++ b/src/main/java/net/minecraftforge/registries/GameData.java @@ -179,7 +179,7 @@ public class GameData makeRegistry(TREE_DECORATOR_TYPES, c(TreeDecoratorType.class)).disableSaving().disableSync().create(); // Dynamic Worldgen - makeRegistry(BIOMES, Biome.class).tagFolder("biomes").create(); + makeRegistry(BIOMES, Biome.class).create(); // Custom forge registries makeRegistry(DATA_SERIALIZERS, DataSerializerEntry.class, 256 /*vanilla space*/, MAX_VARINT).disableSaving().disableOverrides().addCallback(SerializerCallbacks.INSTANCE).create(); diff --git a/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java b/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java index 8793da312..6d5669b63 100644 --- a/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java +++ b/src/test/java/net/minecraftforge/debug/misc/CustomTagTypesTest.java @@ -19,44 +19,54 @@ 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.data.DataGenerator; +import net.minecraft.item.Item; +import net.minecraft.item.Items; import net.minecraft.tags.ITag; +import net.minecraft.tags.ItemTags; +import net.minecraft.tags.TagCollectionManager; import net.minecraft.util.ResourceLocation; -import net.minecraft.world.biome.Biome; -import net.minecraft.world.biome.Biomes; 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.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; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; @Mod(CustomTagTypesTest.MODID) public class CustomTagTypesTest { public static final String MODID = "custom_tag_types_test"; + private static final Logger LOGGER = LogManager.getLogger(MODID); 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 Supplier> CUSTOM_REG = CUSTOMS.makeRegistry(customRegistryName.getPath(), () -> new RegistryBuilder().tagFolder(MODID + "/custom_types")); - private static final ITag.INamedTag OCEANS = ForgeTagHandler.makeWrapperTag(ForgeRegistries.BIOMES, new ResourceLocation(MODID, "oceans")); - private static final ITag.INamedTag TESTS = ForgeTagHandler.makeWrapperTag(customRegistryName, new ResourceLocation(MODID, "tests")); + 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)); public CustomTagTypesTest() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); CUSTOMS.register(modBus); modBus.addListener(this::gatherData); + MinecraftForge.EVENT_BUS.addListener(this::projectileImpact); } private void gatherData(GatherDataEvent event) @@ -64,32 +74,27 @@ public class CustomTagTypesTest if (event.includeServer()) { DataGenerator gen = event.getGenerator(); - ExistingFileHelper existingFileHelper = event.getExistingFileHelper(); - gen.addProvider(new BiomeTags(gen, existingFileHelper)); - gen.addProvider(new CustomRegistryTags(gen, existingFileHelper)); + gen.addProvider(new CustomRegistryTags(gen, event.getExistingFileHelper())); } } - public static class Custom extends ForgeRegistryEntry { - } - - public static class BiomeTags extends ForgeRegistryTagsProvider + private void projectileImpact(ProjectileImpactEvent.Arrow event) { - public BiomeTags(DataGenerator gen, @Nullable ExistingFileHelper existingFileHelper) + LOGGER.info("{} {} {}", Items.BONE.getTags(), OPTIONAL_TEST.func_230236_b_().size(), TagCollectionManager.func_242178_a().func_241836_b().get(new ResourceLocation(MODID, "optional_test"))); + } + + public static class Custom extends ForgeRegistryEntry + { + private final ReverseTagWrapper reverseTags = new ReverseTagWrapper<>(this, () -> TagCollectionManager.func_242178_a().getCustomTypeCollection(CUSTOM_REG.get())); + + public Set getTags() { - super(gen, ForgeRegistries.BIOMES, MODID, existingFileHelper); + return reverseTags.getTagNames(); } - @Override - protected void registerTags() + public boolean isIn(ITag tag) { - func_240522_a_(OCEANS).add(Biomes.OCEAN, Biomes.FROZEN_OCEAN, Biomes.DEEP_OCEAN, Biomes.WARM_OCEAN, Biomes.LUKEWARM_OCEAN, - Biomes.COLD_OCEAN, Biomes.DEEP_WARM_OCEAN, Biomes.DEEP_LUKEWARM_OCEAN, Biomes.DEEP_COLD_OCEAN, Biomes.DEEP_FROZEN_OCEAN); - } - - @Override - public String getName() { - return "Biome Tags"; + return tag.func_230235_a_(this); } }