Skip to content

Commit

Permalink
fix: use reflection to get top inventory
Browse files Browse the repository at this point in the history
Signed-off-by: Cristóbal Veas <[email protected]>
  • Loading branch information
zetastormy committed Jul 21, 2024
1 parent b03e1c9 commit 973f497
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public List<UUID> 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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, AbstractInventory> getInventories() {
return inventories;
}
Expand Down

0 comments on commit 973f497

Please sign in to comment.