Skip to content

Commit

Permalink
GUI-API Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Flo56958 committed Dec 24, 2023
1 parent 4594ebb commit 9be95a1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 81 deletions.
55 changes: 19 additions & 36 deletions src/main/java/de/flo56958/minetinker/api/gui/ButtonAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,16 @@ protected ButtonAction(GUI.Window.Button button) {
public void run() {
}

public static class NOTHING extends ButtonAction {

private static NOTHING instance;

private NOTHING() {
super(null);
}

public static NOTHING instance() {
synchronized (NOTHING.class) {
if (instance == null) instance = new NOTHING();
}
return instance;
}
}

public static class PAGE_UP extends ButtonAction implements PlayerAction {

public PAGE_UP(@NotNull GUI.Window.Button button) {
super(button);
}

public void run(Player player) {
GUI gui = this.button.getWindow().getGUI();
int pageNo = gui.getWindowNumber(this.button.getWindow());
gui.show(player, ++pageNo % gui.getWindowAmount());
final GUI gui = this.button.getWindow().getGUI();
final int pageNo = gui.getWindowNumber(this.button.getWindow()) + 1;
gui.show(player, pageNo % gui.getWindowAmount());
}
}

Expand All @@ -69,10 +53,8 @@ public PAGE_DOWN(@NotNull GUI.Window.Button button) {
}

public void run(Player player) {
GUI gui = this.button.getWindow().getGUI();
int pageNo = gui.getWindowNumber(this.button.getWindow());

--pageNo;
final GUI gui = this.button.getWindow().getGUI();
int pageNo = gui.getWindowNumber(this.button.getWindow()) - 1;

if (pageNo == -1) {
pageNo = gui.getWindowAmount() - 1;
Expand All @@ -87,29 +69,29 @@ public static class PAGE_GOTO extends ButtonAction implements PlayerAction {
private final int page;
protected final GUI.Window window;

public PAGE_GOTO(@NotNull GUI.Window.Button button, int page) {
public PAGE_GOTO(@NotNull final GUI.Window.Button button, int page) {
super(button);
this.page = page;
this.window = null;
}

public PAGE_GOTO(@NotNull GUI.Window.Button button, @NotNull GUI.Window window) {
public PAGE_GOTO(@NotNull final GUI.Window.Button button, @NotNull final GUI.Window window) {
super(button);
this.window = window;
this.page = -1;
}

public void run(Player player) {
GUI gui = (window != null) ? window.getGUI() : button.getWindow().getGUI();
int pageNo = (window != null) ? gui.getWindowNumber(window) : page;
public void run(@NotNull final Player player) {
final GUI gui = (window != null) ? window.getGUI() : button.getWindow().getGUI();
final int pageNo = (window != null) ? gui.getWindowNumber(window) : page;
gui.show(player, pageNo);
}
}

public static class RUN_RUNNABLE extends ButtonAction {
private final Runnable runnable;

public RUN_RUNNABLE(GUI.Window.Button button, Runnable runnable) {
public RUN_RUNNABLE(final GUI.Window.Button button, final Runnable runnable) {
super(button);
this.runnable = runnable;
}
Expand All @@ -123,7 +105,7 @@ public void run() {
public static class RUN_RUNNABLE_ON_PLAYER extends ButtonAction implements PlayerAction {
private final PlayerRunnable runnable;

public RUN_RUNNABLE_ON_PLAYER(GUI.Window.Button button, PlayerRunnable runnable) {
public RUN_RUNNABLE_ON_PLAYER(final GUI.Window.Button button, final PlayerRunnable runnable) {
super(button);
this.runnable = runnable;
}
Expand All @@ -141,22 +123,23 @@ public static class REQUEST_INPUT extends ButtonAction implements PlayerAction {
private final PlayerRunnable runnable;
private final String data;

public REQUEST_INPUT(GUI.Window.Button button, PlayerRunnable runnable, String data) {
public REQUEST_INPUT(final GUI.Window.Button button, final PlayerRunnable runnable, final String data) {
super(button);
this.runnable = runnable;
this.data = data;
}

@Override
public void run(Player player) {
public void run(@NotNull final Player player) {
playerToAction.put(player, this);
player.closeInventory();
ChatWriter.sendMessage(player, ChatColor.RED, LanguageManager.getString("GUI.ButtonAction.REQUEST_INPUT")
.replace("%data", data + ChatColor.RESET + ChatColor.RED));
}

private void afterRun(Player player) {
Bukkit.getScheduler().runTaskLater(MineTinker.getPlugin(), () -> button.getWindow().getGUI().show(player, button.getWindow()), 10);
private void afterRun(@NotNull final Player player) {
Bukkit.getScheduler().runTaskLater(MineTinker.getPlugin(),
() -> button.getWindow().getGUI().show(player, button.getWindow()), 10);
}
}

Expand All @@ -167,8 +150,8 @@ public interface PlayerRunnable {
private static class ChatListener implements Listener {

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public static void onChat(AsyncPlayerChatEvent event) {
REQUEST_INPUT ri = REQUEST_INPUT.playerToAction.remove(event.getPlayer());
public static void onChat(@NotNull final AsyncPlayerChatEvent event) {
final REQUEST_INPUT ri = REQUEST_INPUT.playerToAction.remove(event.getPlayer());
if (ri == null) return;
event.setCancelled(true);

Expand Down
84 changes: 41 additions & 43 deletions src/main/java/de/flo56958/minetinker/api/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

/**
* This class is for all User-Interfaces and Items within them.
* This class should be thread-safe to use.
*
* @author Flo56958
*/
Expand All @@ -50,17 +49,17 @@ public Window addWindow(final int size, @NotNull final String title) {
if (isClosed)
throw new IllegalStateException("GUI (" + this.hashCode() + ") is already closed!");

Window window = new Window(size, title, this);
final Window window = new Window(size, title, this);
windows.add(window);

return window;
}

public Window addWindow(@NotNull Inventory inventory) {
public Window addWindow(@NotNull final Inventory inventory) {
if (isClosed)
throw new IllegalStateException("GUI (" + this.hashCode() + ") is already closed!");

Window window = new Window(inventory, this);
final Window window = new Window(inventory, this);
windows.add(window);

return window;
Expand Down Expand Up @@ -146,9 +145,7 @@ public int getWindowAmount() {
*/
public void open() {
synchronized (this) {
if (!isClosed) {
return;
}
if (!isClosed) throw new IllegalStateException("GUI (" + this.hashCode() + ") is already open!");

Bukkit.getPluginManager().registerEvents(this, this.plugin);
isClosed = false;
Expand All @@ -158,11 +155,13 @@ public void open() {
/**
* this will close the Listener section of the GUI
* show() will throw Exception when called after close()
* can be used to micromanage the performance of the GUIs
* can be used to micromanage the performance of the GUIs.
* <p>
* close() will also close GUIs that are connected to the GUI via {@link ButtonAction.PAGE_GOTO}
*/
public void close() {
synchronized (this) {
if (isClosed) return;
if (isClosed) throw new IllegalStateException("GUI (" + this.hashCode() + ") is already closed!");

isClosed = true;
windows.forEach(Window::close);
Expand All @@ -171,49 +170,54 @@ public void close() {
}
}

public boolean isClosed() {
return isClosed;
}

//<------------------------------------Events------------------------------------------->

@EventHandler(ignoreCancelled = true)
public void onDisable(@NotNull PluginDisableEvent event) {
if (event.getPlugin().equals(this.plugin)) this.close();
public void onDisable(@NotNull final PluginDisableEvent event) {
// check if GUI is already closed due to a connecting Button Action
if (this.plugin.equals(event.getPlugin()) && !this.isClosed()) this.close();
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onClick(@NotNull InventoryClickEvent event) {
public void onClick(@NotNull final InventoryClickEvent event) {
if (event.getClickedInventory() == null) return;
Window w1 = getWindowFromInventory(event.getClickedInventory());
Window w2 = getWindowFromInventory(event.getWhoClicked().getOpenInventory().getTopInventory());
final Window w1 = getWindowFromInventory(event.getClickedInventory());
final Window w2 = getWindowFromInventory(event.getWhoClicked().getOpenInventory().getTopInventory());
if (w1 == null && w2 == null) return;

event.setCancelled(true);

if (w1 == null) return;
Window.Button clickedButton = w1.getButton(event.getSlot());
final Window.Button clickedButton = w1.getButton(event.getSlot());

if (clickedButton != null) clickedButton.executeAction(event);
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onDrag(@NotNull InventoryDragEvent event) {
Window w = getWindowFromInventory(event.getInventory());
public void onDrag(@NotNull final InventoryDragEvent event) {
final Window w = getWindowFromInventory(event.getInventory());
if (w == null) return;

event.setCancelled(true);
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onMove(@NotNull InventoryMoveItemEvent event) {
Window w1 = getWindowFromInventory(event.getDestination());
Window w2 = getWindowFromInventory(event.getInitiator());
Window w3 = getWindowFromInventory(event.getSource());
public void onMove(@NotNull final InventoryMoveItemEvent event) {
final Window w1 = getWindowFromInventory(event.getDestination());
final Window w2 = getWindowFromInventory(event.getInitiator());
final Window w3 = getWindowFromInventory(event.getSource());
if (w1 == null && w2 == null && w3 == null) return;

event.setCancelled(true);
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onEvent(@NotNull InventoryInteractEvent event) {
Window w = getWindowFromInventory(event.getInventory());
public void onEvent(@NotNull final InventoryInteractEvent event) {
final Window w = getWindowFromInventory(event.getInventory());
if (w == null) return;

event.setCancelled(true);
Expand All @@ -225,7 +229,6 @@ public void onEvent(@NotNull InventoryInteractEvent event) {
* A wrapper class for the Minecraft Inventory Window
*/
public static class Window {

private final Inventory inventory;
private final GUI gui;
private final Button[] buttonMap;
Expand Down Expand Up @@ -272,7 +275,7 @@ private int getSlot(final int x, final int y) throws IllegalArgumentException {
if (x < 0 || y < 0 || x > 8)
throw new IllegalArgumentException("Coordinates can not be less than ZERO or too big!");

int slot = (9 * y) + x;
final int slot = (9 * y) + x;

if (slot >= this.inventory.getSize())
throw new IllegalArgumentException("Coordinates are to big for the given Inventory!");
Expand Down Expand Up @@ -341,7 +344,7 @@ public Window setShowRunnable(final Runnable runnable, final int repeatTime) {
return this;
}

public void show(final Player player) {
private void show(@NotNull final Player player) {
player.openInventory(this.inventory);

if (showRunnable != null && this.showRunnableTaskID == -1) {
Expand All @@ -357,24 +360,23 @@ public void show(final Player player) {
}
}

public void close() {
private void close() {
if (showRunnableTaskID != -1) {
Bukkit.getScheduler().cancelTask(this.showRunnableTaskID);
this.showRunnableTaskID = -1;
}
for (HumanEntity humanEntity : new ArrayList<>(this.inventory.getViewers())) {
//new ArrayList is required as of ModificationException
for (final HumanEntity humanEntity : new ArrayList<>(this.inventory.getViewers())) {
// new ArrayList is required because of ModificationException
humanEntity.closeInventory();
}

for (GUI.Window.Button button : this.buttonMap) {
for (final GUI.Window.Button button : this.buttonMap) {
if (button == null) continue;
for (ButtonAction action : button.actions.values()) {
if (action instanceof ButtonAction.PAGE_GOTO) {
GUI other = ((ButtonAction.PAGE_GOTO) action).window.gui;
if (!other.equals(this.gui)) { //Close other GUIs
other.close();
}
for (final ButtonAction action : button.actions.values()) {
if (!(action instanceof ButtonAction.PAGE_GOTO gotoAction)) continue;
final GUI other = gotoAction.window.gui;
if (other != null && !other.isClosed()) { //Close other GUIs
other.close();
}
}
}
Expand Down Expand Up @@ -409,15 +411,11 @@ public Button addAction(@NotNull ClickType c_action, @NotNull ButtonAction b_act

private void executeAction(@NotNull InventoryClickEvent event) {
ButtonAction action = actions.get(event.getClick());

if (action == null) {
return;
}
if (action == null) return;

action.run();

if (action instanceof PlayerAction && event.getWhoClicked() instanceof Player) {
((PlayerAction) action).run((Player) event.getWhoClicked());
if (action instanceof PlayerAction playerAction && event.getWhoClicked() instanceof Player player) {
playerAction.run(player);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/flo56958/minetinker/data/GUIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ public void run() {
}

public static void reload() {
if (modGUI != null) modGUI.close();
if (configurationsGUI != null) configurationsGUI.close();
if (modGUI != null && !modGUI.isClosed()) modGUI.close();
if (configurationsGUI != null && !configurationsGUI.isClosed()) configurationsGUI.close();

modGUI = createModGUI(ModManager.instance().getAllowedMods(),
LanguageManager.getString("ToolType.ALL") + ": ");
Expand Down

0 comments on commit 9be95a1

Please sign in to comment.