diff --git a/src/main/java/codechicken/nei/ItemZoom.java b/src/main/java/codechicken/nei/ItemZoom.java index ac866dcb..d3786363 100644 --- a/src/main/java/codechicken/nei/ItemZoom.java +++ b/src/main/java/codechicken/nei/ItemZoom.java @@ -127,7 +127,12 @@ public class ItemZoom extends Widget implements IContainerInputHandler { this.displayName = "Unnamed"; } - this.stack = StackInfo.loadFromNBT(StackInfo.itemStackToNBT(stack), 0); + // Copying the stack copies its whole NBT, so it is only done when the hovered stack changes + final boolean sameStack = this.stack != null && this.stack.isItemEqual(stack) + && ItemStack.areItemStackTagsEqual(this.stack, stack); + if (!sameStack) { + this.stack = StackInfo.loadFromNBT(StackInfo.itemStackToNBT(stack), 0); + } } else { this.stack = null; this.displayName = ""; diff --git a/src/main/java/codechicken/nei/util/FavoriteStorage.java b/src/main/java/codechicken/nei/util/FavoriteStorage.java index 44454088..6ea7e4a6 100644 --- a/src/main/java/codechicken/nei/util/FavoriteStorage.java +++ b/src/main/java/codechicken/nei/util/FavoriteStorage.java @@ -26,6 +26,9 @@ public class FavoriteStorage { protected final Map recipeIdToItemHash = new HashMap<>(); protected final Map itemHashToItemStack = new HashMap<>(); + /** The key of the last looked up stack. Its NBT copies the whole stack, which is looked up every frame. */ + private static volatile LookupKey lastLookupKey = null; + public void clear() { this.itemStackToRecipeId.clear(); this.fluidToRecipeId.clear(); @@ -95,7 +98,7 @@ public class FavoriteStorage { RecipeId recipeId = this.itemHashToRecipeId.get(getItemKey(stack)); if (recipeId == null) { - recipeId = this.itemStackToRecipeId.get(StackInfo.itemStackToNBT(stack, false)); + recipeId = this.itemStackToRecipeId.get(getLookupKey(stack)); } if (recipeId == null) { @@ -105,6 +108,28 @@ public class FavoriteStorage { return recipeId; } + private static NBTTagCompound getLookupKey(ItemStack stack) { + LookupKey key = lastLookupKey; + + if (key == null || !key.stack.isItemEqual(stack) || !ItemStack.areItemStackTagsEqual(key.stack, stack)) { + key = new LookupKey(stack.copy(), StackInfo.itemStackToNBT(stack, false)); + lastLookupKey = key; + } + + return key.nbt; + } + + private static class LookupKey { + + private final ItemStack stack; + private final NBTTagCompound nbt; + + private LookupKey(ItemStack stack, NBTTagCompound nbt) { + this.stack = stack; + this.nbt = nbt; + } + } + public ItemStack getItemStack(RecipeId recipeId) { NBTTagCompound itemStackNBT = this.recipeIdToItemStack.get(recipeId);