Fix and improve Ingredient invalidation (#8361)

This commit is contained in:
Take Weiland authored and GitHub committed 2022-01-14 15:05:57 +00:00
1 parent 15d3df2818
commit 57a916bf09
5 files changed
+125 -9

No files matched your search

@@ -5,9 +5,9 @@
public class Ingredient implements Predicate<ItemStack> {
+ //Because Mojang caches things... we need to invalidate them.. so... here we go..
+ private static final java.util.Set<Ingredient> INSTANCES = java.util.Collections.newSetFromMap(new java.util.WeakHashMap<Ingredient, Boolean>());
+ 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<? extends Ingredient.Value> 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;
@@ -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();
}
@@ -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
@@ -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.<br>
* 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;
}
}
}
}
+2
View File
@@ -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