Fix AbstractWindow#handleCloseEvent not being called on disable

This commit is contained in:
NichtStudioCode 2023-04-16 17:19:16 +02:00
parent 206a709335
commit ff8f96a36e
2 changed files with 18 additions and 20 deletions

@ -187,7 +187,7 @@ public abstract class AbstractWindow implements Window, GuiParent {
throw new IllegalStateException("Window is already closed!"); throw new IllegalStateException("Window is already closed!");
currentlyOpen = false; currentlyOpen = false;
remove(false); 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
if (!hasHandledClose) { if (!hasHandledClose) {
@ -250,7 +250,7 @@ public abstract class AbstractWindow implements Window, GuiParent {
&& ((SlotElement.InventorySlotElement) element).getInventory() == inventory); && ((SlotElement.InventorySlotElement) element).getInventory() == inventory);
} }
public void remove(boolean closeForViewer) { private void remove() {
WindowManager.getInstance().removeWindow(this); WindowManager.getInstance().removeWindow(this);
Arrays.stream(elementsDisplayed) Arrays.stream(elementsDisplayed)
@ -266,8 +266,6 @@ public abstract class AbstractWindow implements Window, GuiParent {
Arrays.stream(getGuis()) Arrays.stream(getGuis())
.forEach(gui -> gui.removeParent(this)); .forEach(gui -> gui.removeParent(this));
if (closeForViewer) close();
} }
@Override @Override

@ -26,12 +26,12 @@ public class WindowManager implements Listener {
private static WindowManager instance; private static WindowManager instance;
private final Map<Inventory, AbstractWindow> windows = new HashMap<>(); private final Map<Inventory, AbstractWindow> windowsByInventory = new HashMap<>();
private final Map<Player, AbstractWindow> openWindows = new HashMap<>(); private final Map<Player, AbstractWindow> windowsByPlayer = new HashMap<>();
private WindowManager() { private WindowManager() {
Bukkit.getPluginManager().registerEvents(this, InvUI.getInstance().getPlugin()); Bukkit.getPluginManager().registerEvents(this, InvUI.getInstance().getPlugin());
InvUI.getInstance().addDisableHandler(() -> new HashSet<>(windows.values()).forEach(window -> window.remove(true))); InvUI.getInstance().addDisableHandler(() -> new HashSet<>(windowsByPlayer.values()).forEach(AbstractWindow::close));
} }
/** /**
@ -50,7 +50,7 @@ public class WindowManager implements Listener {
* @param window The {@link AbstractWindow} to add * @param window The {@link AbstractWindow} to add
*/ */
public void addWindow(AbstractWindow window) { public void addWindow(AbstractWindow window) {
windows.put(window.getInventories()[0], window); windowsByInventory.put(window.getInventories()[0], window);
} }
/** /**
@ -60,7 +60,7 @@ public class WindowManager implements Listener {
* @param window The {@link AbstractWindow} to remove * @param window The {@link AbstractWindow} to remove
*/ */
public void removeWindow(AbstractWindow window) { public void removeWindow(AbstractWindow window) {
windows.remove(window.getInventories()[0]); windowsByInventory.remove(window.getInventories()[0]);
} }
/** /**
@ -71,7 +71,7 @@ public class WindowManager implements Listener {
*/ */
@Nullable @Nullable
public Window getWindow(Inventory inventory) { public Window getWindow(Inventory inventory) {
return windows.get(inventory); return windowsByInventory.get(inventory);
} }
/** /**
@ -82,25 +82,25 @@ public class WindowManager implements Listener {
*/ */
@Nullable @Nullable
public Window getOpenWindow(Player player) { public Window getOpenWindow(Player player) {
return openWindows.get(player); return windowsByPlayer.get(player);
} }
/** /**
* Gets a set of all registered {@link Window Windows}. * Gets a set of all open {@link Window Windows}.
* *
* @return A set of all {@link Window Windows} * @return A set of all {@link Window Windows}
*/ */
public Set<Window> getWindows() { public Set<Window> getWindows() {
return new HashSet<>(windows.values()); return new HashSet<>(windowsByInventory.values());
} }
/** /**
* Gets a set of all currently opened {@link Window Windows}. * Gets a set of all open {@link Window Windows}.
* * @deprecated Use {@link #getWindows()} instead
* @return A set of all opened {@link Window Windows}
*/ */
@Deprecated
public Set<Window> getOpenWindows() { public Set<Window> getOpenWindows() {
return new HashSet<>(openWindows.values()); return getWindows();
} }
@EventHandler @EventHandler
@ -127,7 +127,7 @@ public class WindowManager implements Listener {
window.handleCloseEvent(player); window.handleCloseEvent(player);
} }
openWindows.remove(player); windowsByPlayer.remove(player);
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -135,7 +135,7 @@ public class WindowManager implements Listener {
AbstractWindow window = (AbstractWindow) getWindow(event.getInventory()); AbstractWindow window = (AbstractWindow) getWindow(event.getInventory());
if (window != null) { if (window != null) {
window.handleOpenEvent(event); window.handleOpenEvent(event);
openWindows.put((Player) event.getPlayer(), window); windowsByPlayer.put((Player) event.getPlayer(), window);
} }
} }
@ -145,7 +145,7 @@ public class WindowManager implements Listener {
AbstractWindow window = (AbstractWindow) getOpenWindow(player); AbstractWindow window = (AbstractWindow) getOpenWindow(player);
if (window != null) { if (window != null) {
window.handleCloseEvent(player); window.handleCloseEvent(player);
openWindows.remove(player); windowsByPlayer.remove(player);
} }
} }