mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-28 23:38:25 +01:00
Implement removeItem(ItemStack...)
By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
parent
2f53dbcd0f
commit
06f8475467
1 changed files with 41 additions and 0 deletions
41
paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
Executable file → Normal file
41
paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
Executable file → Normal file
|
@ -218,6 +218,47 @@ public class CraftInventory implements org.bukkit.inventory.Inventory {
|
|||
return leftover;
|
||||
}
|
||||
|
||||
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) {
|
||||
HashMap<Integer,ItemStack> leftover = new HashMap<Integer,ItemStack>();
|
||||
|
||||
// TODO: optimization
|
||||
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
ItemStack item = items[i];
|
||||
int toDelete = item.getAmount();
|
||||
|
||||
while (true) {
|
||||
int first = first(item.getType());
|
||||
|
||||
// Drat! we don't have this type in the inventory
|
||||
if (first == -1) {
|
||||
item.setAmount( toDelete );
|
||||
leftover.put(i, item);
|
||||
} else {
|
||||
ItemStack itemStack = getItem(first);
|
||||
int amount = itemStack.getAmount();
|
||||
|
||||
if (amount <= toDelete) {
|
||||
toDelete -= amount;
|
||||
// clear the slot, all used up
|
||||
clear( i );
|
||||
} else {
|
||||
// split the stack and store
|
||||
itemStack.setAmount( amount - toDelete );
|
||||
setItem( i, itemStack );
|
||||
toDelete = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Bail when done
|
||||
if (toDelete <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return leftover;
|
||||
}
|
||||
|
||||
private int getMaxItemStack() {
|
||||
return getInventory().c();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue