Added closeHandlers in Window

This commit is contained in:
NichtStudioCode 2021-09-04 13:54:36 +02:00
parent 5fcd728008
commit 52aa81e893
2 changed files with 25 additions and 0 deletions

@ -123,6 +123,18 @@ public interface Window extends GUIParent {
*/ */
void handleVirtualInventoryUpdate(VirtualInventory virtualInventory); 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. * Removes the {@link Window} from the {@link WindowManager} list.
* If this method is called, the {@link Window} can't be shown again. * If this method is called, the {@link Window} can't be shown again.

@ -39,6 +39,8 @@ public abstract class BaseWindow implements Window {
private boolean closeable; private boolean closeable;
private boolean closed; private boolean closed;
private final ArrayList<Runnable> closeHandlers = new ArrayList<>();
public BaseWindow(UUID viewerUUID, BaseComponent[] title, int size, boolean closeable, boolean closeOnEvent) { public BaseWindow(UUID viewerUUID, BaseComponent[] title, int size, boolean closeable, boolean closeOnEvent) {
this.viewerUUID = viewerUUID; this.viewerUUID = viewerUUID;
this.title = title; this.title = title;
@ -142,6 +144,7 @@ public abstract class BaseWindow implements Window {
if (closeable) { if (closeable) {
if (closeOnEvent) close(false); if (closeOnEvent) close(false);
handleClosed(); handleClosed();
closeHandlers.forEach(Runnable::run);
} else { } else {
if (player.equals(getViewer())) if (player.equals(getViewer()))
Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), this::show, 0); Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), this::show, 0);
@ -225,6 +228,16 @@ public abstract class BaseWindow implements Window {
changeTitle(TextComponent.fromLegacyText(title)); changeTitle(TextComponent.fromLegacyText(title));
} }
@Override
public void addCloseHandler(Runnable closeHandler) {
closeHandlers.add(closeHandler);
}
@Override
public void removeCloseHandler(Runnable closeHandler) {
closeHandlers.remove(closeHandler);
}
@Override @Override
public Player getCurrentViewer() { public Player getCurrentViewer() {
List<HumanEntity> viewers = getInventories()[0].getViewers(); List<HumanEntity> viewers = getInventories()[0].getViewers();