This commit is contained in:
SeanOMik 2020-07-08 12:39:53 -05:00
parent 9257ea1bdb
commit 1ad9a3f3ea
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
2 changed files with 18 additions and 5 deletions

View File

@ -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);
}

View File

@ -66,8 +66,21 @@ public class Utils {
return false;
}
public static void removeSimilarItem(Map<ItemStack, Integer> itemStacks, ItemStack item) {
itemStacks.entrySet().removeIf(entry -> removeAmountFromLore(entry.getKey()).isSimilar(item));
public static Map<ItemStack, Integer> removeSimilarItem(Map<ItemStack, Integer> 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<ItemStack, Integer> items = new HashMap<>();
for (Map.Entry<ItemStack, Integer> entry : itemStacks.entrySet()) {
if (!removeAmountFromLore(entry.getKey()).isSimilar(item)) {
items.put(entry.getKey(), entry.getValue());
}
}
return items;
}
public static int indexOfSimilarItem(List<ItemStack> itemStacks, ItemStack item) {