Fix Window closeable

This commit is contained in:
NichtStudioCode 2023-06-09 18:32:20 +02:00
parent 43263e36ad
commit 82e5408bed
2 changed files with 10 additions and 9 deletions

@ -181,12 +181,14 @@ public abstract class AbstractWindow implements Window, GuiParent {
} }
} }
public void handleCloseEvent(Player player) { public void handleCloseEvent(Player player, boolean forceClose) {
if (closeable) { if (closeable || forceClose) {
if (!currentlyOpen) if (!currentlyOpen)
throw new IllegalStateException("Window is already closed!"); throw new IllegalStateException("Window is already closed!");
closeable = true;
currentlyOpen = false; currentlyOpen = false;
remove(); remove();
// handleClosed() might have already been called by open() if the window was replaced by another one // 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) { if (closeHandlers != null) {
closeHandlers.forEach(Runnable::run); closeHandlers.forEach(Runnable::run);
} }
} else { } else if (player.equals(getViewer())) {
if (player.equals(getViewer())) Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), () -> openInventory(player), 0);
Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), this::open, 0);
} }
} }
@ -282,9 +283,9 @@ public abstract class AbstractWindow implements Window, GuiParent {
public void open() { public void open() {
Player viewer = getViewer(); Player viewer = getViewer();
if (viewer == null) if (viewer == null)
throw new IllegalStateException("The player is not online."); throw new IllegalStateException("Viewer is not online");
if (currentlyOpen) if (currentlyOpen)
throw new IllegalStateException("Window is already opened!"); throw new IllegalStateException("Window is already open");
// call handleClosed() close for currently open window // call handleClosed() close for currently open window
AbstractWindow openWindow = (AbstractWindow) WindowManager.getInstance().getOpenWindow(viewer); AbstractWindow openWindow = (AbstractWindow) WindowManager.getInstance().getOpenWindow(viewer);

@ -124,7 +124,7 @@ public class WindowManager implements Listener {
Player player = (Player) event.getPlayer(); Player player = (Player) event.getPlayer();
AbstractWindow window = (AbstractWindow) getWindow(event.getInventory()); AbstractWindow window = (AbstractWindow) getWindow(event.getInventory());
if (window != null) { if (window != null) {
window.handleCloseEvent(player); window.handleCloseEvent(player, false);
} }
windowsByPlayer.remove(player); windowsByPlayer.remove(player);
@ -144,7 +144,7 @@ public class WindowManager implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
AbstractWindow window = (AbstractWindow) getOpenWindow(player); AbstractWindow window = (AbstractWindow) getOpenWindow(player);
if (window != null) { if (window != null) {
window.handleCloseEvent(player); window.handleCloseEvent(player, true);
windowsByPlayer.remove(player); windowsByPlayer.remove(player);
} }
} }