From 2bc82dd3cf4b4080721254df5a584e4b25701b6c Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 19 Sep 2023 21:07:28 -0700 Subject: [PATCH] Add checking for enchantment table inventory (#2191) Fixes #2190 --- .../flags/protection/InventoryListener.java | 10 ++++- .../protection/InventoryListenerTest.java | 41 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListener.java index 3dfe3b6ab..084585ccd 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListener.java @@ -22,6 +22,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.inventory.EnchantingInventory; import org.bukkit.inventory.InventoryHolder; import world.bentobox.bentobox.api.flags.FlagListener; @@ -69,6 +70,14 @@ public class InventoryListener extends FlagListener public void onInventoryClick(InventoryClickEvent e) { Player player = (Player) e.getWhoClicked(); + + // Enchanting + if (e.getInventory() instanceof EnchantingInventory) { + this.checkIsland(e, player, e.getInventory().getLocation(), Flags.ENCHANTING); + return; + } + + // Inventory holders InventoryHolder inventoryHolder = e.getInventory().getHolder(); if (inventoryHolder == null || !(e.getWhoClicked() instanceof Player)) @@ -130,7 +139,6 @@ public class InventoryListener extends FlagListener } else if (inventoryHolder instanceof ChestBoat) { - // TODO: 1.19 added chest boat. Remove compatibility check when 1.18 is dropped. this.checkIsland(e, player, e.getInventory().getLocation(), Flags.CHEST); } else if (!(inventoryHolder instanceof Player)) diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java index 391805324..f23d3463c 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java @@ -32,6 +32,7 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryType.SlotType; +import org.bukkit.inventory.EnchantingInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryView; @@ -82,6 +83,25 @@ public class InventoryListenerTest extends AbstractCommonSetup { l = new InventoryListener(); } + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}. + */ + @Test + public void testOnInventoryClickEnchantingAllowed() { + InventoryView view = mock(InventoryView.class); + when(view.getPlayer()).thenReturn(player); + EnchantingInventory inv = mock(EnchantingInventory.class); + when(inv.getSize()).thenReturn(9); + when(view.getTopInventory()).thenReturn(inv); + when(inv.getLocation()).thenReturn(location); + when(view.getBottomInventory()).thenReturn(inv); + SlotType slotType = SlotType.CRAFTING; + InventoryAction action = InventoryAction.PLACE_ONE; + InventoryClickEvent e = new InventoryClickEvent(view, slotType, 0, ClickType.LEFT, action ); + l.onInventoryClick(e); + assertFalse(e.isCancelled()); + } + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}. */ @@ -195,6 +215,27 @@ public class InventoryListenerTest extends AbstractCommonSetup { verify(notifier, times(HOLDERS.size())).notify(any(), eq("protection.protected")); } + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}. + */ + @Test + public void testOnInventoryClickEnchantingNotAllowed() { + when(island.isAllowed(any(), any())).thenReturn(false); + InventoryView view = mock(InventoryView.class); + when(view.getPlayer()).thenReturn(player); + EnchantingInventory inv = mock(EnchantingInventory.class); + when(inv.getSize()).thenReturn(9); + when(view.getTopInventory()).thenReturn(inv); + when(inv.getLocation()).thenReturn(location); + when(view.getBottomInventory()).thenReturn(inv); + SlotType slotType = SlotType.CRAFTING; + InventoryAction action = InventoryAction.PLACE_ONE; + InventoryClickEvent e = new InventoryClickEvent(view, slotType, 0, ClickType.LEFT, action ); + l.onInventoryClick(e); + assertTrue(e.isCancelled()); + verify(notifier).notify(any(), eq("protection.protected")); + } + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}. */