From 1fa3015b366deaca28c0e24a9b3738bf5aa8a741 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 2 May 2021 16:11:47 +0200 Subject: [PATCH] Add Watchdog Signed-off-by: Chaoscaot --- .../features/gui/editor/BauGuiEditor.java | 7 +++- .../features/gui/editor/BauGuiMapping.java | 37 ++++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java index 0e1a30d8..c1697c6b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiEditor.java @@ -27,6 +27,7 @@ import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; +import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -52,6 +53,7 @@ import java.util.Map; @Linked(LinkageType.LISTENER) public class BauGuiEditor implements Listener { + @Getter private static final List open_Edits = new ArrayList<>(); public static void openGuiEditor(Player p, ItemStack cursor) { @@ -78,6 +80,7 @@ public class BauGuiEditor implements Listener { p.openInventory(inv); p.getOpenInventory().setCursor(cursor == null ? new SWItem().getItemStack() : cursor); open_Edits.add(p); + BauGuiMapping.startWatchdog(); } private static ItemStack addId(ItemStack itemStack, int id) { @@ -180,7 +183,9 @@ public class BauGuiEditor implements Listener { @EventHandler public void onInventoryDrag(InventoryDragEvent event) { if (open_Edits.contains(event.getWhoClicked())) { - event.setCancelled(true); + if (event.getRawSlots().stream().anyMatch(integer -> event.getView().getInventory(integer) == event.getWhoClicked().getInventory())) { + event.setCancelled(true); + } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java index 36bd3146..d7bd306a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/editor/BauGuiMapping.java @@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; import java.util.*; @@ -31,10 +32,28 @@ public class BauGuiMapping { private static final HashMap fromUUID = new HashMap<>(); private static final List mappings = new ArrayList<>(); - static { - Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { - mappings.forEach(BauGuiMapping::tick); - }, 1, 1); + private static BukkitTask task; + + public static void startWatchdog() { + if (task == null) { + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), bukkitTask -> { + mappings.forEach(BauGuiMapping::tick); + if (BauGuiEditor.getOpen_Edits().isEmpty()) { + bukkitTask.cancel(); + task = null; + } + }, 1, 1); + } + } + + public static BauGuiMapping getGuiMapping(Player p) { + BauGuiMapping mapping = fromUUID.get(p.getUniqueId()); + + if (mapping == null) { + mapping = TempClass.DEFAULT_GUI.apply(p.getUniqueId()); + } + + return mapping; } @Getter @@ -61,16 +80,6 @@ public class BauGuiMapping { mappings.add(this); } - public static BauGuiMapping getGuiMapping(Player p) { - BauGuiMapping mapping = fromUUID.get(p.getUniqueId()); - - if (mapping == null) { - mapping = TempClass.DEFAULT_GUI.apply(p.getUniqueId()); - } - - return mapping; - } - public boolean isShown(int id) { if (!mapping.containsKey(id)) { return false;