Simplified code regarding VirtualInventory

This commit is contained in:
NichtStudioCode 2021-01-24 23:13:34 +01:00
parent d50f24e557
commit a4bdf57360
2 changed files with 45 additions and 60 deletions

@ -51,69 +51,57 @@ abstract class IndexedGUI implements GUI {
ItemStack cursor = event.getCursor();
ItemStack clicked = event.getCurrentItem();
switch (event.getAction()) {
if (virtualInventory.isSynced(index, clicked)) {
switch (event.getAction()) {
case CLONE_STACK:
case DROP_ALL_CURSOR:
case DROP_ONE_CURSOR:
// empty, this does not affect anything
break;
case CLONE_STACK:
case DROP_ALL_CURSOR:
case DROP_ONE_CURSOR:
// empty, this does not affect anything
break;
case DROP_ONE_SLOT:
case PICKUP_ONE:
if (virtualInventory.isSynced(index, clicked)) {
case DROP_ONE_SLOT:
case PICKUP_ONE:
virtualInventory.removeOne(index);
} else event.setCancelled(true);
break;
break;
case DROP_ALL_SLOT:
case PICKUP_ALL:
if (virtualInventory.isSynced(index, clicked)) {
case DROP_ALL_SLOT:
case PICKUP_ALL:
virtualInventory.removeItem(index);
} else event.setCancelled(true);
break;
break;
case PICKUP_HALF:
if (virtualInventory.isSynced(index, clicked)) {
case PICKUP_HALF:
virtualInventory.removeHalf(index);
} else event.setCancelled(true);
break;
break;
case PLACE_ALL:
if (virtualInventory.isSynced(index, clicked)) {
case PLACE_ALL:
virtualInventory.place(index, cursor);
} else event.setCancelled(true);
break;
break;
case PLACE_ONE:
if (virtualInventory.isSynced(index, clicked)) {
case PLACE_ONE:
virtualInventory.placeOne(index, cursor);
} else event.setCancelled(true);
break;
break;
case PLACE_SOME:
if (virtualInventory.isSynced(index, clicked)) {
case PLACE_SOME:
virtualInventory.setMaxAmount(index);
} else event.setCancelled(true);
break;
break;
case MOVE_TO_OTHER_INVENTORY:
event.setCancelled(true);
if (virtualInventory.isSynced(index, clicked)) {
case MOVE_TO_OTHER_INVENTORY:
event.setCancelled(true);
int leftOverAmount = 0;
HashMap<Integer, ItemStack> leftover =
event.getWhoClicked().getInventory().addItem(virtualInventory.getItemStack(index));
if (!leftover.isEmpty()) leftOverAmount = leftover.get(0).getAmount();
virtualInventory.setAmount(index, leftOverAmount);
}
break;
break;
default:
// action not supported
event.setCancelled(true);
break;
}
default:
// action not supported
event.setCancelled(true);
break;
}
} else event.setCancelled(true);
}
@Override

@ -54,15 +54,12 @@ public class VirtualInventory implements ConfigurationSerializable {
final int originalAmount = itemStack.getAmount();
int amountLeft = originalAmount;
addItems:
{
// find all slots where the item partially fits and add it there
ItemStack partialStack;
while ((partialStack = findPartialSlot(itemStack)) != null) {
amountLeft = addTo(partialStack, amountLeft);
if (amountLeft == 0) break addItems;
}
// find all slots where the item partially fits and add it there
ItemStack partialStack;
while ((partialStack = findPartialSlot(itemStack)) != null && amountLeft != 0)
amountLeft = addTo(partialStack, amountLeft);
if (amountLeft != 0) {
// there are still items left, put the rest on an empty slot
int emptyIndex = ArrayUtils.findFirstEmptyIndex(items);
if (emptyIndex != -1) {