From ccc9648c8759a012a6fcff627df34f2cc7992189 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 2 Nov 2023 12:07:37 +0000 Subject: [PATCH] fix: Make disable all interactions always cancel everything --- .../dev/triumphteam/gui/guis/BaseGui.java | 204 +++++++++--------- .../gui/guis/InteractionModifierListener.java | 12 ++ 2 files changed, 113 insertions(+), 103 deletions(-) diff --git a/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java b/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java index 708d7a1e..cc170061 100644 --- a/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java +++ b/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java @@ -1,18 +1,18 @@ /** * MIT License - * + *

* Copyright (c) 2021 TriumphTeam - * + *

* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -73,27 +73,21 @@ public abstract class BaseGui implements InventoryHolder { Bukkit.getPluginManager().registerEvents(new InteractionModifierListener(), plugin); } - // Main inventory. - private Inventory inventory; - - // title - private String title; - // Gui filler. private final GuiFiller filler = new GuiFiller(this); - - private int rows = 1; - - // Gui type, defaults to chest. - private GuiType guiType = GuiType.CHEST; - // Contains all items the GUI will have. private final Map guiItems; - // Actions for specific slots. private final Map> slotActions; // Interaction modifiers. private final Set interactionModifiers; + // Main inventory. + private Inventory inventory; + // title + private String title; + private int rows = 1; + // Gui type, defaults to chest. + private GuiType guiType = GuiType.CHEST; // Action to execute when clicking on any item. private GuiAction defaultClickAction; // Action to execute when clicking on the top part of the GUI only. @@ -154,18 +148,6 @@ public BaseGui(@NotNull final GuiType guiType, @NotNull final String title, @Not this.guiItems = new LinkedHashMap<>(inventorySize); } - /** - * Copy a set into an EnumSet, required because {@link EnumSet#copyOf(EnumSet)} throws an exception if the collection passed as argument is empty. - * - * @param set The set to be copied. - * @return An EnumSet with the provided elements from the original set. - */ - @NotNull - private EnumSet safeCopyOf(@NotNull final Set set) { - if (set.isEmpty()) return EnumSet.noneOf(InteractionModifier.class); - else return EnumSet.copyOf(set); - } - /** * Legacy constructor that takes rows and title. * @@ -204,6 +186,18 @@ public BaseGui(@NotNull final GuiType guiType, @NotNull final String title) { guiItems = new LinkedHashMap<>(); } + /** + * Copy a set into an EnumSet, required because {@link EnumSet#copyOf(EnumSet)} throws an exception if the collection passed as argument is empty. + * + * @param set The set to be copied. + * @return An EnumSet with the provided elements from the original set. + */ + @NotNull + private Set safeCopyOf(@NotNull final Set set) { + if (set.isEmpty()) return EnumSet.noneOf(InteractionModifier.class); + else return EnumSet.copyOf(set); + } + /** * Gets the GUI's title as string. * @@ -361,71 +355,6 @@ public void addItem(final boolean expandIfFull, @NotNull final GuiItem... items) this.addItem(true, notAddedItems.toArray(new GuiItem[0])); } - /** - * Sets the {@link GuiAction} of a default click on any item. - * See {@link InventoryClickEvent}. - * - * @param defaultClickAction {@link GuiAction} to resolve when any item is clicked. - */ - public void setDefaultClickAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> defaultClickAction) { - this.defaultClickAction = defaultClickAction; - } - - /** - * Sets the {@link GuiAction} of a default click on any item on the top part of the GUI. - * Top inventory being for example chests etc, instead of the {@link Player} inventory. - * See {@link InventoryClickEvent}. - * - * @param defaultTopClickAction {@link GuiAction} to resolve when clicking on the top inventory. - */ - public void setDefaultTopClickAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> defaultTopClickAction) { - this.defaultTopClickAction = defaultTopClickAction; - } - - public void setPlayerInventoryAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> playerInventoryAction) { - this.playerInventoryAction = playerInventoryAction; - } - - /** - * Sets the {@link GuiAction} to run when clicking on the outside of the inventory. - * See {@link InventoryClickEvent}. - * - * @param outsideClickAction {@link GuiAction} to resolve when clicking outside of the inventory. - */ - public void setOutsideClickAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> outsideClickAction) { - this.outsideClickAction = outsideClickAction; - } - - /** - * Sets the {@link GuiAction} of a default drag action. - * See {@link InventoryDragEvent}. - * - * @param dragAction {@link GuiAction} to resolve. - */ - public void setDragAction(@Nullable final GuiAction<@NotNull InventoryDragEvent> dragAction) { - this.dragAction = dragAction; - } - - /** - * Sets the {@link GuiAction} to run once the inventory is closed. - * See {@link InventoryCloseEvent}. - * - * @param closeGuiAction {@link GuiAction} to resolve when the inventory is closed. - */ - public void setCloseGuiAction(@Nullable final GuiAction<@NotNull InventoryCloseEvent> closeGuiAction) { - this.closeGuiAction = closeGuiAction; - } - - /** - * Sets the {@link GuiAction} to run when the GUI opens. - * See {@link InventoryOpenEvent}. - * - * @param openGuiAction {@link GuiAction} to resolve when opening the inventory. - */ - public void setOpenGuiAction(@Nullable final GuiAction<@NotNull InventoryOpenEvent> openGuiAction) { - this.openGuiAction = openGuiAction; - } - /** * Adds a {@link GuiAction} for when clicking on a specific slot. * See {@link InventoryClickEvent}. @@ -767,6 +696,10 @@ public BaseGui enableAllInteractions() { return this; } + public boolean allInteractionsDisabled() { + return interactionModifiers.size() == InteractionModifier.VALUES.size(); + } + /** * Check if item placement is allowed inside this GUI. * @@ -851,6 +784,15 @@ public Inventory getInventory() { return inventory; } + /** + * Sets the new inventory of the GUI. + * + * @param inventory The new inventory. + */ + public void setInventory(@NotNull final Inventory inventory) { + this.inventory = inventory; + } + /** * Gets the amount of {@link #rows}. * @@ -878,6 +820,16 @@ GuiAction getDefaultClickAction() { return defaultClickAction; } + /** + * Sets the {@link GuiAction} of a default click on any item. + * See {@link InventoryClickEvent}. + * + * @param defaultClickAction {@link GuiAction} to resolve when any item is clicked. + */ + public void setDefaultClickAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> defaultClickAction) { + this.defaultClickAction = defaultClickAction; + } + /** * Gets the default top click resolver. */ @@ -886,6 +838,17 @@ GuiAction getDefaultTopClickAction() { return defaultTopClickAction; } + /** + * Sets the {@link GuiAction} of a default click on any item on the top part of the GUI. + * Top inventory being for example chests etc, instead of the {@link Player} inventory. + * See {@link InventoryClickEvent}. + * + * @param defaultTopClickAction {@link GuiAction} to resolve when clicking on the top inventory. + */ + public void setDefaultTopClickAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> defaultTopClickAction) { + this.defaultTopClickAction = defaultTopClickAction; + } + /** * Gets the player inventory action. */ @@ -894,6 +857,10 @@ GuiAction getPlayerInventoryAction() { return playerInventoryAction; } + public void setPlayerInventoryAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> playerInventoryAction) { + this.playerInventoryAction = playerInventoryAction; + } + /** * Gets the default drag resolver. */ @@ -902,6 +869,16 @@ GuiAction getDragAction() { return dragAction; } + /** + * Sets the {@link GuiAction} of a default drag action. + * See {@link InventoryDragEvent}. + * + * @param dragAction {@link GuiAction} to resolve. + */ + public void setDragAction(@Nullable final GuiAction<@NotNull InventoryDragEvent> dragAction) { + this.dragAction = dragAction; + } + /** * Gets the close gui resolver. */ @@ -910,6 +887,16 @@ GuiAction getCloseGuiAction() { return closeGuiAction; } + /** + * Sets the {@link GuiAction} to run once the inventory is closed. + * See {@link InventoryCloseEvent}. + * + * @param closeGuiAction {@link GuiAction} to resolve when the inventory is closed. + */ + public void setCloseGuiAction(@Nullable final GuiAction<@NotNull InventoryCloseEvent> closeGuiAction) { + this.closeGuiAction = closeGuiAction; + } + /** * Gets the open gui resolver. */ @@ -918,6 +905,16 @@ GuiAction getOpenGuiAction() { return openGuiAction; } + /** + * Sets the {@link GuiAction} to run when the GUI opens. + * See {@link InventoryOpenEvent}. + * + * @param openGuiAction {@link GuiAction} to resolve when opening the inventory. + */ + public void setOpenGuiAction(@Nullable final GuiAction<@NotNull InventoryOpenEvent> openGuiAction) { + this.openGuiAction = openGuiAction; + } + /** * Gets the resolver for the outside click. */ @@ -926,6 +923,16 @@ GuiAction getOutsideClickAction() { return outsideClickAction; } + /** + * Sets the {@link GuiAction} to run when clicking on the outside of the inventory. + * See {@link InventoryClickEvent}. + * + * @param outsideClickAction {@link GuiAction} to resolve when clicking outside of the inventory. + */ + public void setOutsideClickAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> outsideClickAction) { + this.outsideClickAction = outsideClickAction; + } + /** * Gets the action for the specified slot. * @@ -964,15 +971,6 @@ int getSlotFromRowCol(final int row, final int col) { return (col + (row - 1) * 9) - 1; } - /** - * Sets the new inventory of the GUI. - * - * @param inventory The new inventory. - */ - public void setInventory(@NotNull final Inventory inventory) { - this.inventory = inventory; - } - /* TODO fix this part, find a better solution for using Paper protected Inventory createRowedInventory(@NotNull final Component title) { diff --git a/core/src/main/java/dev/triumphteam/gui/guis/InteractionModifierListener.java b/core/src/main/java/dev/triumphteam/gui/guis/InteractionModifierListener.java index 29f88157..e40d8a5a 100644 --- a/core/src/main/java/dev/triumphteam/gui/guis/InteractionModifierListener.java +++ b/core/src/main/java/dev/triumphteam/gui/guis/InteractionModifierListener.java @@ -59,6 +59,12 @@ public void onGuiClick(final InventoryClickEvent event) { // Gui final BaseGui gui = (BaseGui) event.getInventory().getHolder(); + if (gui.allInteractionsDisabled()) { + event.setCancelled(true); + event.setResult(Event.Result.DENY); + return; + } + // if player is trying to do a disabled action, cancel it if ((!gui.canPlaceItems() && isPlaceItemEvent(event)) || (!gui.canTakeItems() && isTakeItemEvent(event)) || (!gui.canSwapItems() && isSwapItemEvent(event)) || (!gui.canDropItems() && isDropItemEvent(event)) || (!gui.allowsOtherActions() && isOtherEvent(event))) { event.setCancelled(true); @@ -80,6 +86,12 @@ public void onGuiDrag(final InventoryDragEvent event) { // Gui final BaseGui gui = (BaseGui) event.getInventory().getHolder(); + if (gui.allInteractionsDisabled()) { + event.setCancelled(true); + event.setResult(Event.Result.DENY); + return; + } + // if players are allowed to place items on the GUI, or player is not dragging on GUI, return if (gui.canPlaceItems() || !isDraggingOnGui(event)) return;