From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Wed, 17 Aug 2022 11:04:12 +0800 Subject: [PATCH] Remove iterators from inventory contains This patch is Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish) diff --git a/src/main/java/net/minecraft/world/entity/player/Inventory.java b/src/main/java/net/minecraft/world/entity/player/Inventory.java index 27c028ab6b1edb6e413af3bbaa27bf30f2d85540..4a89ce4fbd8b19ce1cff5905d779d2e4c63e89c4 100644 --- a/src/main/java/net/minecraft/world/entity/player/Inventory.java +++ b/src/main/java/net/minecraft/world/entity/player/Inventory.java @@ -682,21 +682,35 @@ public class Inventory implements Container, Nameable { } public boolean contains(ItemStack stack) { - Iterator iterator = this.compartments.iterator(); + // Leaves start - don't allocate iterators + if (top.leavesmc.leaves.LeavesConfig.removeInventoryContainsIterators) { + for (int i = 0; i < this.compartments.size(); i++) { + List list = this.compartments.get(i); + for (int j = 0; j < list.size(); j++) { + ItemStack itemstack1 = list.get(j); + + if (!itemstack1.isEmpty() && itemstack1.sameItem(stack)) { + return true; + } + } + } + } else { + Iterator iterator = this.compartments.iterator(); - while (iterator.hasNext()) { - List list = (List) iterator.next(); - Iterator iterator1 = list.iterator(); + while (iterator.hasNext()) { + List list = (List) iterator.next(); + Iterator iterator1 = list.iterator(); - while (iterator1.hasNext()) { - ItemStack itemstack1 = (ItemStack) iterator1.next(); + while (iterator1.hasNext()) { + ItemStack itemstack1 = (ItemStack) iterator1.next(); - if (!itemstack1.isEmpty() && ItemStack.isSameItemSameTags(itemstack1, stack)) { - return true; + if (!itemstack1.isEmpty() && ItemStack.isSameItemSameTags(itemstack1, stack)) { + return true; + } } } } - + // Leaves end - don't allocate iterators return false; } diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index 345b9ef0af9f8f08af00693e562e5f39a8ad3362..a5fece21769900d5600df3284b7d3191121f10a5 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -329,6 +329,11 @@ public final class LeavesConfig { removeTickGuardLambda = getBoolean("settings.performance.remove.tick-guard-lambda", removeTickGuardLambda); } + public static boolean removeInventoryContainsIterators = true; + private static void removeInventoryContainsIterators() { + removeInventoryContainsIterators = getBoolean("settings.performance.remove.inventory-contains-iterators", removeInventoryContainsIterators); + } + public static final class WorldConfig { public final String worldName;