Skip to content

Commit 6bbc327

Browse files
changes
1 parent 103a6ea commit 6bbc327

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed
16.8 KB
Binary file not shown.

docs/invui2/inventory.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ InvUI's inventories have a powerful event system. There are multiple events that
4444

4545
#### ItemPreUpdateEvent
4646

47-
This event is called before changes were fully processed. Cancelling this event or changing the amount of items that were added or removed will affect the source of the change (i.e. the player's cursor most of the time) appropriately, if possible. This allows restricting which or how many items can be put into an inventory or even a specific slot of an inventory.
47+
This event is called before changes were fully processed. Cancelling this event will affect the source of the change (i.e. the player's cursor most of the time) appropriately, if possible. This allows restricting which items can be put into an inventory or even a specific slot of an inventory.
4848

49-
In the following example, the `ItemPreUpdateEvent` is cancelled or handled in such a way that only red and orange wool can be put into the inventory. Additionally, orange wool can no longer be stacked:
49+
In the following example, the `ItemPreUpdateEvent` is cancelled in such a way that only red wool can be placed on slot 10, and only orange wool can be placed on the other slots:
5050

5151
=== "Kotlin"
5252
```kotlin
5353
inv.addPreUpdateHandler { event ->
5454
if (event.isAdd || event.isSwap) {
55-
when (event.newItem?.type) {
56-
Material.RED_WOOL -> Unit // red wool can be added normally
57-
Material.ORANGE_WOOL -> event.newItem?.amount = 1 // orange wool stack size is limited to 1
58-
else -> event.isCancelled = true // cancel event for all other item types
55+
if (event.slot == 10) {
56+
event.isCancelled = event.newItem?.type != Material.RED_WOOL
57+
} else {
58+
event.isCancelled = event.newItem?.type != Material.ORANGE_WOOL
5959
}
6060
}
6161
}
@@ -65,10 +65,10 @@ In the following example, the `ItemPreUpdateEvent` is cancelled or handled in su
6565
```java
6666
inv.addPreUpdateHandler(event -> {
6767
if (event.isAdd() || event.isSwap()) {
68-
switch (event.getNewItem().getType()) {
69-
case Material.RED_WOOL -> {} // red wool can be added normally
70-
case Material.ORANGE_WOOL -> event.getNewItem().setAmount(1); // orange wool stack size is limited to 1
71-
default -> event.setCancelled(true); // cancel event for all other item types
68+
if (event.getSlot() == 10) {
69+
event.setCancelled(event.getNewItem().getType() != Material.RED_WOOL);
70+
} else {
71+
event.setCancelled(event.getNewItem().getType() != Material.ORANGE_WOOL);
7272
}
7373
}
7474
});

0 commit comments

Comments
 (0)