diff --git a/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/menu/MenuPositionListener.java b/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/menu/MenuPositionListener.java index 418ebc4..2eff874 100644 --- a/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/menu/MenuPositionListener.java +++ b/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/menu/MenuPositionListener.java @@ -1,5 +1,6 @@ package net.anweisen.utilities.bukkit.utils.menu; +import net.anweisen.utilities.bukkit.utils.misc.CompatibilityUtils; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -26,7 +27,7 @@ public void onClick(@Nonnull InventoryClickEvent event) { Inventory inventory = event.getClickedInventory(); if (inventory == null) return; - if (inventory == event.getView().getTopInventory()) { + if (inventory == CompatibilityUtils.getTopInventory(event)) { if (inventory.getHolder() != MenuPosition.HOLDER) return; // No menu inventory diff --git a/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/misc/CompatibilityUtils.java b/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/misc/CompatibilityUtils.java new file mode 100644 index 0000000..2f69a02 --- /dev/null +++ b/bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/misc/CompatibilityUtils.java @@ -0,0 +1,49 @@ +package net.anweisen.utilities.bukkit.utils.misc; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import javax.annotation.Nonnull; +import net.anweisen.utilities.common.logging.ILogger; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * In API version 1.20.6 and earlier, InventoryView is an abstract class + * In API version 1.21, InventoryView is an interface + */ +public class CompatibilityUtils { + + protected static final ILogger logger = ILogger.forThisClass(); + private static final Logger log = LoggerFactory.getLogger(CompatibilityUtils.class); + + private CompatibilityUtils() { + } + + public static Inventory getTopInventory(@Nonnull Player player) { + InventoryView view = player.getOpenInventory(); + + try { + Method getTopInventory = InventoryView.class.getMethod("getTopInventory"); + return (Inventory) getTopInventory.invoke(view); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { + logger.error("Failed to get top inventory", ex); + return null; + } + } + + public static Inventory getTopInventory(@Nonnull InventoryClickEvent event) { + InventoryView view = event.getView(); + + try { + Method getTopInventory = InventoryView.class.getMethod("getTopInventory"); + return (Inventory) getTopInventory.invoke(view); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { + logger.error("Failed to get top inventory", ex); + return null; + } + } +}