Skip to content

Commit 40c6986

Browse files
Clone item stacks in ItemUpdateEvent constructor
1 parent 12cba57 commit 40c6986

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

invui/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ public void callClickEvent(int slot, InventoryClickEvent event) {
472472
* @param updateReason The {@link UpdateReason}.
473473
* @param slot The slot of the affected {@link ItemStack}.
474474
* @param previousItemStack The {@link ItemStack} that was previously on that slot.
475+
* Will be cloned to prevent modifications.
475476
* @param newItemStack The {@link ItemStack} that will be on that slot.
477+
* Will be cloned to prevent modifications.
476478
* @return The {@link ItemPreUpdateEvent} after it has been handled by the pre update handlers.
477479
*/
478480
public ItemPreUpdateEvent callPreUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack previousItemStack, @Nullable ItemStack newItemStack) {
@@ -496,7 +498,9 @@ public ItemPreUpdateEvent callPreUpdateEvent(@Nullable UpdateReason updateReason
496498
* @param updateReason The {@link UpdateReason}.
497499
* @param slot The slot of the affected {@link ItemStack}.
498500
* @param previousItemStack The {@link ItemStack} that was on that slot previously.
501+
* Will be cloned to prevent modifications.
499502
* @param newItemStack The {@link ItemStack} that is on that slot now.
503+
* Will be cloned to prevent modifications.
500504
*/
501505
public void callPostUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack previousItemStack, @Nullable ItemStack newItemStack) {
502506
if (updateReason == UpdateReason.SUPPRESSED)

invui/src/main/java/xyz/xenondevs/invui/inventory/event/ItemPreUpdateEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.inventory.ItemStack;
55
import org.jspecify.annotations.Nullable;
66
import xyz.xenondevs.invui.inventory.Inventory;
7+
import xyz.xenondevs.invui.util.ItemUtils;
78

89
/**
910
* An event that is called whenever a slot inside a {@link Inventory} gets updated.
@@ -38,7 +39,7 @@ public ItemPreUpdateEvent(Inventory inventory, int slot, @Nullable UpdateReason
3839
* if the {@link ItemPreUpdateEvent} is not cancelled.
3940
*/
4041
public void setNewItem(@Nullable ItemStack newItem) {
41-
this.newItemStack = newItem;
42+
this.newItemStack = ItemUtils.takeUnlessEmpty(newItem);
4243
}
4344

4445
/**

invui/src/main/java/xyz/xenondevs/invui/inventory/event/ItemUpdateEvent.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.inventory.ItemStack;
55
import org.jspecify.annotations.Nullable;
66
import xyz.xenondevs.invui.inventory.Inventory;
7+
import xyz.xenondevs.invui.util.ItemUtils;
78

89
abstract class ItemUpdateEvent {
910

@@ -31,8 +32,8 @@ public ItemUpdateEvent(Inventory inventory, int slot, @Nullable UpdateReason upd
3132
this.inventory = inventory;
3233
this.slot = slot;
3334
this.updateReason = updateReason;
34-
this.previousItemStack = previousItem;
35-
this.newItemStack = newItem;
35+
this.previousItemStack = ItemUtils.cloneUnlessEmpty(previousItem);
36+
this.newItemStack = ItemUtils.cloneUnlessEmpty(newItem);
3637
}
3738

3839
/**
@@ -63,7 +64,7 @@ public Inventory getInventory() {
6364
}
6465

6566
/**
66-
* Gets clone of the new {@link ItemStack} that will be there if the event isn't cancelled.
67+
* Gets a clone of the new {@link ItemStack} that will be there if the event isn't cancelled.
6768
*
6869
* @return The new {@link ItemStack}
6970
*/

invui/src/main/java/xyz/xenondevs/invui/util/ItemUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,17 @@ public static boolean isEmpty(@Nullable ItemStack itemStack) {
5555
return clone;
5656
}
5757

58+
/**
59+
* Clones the given {@link ItemStack} and returns it, unless it is empty, in which case null is returned.
60+
*
61+
* @param itemStack The {@link ItemStack} to clone.
62+
* @return The cloned {@link ItemStack} or null if it is empty.
63+
*/
64+
public static @Nullable ItemStack cloneUnlessEmpty(@Nullable ItemStack itemStack) {
65+
if (isEmpty(itemStack))
66+
return null;
67+
68+
return itemStack.clone();
69+
}
70+
5871
}

0 commit comments

Comments
 (0)