From 57a916bf0964cd0f01c66461f180e7af05990233 Mon Sep 17 00:00:00 2001 From: Take Weiland Date: Fri, 14 Jan 2022 16:05:57 +0100 Subject: [PATCH] Fix and improve Ingredient invalidation (#8361) --- .../world/item/crafting/Ingredient.java.patch | 31 ++++++- .../net/minecraftforge/common/ForgeMod.java | 3 +- .../common/crafting/CompoundIngredient.java | 12 ++- .../item/IngredientInvalidationTest.java | 86 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + 5 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/world/item/IngredientInvalidationTest.java diff --git a/patches/minecraft/net/minecraft/world/item/crafting/Ingredient.java.patch b/patches/minecraft/net/minecraft/world/item/crafting/Ingredient.java.patch index 3581de2a5f..2ab8077a74 100644 --- a/patches/minecraft/net/minecraft/world/item/crafting/Ingredient.java.patch +++ b/patches/minecraft/net/minecraft/world/item/crafting/Ingredient.java.patch @@ -5,9 +5,9 @@ public class Ingredient implements Predicate { + //Because Mojang caches things... we need to invalidate them.. so... here we go.. -+ private static final java.util.Set INSTANCES = java.util.Collections.newSetFromMap(new java.util.WeakHashMap()); ++ private static final java.util.concurrent.atomic.AtomicInteger INVALIDATION_COUNTER = new java.util.concurrent.atomic.AtomicInteger(); + public static void invalidateAll() { -+ INSTANCES.stream().filter(e -> e != null).forEach(i -> i.invalidate()); ++ INVALIDATION_COUNTER.incrementAndGet(); + } + public static final Ingredient f_43901_ = new Ingredient(Stream.empty()); @@ -16,6 +16,7 @@ private ItemStack[] f_43903_; @Nullable private IntList f_43904_; ++ private int invalidationCounter; + private final boolean isSimple; protected Ingredient(Stream p_43907_) { @@ -23,10 +24,19 @@ return new Ingredient.Value[p_43933_]; }); + this.isSimple = !net.minecraftforge.data.loading.DatagenModLoader.isRunningDataGen() && !Arrays.stream(f_43902_).anyMatch(list -> list.m_6223_().stream().anyMatch(stack -> stack.m_41720_().isDamageable(stack))); -+ Ingredient.INSTANCES.add(this); } public ItemStack[] m_43908_() { +@@ -78,7 +_,8 @@ + } + + public IntList m_43931_() { +- if (this.f_43904_ == null) { ++ if (this.f_43904_ == null || checkInvalidation()) { ++ this.markValid(); + this.m_43948_(); + this.f_43904_ = new IntArrayList(this.f_43903_.length); + @@ -94,6 +_,10 @@ public final void m_43923_(FriendlyByteBuf p_43924_) { @@ -38,10 +48,23 @@ p_43924_.m_178352_(Arrays.asList(this.f_43903_), FriendlyByteBuf::m_130055_); } -@@ -115,6 +_,25 @@ +@@ -115,6 +_,38 @@ return this.f_43902_.length == 0 && (this.f_43903_ == null || this.f_43903_.length == 0) && (this.f_43904_ == null || this.f_43904_.isEmpty()); } ++ public final boolean checkInvalidation() { ++ int currentInvalidationCounter = INVALIDATION_COUNTER.get(); ++ if (this.invalidationCounter != currentInvalidationCounter) { ++ invalidate(); ++ return true; ++ } ++ return false; ++ } ++ ++ protected final void markValid() { ++ this.invalidationCounter = INVALIDATION_COUNTER.get(); ++ } ++ + protected void invalidate() { + this.f_43903_ = null; + this.f_43904_ = null; diff --git a/src/main/java/net/minecraftforge/common/ForgeMod.java b/src/main/java/net/minecraftforge/common/ForgeMod.java index 1b810c2c89..aa3f4a78a5 100644 --- a/src/main/java/net/minecraftforge/common/ForgeMod.java +++ b/src/main/java/net/minecraftforge/common/ForgeMod.java @@ -1,6 +1,6 @@ /* * Minecraft Forge - * Copyright (c) 2016-2021. + * Copyright (c) 2016-2022. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -164,6 +164,7 @@ public class ForgeMod MinecraftForge.EVENT_BUS.addListener(VillagerTradingManager::loadTrades); MinecraftForge.EVENT_BUS.register(MinecraftForge.INTERNAL_HANDLER); + MinecraftForge.EVENT_BUS.addListener(this::mappingChanged); BiomeDictionary.init(); } diff --git a/src/main/java/net/minecraftforge/common/crafting/CompoundIngredient.java b/src/main/java/net/minecraftforge/common/crafting/CompoundIngredient.java index 24858bebf7..46f423cb1f 100644 --- a/src/main/java/net/minecraftforge/common/crafting/CompoundIngredient.java +++ b/src/main/java/net/minecraftforge/common/crafting/CompoundIngredient.java @@ -1,6 +1,6 @@ /* * Minecraft Forge - * Copyright (c) 2016-2021. + * Copyright (c) 2016-2022. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -74,9 +74,14 @@ public class CompoundIngredient extends Ingredient @Nonnull public IntList getStackingIds() { - //TODO: Add a child.isInvalid()? - if (this.itemIds == null) + boolean childrenNeedInvalidation = false; + for (Ingredient child : children) { + childrenNeedInvalidation |= child.checkInvalidation(); + } + if (childrenNeedInvalidation || this.itemIds == null || checkInvalidation()) + { + this.markValid(); this.itemIds = new IntArrayList(); for (Ingredient child : children) this.itemIds.addAll(child.getStackingIds()); @@ -100,7 +105,6 @@ public class CompoundIngredient extends Ingredient { this.itemIds = null; this.stacks = null; - //Shouldn't need to invalidate children as this is only called form invalidateAll.. } @Override diff --git a/src/test/java/net/minecraftforge/debug/world/item/IngredientInvalidationTest.java b/src/test/java/net/minecraftforge/debug/world/item/IngredientInvalidationTest.java new file mode 100644 index 0000000000..f37ea16928 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/world/item/IngredientInvalidationTest.java @@ -0,0 +1,86 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2022. + * + * 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.world.item; + +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.WorldEvent; +import net.minecraftforge.fml.common.Mod; + +import java.util.stream.Stream; + +/** + * This class validates that {@link Ingredient#invalidate()} is called correctly.
+ * To verify, join a world, then leave it. Then join a world again in the same game session. If invalidation is not working, + * the 2nd world join will trigger an exception. + */ +@Mod(IngredientInvalidationTest.MOD_ID) +public class IngredientInvalidationTest +{ + public static final String MOD_ID = "ingredient_invalidation"; + + private static final boolean ENABLED = true; + + private static boolean invalidateExpected = false; + private static boolean gotInvalidate = false; + + private static final Ingredient TEST_INGREDIENT = new Ingredient(Stream.of(new Ingredient.ItemValue(new ItemStack(Items.WHEAT)))) + { + @Override + protected void invalidate() + { + super.invalidate(); + gotInvalidate = true; + } + }; + + public IngredientInvalidationTest() + { + if (!ENABLED) + return; + + MinecraftForge.EVENT_BUS.addListener(IngredientInvalidationTest::worldLoad); + } + + private static void worldLoad(WorldEvent.Load event) + { + if (event.getWorld() instanceof ServerLevel level && level.dimension().equals(Level.OVERWORLD)) + { + TEST_INGREDIENT.getStackingIds(); // force invalidation if necessary + if (!invalidateExpected) + { + invalidateExpected = true; + } + else if (!gotInvalidate) + { + throw new IllegalStateException("Ingredient.invalidate was not called."); + } + else + { + gotInvalidate = false; + } + } + } + +} \ No newline at end of file diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 80d1cc840f..2e11234306 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -176,5 +176,7 @@ license="LGPL v2.1" modId="capabilities_test" [[mods]] modId="custom_particle_type_test" +[[mods]] + modId="ingredient_invalidation" # ADD ABOVE THIS LINE