Skip to content

Commit

Permalink
fix: Make disable all interactions always cancel everything
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Nov 2, 2023
1 parent ba2e195 commit ccc9648
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 103 deletions.
204 changes: 101 additions & 103 deletions core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* MIT License
*
* <p>
* Copyright (c) 2021 TriumphTeam
*
* <p>
* 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:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* 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
Expand Down Expand Up @@ -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<Integer, GuiItem> guiItems;

// Actions for specific slots.
private final Map<Integer, GuiAction<InventoryClickEvent>> slotActions;
// Interaction modifiers.
private final Set<InteractionModifier> 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<InventoryClickEvent> defaultClickAction;
// Action to execute when clicking on the top part of the GUI only.
Expand Down Expand Up @@ -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<InteractionModifier> safeCopyOf(@NotNull final Set<InteractionModifier> set) {
if (set.isEmpty()) return EnumSet.noneOf(InteractionModifier.class);
else return EnumSet.copyOf(set);
}

/**
* Legacy constructor that takes rows and title.
*
Expand Down Expand Up @@ -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<InteractionModifier> safeCopyOf(@NotNull final Set<InteractionModifier> set) {
if (set.isEmpty()) return EnumSet.noneOf(InteractionModifier.class);
else return EnumSet.copyOf(set);
}

/**
* Gets the GUI's title as string.
*
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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}.
*
Expand Down Expand Up @@ -878,6 +820,16 @@ GuiAction<InventoryClickEvent> 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.
*/
Expand All @@ -886,6 +838,17 @@ GuiAction<InventoryClickEvent> 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.
*/
Expand All @@ -894,6 +857,10 @@ GuiAction<InventoryClickEvent> getPlayerInventoryAction() {
return playerInventoryAction;
}

public void setPlayerInventoryAction(@Nullable final GuiAction<@NotNull InventoryClickEvent> playerInventoryAction) {
this.playerInventoryAction = playerInventoryAction;
}

/**
* Gets the default drag resolver.
*/
Expand All @@ -902,6 +869,16 @@ GuiAction<InventoryDragEvent> 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.
*/
Expand All @@ -910,6 +887,16 @@ GuiAction<InventoryCloseEvent> 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.
*/
Expand All @@ -918,6 +905,16 @@ GuiAction<InventoryOpenEvent> 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.
*/
Expand All @@ -926,6 +923,16 @@ GuiAction<InventoryClickEvent> 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.
*
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down

0 comments on commit ccc9648

Please sign in to comment.