@@ -167,6 +167,16 @@ public InventoryAPI setCloseFunction(final Consumer<InventoryCloseEvent> functio
167167 return this ;
168168 }
169169
170+ /**
171+ * Define the click function.
172+ * @param function A consumer, that will be triggered when the player click while the inventory is opened.
173+ * @return Your InventoryAPI object
174+ */
175+ public InventoryAPI setClickFunction (final Consumer <InventoryClickEvent > function ) {
176+ this .clickEvent = function ;
177+ return this ;
178+ }
179+
170180 /**
171181 * Enable interaction protection for your Inventory
172182 * @param interactionCancelled A boolean, to enable/disable
@@ -239,15 +249,23 @@ Inventory getInventory() {
239249 * @return A consumer, that corresponds to the refresh task.
240250 */
241251 public Consumer <InventoryAPI > getFunction () {
242- return function ;
252+ return this . function ;
243253 }
244254
245255 /**
246256 * Get the consumer which will be triggered when the player will close the inventory.
247257 * @return A consumer, that corresponds to close event.
248258 */
249259 public Consumer <InventoryCloseEvent > getCloseEvent () {
250- return closeEvent ;
260+ return this .closeEvent ;
261+ }
262+
263+ /**
264+ * Get the consumer which will be triggered when the player will click will the inventory is opened.
265+ * @return A consumer, that corresponds to click event.
266+ */
267+ public Consumer <InventoryClickEvent > getClickEvent () {
268+ return this .clickEvent ;
251269 }
252270
253271 /**
@@ -487,20 +505,24 @@ private Inventory generate() {
487505 @ EventHandler
488506 public void onClose (final InventoryCloseEvent e ) {
489507 if (e .getView ().getTopInventory ().equals (this .inventory )) {
508+ if (this .closeEvent != null )
509+ this .closeEvent .accept (e );
490510 this .stop ();
491- this .closeEvent .accept (e );
492511 }
493512 if (!e .getInventory ().equals (this .inventory ))
494513 return ;
495514 if (e .getInventory ().getHolder () == null ) {
515+ if (this .closeEvent != null )
516+ this .closeEvent .accept (e );
496517 this .stop ();
497- this .closeEvent .accept (e );
498518 }
499519 }
500520
501521 @ EventHandler
502522 public void onInteract (final InventoryClickEvent e ) {
503- if (e .getInventory ().equals (this .inventory ))
523+ if (e .getView () == null || e .getView ().getTopInventory () == null )
524+ return ;
525+ if (e .getView ().getTopInventory ().equals (this .inventory ))
504526 if (this .clickEvent != null )
505527 this .clickEvent .accept (e );
506528 if (e .getClickedInventory () == null )
0 commit comments