Skip to content

Commit

Permalink
optionally cancel click events coming from player inventory in chest …
Browse files Browse the repository at this point in the history
…views
  • Loading branch information
broccolai committed Oct 31, 2023
1 parent 16b1ed2 commit 4946485
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChestPane, InventoryClickEvent, PlayerViewer, InventoryClickContext<ChestPane,
ChestView>> clickHandler;

Expand All @@ -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(
Expand All @@ -57,6 +59,7 @@ public ChestInterface(
final @NonNull List<CloseHandler<ChestPane>> closeHandlers,
final boolean updates,
final int updateDelay,
final boolean cancelClicksInPlayerInventory,
final @NonNull ClickHandler<ChestPane, InventoryClickEvent, PlayerViewer, InventoryClickContext<ChestPane,
ChestView>> clickHandler
) {
Expand All @@ -66,6 +69,7 @@ public ChestInterface(
this.updates = updates;
this.updateDelay = updateDelay;
this.rows = rows;
this.cancelClicksInPlayerInventory = cancelClicksInPlayerInventory;
this.clickHandler = clickHandler;
}

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -243,6 +255,8 @@ public static final class Builder implements Interface.Builder<ChestPane, Player
*/
private final int updateDelay;

private final boolean cancelClicksInPlayerInventory;

/**
* The top click handler.
*/
Expand All @@ -259,6 +273,7 @@ public Builder() {
this.title = Component.empty();
this.updates = false;
this.updateDelay = 1;
this.cancelClicksInPlayerInventory = false;
this.clickHandler = ClickHandler.cancel();
}

Expand All @@ -269,6 +284,7 @@ private Builder(
final @NonNull Component title,
final boolean updates,
final int updateDelay,
final boolean cancelClicksInPlayerInventory,
final @NonNull ClickHandler<ChestPane, InventoryClickEvent, PlayerViewer, InventoryClickContext<ChestPane,
ChestView>> clickHandler
) {
Expand All @@ -278,6 +294,7 @@ private Builder(
this.title = title;
this.updates = updates;
this.updateDelay = updateDelay;
this.cancelClicksInPlayerInventory = cancelClicksInPlayerInventory;
this.clickHandler = clickHandler;
}

Expand All @@ -304,6 +321,7 @@ public int rows() {
this.title,
this.updates,
this.updateDelay,
this.cancelClicksInPlayerInventory,
this.clickHandler
);
}
Expand Down Expand Up @@ -331,6 +349,7 @@ public int rows() {
title,
this.updates,
this.updateDelay,
this.cancelClicksInPlayerInventory,
this.clickHandler
);
}
Expand All @@ -352,6 +371,7 @@ public int rows() {
this.title,
this.updates,
this.updateDelay,
this.cancelClicksInPlayerInventory,
this.clickHandler
);
}
Expand Down Expand Up @@ -384,6 +404,7 @@ public int rows() {
this.title,
this.updates,
this.updateDelay,
this.cancelClicksInPlayerInventory,
this.clickHandler
);
}
Expand Down Expand Up @@ -424,6 +445,7 @@ InventoryClickContext<ChestPane, ChestView>> clickHandler() {
this.title,
this.updates,
this.updateDelay,
this.cancelClicksInPlayerInventory,
handler
);
}
Expand All @@ -443,6 +465,25 @@ InventoryClickContext<ChestPane, ChestView>> 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
);
}
Expand All @@ -461,6 +502,7 @@ InventoryClickContext<ChestPane, ChestView>> clickHandler() {
this.closeHandlerList,
this.updates,
this.updateDelay,
this.cancelClicksInPlayerInventory,
this.clickHandler
);
}
Expand Down

0 comments on commit 4946485

Please sign in to comment.