From f16389d33dd1878cc39cc4646608d998e8299344 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 24 Aug 2023 01:21:27 +0200 Subject: [PATCH] Upgrade GUIHotBar: add ability to unregister the event listeners of the hotbar + optional cleanup of inventory on player removal --- .../fr/pandacube/lib/paper/gui/GUIHotBar.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java index 6d58727..72ac1d3 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java @@ -43,6 +43,12 @@ public class GUIHotBar implements Listener { BukkitEvent.register(this); } + + public void disable(boolean clearPlayerMenuItems) { + removeAllPlayers(clearPlayerMenuItems); + + BukkitEvent.unregister(this); + } /** * Add the item to this hot bar menu. if there is already players hooked to this hot bar, the item will be directly added to @@ -62,9 +68,9 @@ public class GUIHotBar implements Listener { } /** - * Add the hot bar elements to this player. + * Add the hot bar elements to this player, or update them if applicable. * - * The player is automatically removed when they quit. You can remove it before by calling {@link #removePlayer(Player)}. + * The player is automatically removed when they quit. You can remove it before by calling {@link #removePlayer(Player, boolean)}. */ public void addPlayer(Player p) { if (!currentPlayers.contains(p)) @@ -81,11 +87,20 @@ public class GUIHotBar implements Listener { * Detach this player from this hot bar manager and removes the managed items from the players inventory. */ public void removePlayer(Player p) { + removePlayer(p, true); + } + + /** + * Detach this player from this hot bar manager and optionally removes the managed items from the players inventory. + */ + public void removePlayer(Player p, boolean clearMenuItems) { if (!currentPlayers.contains(p)) return; - - for (ItemStack is : itemsAndSetters.keySet()) { - removeItemFromPlayer(p, is); + + if (clearMenuItems) { + for (ItemStack is : itemsAndSetters.keySet()) { + removeItemFromPlayer(p, is); + } } currentPlayers.remove(p); @@ -100,8 +115,12 @@ public class GUIHotBar implements Listener { public void removeAllPlayers() { + removeAllPlayers(true); + } + + public void removeAllPlayers(boolean clearMenuItems) { for (Player p : new ArrayList<>(currentPlayers)) - removePlayer(p); + removePlayer(p, clearMenuItems); } @@ -211,7 +230,6 @@ public class GUIHotBar implements Listener { if (!currentPlayers.contains(event.getPlayer())) return; - currentPlayers.remove(event.getPlayer()); addPlayer(event.getPlayer()); }