Skip to content

Commit c956e45

Browse files
Fix slot click handling to consider all layouts (#34)
* Improve click handling to check if a clicked slot is in both layouts present * Add test if the clicked item passes through the static layout of the inventory * Cleanup imports and improve variable name --------- Co-authored-by: theEvilReaper <theEvilReaper@users.noreply.github.com>
1 parent 947dc14 commit c956e45

2 files changed

Lines changed: 64 additions & 16 deletions

File tree

src/main/java/net/theevilreaper/aves/inventory/InventoryBuilder.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,17 @@ protected InventoryBuilder(@NotNull InventoryType type) {
6262
return;
6363
}
6464

65+
ISlot clickedSlot = null;
66+
6567
if (this.dataLayout != null) {
66-
var clickedSlot = this.dataLayout.getSlot(slot);
67-
acceptClick(clickedSlot, player, clickType, slot, stack, result);
68-
return;
68+
clickedSlot = this.dataLayout.getSlot(slot);
6969
}
7070

71-
if (this.inventoryLayout != null) {
72-
var clickedSlot = this.inventoryLayout.getSlot(slot);
73-
acceptClick(clickedSlot, player, clickType, slot, stack, result);
74-
return;
71+
if ((clickedSlot == null || clickedSlot instanceof EmptySlot) && this.inventoryLayout != null) {
72+
clickedSlot = this.inventoryLayout.getSlot(slot);
7573
}
7674

77-
result.accept(ClickHolder.noClick());
75+
acceptClick(clickedSlot, player, clickType, slot, stack, result);
7876
};
7977
}
8078

src/test/java/net/theevilreaper/aves/inventory/InventoryClickLogicIntegrationTest.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,28 @@
1616
import net.minestom.testing.extension.MicrotusExtension;
1717
import net.theevilreaper.aves.inventory.click.ClickHolder;
1818
import org.jetbrains.annotations.NotNull;
19+
import org.junit.jupiter.api.BeforeAll;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.extension.ExtendWith;
2122

2223
import java.util.HashMap;
24+
import java.util.List;
2325
import java.util.concurrent.atomic.AtomicBoolean;
2426

25-
import static org.junit.jupiter.api.Assertions.assertNotNull;
26-
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
import static org.junit.jupiter.api.Assertions.*;
2728

2829
@ExtendWith(MicrotusExtension.class)
2930
class InventoryClickLogicIntegrationTest {
3031

32+
private static ItemStack testItem;
33+
34+
@BeforeAll
35+
static void setup() {
36+
testItem = ItemStack.builder(Material.DIAMOND)
37+
.customName(Component.text("Test Item"))
38+
.build();
39+
}
40+
3141
@Test
3242
void testInventoryClickFlow(@NotNull Env env) {
3343
Instance instance = env.createEmptyInstance();
@@ -38,12 +48,8 @@ void testInventoryClickFlow(@NotNull Env env) {
3848
assertNotNull(builder);
3949

4050
InventoryLayout layout = InventoryLayout.fromType(builder.getType());
41-
ItemStack item = ItemStack.builder(Material.DIAMOND)
42-
.customName(Component.text("Test Item"))
43-
.build();
44-
4551
AtomicBoolean clicked = new AtomicBoolean(false);
46-
layout.setItem(0, item, (player, slot, click, stack, result) -> {
52+
layout.setItem(0, testItem, (player, slot, click, stack, result) -> {
4753
result.accept(ClickHolder.cancelClick());
4854
clicked.set(true);
4955
});
@@ -57,7 +63,7 @@ void testInventoryClickFlow(@NotNull Env env) {
5763

5864
Collector<WindowItemsPacket> windowsPacketCollector = testConnection.trackIncoming(WindowItemsPacket.class);
5965

60-
testLeftClick(testPlayer, 0, item);
66+
testLeftClick(testPlayer, 0, testItem);
6167
// The return value should only be one packet, the one that updates the slots
6268
windowsPacketCollector.assertCount(2);
6369
assertTrue(clicked.get());
@@ -68,6 +74,50 @@ void testInventoryClickFlow(@NotNull Env env) {
6874
}
6975

7076

77+
@Test
78+
void testIfClickPassesInventoryLayout(@NotNull Env env) {
79+
Instance instance = env.createEmptyInstance();
80+
TestConnection testConnection = env.createConnection();
81+
Player testPlayer = testConnection.connect(instance);
82+
InventoryBuilder builder = new GlobalInventoryBuilder(Component.text("Test Inventory"), InventoryType.CHEST_3_ROW);
83+
84+
assertNotNull(builder);
85+
86+
InventoryLayout layout = InventoryLayout.fromType(builder.getType());
87+
88+
AtomicBoolean clicked = new AtomicBoolean(false);
89+
layout.setItem(0, testItem, (player, slot, click, stack, result) -> {
90+
result.accept(ClickHolder.cancelClick());
91+
clicked.set(true);
92+
});
93+
94+
builder.setLayout(layout);
95+
builder.register();
96+
97+
testPlayer.openInventory(builder.getInventory());
98+
99+
assertNotNull(testPlayer.getOpenInventory());
100+
101+
Collector<WindowItemsPacket> windowsPacketCollector = testConnection.trackIncoming(WindowItemsPacket.class);
102+
103+
testLeftClick(testPlayer, 0, testItem);
104+
// The return value should only be one packet, the one that updates the slots
105+
windowsPacketCollector.assertCount(2);
106+
107+
WindowItemsPacket inventoryPacket = windowsPacketCollector.collect().getLast();
108+
assertNotNull(inventoryPacket);
109+
110+
List<ItemStack> items = inventoryPacket.items();
111+
assertNotNull(items, "Items in the inventory packet should not be null");
112+
assertEquals(layout.getSize(), items.size(), "Items size should match layout size");
113+
114+
assertEquals(testItem, items.getFirst(), "First item should be the test item");
115+
116+
env.destroyInstance(instance, true);
117+
assertTrue(instance.getPlayers().isEmpty(), "Instance should not have any players");
118+
}
119+
120+
71121
private void testLeftClick(@NotNull Player player, int slot, @NotNull ItemStack stack) {
72122
_leftClick(player.getOpenInventory(), true, player, slot, stack);
73123
}

0 commit comments

Comments
 (0)