diff --git a/eco-api/src/main/java/com/willfp/eco/core/display/Display.java b/eco-api/src/main/java/com/willfp/eco/core/display/Display.java index 7285029d..6e980207 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/display/Display.java +++ b/eco-api/src/main/java/com/willfp/eco/core/display/Display.java @@ -1,6 +1,7 @@ package com.willfp.eco.core.display; import com.willfp.eco.core.fast.FastItemStack; +import com.willfp.eco.core.integrations.guidetection.GUIDetectionManager; import com.willfp.eco.util.NamespacedKeyUtils; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; @@ -71,7 +72,7 @@ public final class Display { ItemStack original = itemStack.clone(); Inventory inventory = player == null ? null : player.getOpenInventory().getTopInventory(); boolean inInventory = inventory != null && inventory.contains(original); - boolean inGui = inventory != null && inventory.getHolder() == null; + boolean inGui = player != null && GUIDetectionManager.hasGUIOpen(player); DisplayProperties properties = new DisplayProperties( inInventory, diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/guidetection/GUIDetectionIntegration.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/guidetection/GUIDetectionIntegration.java new file mode 100644 index 00000000..a06e7ee5 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/guidetection/GUIDetectionIntegration.java @@ -0,0 +1,18 @@ +package com.willfp.eco.core.integrations.guidetection; + +import com.willfp.eco.core.integrations.Integration; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +/** + * Wrapper class for GUI integrations. + */ +public interface GUIDetectionIntegration extends Integration { + /** + * Determine if a player is in a GUI. + * + * @param player The player. + * @return If the player is in a GUI. + */ + boolean hasGUIOpen(@NotNull final Player player); +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/guidetection/GUIDetectionManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/guidetection/GUIDetectionManager.java new file mode 100644 index 00000000..07bd28f6 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/guidetection/GUIDetectionManager.java @@ -0,0 +1,52 @@ +package com.willfp.eco.core.integrations.guidetection; + +import com.willfp.eco.util.MenuUtils; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.HashSet; +import java.util.Set; + +/** + * Class to handle GUI detection. + */ +public final class GUIDetectionManager { + /** + * A set of all registered integrations. + */ + private static final Set REGISTERED = new HashSet<>(); + + /** + * Register a new integration. + * + * @param integration The integration to register. + */ + public static void register(@NotNull final GUIDetectionIntegration integration) { + REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName())); + REGISTERED.add(integration); + } + + /** + * Get if a player is in a GUI. + * + * @param player The player. + * @return If the player has a GUI open. + */ + public static boolean hasGUIOpen(@NotNull final Player player) { + if (MenuUtils.getOpenMenu(player) != null) { + return true; + } + + for (GUIDetectionIntegration integration : REGISTERED) { + if (integration.hasGUIOpen(player)) { + return true; + } + } + + return false; + } + + private GUIDetectionManager() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/guidetection/GUIDetectionDeluxeMenus.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/guidetection/GUIDetectionDeluxeMenus.kt new file mode 100644 index 00000000..09a5f000 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/guidetection/GUIDetectionDeluxeMenus.kt @@ -0,0 +1,15 @@ +package com.willfp.eco.internal.spigot.integrations.guidetection + +import com.extendedclip.deluxemenus.menu.Menu +import com.willfp.eco.core.integrations.guidetection.GUIDetectionIntegration +import org.bukkit.entity.Player + +class GUIDetectionDeluxeMenus: GUIDetectionIntegration { + override fun hasGUIOpen(player: Player): Boolean { + return Menu.inMenu(player) + } + + override fun getPluginName(): String { + return "DeluxeMenus" + } +} diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 75960969..c138cc29 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -51,3 +51,4 @@ softdepend: - Scyther - ModelEngine - PvPManager + - DeluxeMenus diff --git a/lib/DeluxeMenus-1.13.7-DEV-156.jar b/lib/DeluxeMenus-1.13.7-DEV-156.jar new file mode 100644 index 00000000..328de4b4 Binary files /dev/null and b/lib/DeluxeMenus-1.13.7-DEV-156.jar differ