From b3e1f236838d8da9ca44ffda5e03714d351f0a61 Mon Sep 17 00:00:00 2001 From: slprime <31038811+slprime@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:50:16 +0200 Subject: [PATCH] Restore Issue 583 (#593) Co-authored-by: slprime --- .../java/codechicken/nei/NEIClientConfig.java | 25 +++++++------- .../codechicken/nei/recipe/StackInfo.java | 20 ++++++----- .../codechicken/nei/util/ItemStackKey.java | 33 +++++++++++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 src/main/java/codechicken/nei/util/ItemStackKey.java diff --git a/src/main/java/codechicken/nei/NEIClientConfig.java b/src/main/java/codechicken/nei/NEIClientConfig.java index 9e838c08d..93a22acde 100644 --- a/src/main/java/codechicken/nei/NEIClientConfig.java +++ b/src/main/java/codechicken/nei/NEIClientConfig.java @@ -723,21 +723,24 @@ public static OptionList getOptionList() { public static void loadWorld(String worldPath) { unloadWorld(); - NEIClientConfig.worldPath = worldPath; - setInternalEnabled(true); - logger.debug("Loading " + (Minecraft.getMinecraft().isSingleplayer() ? "Local" : "Remote") + " World"); - final File specificDir = new File(CommonUtils.getMinecraftDir(), "saves/NEI/" + worldPath); - final boolean newWorld = !specificDir.exists(); + if (!worldPath.equals(NEIClientConfig.worldPath)) { + NEIClientConfig.worldPath = worldPath; - if (newWorld) { - specificDir.mkdirs(); - } + logger.debug("Loading " + (Minecraft.getMinecraft().isSingleplayer() ? "Local" : "Remote") + " World"); + + final File specificDir = new File(CommonUtils.getMinecraftDir(), "saves/NEI/" + worldPath); + final boolean newWorld = !specificDir.exists(); - world = new ConfigSet(new File(specificDir, "NEI.dat"), new ConfigFile(new File(specificDir, "NEI.cfg"))); - bootNEI(ClientUtils.getWorld()); - onWorldLoad(newWorld); + if (newWorld) { + specificDir.mkdirs(); + } + + world = new ConfigSet(new File(specificDir, "NEI.dat"), new ConfigFile(new File(specificDir, "NEI.cfg"))); + bootNEI(ClientUtils.getWorld()); + onWorldLoad(newWorld); + } } public static String getWorldPath() { diff --git a/src/main/java/codechicken/nei/recipe/StackInfo.java b/src/main/java/codechicken/nei/recipe/StackInfo.java index 26587ab8a..612be8ec5 100644 --- a/src/main/java/codechicken/nei/recipe/StackInfo.java +++ b/src/main/java/codechicken/nei/recipe/StackInfo.java @@ -10,6 +10,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.fluids.FluidContainerRegistry; +import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidContainerItem; @@ -18,19 +19,21 @@ import codechicken.nei.api.IStackStringifyHandler; import codechicken.nei.recipe.stackinfo.DefaultStackStringifyHandler; import codechicken.nei.recipe.stackinfo.GTFluidStackStringifyHandler; +import codechicken.nei.util.ItemStackKey; public class StackInfo { + private static final FluidStack NULL_FLUID = new FluidStack(FluidRegistry.WATER, 0); public static final ArrayList stackStringifyHandlers = new ArrayList<>(); private static final HashMap> guidfilters = new HashMap<>(); private static final ItemStackMap guidcache = new ItemStackMap<>(); - private static final LinkedHashMap fluidcache = new LinkedHashMap<>() { + private static final LinkedHashMap fluidcache = new LinkedHashMap<>() { private static final long serialVersionUID = 1042213947848622164L; @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > 20; + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > 200; } }; @@ -92,19 +95,20 @@ public static boolean equalItemAndNBT(ItemStack stackA, ItemStack stackB, boolea return true; } - public static FluidStack getFluid(ItemStack stack) { - FluidStack fluid = fluidcache.get(stack); + public static synchronized FluidStack getFluid(ItemStack stack) { + ItemStackKey key = new ItemStackKey(stack); + FluidStack fluid = fluidcache.get(key); - if (fluid == null && !fluidcache.containsKey(stack)) { + if (fluid == null) { for (int i = stackStringifyHandlers.size() - 1; i >= 0 && fluid == null; i--) { fluid = stackStringifyHandlers.get(i).getFluid(stack); } - fluidcache.put(stack, fluid); + fluidcache.put(key, fluid == null ? NULL_FLUID : fluid); } - return fluid; + return fluid == NULL_FLUID ? null : fluid; } public static boolean isFluidContainer(ItemStack stack) { diff --git a/src/main/java/codechicken/nei/util/ItemStackKey.java b/src/main/java/codechicken/nei/util/ItemStackKey.java new file mode 100644 index 000000000..e695aefd4 --- /dev/null +++ b/src/main/java/codechicken/nei/util/ItemStackKey.java @@ -0,0 +1,33 @@ +package codechicken.nei.util; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; + +public class ItemStackKey { + + public final ItemStack stack; + private int hashCode = 1; + + public ItemStackKey(ItemStack stack) { + this.stack = stack; + + if (this.stack != null) { + this.hashCode = 31 * this.hashCode + stack.stackSize; + this.hashCode = 31 * this.hashCode + Item.getIdFromItem(stack.getItem()); + this.hashCode = 31 * this.hashCode + stack.getItemDamage(); + this.hashCode = 31 * this.hashCode + (!stack.hasTagCompound() ? 0 : stack.getTagCompound().hashCode()); + } + } + + @Override + public int hashCode() { + return this.hashCode; + } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof ItemStackKey)) return false; + return ItemStack.areItemStacksEqual(this.stack, ((ItemStackKey) o).stack); + } +}