From e94e5f047e21578b7e71be22fd0828df78bc8d54 Mon Sep 17 00:00:00 2001 From: domirusz24 Date: Sun, 21 Jan 2024 18:12:44 +0100 Subject: [PATCH] Added `DeluxeMenusOpenMenuEvent` and `DeluxeMenusPreOpenMenuEvent` --- .../events/DeluxeMenusOpenMenuEvent.java | 31 ++++++++++++++++ .../events/DeluxeMenusPreOpenMenuEvent.java | 37 +++++++++++++++++++ .../extendedclip/deluxemenus/menu/Menu.java | 12 ++++++ 3 files changed, 80 insertions(+) create mode 100644 src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusOpenMenuEvent.java create mode 100644 src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusPreOpenMenuEvent.java diff --git a/src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusOpenMenuEvent.java b/src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusOpenMenuEvent.java new file mode 100644 index 0000000..655c483 --- /dev/null +++ b/src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusOpenMenuEvent.java @@ -0,0 +1,31 @@ +package com.extendedclip.deluxemenus.events; + +import com.extendedclip.deluxemenus.menu.Menu; +import com.extendedclip.deluxemenus.menu.MenuHolder; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +public class DeluxeMenusOpenMenuEvent extends PlayerEvent { + private static final HandlerList HANDLERS = new HandlerList(); + private final MenuHolder holder; + + public DeluxeMenusOpenMenuEvent(Player player, MenuHolder holder) { + super(player); + this.holder = holder; + } + + public MenuHolder getHolder() { + return holder; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } +} diff --git a/src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusPreOpenMenuEvent.java b/src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusPreOpenMenuEvent.java new file mode 100644 index 0000000..35d01f8 --- /dev/null +++ b/src/main/java/com/extendedclip/deluxemenus/events/DeluxeMenusPreOpenMenuEvent.java @@ -0,0 +1,37 @@ +package com.extendedclip.deluxemenus.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +public class DeluxeMenusPreOpenMenuEvent extends PlayerEvent implements Cancellable { + private static final HandlerList HANDLERS = new HandlerList(); + + private boolean cancelled = false; + + public DeluxeMenusPreOpenMenuEvent(Player player) { + super(player); + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } +} + diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java b/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java index 6bb91b4..6a5f576 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java @@ -3,6 +3,8 @@ import com.extendedclip.deluxemenus.DeluxeMenus; import com.extendedclip.deluxemenus.action.ClickHandler; import com.extendedclip.deluxemenus.dupe.MenuItemMarker; +import com.extendedclip.deluxemenus.events.DeluxeMenusOpenMenuEvent; +import com.extendedclip.deluxemenus.events.DeluxeMenusPreOpenMenuEvent; import com.extendedclip.deluxemenus.requirement.RequirementList; import com.extendedclip.deluxemenus.utils.DebugLevel; import com.extendedclip.deluxemenus.utils.StringUtils; @@ -371,6 +373,11 @@ public void openMenu(final Player viewer, final Map args, final return; } + DeluxeMenusPreOpenMenuEvent preOpenEvent = new DeluxeMenusPreOpenMenuEvent(viewer); + Bukkit.getPluginManager().callEvent(preOpenEvent); + + if (preOpenEvent.isCancelled()) return; + final MenuHolder holder = new MenuHolder(viewer); if (placeholderPlayer != null) { holder.setPlaceholderPlayer(placeholderPlayer); @@ -488,6 +495,11 @@ public void openMenu(final Player viewer, final Map args, final holder.startUpdatePlaceholdersTask(); } }); + + Bukkit.getScheduler().runTask(DeluxeMenus.getInstance(), () -> { + DeluxeMenusOpenMenuEvent openEvent = new DeluxeMenusOpenMenuEvent(viewer, holder); + Bukkit.getPluginManager().callEvent(openEvent); + }); }); }