From 4946485e40e0fc57a37f9e28c9cba9d0cf2378fa Mon Sep 17 00:00:00 2001 From: broccolai Date: Tue, 31 Oct 2023 13:07:40 +0000 Subject: [PATCH] optionally cancel click events coming from player inventory in chest views --- .../paper/PaperInterfaceListeners.java | 32 ++++++++------ .../interfaces/paper/type/ChestInterface.java | 42 +++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/paper/src/main/java/org/incendo/interfaces/paper/PaperInterfaceListeners.java b/paper/src/main/java/org/incendo/interfaces/paper/PaperInterfaceListeners.java index 4300b777..9fa85c37 100644 --- a/paper/src/main/java/org/incendo/interfaces/paper/PaperInterfaceListeners.java +++ b/paper/src/main/java/org/incendo/interfaces/paper/PaperInterfaceListeners.java @@ -433,8 +433,15 @@ public void onInventoryClick(final @NonNull InventoryClickEvent event) { } private void handleChestViewClick(final @NonNull InventoryClickEvent event, final @NonNull InventoryHolder holder) { + ChestView chestView = (ChestView) holder; + if (event.getSlot() != event.getRawSlot()) { - this.handlePlayerViewClick(event); + if (chestView.backing().cancelClicksInPlayerInventory()) { + event.setCancelled(true); + } else { + this.handlePlayerViewClick(event); + } + return; } @@ -444,21 +451,20 @@ private void handleChestViewClick(final @NonNull InventoryClickEvent event, fina false ); - ChestView chestView = (ChestView) holder; - // Handle element click event - if (event.getSlotType() == InventoryType.SlotType.CONTAINER) { - int slot = event.getSlot(); - int x = slot % 9; - int y = slot / 9; - ChestPane pane = chestView.pane(); + if (event.getSlotType() != InventoryType.SlotType.CONTAINER) { + return; + } - if (y < pane.rows()) { - // Handle parent interface click event - chestView.backing().clickHandler().accept(context); + int slot = event.getSlot(); + int x = slot % 9; + int y = slot / 9; + ChestPane pane = chestView.pane(); - pane.element(x, y).clickHandler().accept(context); - } + if (y < pane.rows()) { + // Handle parent interface click event + chestView.backing().clickHandler().accept(context); + pane.element(x, y).clickHandler().accept(context); } } diff --git a/paper/src/main/java/org/incendo/interfaces/paper/type/ChestInterface.java b/paper/src/main/java/org/incendo/interfaces/paper/type/ChestInterface.java index 080a0168..d22ec09a 100644 --- a/paper/src/main/java/org/incendo/interfaces/paper/type/ChestInterface.java +++ b/paper/src/main/java/org/incendo/interfaces/paper/type/ChestInterface.java @@ -36,6 +36,7 @@ public final class ChestInterface implements private final @NonNull Component title; private final boolean updates; private final int updateDelay; + private final boolean cancelClicksInPlayerInventory; private final @NonNull ClickHandler> clickHandler; @@ -48,6 +49,7 @@ public final class ChestInterface implements * @param closeHandlers the close handlers to apply * @param updates {@code true} if the interface is an updating interface * @param updateDelay the update delay + * @param cancelClicksInPlayerInventory whether to cancel clicks in the players inventory * @param clickHandler the handler to run on click */ public ChestInterface( @@ -57,6 +59,7 @@ public ChestInterface( final @NonNull List> closeHandlers, final boolean updates, final int updateDelay, + final boolean cancelClicksInPlayerInventory, final @NonNull ClickHandler> clickHandler ) { @@ -66,6 +69,7 @@ public ChestInterface( this.updates = updates; this.updateDelay = updateDelay; this.rows = rows; + this.cancelClicksInPlayerInventory = cancelClicksInPlayerInventory; this.clickHandler = clickHandler; } @@ -208,6 +212,14 @@ public int updateDelay() { return this.updateDelay; } + /** + * Whether interfaces should cancel events that come from the players inventories. + * @return true if it should cancel the events + */ + public boolean cancelClicksInPlayerInventory() { + return this.cancelClicksInPlayerInventory; + } + /** * A class that builds a chest interface. */ @@ -243,6 +255,8 @@ public static final class Builder implements Interface.Builder> clickHandler ) { @@ -278,6 +294,7 @@ private Builder( this.title = title; this.updates = updates; this.updateDelay = updateDelay; + this.cancelClicksInPlayerInventory = cancelClicksInPlayerInventory; this.clickHandler = clickHandler; } @@ -304,6 +321,7 @@ public int rows() { this.title, this.updates, this.updateDelay, + this.cancelClicksInPlayerInventory, this.clickHandler ); } @@ -331,6 +349,7 @@ public int rows() { title, this.updates, this.updateDelay, + this.cancelClicksInPlayerInventory, this.clickHandler ); } @@ -352,6 +371,7 @@ public int rows() { this.title, this.updates, this.updateDelay, + this.cancelClicksInPlayerInventory, this.clickHandler ); } @@ -384,6 +404,7 @@ public int rows() { this.title, this.updates, this.updateDelay, + this.cancelClicksInPlayerInventory, this.clickHandler ); } @@ -424,6 +445,7 @@ InventoryClickContext> clickHandler() { this.title, this.updates, this.updateDelay, + this.cancelClicksInPlayerInventory, handler ); } @@ -443,6 +465,25 @@ InventoryClickContext> clickHandler() { this.title, updates, updateDelay, + this.cancelClicksInPlayerInventory, + this.clickHandler + ); + } + + /** + * Controls if the interface should stop items coming from the player's inventory. + * @param cancelClicksInPlayerInventory true if it should stop items. + * @return new builder instance + */ + public @NonNull Builder cancelClicksInPlayerInventory(final boolean cancelClicksInPlayerInventory) { + return new Builder( + this.transformsList, + this.closeHandlerList, + this.rows, + this.title, + this.updates, + this.updateDelay, + cancelClicksInPlayerInventory, this.clickHandler ); } @@ -461,6 +502,7 @@ InventoryClickContext> clickHandler() { this.closeHandlerList, this.updates, this.updateDelay, + this.cancelClicksInPlayerInventory, this.clickHandler ); }