Skip to content

Commit

Permalink
fix: prevent gui from wried actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu2002 committed Aug 18, 2024
1 parent 1202dc6 commit 24e8fbe
Showing 1 changed file with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand Down Expand Up @@ -318,16 +319,12 @@ private void onInventoryClose(@NotNull InventoryCloseEvent event) {
}

private void onInventoryClick(@NotNull InventoryClickEvent event) {
if (!this.isInventory(event.getClickedInventory())) {
switch (event.getAction()) {
case COLLECT_TO_CURSOR:
case MOVE_TO_OTHER_INVENTORY:
case UNKNOWN:
event.setCancelled(true);
break;
default:
break;
}

if (!isInventory(getOpenedTopInventory(event.getWhoClicked())))
return;

if (isTryingToShiftItemFromPlayerInvToGui(event) || isNotClickingGuiInvWhileOpenIt(event)) {
event.setCancelled(true);
return;
}

Expand All @@ -338,12 +335,41 @@ private void onInventoryClick(@NotNull InventoryClickEvent event) {
return;

GuiSlot guiSlot = guiSlots[slot];

if (guiSlot == null)
return;

guiSlot.onInventoryClick(event, this);
}

private boolean isNotClickingGuiInvWhileOpenIt(InventoryClickEvent event) {
return !this.isInventory(event.getClickedInventory()) && this.isInventory(getOpenedTopInventory(event.getWhoClicked()));
}

private boolean isTryingToShiftItemFromPlayerInvToGui(InventoryClickEvent event) {
if (this.isInventory(event.getClickedInventory())) {
return false;
}

if (!this.isInventory(getOpenedTopInventory(event.getWhoClicked()))) {
return false;
}

switch (event.getAction()) {
case COLLECT_TO_CURSOR:
case MOVE_TO_OTHER_INVENTORY:
case UNKNOWN:
return true;
default:
return false;
}
}

private Inventory getOpenedTopInventory(HumanEntity player) {
InventoryView view = player.getOpenInventory();
return view == null ? null : view.getTopInventory();
}

private void onInventoryDrag(@NotNull InventoryDragEvent event) {
if (!this.isInventory(event.getInventory()))
return;
Expand Down

0 comments on commit 24e8fbe

Please sign in to comment.