Skip to content

Commit 4946485

Browse files
committed
optionally cancel click events coming from player inventory in chest views
1 parent 16b1ed2 commit 4946485

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

paper/src/main/java/org/incendo/interfaces/paper/PaperInterfaceListeners.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,15 @@ public void onInventoryClick(final @NonNull InventoryClickEvent event) {
433433
}
434434

435435
private void handleChestViewClick(final @NonNull InventoryClickEvent event, final @NonNull InventoryHolder holder) {
436+
ChestView chestView = (ChestView) holder;
437+
436438
if (event.getSlot() != event.getRawSlot()) {
437-
this.handlePlayerViewClick(event);
439+
if (chestView.backing().cancelClicksInPlayerInventory()) {
440+
event.setCancelled(true);
441+
} else {
442+
this.handlePlayerViewClick(event);
443+
}
444+
438445
return;
439446
}
440447

@@ -444,21 +451,20 @@ private void handleChestViewClick(final @NonNull InventoryClickEvent event, fina
444451
false
445452
);
446453

447-
ChestView chestView = (ChestView) holder;
448-
449454
// Handle element click event
450-
if (event.getSlotType() == InventoryType.SlotType.CONTAINER) {
451-
int slot = event.getSlot();
452-
int x = slot % 9;
453-
int y = slot / 9;
454-
ChestPane pane = chestView.pane();
455+
if (event.getSlotType() != InventoryType.SlotType.CONTAINER) {
456+
return;
457+
}
455458

456-
if (y < pane.rows()) {
457-
// Handle parent interface click event
458-
chestView.backing().clickHandler().accept(context);
459+
int slot = event.getSlot();
460+
int x = slot % 9;
461+
int y = slot / 9;
462+
ChestPane pane = chestView.pane();
459463

460-
pane.element(x, y).clickHandler().accept(context);
461-
}
464+
if (y < pane.rows()) {
465+
// Handle parent interface click event
466+
chestView.backing().clickHandler().accept(context);
467+
pane.element(x, y).clickHandler().accept(context);
462468
}
463469
}
464470

paper/src/main/java/org/incendo/interfaces/paper/type/ChestInterface.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public final class ChestInterface implements
3636
private final @NonNull Component title;
3737
private final boolean updates;
3838
private final int updateDelay;
39+
private final boolean cancelClicksInPlayerInventory;
3940
private final @NonNull ClickHandler<ChestPane, InventoryClickEvent, PlayerViewer, InventoryClickContext<ChestPane,
4041
ChestView>> clickHandler;
4142

@@ -48,6 +49,7 @@ public final class ChestInterface implements
4849
* @param closeHandlers the close handlers to apply
4950
* @param updates {@code true} if the interface is an updating interface
5051
* @param updateDelay the update delay
52+
* @param cancelClicksInPlayerInventory whether to cancel clicks in the players inventory
5153
* @param clickHandler the handler to run on click
5254
*/
5355
public ChestInterface(
@@ -57,6 +59,7 @@ public ChestInterface(
5759
final @NonNull List<CloseHandler<ChestPane>> closeHandlers,
5860
final boolean updates,
5961
final int updateDelay,
62+
final boolean cancelClicksInPlayerInventory,
6063
final @NonNull ClickHandler<ChestPane, InventoryClickEvent, PlayerViewer, InventoryClickContext<ChestPane,
6164
ChestView>> clickHandler
6265
) {
@@ -66,6 +69,7 @@ public ChestInterface(
6669
this.updates = updates;
6770
this.updateDelay = updateDelay;
6871
this.rows = rows;
72+
this.cancelClicksInPlayerInventory = cancelClicksInPlayerInventory;
6973
this.clickHandler = clickHandler;
7074
}
7175

@@ -208,6 +212,14 @@ public int updateDelay() {
208212
return this.updateDelay;
209213
}
210214

215+
/**
216+
* Whether interfaces should cancel events that come from the players inventories.
217+
* @return true if it should cancel the events
218+
*/
219+
public boolean cancelClicksInPlayerInventory() {
220+
return this.cancelClicksInPlayerInventory;
221+
}
222+
211223
/**
212224
* A class that builds a chest interface.
213225
*/
@@ -243,6 +255,8 @@ public static final class Builder implements Interface.Builder<ChestPane, Player
243255
*/
244256
private final int updateDelay;
245257

258+
private final boolean cancelClicksInPlayerInventory;
259+
246260
/**
247261
* The top click handler.
248262
*/
@@ -259,6 +273,7 @@ public Builder() {
259273
this.title = Component.empty();
260274
this.updates = false;
261275
this.updateDelay = 1;
276+
this.cancelClicksInPlayerInventory = false;
262277
this.clickHandler = ClickHandler.cancel();
263278
}
264279

@@ -269,6 +284,7 @@ private Builder(
269284
final @NonNull Component title,
270285
final boolean updates,
271286
final int updateDelay,
287+
final boolean cancelClicksInPlayerInventory,
272288
final @NonNull ClickHandler<ChestPane, InventoryClickEvent, PlayerViewer, InventoryClickContext<ChestPane,
273289
ChestView>> clickHandler
274290
) {
@@ -278,6 +294,7 @@ private Builder(
278294
this.title = title;
279295
this.updates = updates;
280296
this.updateDelay = updateDelay;
297+
this.cancelClicksInPlayerInventory = cancelClicksInPlayerInventory;
281298
this.clickHandler = clickHandler;
282299
}
283300

@@ -304,6 +321,7 @@ public int rows() {
304321
this.title,
305322
this.updates,
306323
this.updateDelay,
324+
this.cancelClicksInPlayerInventory,
307325
this.clickHandler
308326
);
309327
}
@@ -331,6 +349,7 @@ public int rows() {
331349
title,
332350
this.updates,
333351
this.updateDelay,
352+
this.cancelClicksInPlayerInventory,
334353
this.clickHandler
335354
);
336355
}
@@ -352,6 +371,7 @@ public int rows() {
352371
this.title,
353372
this.updates,
354373
this.updateDelay,
374+
this.cancelClicksInPlayerInventory,
355375
this.clickHandler
356376
);
357377
}
@@ -384,6 +404,7 @@ public int rows() {
384404
this.title,
385405
this.updates,
386406
this.updateDelay,
407+
this.cancelClicksInPlayerInventory,
387408
this.clickHandler
388409
);
389410
}
@@ -424,6 +445,7 @@ InventoryClickContext<ChestPane, ChestView>> clickHandler() {
424445
this.title,
425446
this.updates,
426447
this.updateDelay,
448+
this.cancelClicksInPlayerInventory,
427449
handler
428450
);
429451
}
@@ -443,6 +465,25 @@ InventoryClickContext<ChestPane, ChestView>> clickHandler() {
443465
this.title,
444466
updates,
445467
updateDelay,
468+
this.cancelClicksInPlayerInventory,
469+
this.clickHandler
470+
);
471+
}
472+
473+
/**
474+
* Controls if the interface should stop items coming from the player's inventory.
475+
* @param cancelClicksInPlayerInventory true if it should stop items.
476+
* @return new builder instance
477+
*/
478+
public @NonNull Builder cancelClicksInPlayerInventory(final boolean cancelClicksInPlayerInventory) {
479+
return new Builder(
480+
this.transformsList,
481+
this.closeHandlerList,
482+
this.rows,
483+
this.title,
484+
this.updates,
485+
this.updateDelay,
486+
cancelClicksInPlayerInventory,
446487
this.clickHandler
447488
);
448489
}
@@ -461,6 +502,7 @@ InventoryClickContext<ChestPane, ChestView>> clickHandler() {
461502
this.closeHandlerList,
462503
this.updates,
463504
this.updateDelay,
505+
this.cancelClicksInPlayerInventory,
464506
this.clickHandler
465507
);
466508
}

0 commit comments

Comments
 (0)