From a4bdf57360ed6ce439ba1e4e06a535869e6534e2 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sun, 24 Jan 2021 23:13:34 +0100 Subject: [PATCH] Simplified code regarding VirtualInventory --- .../invgui/gui/impl/IndexedGUI.java | 90 ++++++++----------- .../virtualinventory/VirtualInventory.java | 15 ++-- 2 files changed, 45 insertions(+), 60 deletions(-) diff --git a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java index e3d0a33..2bef502 100644 --- a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java +++ b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java @@ -51,69 +51,57 @@ abstract class IndexedGUI implements GUI { ItemStack cursor = event.getCursor(); ItemStack clicked = event.getCurrentItem(); - switch (event.getAction()) { - - 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)) { + 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 DROP_ONE_SLOT: + case PICKUP_ONE: virtualInventory.removeOne(index); - } else event.setCancelled(true); - break; - - case DROP_ALL_SLOT: - case PICKUP_ALL: - if (virtualInventory.isSynced(index, clicked)) { + break; + + case DROP_ALL_SLOT: + case PICKUP_ALL: virtualInventory.removeItem(index); - } else event.setCancelled(true); - break; - - case PICKUP_HALF: - if (virtualInventory.isSynced(index, clicked)) { + break; + + case PICKUP_HALF: virtualInventory.removeHalf(index); - } else event.setCancelled(true); - break; - - case PLACE_ALL: - if (virtualInventory.isSynced(index, clicked)) { + break; + + case PLACE_ALL: virtualInventory.place(index, cursor); - } else event.setCancelled(true); - break; - - case PLACE_ONE: - if (virtualInventory.isSynced(index, clicked)) { + break; + + case PLACE_ONE: virtualInventory.placeOne(index, cursor); - } else event.setCancelled(true); - break; - - case PLACE_SOME: - if (virtualInventory.isSynced(index, clicked)) { + break; + + case PLACE_SOME: virtualInventory.setMaxAmount(index); - } else event.setCancelled(true); - break; - - case MOVE_TO_OTHER_INVENTORY: - event.setCancelled(true); - if (virtualInventory.isSynced(index, clicked)) { + break; + + case MOVE_TO_OTHER_INVENTORY: + event.setCancelled(true); int leftOverAmount = 0; HashMap leftover = event.getWhoClicked().getInventory().addItem(virtualInventory.getItemStack(index)); if (!leftover.isEmpty()) leftOverAmount = leftover.get(0).getAmount(); virtualInventory.setAmount(index, leftOverAmount); - } - break; - - default: - // action not supported - event.setCancelled(true); - break; - } + break; + + default: + // action not supported + event.setCancelled(true); + break; + } + } else event.setCancelled(true); } @Override diff --git a/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java b/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java index a93310f..cc98301 100644 --- a/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java +++ b/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java @@ -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) {