From 82e5408bed2c592f68dfef8183a802326c60e8b5 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Fri, 9 Jun 2023 18:32:20 +0200 Subject: [PATCH] Fix Window closeable --- .../xenondevs/invui/window/AbstractWindow.java | 15 ++++++++------- .../xyz/xenondevs/invui/window/WindowManager.java | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java b/invui-core/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java index 6def161..53aa21b 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java @@ -181,12 +181,14 @@ public abstract class AbstractWindow implements Window, GuiParent { } } - public void handleCloseEvent(Player player) { - if (closeable) { + public void handleCloseEvent(Player player, boolean forceClose) { + if (closeable || forceClose) { if (!currentlyOpen) throw new IllegalStateException("Window is already closed!"); + closeable = true; currentlyOpen = false; + remove(); // handleClosed() might have already been called by open() if the window was replaced by another one @@ -198,9 +200,8 @@ public abstract class AbstractWindow implements Window, GuiParent { if (closeHandlers != null) { closeHandlers.forEach(Runnable::run); } - } else { - if (player.equals(getViewer())) - Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), this::open, 0); + } else if (player.equals(getViewer())) { + Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), () -> openInventory(player), 0); } } @@ -282,9 +283,9 @@ public abstract class AbstractWindow implements Window, GuiParent { public void open() { Player viewer = getViewer(); if (viewer == null) - throw new IllegalStateException("The player is not online."); + throw new IllegalStateException("Viewer is not online"); if (currentlyOpen) - throw new IllegalStateException("Window is already opened!"); + throw new IllegalStateException("Window is already open"); // call handleClosed() close for currently open window AbstractWindow openWindow = (AbstractWindow) WindowManager.getInstance().getOpenWindow(viewer); diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/window/WindowManager.java b/invui-core/src/main/java/xyz/xenondevs/invui/window/WindowManager.java index d4a3cde..743452e 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/window/WindowManager.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/window/WindowManager.java @@ -124,7 +124,7 @@ public class WindowManager implements Listener { Player player = (Player) event.getPlayer(); AbstractWindow window = (AbstractWindow) getWindow(event.getInventory()); if (window != null) { - window.handleCloseEvent(player); + window.handleCloseEvent(player, false); } windowsByPlayer.remove(player); @@ -144,7 +144,7 @@ public class WindowManager implements Listener { Player player = event.getPlayer(); AbstractWindow window = (AbstractWindow) getOpenWindow(player); if (window != null) { - window.handleCloseEvent(player); + window.handleCloseEvent(player, true); windowsByPlayer.remove(player); } }