diff --git a/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java b/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java index a3435e5..1c74c5f 100644 --- a/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java +++ b/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java @@ -122,7 +122,7 @@ public class ESDrive implements Cloneable { // The item is contained, then update the amount. if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) { int amount = (int) items.values().toArray()[Utils.indexOfSimilarItem(new ArrayList<>(items.keySet()), item)] + item.getAmount(); - Utils.removeSimilarItem(items, item); + items = Utils.removeSimilarItem(items, item); items.put(item, amount); } else { items.put(item, item.getAmount()); @@ -138,12 +138,12 @@ public class ESDrive implements Cloneable { // If there isn't enough items stored to take out the requested amount, then just take out all that we can. int foundItemAmount = (int) items.values().toArray()[Utils.indexOfSimilarItem(new ArrayList<>(items.keySet()), item)]; if (foundItemAmount - item.getAmount() < 1) { - Utils.removeSimilarItem(items, item); + items = Utils.removeSimilarItem(items, item); item.setAmount(foundItemAmount); } else { int newAmount = foundItemAmount - item.getAmount(); - Utils.removeSimilarItem(items, item); + items = Utils.removeSimilarItem(items, item); items.put(item, newAmount); } diff --git a/src/main/java/net/seanomik/energeticstorage/utils/Utils.java b/src/main/java/net/seanomik/energeticstorage/utils/Utils.java index b6b6e20..ce6163d 100644 --- a/src/main/java/net/seanomik/energeticstorage/utils/Utils.java +++ b/src/main/java/net/seanomik/energeticstorage/utils/Utils.java @@ -66,8 +66,21 @@ public class Utils { return false; } - public static void removeSimilarItem(Map itemStacks, ItemStack item) { - itemStacks.entrySet().removeIf(entry -> removeAmountFromLore(entry.getKey()).isSimilar(item)); + public static Map removeSimilarItem(Map itemStacks, ItemStack item) { + removeAmountFromLore(item); + // Doing this does not work. So we're gonna have to make some ugly code... + // The reason it doesn't work is due to Spigot implementing its own `hashCode` implementation, + // which Java's Iterator#remove method relies on and regenerates the hashCode when removing. + //itemStacks.entrySet().removeIf(entry -> removeAmountFromLore(entry.getKey()).isSimilar(item)); + + Map items = new HashMap<>(); + for (Map.Entry entry : itemStacks.entrySet()) { + if (!removeAmountFromLore(entry.getKey()).isSimilar(item)) { + items.put(entry.getKey(), entry.getValue()); + } + } + + return items; } public static int indexOfSimilarItem(List itemStacks, ItemStack item) {