Skip to content

Commit ff8f96a

Browse files
Fix AbstractWindow#handleCloseEvent not being called on disable
1 parent 206a709 commit ff8f96a

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

invui-core/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void handleCloseEvent(Player player) {
187187
throw new IllegalStateException("Window is already closed!");
188188

189189
currentlyOpen = false;
190-
remove(false);
190+
remove();
191191

192192
// handleClosed() might have already been called by open() if the window was replaced by another one
193193
if (!hasHandledClose) {
@@ -250,7 +250,7 @@ protected Map<Integer, SlotElement> getInvSlotElements(Inventory inventory) {
250250
&& ((SlotElement.InventorySlotElement) element).getInventory() == inventory);
251251
}
252252

253-
public void remove(boolean closeForViewer) {
253+
private void remove() {
254254
WindowManager.getInstance().removeWindow(this);
255255

256256
Arrays.stream(elementsDisplayed)
@@ -266,8 +266,6 @@ public void remove(boolean closeForViewer) {
266266

267267
Arrays.stream(getGuis())
268268
.forEach(gui -> gui.removeParent(this));
269-
270-
if (closeForViewer) close();
271269
}
272270

273271
@Override

invui-core/src/main/java/xyz/xenondevs/invui/window/WindowManager.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public class WindowManager implements Listener {
2626

2727
private static WindowManager instance;
2828

29-
private final Map<Inventory, AbstractWindow> windows = new HashMap<>();
30-
private final Map<Player, AbstractWindow> openWindows = new HashMap<>();
29+
private final Map<Inventory, AbstractWindow> windowsByInventory = new HashMap<>();
30+
private final Map<Player, AbstractWindow> windowsByPlayer = new HashMap<>();
3131

3232
private WindowManager() {
3333
Bukkit.getPluginManager().registerEvents(this, InvUI.getInstance().getPlugin());
34-
InvUI.getInstance().addDisableHandler(() -> new HashSet<>(windows.values()).forEach(window -> window.remove(true)));
34+
InvUI.getInstance().addDisableHandler(() -> new HashSet<>(windowsByPlayer.values()).forEach(AbstractWindow::close));
3535
}
3636

3737
/**
@@ -50,7 +50,7 @@ public static WindowManager getInstance() {
5050
* @param window The {@link AbstractWindow} to add
5151
*/
5252
public void addWindow(AbstractWindow window) {
53-
windows.put(window.getInventories()[0], window);
53+
windowsByInventory.put(window.getInventories()[0], window);
5454
}
5555

5656
/**
@@ -60,7 +60,7 @@ public void addWindow(AbstractWindow window) {
6060
* @param window The {@link AbstractWindow} to remove
6161
*/
6262
public void removeWindow(AbstractWindow window) {
63-
windows.remove(window.getInventories()[0]);
63+
windowsByInventory.remove(window.getInventories()[0]);
6464
}
6565

6666
/**
@@ -71,7 +71,7 @@ public void removeWindow(AbstractWindow window) {
7171
*/
7272
@Nullable
7373
public Window getWindow(Inventory inventory) {
74-
return windows.get(inventory);
74+
return windowsByInventory.get(inventory);
7575
}
7676

7777
/**
@@ -82,25 +82,25 @@ public Window getWindow(Inventory inventory) {
8282
*/
8383
@Nullable
8484
public Window getOpenWindow(Player player) {
85-
return openWindows.get(player);
85+
return windowsByPlayer.get(player);
8686
}
8787

8888
/**
89-
* Gets a set of all registered {@link Window Windows}.
89+
* Gets a set of all open {@link Window Windows}.
9090
*
9191
* @return A set of all {@link Window Windows}
9292
*/
9393
public Set<Window> getWindows() {
94-
return new HashSet<>(windows.values());
94+
return new HashSet<>(windowsByInventory.values());
9595
}
9696

9797
/**
98-
* Gets a set of all currently opened {@link Window Windows}.
99-
*
100-
* @return A set of all opened {@link Window Windows}
98+
* Gets a set of all open {@link Window Windows}.
99+
* @deprecated Use {@link #getWindows()} instead
101100
*/
101+
@Deprecated
102102
public Set<Window> getOpenWindows() {
103-
return new HashSet<>(openWindows.values());
103+
return getWindows();
104104
}
105105

106106
@EventHandler
@@ -127,15 +127,15 @@ private void handleInventoryClose(InventoryCloseEvent event) {
127127
window.handleCloseEvent(player);
128128
}
129129

130-
openWindows.remove(player);
130+
windowsByPlayer.remove(player);
131131
}
132132

133133
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
134134
private void handleInventoryOpen(InventoryOpenEvent event) {
135135
AbstractWindow window = (AbstractWindow) getWindow(event.getInventory());
136136
if (window != null) {
137137
window.handleOpenEvent(event);
138-
openWindows.put((Player) event.getPlayer(), window);
138+
windowsByPlayer.put((Player) event.getPlayer(), window);
139139
}
140140
}
141141

@@ -145,7 +145,7 @@ private void handlePlayerQuit(PlayerQuitEvent event) {
145145
AbstractWindow window = (AbstractWindow) getOpenWindow(player);
146146
if (window != null) {
147147
window.handleCloseEvent(player);
148-
openWindows.remove(player);
148+
windowsByPlayer.remove(player);
149149
}
150150
}
151151

0 commit comments

Comments
 (0)