Skip to content

Commit 7f789a6

Browse files
Make ItemPreUpdateEvent#newItem immutable
Previously, the newItem could be modified / replaced and changes would sometimes be reflected in the source. However, this was not consistent and could in other cases lead to e.g. items being deleted. This commit removes the functionality to modify the newItem.
1 parent ab83600 commit 7f789a6

File tree

4 files changed

+6
-29
lines changed

4 files changed

+6
-29
lines changed

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
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;
87

98
/**
109
* An event that is called before a slot in an {@link Inventory} is updated.
1110
* <p>
1211
* Cancelling this event affects the source of the change, i.e. a {@link Player} would for example keep the item they
13-
* tried to place on their cursor. In certain situations, changing the amount of {@link #getNewItem()} will also be
14-
* reflected in the source.
12+
* tried to place on their cursor.
1513
* <p>
1614
* Note that a fired {@link ItemPreUpdateEvent} does not necessitate that the given action will actually take place,
1715
* even if the event remains uncancelled. For example, moving an item from one inventory to another via shift-clicking
@@ -41,17 +39,6 @@ public ItemPreUpdateEvent(Inventory inventory, int slot, @Nullable UpdateReason
4139
super(inventory, slot, updateReason, previousItem, newItem);
4240
}
4341

44-
/**
45-
* Change the {@link ItemStack} that will appear in the {@link Inventory}
46-
* to a different one.
47-
*
48-
* @param newItem The {@link ItemStack} to appear in the {@link Inventory}
49-
* if the {@link ItemPreUpdateEvent} is not cancelled.
50-
*/
51-
public void setNewItem(@Nullable ItemStack newItem) {
52-
this.newItemStack = ItemUtils.takeUnlessEmpty(newItem);
53-
}
54-
5542
/**
5643
* Gets the cancellation state of this event.
5744
*

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ abstract class ItemUpdateEvent {
2828
*/
2929
public ItemUpdateEvent(Inventory inventory, int slot, @Nullable UpdateReason updateReason,
3030
@Nullable ItemStack previousItem, @Nullable ItemStack newItem) {
31+
if (updateReason == UpdateReason.SUPPRESSED)
32+
throw new IllegalArgumentException("UpdateReason.SUPPRESSED cannot be used for ItemUpdateEvents");
3133

3234
this.inventory = inventory;
3335
this.slot = slot;
@@ -60,7 +62,7 @@ public Inventory getInventory() {
6062
* @return The {@link ItemStack}
6163
*/
6264
public @Nullable ItemStack getPreviousItem() {
63-
return previousItemStack;
65+
return ItemUtils.cloneUnlessEmpty(previousItemStack);
6466
}
6567

6668
/**
@@ -69,7 +71,7 @@ public Inventory getInventory() {
6971
* @return The new {@link ItemStack}
7072
*/
7173
public @Nullable ItemStack getNewItem() {
72-
return newItemStack;
74+
return ItemUtils.cloneUnlessEmpty(newItemStack);
7375
}
7476

7577
/**
@@ -128,7 +130,7 @@ public int getRemovedAmount() {
128130
throw new IllegalStateException("No items have been removed");
129131

130132
assert previousItemStack != null;
131-
if (newItemStack == null)
133+
if (newItemStack == null)
132134
return previousItemStack.getAmount();
133135
return previousItemStack.getAmount() - newItemStack.getAmount();
134136
}

invui/src/test/java/xyz/xenondevs/invui/inventory/CompositeInventoryTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import java.util.concurrent.atomic.AtomicBoolean;
1616
import java.util.concurrent.atomic.AtomicInteger;
17-
import java.util.concurrent.atomic.AtomicReference;
1817

1918
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2019
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -185,22 +184,17 @@ public void testPreUpdateEventStateVisibleInCompositeHandler() {
185184
var composite = new CompositeInventory(inv1);
186185

187186
AtomicBoolean doCancel = new AtomicBoolean();
188-
AtomicReference<ItemStack> newItemStack = new AtomicReference<>(ItemStack.empty());
189187
inv1.addPreUpdateHandler(event -> {
190188
event.setCancelled(doCancel.get());
191-
event.setNewItem(newItemStack.get());
192189
});
193190
composite.addPreUpdateHandler(event -> {
194191
assertEquals(event.isCancelled(), doCancel.get());
195-
assertEquals(event.getNewItem(), newItemStack.get());
196192
});
197193

198194
doCancel.set(false);
199-
newItemStack.set(ItemStack.of(Material.DIRT));
200195
composite.setItem(null, 0, ItemStack.of(Material.DIAMOND));
201196

202197
doCancel.set(true);
203-
newItemStack.set(ItemStack.of(Material.STONE));
204198
composite.setItem(null, 0, ItemStack.of(Material.DIAMOND));
205199
}
206200

invui/src/test/java/xyz/xenondevs/invui/inventory/ObscuredInventoryTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import java.util.concurrent.atomic.AtomicBoolean;
1616
import java.util.concurrent.atomic.AtomicInteger;
17-
import java.util.concurrent.atomic.AtomicReference;
1817

1918
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2019
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -173,22 +172,17 @@ public void testPreUpdateEventStateVisibleInObscuredHandler() {
173172
var obscured = new ObscuredInventory(backing, i -> i == 0);
174173

175174
AtomicBoolean doCancel = new AtomicBoolean();
176-
AtomicReference<ItemStack> newItemStack = new AtomicReference<>(ItemStack.empty());
177175
backing.addPreUpdateHandler(event -> {
178176
event.setCancelled(doCancel.get());
179-
event.setNewItem(newItemStack.get());
180177
});
181178
obscured.addPreUpdateHandler(event -> {
182179
assertEquals(event.isCancelled(), doCancel.get());
183-
assertEquals(event.getNewItem(), newItemStack.get());
184180
});
185181

186182
doCancel.set(false);
187-
newItemStack.set(ItemStack.of(Material.STONE));
188183
obscured.setItem(null, 0, ItemStack.of(Material.DIAMOND));
189184

190185
doCancel.set(true);
191-
newItemStack.set(ItemStack.of(Material.STONE));
192186
obscured.setItem(null, 0, ItemStack.of(Material.DIAMOND));
193187
}
194188

0 commit comments

Comments
 (0)