Fixes #1
This commit is contained in:
parent
9257ea1bdb
commit
1ad9a3f3ea
|
@ -122,7 +122,7 @@ public class ESDrive implements Cloneable {
|
||||||
// The item is contained, then update the amount.
|
// The item is contained, then update the amount.
|
||||||
if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) {
|
if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) {
|
||||||
int amount = (int) items.values().toArray()[Utils.indexOfSimilarItem(new ArrayList<>(items.keySet()), item)] + item.getAmount();
|
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);
|
items.put(item, amount);
|
||||||
} else {
|
} else {
|
||||||
items.put(item, item.getAmount());
|
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.
|
// 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)];
|
int foundItemAmount = (int) items.values().toArray()[Utils.indexOfSimilarItem(new ArrayList<>(items.keySet()), item)];
|
||||||
if (foundItemAmount - item.getAmount() < 1) {
|
if (foundItemAmount - item.getAmount() < 1) {
|
||||||
Utils.removeSimilarItem(items, item);
|
items = Utils.removeSimilarItem(items, item);
|
||||||
item.setAmount(foundItemAmount);
|
item.setAmount(foundItemAmount);
|
||||||
} else {
|
} else {
|
||||||
int newAmount = foundItemAmount - item.getAmount();
|
int newAmount = foundItemAmount - item.getAmount();
|
||||||
|
|
||||||
Utils.removeSimilarItem(items, item);
|
items = Utils.removeSimilarItem(items, item);
|
||||||
items.put(item, newAmount);
|
items.put(item, newAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,8 +66,21 @@ public class Utils {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void removeSimilarItem(Map<ItemStack, Integer> itemStacks, ItemStack item) {
|
public static Map<ItemStack, Integer> removeSimilarItem(Map<ItemStack, Integer> itemStacks, ItemStack item) {
|
||||||
itemStacks.entrySet().removeIf(entry -> removeAmountFromLore(entry.getKey()).isSimilar(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) {
|
public static int indexOfSimilarItem(List<ItemStack> itemStacks, ItemStack item) {
|
||||||
|
|
Loading…
Reference in New Issue