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 5806498..b7b1f08 100644 --- a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java +++ b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java @@ -9,6 +9,8 @@ import de.studiocode.invgui.gui.SlotElement.VISlotElement; import de.studiocode.invgui.item.Item; import de.studiocode.invgui.util.ArrayUtils; import de.studiocode.invgui.virtualinventory.VirtualInventory; +import de.studiocode.invgui.virtualinventory.event.ItemUpdateEvent; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; @@ -89,13 +91,22 @@ abstract class IndexedGUI implements GUI { 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(); - // TODO: find a way to cancel at this point - virtualInventory.setAmount(player, index, leftOverAmount, true); + ItemStack invStack = virtualInventory.getItemStack(index); + ItemUpdateEvent updateEvent = new ItemUpdateEvent(virtualInventory, player, invStack, + index, invStack.getAmount(), -1); + Bukkit.getPluginManager().callEvent(updateEvent); + + if (!updateEvent.isCancelled()) { + int leftOverAmount = 0; + HashMap leftover = + event.getWhoClicked().getInventory().addItem(virtualInventory.getItemStack(index)); + + if (!leftover.isEmpty()) leftOverAmount = leftover.get(0).getAmount(); + + virtualInventory.setAmountSilently(index, leftOverAmount); + } + break; default: diff --git a/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java b/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java index f8f8fd1..31db8ab 100644 --- a/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java +++ b/src/main/java/de/studiocode/invgui/virtualinventory/VirtualInventory.java @@ -192,31 +192,18 @@ public class VirtualInventory implements ConfigurationSerializable { } /** - * Changes the amount of an {@link ItemStack} on a specific slot. + * Changes the amount of an {@link ItemStack} on a specific slot without calling the {@link ItemUpdateEvent} * - * @param player The player that did this or null if it wasn't a player. * @param index The slot index * @param amount The new amount - * @return If the action has been cancelled */ - public boolean setAmount(Player player, int index, int amount, boolean ignoreCancelled) { + public void setAmountSilently(int index, int amount) { ItemStack itemStack = items[index]; if (itemStack != null) { - int currentAmount = itemStack.getAmount(); - - ItemUpdateEvent event = createAndCallEvent(player, itemStack, index, currentAmount, amount); - - if (!event.isCancelled() || ignoreCancelled) { - if (amount == 0) items[index] = null; - else itemStack.setAmount(amount); - - notifyWindows(); - - return false; - } + if (amount == 0) items[index] = null; + else itemStack.setAmount(amount); + notifyWindows(); } - - return true; } /** @@ -256,7 +243,7 @@ public class VirtualInventory implements ConfigurationSerializable { ItemStack itemStack = items[index]; if (itemStack != null) { - ItemUpdateEvent event =createAndCallEvent(player, itemStack, index, + ItemUpdateEvent event = createAndCallEvent(player, itemStack, index, itemStack.getAmount(), 0); if (!event.isCancelled()) { diff --git a/src/main/java/de/studiocode/invgui/virtualinventory/event/ItemUpdateEvent.java b/src/main/java/de/studiocode/invgui/virtualinventory/event/ItemUpdateEvent.java index 16cd83a..e59b61d 100644 --- a/src/main/java/de/studiocode/invgui/virtualinventory/event/ItemUpdateEvent.java +++ b/src/main/java/de/studiocode/invgui/virtualinventory/event/ItemUpdateEvent.java @@ -34,7 +34,8 @@ public class ItemUpdateEvent extends Event implements Cancellable { * @param itemStack The {@link ItemStack} that is affected * @param slot The slot that is affected * @param previousAmount The previous amount of the {@link ItemStack} - * @param newAmount The amount that the {@link ItemStack} will have if the event is not being cancelled + * @param newAmount The amount that the {@link ItemStack} will have if the event is not being + * cancelled or -1 if not known */ public ItemUpdateEvent(@NotNull VirtualInventory virtualInventory, @Nullable Player player, @NotNull ItemStack itemStack, int slot, int previousAmount, int newAmount) { @@ -48,7 +49,7 @@ public class ItemUpdateEvent extends Event implements Cancellable { /** * Gets the {@link HandlerList} of this {@link Event} - * + * * @return The {@link HandlerList} of this {@link Event} */ public static HandlerList getHandlerList() { @@ -57,7 +58,7 @@ public class ItemUpdateEvent extends Event implements Cancellable { /** * Gets the {@link VirtualInventory} where this action takes place. - * + * * @return The {@link VirtualInventory} */ public VirtualInventory getVirtualInventory() { @@ -76,7 +77,7 @@ public class ItemUpdateEvent extends Event implements Cancellable { /** * Gets the {@link ItemStack} involved in this action. - * + * * @return The {@link ItemStack} */ public ItemStack getItemStack() { @@ -85,7 +86,7 @@ public class ItemUpdateEvent extends Event implements Cancellable { /** * Gets the slot that is affected. - * + * * @return The slot */ public int getSlot() { @@ -94,7 +95,7 @@ public class ItemUpdateEvent extends Event implements Cancellable { /** * Gets the previous amount of the {@link ItemStack} - * + * * @return The previous amount */ public int getPreviousAmount() { @@ -103,8 +104,8 @@ public class ItemUpdateEvent extends Event implements Cancellable { /** * Gets the new amount of the {@link ItemStack} if the event - * isn't being cancelled. - * + * isn't being cancelled or -1 if not unknown. + * * @return The new amount */ public int getNewAmount() { @@ -131,5 +132,5 @@ public class ItemUpdateEvent extends Event implements Cancellable { public HandlerList getHandlers() { return handlers; } - + }