From 6c6a4817e733d006d9d1455668ee6d244271adad Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Fri, 21 Jan 2022 04:26:54 +0800 Subject: [PATCH] Merge values of defaulted optional tags, Fixes issue where multiple mods declare the same optional tag. (#8250) --- .../minecraft/tags/StaticTagHelper.java.patch | 2 +- .../debug/DuplicateOptionalTagTest.java | 99 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/minecraftforge/debug/DuplicateOptionalTagTest.java diff --git a/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch b/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch index d9e7568822..6600528918 100644 --- a/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch +++ b/patches/minecraft/net/minecraft/tags/StaticTagHelper.java.patch @@ -63,7 +63,7 @@ + OptionalNamedTag optionalNamedTag = (OptionalNamedTag) namedTag; + optionalNamedTag.defaulted = true; + return optionalNamedTag.resolveDefaulted(); -+ })); ++ }, (existingTag, newTag) -> SetTag.m_13222_(ImmutableSet.builder().addAll(existingTag.m_6497_()).addAll(newTag.m_6497_()).build()))); + if (!missingOptionals.isEmpty()) { + missingOptionals.putAll(currentTags); + return TagCollection.m_13396_(missingOptionals); diff --git a/src/test/java/net/minecraftforge/debug/DuplicateOptionalTagTest.java b/src/test/java/net/minecraftforge/debug/DuplicateOptionalTagTest.java new file mode 100644 index 0000000000..51295c5e1d --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/DuplicateOptionalTagTest.java @@ -0,0 +1,99 @@ +/* + * 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.debug; + +import com.google.common.collect.Sets; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraftforge.common.ForgeTagHandler; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.Tags; +import net.minecraftforge.event.server.ServerStartedEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.registries.ForgeRegistries; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; +import java.util.Set; +import java.util.function.Supplier; + +/** + * Tests that the values for defaulted optional tags defined in multiple places are combined. + * + *

The optional tag defined by this mod is deliberately not defined in a data pack, to cause it to 'default' and + * trigger the behavior being tested.

+ * + * @see MinecraftForge/MinecraftForge#7570 + */ +@Mod(DuplicateOptionalTagTest.MODID) +public class DuplicateOptionalTagTest +{ + private static final Logger LOGGER = LogManager.getLogger(); + + static final String MODID = "duplicate_optional_tag_test"; + private static final ResourceLocation TAG_NAME = new ResourceLocation(MODID, "test_optional_tag"); + + private static final Set> TAG_A_DEFAULTS = Set.of(Blocks.BEDROCK.delegate); + private static final Set> TAG_B_DEFAULTS = Set.of(Blocks.WHITE_WOOL.delegate); + + private static final Tags.IOptionalNamedTag TAG_A = ForgeTagHandler.createOptionalTag(ForgeRegistries.BLOCKS, TAG_NAME, + TAG_A_DEFAULTS); + private static final Tags.IOptionalNamedTag TAG_B = ForgeTagHandler.createOptionalTag(ForgeRegistries.BLOCKS, TAG_NAME, + TAG_B_DEFAULTS); + + public DuplicateOptionalTagTest() + { + MinecraftForge.EVENT_BUS.addListener(this::onServerStarted); + } + + private void onServerStarted(ServerStartedEvent event) + { + if (!TAG_A.isDefaulted()) + { + LOGGER.warn("First instance of optional tag is not defaulted!"); + } + + if (!TAG_B.isDefaulted()) + { + LOGGER.warn("Second instance of optional tag is not defaulted!"); + } + + if (!TAG_A.getValues().equals(TAG_B.getValues())) + { + LOGGER.error("Values of both optional tag instances are not the same: first instance: {}, second instance: {}", + TAG_A.getValues(), TAG_B.getValues()); + return; + } + + final List expected = Sets.union(TAG_A_DEFAULTS, TAG_B_DEFAULTS).stream() + .map(Supplier::get) + .toList(); + if (!TAG_A.getValues().equals(expected)) + { + LOGGER.error("Values of the optional tag do not match the expected union of their defaults: expected {}, got {}", + expected, TAG_A.getValues()); + return; + } + + LOGGER.info("Optional tag instances match each other and the expected union of their defaults"); + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index dec32996f7..46ab655ce8 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -174,6 +174,8 @@ license="LGPL v2.1" modId="part_entity_test" [[mods]] modId="capabilities_test" +[[mods]] + modId="duplicate_optional_tag_test" [[mods]] modId="custom_particle_type_test" [[mods]]