diff --git a/src/main/java/team/devblook/akropolis/inventory/AbstractInventory.java b/src/main/java/team/devblook/akropolis/inventory/AbstractInventory.java index e02d22f..622bf40 100644 --- a/src/main/java/team/devblook/akropolis/inventory/AbstractInventory.java +++ b/src/main/java/team/devblook/akropolis/inventory/AbstractInventory.java @@ -100,7 +100,7 @@ public List getOpenInventories() { @EventHandler public void onInventoryClose(InventoryCloseEvent event) { - if (event.getView().getTopInventory().getHolder() instanceof InventoryBuilder && refreshEnabled) { + if (InventoryManager.getTopInventory(event).getHolder() instanceof InventoryBuilder && refreshEnabled) { openInventories.remove(event.getPlayer().getUniqueId()); } } diff --git a/src/main/java/team/devblook/akropolis/inventory/InventoryListener.java b/src/main/java/team/devblook/akropolis/inventory/InventoryListener.java index 1515cad..f85594f 100644 --- a/src/main/java/team/devblook/akropolis/inventory/InventoryListener.java +++ b/src/main/java/team/devblook/akropolis/inventory/InventoryListener.java @@ -30,7 +30,7 @@ public class InventoryListener implements Listener { @EventHandler public void onInventoryClick(InventoryClickEvent event) { - if (event.getView().getTopInventory().getHolder() instanceof InventoryBuilder) { + if (InventoryManager.getTopInventory(event).getHolder() instanceof InventoryBuilder) { event.setCancelled(true); if (event.getWhoClicked() instanceof Player player) { @@ -40,7 +40,7 @@ public void onInventoryClick(InventoryClickEvent event) { if (itemStack == null || itemStack.getType() == Material.AIR) return; - InventoryBuilder customHolder = (InventoryBuilder) event.getView().getTopInventory().getHolder(); + InventoryBuilder customHolder = (InventoryBuilder) InventoryManager.getTopInventory(event).getHolder(); InventoryItem item = customHolder.getIcon(event.getRawSlot()); if (item == null) diff --git a/src/main/java/team/devblook/akropolis/inventory/InventoryManager.java b/src/main/java/team/devblook/akropolis/inventory/InventoryManager.java index 6f4f172..10d7d26 100644 --- a/src/main/java/team/devblook/akropolis/inventory/InventoryManager.java +++ b/src/main/java/team/devblook/akropolis/inventory/InventoryManager.java @@ -22,10 +22,14 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryEvent; +import org.bukkit.inventory.Inventory; import team.devblook.akropolis.AkropolisPlugin; import team.devblook.akropolis.inventory.inventories.CustomGUI; import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -114,6 +118,17 @@ private void loadCustomMenus() { } } + public static Inventory getTopInventory(InventoryEvent event) { + try { + Object view = event.getView(); + Method getTopInventory = view.getClass().getMethod("getTopInventory"); + getTopInventory.setAccessible(true); + return (Inventory) getTopInventory.invoke(view); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + public Map getInventories() { return inventories; }