11package xyz .xenondevs .invui .inventory ;
22
33import org .bukkit .inventory .ItemStack ;
4- import org .jetbrains .annotations .UnmodifiableView ;
54import org .jspecify .annotations .Nullable ;
65import xyz .xenondevs .invui .Click ;
6+ import xyz .xenondevs .invui .InvUI ;
77import xyz .xenondevs .invui .Observer ;
88import xyz .xenondevs .invui .internal .util .ArrayUtils ;
99import xyz .xenondevs .invui .inventory .event .InventoryClickEvent ;
10- import xyz .xenondevs .invui .inventory .event .ItemPostUpdateEvent ;
1110import xyz .xenondevs .invui .inventory .event .ItemPreUpdateEvent ;
1211import xyz .xenondevs .invui .inventory .event .UpdateReason ;
1312
1413import java .util .Collection ;
15- import java .util .List ;
16- import java .util .function .Consumer ;
14+ import java .util .logging .Level ;
1715
1816/**
1917 * An {@link Inventory} which is composed of multiple other {@link Inventory Inventories}.
@@ -64,16 +62,18 @@ private static int calculateSize(Collection<? extends Inventory> inventories) {
6462
6563 @ Override
6664 public int [] getIterationOrder (OperationCategory category ) {
65+ int [] compositeIterationOrder = super .getIterationOrder (category );
6766 int [] iterationOrder = new int [getSize ()];
6867 int i = 0 ;
6968 int offset = 0 ;
7069 for (Inventory inventory : inventories ) {
7170 int [] invOrder = inventory .getIterationOrder (category );
7271 for (var slot : invOrder ) {
73- iterationOrder [i ++] = offset + slot ;
72+ iterationOrder [compositeIterationOrder [ i ++] ] = offset + slot ;
7473 }
7574 offset += inventory .getSize ();
7675 }
76+
7777 return iterationOrder ;
7878 }
7979
@@ -187,23 +187,54 @@ public void removeObserver(Observer who, int what, int how) {
187187 @ Override
188188 public boolean callClickEvent (int slot , Click click ) {
189189 var invSlot = findInventory (slot );
190- return invSlot .inventory ().callClickEvent (invSlot .slot (), click );
190+ var cancelled = invSlot .inventory ().callClickEvent (invSlot .slot (), click );
191+
192+ var clickEvent = new InventoryClickEvent (this , slot , click );
193+ clickEvent .setCancelled (cancelled );
194+ for (var handler : getClickHandlers ()) {
195+ try {
196+ handler .accept (clickEvent );
197+ } catch (Throwable t ) {
198+ InvUI .getInstance ().handleUncaughtException ("An exception occurred while handling an inventory event" , t );
199+ }
200+ }
201+
202+ return clickEvent .isCancelled ();
191203 }
192204
193205 @ Override
194206 public ItemPreUpdateEvent callPreUpdateEvent (@ Nullable UpdateReason updateReason , int slot , @ Nullable ItemStack previousItemStack , @ Nullable ItemStack newItemStack ) {
207+ if (updateReason == UpdateReason .SUPPRESSED )
208+ throw new IllegalArgumentException ("Cannot call ItemPreUpdateEvent with UpdateReason.SUPPRESSED" );
209+
195210 var invSlot = findInventory (slot );
196- return invSlot .inventory ().callPreUpdateEvent (updateReason , invSlot .slot (), previousItemStack , newItemStack );
211+ var delegatedEvent = invSlot .inventory ().callPreUpdateEvent (updateReason , invSlot .slot (), previousItemStack , newItemStack );
212+ var event = new ItemPreUpdateEvent (this , slot , updateReason , delegatedEvent .getPreviousItem (), delegatedEvent .getNewItem ());
213+ event .setCancelled (delegatedEvent .isCancelled ());
214+
215+ for (var handler : getPreUpdateHandlers ()) {
216+ try {
217+ handler .accept (event );
218+ } catch (Throwable t ) {
219+ InvUI .getInstance ().handleUncaughtException ("An exception occurred while handling an inventory event" , t );
220+ }
221+ }
222+
223+ return event ;
197224 }
198225
199226 @ Override
200227 public void callPostUpdateEvent (@ Nullable UpdateReason updateReason , int slot , @ Nullable ItemStack previousItemStack , @ Nullable ItemStack newItemStack ) {
201228 var invSlot = findInventory (slot );
202229 invSlot .inventory ().callPostUpdateEvent (updateReason , invSlot .slot (), previousItemStack , newItemStack );
230+ super .callPostUpdateEvent (updateReason , slot , previousItemStack , newItemStack );
203231 }
204232
205233 @ Override
206234 public boolean hasEventHandlers () {
235+ if (super .hasEventHandlers ())
236+ return true ;
237+
207238 for (Inventory inventory : inventories ) {
208239 if (inventory .hasEventHandlers ())
209240 return true ;
@@ -212,69 +243,4 @@ public boolean hasEventHandlers() {
212243 return false ;
213244 }
214245
215- @ Override
216- public void setIterationOrder (OperationCategory category , int [] iterationOrder ) {
217- throw new UnsupportedOperationException ("Iteration order needs to be set in the backing inventories" );
218- }
219-
220- @ Override
221- public @ UnmodifiableView List <Consumer <InventoryClickEvent >> getClickHandlers () {
222- throw new UnsupportedOperationException ("Click handlers need to be set in the backing inventory" );
223- }
224-
225- @ Override
226- public void setClickHandlers (List <? extends Consumer <InventoryClickEvent >> clickHandlers ) {
227- throw new UnsupportedOperationException ("Click handlers need to be set in the backing inventory" );
228- }
229-
230- @ Override
231- public void addClickHandler (Consumer <? super InventoryClickEvent > clickHandler ) {
232- throw new UnsupportedOperationException ("Click handlers need to be set in the backing inventory" );
233- }
234-
235- @ Override
236- public void removeClickHandler (Consumer <? super InventoryClickEvent > clickHandler ) {
237- throw new UnsupportedOperationException ("Click handlers need to be set in the backing inventory" );
238- }
239-
240- @ Override
241- public @ UnmodifiableView List <Consumer <ItemPreUpdateEvent >> getPreUpdateHandlers () {
242- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
243- }
244-
245- @ Override
246- public void setPreUpdateHandlers (List <? extends Consumer <ItemPreUpdateEvent >> preUpdateHandlers ) {
247- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
248- }
249-
250- @ Override
251- public void addPreUpdateHandler (Consumer <? super ItemPreUpdateEvent > preUpdateHandler ) {
252- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
253- }
254-
255- @ Override
256- public void removePreUpdateHandler (Consumer <? super ItemPreUpdateEvent > preUpdateHandler ) {
257- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
258- }
259-
260- @ Override
261- public @ UnmodifiableView List <Consumer <ItemPostUpdateEvent >> getPostUpdateHandlers () {
262- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
263- }
264-
265- @ Override
266- public void setPostUpdateHandlers (List <? extends Consumer <ItemPostUpdateEvent >> postUpdateHandlers ) {
267- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
268- }
269-
270- @ Override
271- public void addPostUpdateHandler (Consumer <? super ItemPostUpdateEvent > postUpdateHandler ) {
272- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
273- }
274-
275- @ Override
276- public void removePostUpdateHandler (Consumer <? super ItemPostUpdateEvent > postUpdateHandler ) {
277- throw new UnsupportedOperationException ("Update handlers need to be set in the backing inventory" );
278- }
279-
280246}
0 commit comments