diff --git a/pom.xml b/pom.xml
index f0e74a5..0a7f001 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
su.nightexpress.nightcore
nightcore
- 2.5.2
+ 2.5.2.1
17
diff --git a/src/main/java/su/nightexpress/nightcore/api/event/PlayerOpenMenuEvent.java b/src/main/java/su/nightexpress/nightcore/api/event/PlayerOpenMenuEvent.java
new file mode 100644
index 0000000..03c8419
--- /dev/null
+++ b/src/main/java/su/nightexpress/nightcore/api/event/PlayerOpenMenuEvent.java
@@ -0,0 +1,53 @@
+package su.nightexpress.nightcore.api.event;
+
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+import su.nightexpress.nightcore.menu.api.Menu;
+
+public class PlayerOpenMenuEvent extends Event implements Cancellable {
+
+ public static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final Player player;
+ private final Menu menu;
+
+ private boolean cancelled;
+
+ public PlayerOpenMenuEvent(@NotNull Player player, @NotNull Menu menu) {
+ this.player = player;
+ this.menu = menu;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+
+ @NotNull
+ public Player getPlayer() {
+ return player;
+ }
+
+ @NotNull
+ public Menu getMenu() {
+ return menu;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancelled) {
+ this.cancelled = cancelled;
+ }
+}
diff --git a/src/main/java/su/nightexpress/nightcore/menu/api/Menu.java b/src/main/java/su/nightexpress/nightcore/menu/api/Menu.java
index 3777e3b..a1f5a4e 100644
--- a/src/main/java/su/nightexpress/nightcore/menu/api/Menu.java
+++ b/src/main/java/su/nightexpress/nightcore/menu/api/Menu.java
@@ -29,8 +29,14 @@ default void flush(@NotNull MenuViewer viewer) {
void flush(@NotNull Player player);
+ default boolean isViewer(@NotNull Player player) {
+ return this.getViewer(player) != null;
+ }
+
void close();
+ void close(@NotNull Player player);
+
boolean close(@NotNull NightCorePlugin plugin);
void runNextTick(@NotNull Runnable runnable);
diff --git a/src/main/java/su/nightexpress/nightcore/menu/impl/AbstractMenu.java b/src/main/java/su/nightexpress/nightcore/menu/impl/AbstractMenu.java
index 72b7584..dfc7af1 100644
--- a/src/main/java/su/nightexpress/nightcore/menu/impl/AbstractMenu.java
+++ b/src/main/java/su/nightexpress/nightcore/menu/impl/AbstractMenu.java
@@ -10,6 +10,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.NightCorePlugin;
+import su.nightexpress.nightcore.api.event.PlayerOpenMenuEvent;
import su.nightexpress.nightcore.dialog.Dialog;
import su.nightexpress.nightcore.menu.api.Menu;
import su.nightexpress.nightcore.menu.link.Linked;
@@ -42,8 +43,7 @@ public static void purge(@NotNull Player player) {
Menu menu = getMenu(player);
if (menu == null) return;
- menu.close();
- PLAYER_MENUS.remove(player.getUniqueId());
+ menu.close(player);
}
protected final P plugin;
@@ -86,10 +86,22 @@ public void clear() {
@Override
public void close() {
- new HashSet<>(this.getViewers()).forEach(viewer -> viewer.getPlayer().closeInventory());
+ new HashSet<>(this.getViewers()).forEach(viewer -> this.close(viewer.getPlayer()));
this.viewers.clear();
}
+ @Override
+ public void close(@NotNull Player player) {
+ Menu current = getMenu(player);
+
+ if (current == this && player.getOpenInventory().getType() != InventoryType.CRAFTING) {
+ player.closeInventory();
+ }
+ else {
+ this.onClose(player);
+ }
+ }
+
@Override
public boolean close(@NotNull NightCorePlugin plugin) {
if (this.plugin == plugin) {
@@ -128,6 +140,13 @@ public boolean open(@NotNull MenuViewer viewer) {
public boolean open(@NotNull Player player) {
if (!this.canOpen(player)) return false;
+ PlayerOpenMenuEvent event = new PlayerOpenMenuEvent(player, this);
+ this.plugin.getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ //this.close(player);
+ return false;
+ }
+
MenuOptions options = new MenuOptions(this.getOptions());
MenuViewer viewer = this.getViewerOrCreate(player);
@@ -190,14 +209,18 @@ public void onDrag(@NotNull MenuViewer viewer, @NotNull InventoryDragEvent event
@Override
public void onClose(@NotNull MenuViewer viewer, @NotNull InventoryCloseEvent event) {
- Player player = viewer.getPlayer();
+ this.onClose(viewer.getPlayer());
+ }
- this.getViewersMap().remove(player.getUniqueId());
- this.getItems().removeIf(menuItem -> menuItem.getOptions().canBeDestroyed(viewer));
+ public void onClose(@NotNull Player player) {
+ MenuViewer viewer = this.viewers.remove(player.getUniqueId());
+ if (viewer != null) {
+ this.getItems().removeIf(menuItem -> menuItem.getOptions().canBeDestroyed(viewer));
+ }
PLAYER_MENUS.remove(player.getUniqueId());
// Do not clear link if entered Editor, so it can reopen menu without data loss when done.
- if (this instanceof Linked> linked && !Dialog.contains(player)) {
+ if (viewer != null && this instanceof Linked> linked && !Dialog.contains(player)) {
linked.getLink().clear(viewer);
}
diff --git a/src/main/java/su/nightexpress/nightcore/util/wrapper/UniSound.java b/src/main/java/su/nightexpress/nightcore/util/wrapper/UniSound.java
index b8856f0..3ed9b99 100644
--- a/src/main/java/su/nightexpress/nightcore/util/wrapper/UniSound.java
+++ b/src/main/java/su/nightexpress/nightcore/util/wrapper/UniSound.java
@@ -43,13 +43,16 @@ public static UniSound of(@NotNull Sound sound, float volume, float pitch) {
public static UniSound read(@NotNull FileConfig cfg, @NotNull String path) {
String soundName = ConfigValue.create(path + ".Name", "null",
"Sound name. You can use Spigot sound names, or ones from your resource pack.",
- "Spigot Sounds: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html").read(cfg);
+ "Spigot Sounds: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html"
+ ).read(cfg);
- float volume = ConfigValue.create(path + ".Volume", 0.8F,
- "Sound volume. From 0.0 to 1.0.").read(cfg).floatValue();
+ float volume = ConfigValue.create(path + ".Volume", 0.8D,
+ "Sound volume. From 0.0 to 1.0."
+ ).read(cfg).floatValue();
float pitch = ConfigValue.create(path + ".Pitch", 1D,
- "Sound speed. From 0.5 to 2.0").read(cfg).floatValue();
+ "Sound speed. From 0.5 to 2.0"
+ ).read(cfg).floatValue();
Sound soundType = StringUtil.getEnum(soundName, Sound.class).orElse(null);