Fixed ItemUpdateEvent cancel not working

This commit is contained in:
NichtStudioCode 2021-01-27 13:58:38 +01:00
parent 1f2ac4a651
commit 5a63384f36
3 changed files with 33 additions and 34 deletions

@ -9,6 +9,8 @@ import de.studiocode.invgui.gui.SlotElement.VISlotElement;
import de.studiocode.invgui.item.Item; import de.studiocode.invgui.item.Item;
import de.studiocode.invgui.util.ArrayUtils; import de.studiocode.invgui.util.ArrayUtils;
import de.studiocode.invgui.virtualinventory.VirtualInventory; 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.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
@ -89,13 +91,22 @@ abstract class IndexedGUI implements GUI {
case MOVE_TO_OTHER_INVENTORY: case MOVE_TO_OTHER_INVENTORY:
event.setCancelled(true); 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();
// TODO: find a way to cancel at this point ItemStack invStack = virtualInventory.getItemStack(index);
virtualInventory.setAmount(player, index, leftOverAmount, true); ItemUpdateEvent updateEvent = new ItemUpdateEvent(virtualInventory, player, invStack,
index, invStack.getAmount(), -1);
Bukkit.getPluginManager().callEvent(updateEvent);
if (!updateEvent.isCancelled()) {
int leftOverAmount = 0;
HashMap<Integer, ItemStack> leftover =
event.getWhoClicked().getInventory().addItem(virtualInventory.getItemStack(index));
if (!leftover.isEmpty()) leftOverAmount = leftover.get(0).getAmount();
virtualInventory.setAmountSilently(index, leftOverAmount);
}
break; break;
default: default:

@ -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 <code>null</code> if it wasn't a player.
* @param index The slot index * @param index The slot index
* @param amount The new amount * @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]; ItemStack itemStack = items[index];
if (itemStack != null) { if (itemStack != null) {
int currentAmount = itemStack.getAmount(); if (amount == 0) items[index] = null;
else itemStack.setAmount(amount);
ItemUpdateEvent event = createAndCallEvent(player, itemStack, index, currentAmount, amount); notifyWindows();
if (!event.isCancelled() || ignoreCancelled) {
if (amount == 0) items[index] = null;
else itemStack.setAmount(amount);
notifyWindows();
return false;
}
} }
return true;
} }
/** /**
@ -256,7 +243,7 @@ public class VirtualInventory implements ConfigurationSerializable {
ItemStack itemStack = items[index]; ItemStack itemStack = items[index];
if (itemStack != null) { if (itemStack != null) {
ItemUpdateEvent event =createAndCallEvent(player, itemStack, index, ItemUpdateEvent event = createAndCallEvent(player, itemStack, index,
itemStack.getAmount(), 0); itemStack.getAmount(), 0);
if (!event.isCancelled()) { if (!event.isCancelled()) {

@ -34,7 +34,8 @@ public class ItemUpdateEvent extends Event implements Cancellable {
* @param itemStack The {@link ItemStack} that is affected * @param itemStack The {@link ItemStack} that is affected
* @param slot The slot that is affected * @param slot The slot that is affected
* @param previousAmount The previous amount of the {@link ItemStack} * @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, public ItemUpdateEvent(@NotNull VirtualInventory virtualInventory, @Nullable Player player, @NotNull ItemStack itemStack, int slot, int previousAmount,
int newAmount) { int newAmount) {
@ -48,7 +49,7 @@ public class ItemUpdateEvent extends Event implements Cancellable {
/** /**
* Gets the {@link HandlerList} of this {@link Event} * Gets the {@link HandlerList} of this {@link Event}
* *
* @return The {@link HandlerList} of this {@link Event} * @return The {@link HandlerList} of this {@link Event}
*/ */
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
@ -57,7 +58,7 @@ public class ItemUpdateEvent extends Event implements Cancellable {
/** /**
* Gets the {@link VirtualInventory} where this action takes place. * Gets the {@link VirtualInventory} where this action takes place.
* *
* @return The {@link VirtualInventory} * @return The {@link VirtualInventory}
*/ */
public VirtualInventory getVirtualInventory() { public VirtualInventory getVirtualInventory() {
@ -76,7 +77,7 @@ public class ItemUpdateEvent extends Event implements Cancellable {
/** /**
* Gets the {@link ItemStack} involved in this action. * Gets the {@link ItemStack} involved in this action.
* *
* @return The {@link ItemStack} * @return The {@link ItemStack}
*/ */
public ItemStack getItemStack() { public ItemStack getItemStack() {
@ -85,7 +86,7 @@ public class ItemUpdateEvent extends Event implements Cancellable {
/** /**
* Gets the slot that is affected. * Gets the slot that is affected.
* *
* @return The slot * @return The slot
*/ */
public int getSlot() { public int getSlot() {
@ -94,7 +95,7 @@ public class ItemUpdateEvent extends Event implements Cancellable {
/** /**
* Gets the previous amount of the {@link ItemStack} * Gets the previous amount of the {@link ItemStack}
* *
* @return The previous amount * @return The previous amount
*/ */
public int getPreviousAmount() { 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 * 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 * @return The new amount
*/ */
public int getNewAmount() { public int getNewAmount() {
@ -131,5 +132,5 @@ public class ItemUpdateEvent extends Event implements Cancellable {
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;
} }
} }