Skip to content

Commit

Permalink
Merge pull request #827 from TonytheMacaroni/main
Browse files Browse the repository at this point in the history
Cancel clicks while in menu
  • Loading branch information
Chronoken authored Dec 31, 2023
2 parents 8fa1925 + 264db36 commit 57dd520
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
16 changes: 7 additions & 9 deletions core/src/main/java/com/nisovin/magicspells/spells/MenuSpell.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ private ItemStack translateItem(Player opener, ItemStack item, SpellData data) {

@EventHandler
public void onInvClick(InventoryClickEvent event) {
Inventory inventory = event.getClickedInventory();
if (inventory == null || !(inventory.getHolder() instanceof MenuInventory menu) || menu.getSpell() != this) return;
InventoryView view = event.getView();

Inventory inventory = view.getTopInventory();
if (!(inventory.getHolder() instanceof MenuInventory menu) || menu.getSpell() != this) return;

event.setCancelled(true);
if (event.getClickedInventory() != inventory) return;

Player player = (Player) event.getWhoClicked();
PostClickState state = castSpells(menu, event.getCurrentItem(), event.getClick());
Expand All @@ -336,13 +340,7 @@ public void onInvDrag(InventoryDragEvent event) {
Inventory inventory = view.getTopInventory();
if (!(inventory.getHolder() instanceof MenuInventory menu) || menu.getSpell() != this) return;

int size = inventory.getSize();
for (int slot : event.getRawSlots()) {
if (slot < size) {
event.setCancelled(true);
return;
}
}
event.setCancelled(true);
}

private PostClickState castSpells(MenuInventory menu, ItemStack item, ClickType click) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ private void open(Player opener, PlayerMenuInventory menu) {

@EventHandler
public void onItemClick(InventoryClickEvent event) {
Inventory inventory = event.getClickedInventory();
if (inventory == null || !(inventory.getHolder() instanceof PlayerMenuInventory menu) || menu.getSpell() != this) return;
InventoryView view = event.getView();

Inventory inventory = view.getTopInventory();
if (!(inventory.getHolder() instanceof PlayerMenuInventory menu) || menu.getSpell() != this) return;

event.setCancelled(true);
if (!(event.getWhoClicked() instanceof Player opener)) return;
if (event.getClickedInventory() != inventory || !(event.getWhoClicked() instanceof Player opener)) return;

ItemStack item = event.getCurrentItem();
if (item == null) return;
Expand Down Expand Up @@ -334,13 +336,7 @@ public void onInvDrag(InventoryDragEvent event) {
Inventory inventory = view.getTopInventory();
if (!(inventory.getHolder() instanceof PlayerMenuInventory menu) || menu.getSpell() != this) return;

int size = inventory.getSize();
for (int slot : event.getRawSlots()) {
if (slot < size) {
event.setCancelled(true);
return;
}
}
event.setCancelled(true);
}

private class PlayerMenuInventory implements InventoryHolder {
Expand Down

0 comments on commit 57dd520

Please sign in to comment.