mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2025-01-23 07:11:20 +01:00
Directly transfer items (Fixes #169)
This commit is contained in:
parent
c1509dc03a
commit
1952e5cb46
@ -162,6 +162,62 @@ public class InventoryUtil {
|
||||
return left <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers an item from one inventory to another one
|
||||
*
|
||||
* @param item Item to transfer
|
||||
* @param sourceInventory Inventory to transfer the item from
|
||||
* @param targetInventory Inventory to transfer the item to
|
||||
* @return Number of leftover items
|
||||
*/
|
||||
public static int transfer(ItemStack item, Inventory sourceInventory, Inventory targetInventory) {
|
||||
return transfer(item, sourceInventory, targetInventory, item.getMaxStackSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers an item from one inventory to another one
|
||||
*
|
||||
* @param item Item to transfer
|
||||
* @param sourceInventory Inventory to transfer the item from
|
||||
* @param targetInventory Inventory to transfer the item to
|
||||
* @param maxStackSize Maximum item's stack size
|
||||
* @return Number of leftover items
|
||||
*/
|
||||
public static int transfer(ItemStack item, Inventory sourceInventory, Inventory targetInventory, int maxStackSize) {
|
||||
if (item.getAmount() < 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int amount = item.getAmount();
|
||||
for (ItemStack currentItem : sourceInventory) {
|
||||
if (MaterialUtil.equals(currentItem, item)) {
|
||||
ItemStack clone = currentItem.clone();
|
||||
if (currentItem.getAmount() >= amount) {
|
||||
clone.setAmount(amount);
|
||||
amount = 0;
|
||||
} else {
|
||||
clone.setAmount(currentItem.getAmount());
|
||||
amount -= clone.getAmount();
|
||||
}
|
||||
int leftOver = add(clone, targetInventory, maxStackSize);
|
||||
if (leftOver > 0) {
|
||||
currentItem.setAmount(currentItem.getAmount() - clone.getAmount() + leftOver);
|
||||
if (amount > 0) {
|
||||
amount += leftOver;
|
||||
} else {
|
||||
return leftOver;
|
||||
}
|
||||
} else {
|
||||
currentItem.setAmount(currentItem.getAmount() - clone.getAmount());
|
||||
}
|
||||
}
|
||||
if (amount <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an item to the inventory with given maximum stack size
|
||||
*
|
||||
|
@ -22,8 +22,7 @@ public class ItemManager implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
removeItems(event.getOwnerInventory(), event.getStock());
|
||||
addItems(event.getClientInventory(), event.getStock());
|
||||
transferItems(event.getOwnerInventory(), event.getClientInventory(), event.getStock());
|
||||
|
||||
event.getClient().updateInventory();
|
||||
}
|
||||
@ -34,26 +33,19 @@ public class ItemManager implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
removeItems(event.getClientInventory(), event.getStock());
|
||||
addItems(event.getOwnerInventory(), event.getStock());
|
||||
transferItems(event.getClientInventory(), event.getOwnerInventory(), event.getStock());
|
||||
|
||||
event.getClient().updateInventory();
|
||||
}
|
||||
|
||||
private static void removeItems(Inventory inventory, ItemStack[] items) {
|
||||
for (ItemStack item : items) {
|
||||
InventoryUtil.remove(item, inventory);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addItems(Inventory inventory, ItemStack[] items) {
|
||||
private static void transferItems(Inventory sourceInventory, Inventory targetInventory, ItemStack[] items) {
|
||||
if (Properties.STACK_TO_64) {
|
||||
for (ItemStack item : items) {
|
||||
InventoryUtil.add(item, inventory, 64);
|
||||
InventoryUtil.transfer(item, sourceInventory, targetInventory, 64);
|
||||
}
|
||||
} else {
|
||||
for (ItemStack item : items) {
|
||||
InventoryUtil.add(item, inventory);
|
||||
InventoryUtil.transfer(item, sourceInventory, targetInventory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user