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

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