diff --git a/InvUI/src/main/java/de/studiocode/invui/window/Window.java b/InvUI/src/main/java/de/studiocode/invui/window/Window.java index 932f099..77c1b35 100644 --- a/InvUI/src/main/java/de/studiocode/invui/window/Window.java +++ b/InvUI/src/main/java/de/studiocode/invui/window/Window.java @@ -123,6 +123,18 @@ public interface Window extends GUIParent { */ void handleVirtualInventoryUpdate(VirtualInventory virtualInventory); + /** + * Adds a close handler that will be called when this window gets closed. + * @param closeHandler The close handler to add + */ + void addCloseHandler(Runnable closeHandler); + + /** + * Removes a close handler that has been added previously. + * @param closeHandler The close handler to remove + */ + void removeCloseHandler(Runnable closeHandler); + /** * Removes the {@link Window} from the {@link WindowManager} list. * If this method is called, the {@link Window} can't be shown again. diff --git a/InvUI/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java b/InvUI/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java index c9f577b..ddad0b9 100644 --- a/InvUI/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java +++ b/InvUI/src/main/java/de/studiocode/invui/window/impl/BaseWindow.java @@ -39,6 +39,8 @@ public abstract class BaseWindow implements Window { private boolean closeable; private boolean closed; + private final ArrayList closeHandlers = new ArrayList<>(); + public BaseWindow(UUID viewerUUID, BaseComponent[] title, int size, boolean closeable, boolean closeOnEvent) { this.viewerUUID = viewerUUID; this.title = title; @@ -142,6 +144,7 @@ public abstract class BaseWindow implements Window { if (closeable) { if (closeOnEvent) close(false); handleClosed(); + closeHandlers.forEach(Runnable::run); } else { if (player.equals(getViewer())) Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), this::show, 0); @@ -225,6 +228,16 @@ public abstract class BaseWindow implements Window { changeTitle(TextComponent.fromLegacyText(title)); } + @Override + public void addCloseHandler(Runnable closeHandler) { + closeHandlers.add(closeHandler); + } + + @Override + public void removeCloseHandler(Runnable closeHandler) { + closeHandlers.remove(closeHandler); + } + @Override public Player getCurrentViewer() { List viewers = getInventories()[0].getViewers();