From 7ce6668de4c3ccf871b9a5287ee714121a1c6967 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 27 Oct 2022 12:58:16 +0200 Subject: [PATCH] Add AnvilInv LeftClick and SchematicSelector AnvilInv returns --- SpigotCore_Main/src/SpigotCore.properties | 1 + SpigotCore_Main/src/SpigotCore_de.properties | 1 + .../src/de/steamwar/inventory/SWAnvilInv.java | 4 +++ .../steamwar/inventory/SchematicSelector.java | 25 ++++++++++++++++--- .../SchematicSelectorInjectable.java | 7 ++++++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/SpigotCore.properties b/SpigotCore_Main/src/SpigotCore.properties index 450de5a..388f441 100644 --- a/SpigotCore_Main/src/SpigotCore.properties +++ b/SpigotCore_Main/src/SpigotCore.properties @@ -42,6 +42,7 @@ SCHEM_SELECTOR_SORTING_UPDATE=Last update SCHEM_SELECTOR_SORTING_DIRECTION=§e{0} §7order SCHEM_SELECTOR_SORTING_ASC=Ascending SCHEM_SELECTOR_SORTING_DSC=Descending +SCHEM_SELECTOR_CLICK_BACK=§7Click to go back SCHEM_SELECTOR_ITEM_NAME=§e{0} SCHEM_SELECTOR_ITEM_NAME_FILTER=§7{0} diff --git a/SpigotCore_Main/src/SpigotCore_de.properties b/SpigotCore_Main/src/SpigotCore_de.properties index b5e089a..1279d36 100644 --- a/SpigotCore_Main/src/SpigotCore_de.properties +++ b/SpigotCore_Main/src/SpigotCore_de.properties @@ -42,6 +42,7 @@ SCHEM_SELECTOR_SORTING_UPDATE=Letztes Update SCHEM_SELECTOR_SORTING_DIRECTION=§7Richtung: §e{0} SCHEM_SELECTOR_SORTING_ASC=Aufsteigend SCHEM_SELECTOR_SORTING_DSC=Absteigend +SCHEM_SELECTOR_CLICK_BACK=§7Klicke hier, um zurückzugehen SCHEM_SELECTOR_CREATE_DIR_TITLE=Ordner erstellen diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWAnvilInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWAnvilInv.java index 8244390..733fae9 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWAnvilInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWAnvilInv.java @@ -66,6 +66,10 @@ public class SWAnvilInv { this.callback = callback; } + public void addLeftCallback(Runnable callback) { + builder.onLeftInputClick(p -> callback.run()); + } + public void addCloseCallback(Runnable callback) { builder.onClose(p -> callback.run()); } diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java index 4620643..6582d12 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java @@ -25,11 +25,13 @@ import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import de.steamwar.sql.SteamwarUser; import lombok.*; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import java.text.MessageFormat; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; public class SchematicSelector { @@ -249,9 +251,23 @@ public class SchematicSelector { return new SWListInv.SWListEntry<>(item, node); } + private void addLeftCloseAction(SWAnvilInv inv, Runnable runnable) { + AtomicBoolean wasLeft = new AtomicBoolean(false); + inv.addCloseCallback(() -> { + if(injectable.onAnvilInvCloseAction(this) == SchematicSelectorInjectable.AnvilInvCloseAction.REOPEN && !wasLeft.get()) { + player.closeInventory(); + Bukkit.getScheduler().runTaskLater(Core.getInstance(), runnable, 1); + } + }); + inv.addLeftCallback(() -> { + wasLeft.set(true); + runnable.run(); + }); + } + private void createFolderIn(SchematicNode parent) { SWAnvilInv inv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_CREATE_DIR_TITLE", player)); - inv.setItem(Material.CHEST); + inv.setItem(Material.CHEST, Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_CLICK_BACK", player)), false); inv.setCallback(s -> { if(!SchematicNode.invalidSchemName(new String[] {s})) { if(injectable.onFolderCreate(this, s)) { @@ -262,6 +278,7 @@ public class SchematicSelector { } player.closeInventory(); }); + addLeftCloseAction(inv, this::reOpen); inv.open(); } @@ -273,11 +290,12 @@ public class SchematicSelector { openFilter(); } else { SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_NAME", player)); - swAnvilInv.setItem(Material.NAME_TAG); + swAnvilInv.setItem(Material.NAME_TAG, Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_CLICK_BACK", player)), false); swAnvilInv.setCallback(s -> { filter.setName(s); openFilter(); }); + addLeftCloseAction(swAnvilInv, this::openFilter); swAnvilInv.open(); } }; @@ -293,13 +311,14 @@ public class SchematicSelector { openFilter(); } else { SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_OWNER", player)); - swAnvilInv.setItem(SWItem.getMaterial("SKULL_ITEM"), (byte) 3); + swAnvilInv.setItem(SWItem.getMaterial("SKULL_ITEM"), (byte) 3, Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_CLICK_BACK", player)), false); swAnvilInv.setCallback(s -> { if(SteamwarUser.get(s) != null) { filter.setOwner(SteamwarUser.get(s).getId()); } openFilter(); }); + addLeftCloseAction(swAnvilInv, this::openFilter); swAnvilInv.open(); } }; diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java index 29bd658..235dda4 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java @@ -45,8 +45,15 @@ public interface SchematicSelectorInjectable { default void onSelectorOpen(SchematicSelector selector, OpenFrom from) {} + default AnvilInvCloseAction onAnvilInvCloseAction(SchematicSelector selector) {return AnvilInvCloseAction.CLOSE;} + enum OpenFrom { FRESH, REOPEN } + + enum AnvilInvCloseAction { + CLOSE, + REOPEN + } }