1616import net .minestom .testing .extension .MicrotusExtension ;
1717import net .theevilreaper .aves .inventory .click .ClickHolder ;
1818import org .jetbrains .annotations .NotNull ;
19+ import org .junit .jupiter .api .BeforeAll ;
1920import org .junit .jupiter .api .Test ;
2021import org .junit .jupiter .api .extension .ExtendWith ;
2122
2223import java .util .HashMap ;
24+ import java .util .List ;
2325import 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 )
2930class 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