From bb618193320676f52a2b63f39599dd960fac9963 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 21 Sep 2021 23:54:32 +0200 Subject: [PATCH] Updated some ChatUtil methods + some refactoring --- Core/pom.xml | 1 - .../fr/pandacube/lib/core/chat/ChatUtil.java | 50 +++++++++------ .../fr/pandacube/lib/core/util/Callback.java | 6 -- .../pandacube/lib/paper/gui/GUIInventory.java | 34 +++++----- .../paper/scheduler/AutoUpdatedObject.java | 64 +++++++++++++++++++ .../SchedulerUtil.java} | 4 +- 6 files changed, 114 insertions(+), 45 deletions(-) delete mode 100644 Core/src/main/java/fr/pandacube/lib/core/util/Callback.java create mode 100644 Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java rename Paper/src/main/java/fr/pandacube/lib/paper/{util/ThreadUtil.java => scheduler/SchedulerUtil.java} (92%) diff --git a/Core/pom.xml b/Core/pom.xml index a96adf4..95d8fdc 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -68,7 +68,6 @@ org.geysermc.floodgate api 2.0-SNAPSHOT - provided diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java index 1d54975..31de15e 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java @@ -184,29 +184,27 @@ public class ChatUtil { - public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, - boolean console) { + /** + * @param decorationColor support null values + */ + public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean console) { return centerText(text, repeatedChar, decorationColor, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH); } + public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean console, int maxWidth) { + return centerText(text, repeatedChar, decorationColor, false, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH); + } /** - * - * @param text - * @param repeatedChar * @param decorationColor support null values - * @param console - * @param maxWidth - * @return */ - public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, - boolean console, int maxWidth) { + public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console, int maxWidth) { int textWidth = componentWidth(text.getAdv(), console); if (textWidth > maxWidth) return text; - int repeatedCharWidth = charW(repeatedChar, console, false); + int repeatedCharWidth = charW(repeatedChar, console, decorationBold); int sideWidth = (maxWidth - textWidth) / 2; int sideNbChar = sideWidth / repeatedCharWidth; @@ -214,24 +212,25 @@ public class ChatUtil { return text; String sideChars = repeatedChar(repeatedChar, sideNbChar); + FormatableChat side = text(sideChars).color(decorationColor); + if (decorationBold) + side.bold(); Chat d = Chat.chat() - .then(text(sideChars).color(decorationColor)) + .then(side) .then(text); if (repeatedChar != ' ') - d.then(text(sideChars).color(decorationColor)); + d.then(side); return d; } - public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, - boolean console) { + public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, boolean console) { return leftText(text, repeatedChar, decorationColor, nbLeft, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH); } - public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, - boolean console, int maxWidth) { + public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, boolean console, int maxWidth) { int textWidth = componentWidth(text.getAdv(), console); int repeatedCharWidth = charW(repeatedChar, console, false); @@ -280,8 +279,19 @@ public class ChatUtil { } public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean console) { - int count = ((console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH) / charW(repeatedChar, console, false); - return text(repeatedChar(repeatedChar, count)).color(decorationColor); + return emptyLine(repeatedChar, decorationColor, false, console); + } + + public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console) { + return emptyLine(repeatedChar, decorationColor, decorationBold, console, (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH); + } + + public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console, int maxWidth) { + int count = maxWidth / charW(repeatedChar, console, decorationBold); + FormatableChat line = text(repeatedChar(repeatedChar, count)).color(decorationColor); + if (decorationBold) + line.bold(); + return line; } private static String repeatedChar(char repeatedChar, int count) { @@ -338,7 +348,7 @@ public class ChatUtil { return (count < 0) ? 0 : count; } - private static int charW(char c, boolean console, boolean bold) { + public static int charW(char c, boolean console, boolean bold) { if (console) return (c == '§') ? -1 : 1; for (int px : CHARS_SIZE.keySet()) if (CHARS_SIZE.get(px).indexOf(c) >= 0) return px + (bold ? 1 : 0); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/Callback.java b/Core/src/main/java/fr/pandacube/lib/core/util/Callback.java deleted file mode 100644 index 697a5e8..0000000 --- a/Core/src/main/java/fr/pandacube/lib/core/util/Callback.java +++ /dev/null @@ -1,6 +0,0 @@ -package fr.pandacube.lib.core.util; - -@FunctionalInterface -public interface Callback { - public void done(T arg); -} diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java b/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java index af40728..19c5686 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.function.Consumer; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -21,7 +22,8 @@ import org.bukkit.plugin.Plugin; import com.google.common.collect.ImmutableMap; import fr.pandacube.lib.core.chat.Chat; -import fr.pandacube.lib.core.util.Callback; +import fr.pandacube.lib.core.players.IPlayerManager; +import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.paper.util.ItemStackBuilder; public class GUIInventory implements Listener { @@ -30,21 +32,20 @@ public class GUIInventory implements Listener { private Player player; private Inventory inv; - private Callback onCloseEvent; + private Consumer onCloseEvent; private boolean isOpened = false; - private Map> onClickEvents; + private Map> onClickEvents; - @Deprecated - public GUIInventory(Player p, int nbLines, String title, Callback closeEventAction, - Plugin pl) { - this(p, nbLines, title == null ? null : Chat.legacyText(title), closeEventAction, pl); - } - public GUIInventory(Player p, int nbLines, Chat title, Callback closeEventAction, + public GUIInventory(Player p, int nbLines, Chat title, Consumer closeEventAction, Plugin pl) { if (title == null) inv = Bukkit.createInventory(null, nbLines * 9); else inv = Bukkit.createInventory(null, nbLines * 9, title.getAdv()); + + if (IPlayerManager.getInstance().get(p.getUniqueId()).isBedrockClient()) { + Log.warning("Opening GUI inventory for player on Bedrock client " + p.getName() + " (" + p.getUniqueId() + "). Please use a Form instead.", new Throwable()); + } setCloseEvent(closeEventAction); @@ -56,7 +57,7 @@ public class GUIInventory implements Listener { } - protected void setCloseEvent(Callback closeEventAction) { + protected void setCloseEvent(Consumer closeEventAction) { onCloseEvent = closeEventAction; } @@ -65,7 +66,7 @@ public class GUIInventory implements Listener { * été pré-annulée. Pour la rétablir, faites un * event.setCancelled(false)). */ - public void setButtonIfEmpty(int p, ItemStack iStack, Callback clickEventActions) { + public void setButtonIfEmpty(int p, ItemStack iStack, Consumer clickEventActions) { if (inv.getItem(p) == null) setButton(p, iStack, clickEventActions); } @@ -75,7 +76,7 @@ public class GUIInventory implements Listener { * été pré-annulée. Pour la rétablir, faites un * event.setCancelled(false)). */ - public void setButton(int p, ItemStack iStack, Callback clickEventActions) { + public void setButton(int p, ItemStack iStack, Consumer clickEventActions) { inv.setItem(p, iStack); changeClickEventAction(p, clickEventActions); } @@ -85,7 +86,7 @@ public class GUIInventory implements Listener { * été pré-annulée. Pour la rétablir, faites un * event.setCancelled(false)). */ - public void changeClickEventAction(int p, Callback clickEventActions) { + public void changeClickEventAction(int p, Consumer clickEventActions) { onClickEvents.put(p, clickEventActions); } @@ -135,8 +136,9 @@ public class GUIInventory implements Listener { // on ne réagit pas aux clics hors de l'inventaire du dessus. if (event.getClickedInventory() != event.getView().getTopInventory()) return; - Callback callback = onClickEvents.get(event.getSlot()); - if (callback != null) callback.done(event); + Consumer callback = onClickEvents.get(event.getSlot()); + if (callback != null) + callback.accept(event); } @@ -149,7 +151,7 @@ public class GUIInventory implements Listener { HandlerList.unregisterAll(this); if (onCloseEvent != null) - onCloseEvent.done(event); + onCloseEvent.accept(event); isOpened = false; } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java b/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java new file mode 100644 index 0000000..6dd380d --- /dev/null +++ b/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java @@ -0,0 +1,64 @@ +package fr.pandacube.lib.paper.scheduler; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; + +import fr.pandacube.lib.paper.PandaLibPaper; + +public class AutoUpdatedObject { + private static Plugin plugin = PandaLibPaper.getPlugin(); + + private Runnable updater; + + private List tasks = new ArrayList<>(); + + + protected AutoUpdatedObject() { } + public AutoUpdatedObject(Runnable updater) { + this.updater = Objects.requireNonNull(updater); + } + + public synchronized void updateSync() { + tasks.add(Bukkit.getScheduler().runTask(plugin, this::update)); + } + + public synchronized void updateAsync() { + tasks.add(Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update)); + } + + public synchronized void updateLater(long delay) + throws IllegalArgumentException, IllegalStateException { + tasks.add(Bukkit.getScheduler().runTaskLater(plugin, this::update, delay)); + } + + public synchronized void updateLaterAsync(long delay) + throws IllegalArgumentException, IllegalStateException { + tasks.add(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this::update, delay)); + } + + public synchronized void updateTimer(long delay, long period) + throws IllegalArgumentException, IllegalStateException { + tasks.add(Bukkit.getScheduler().runTaskTimer(plugin, this::update, delay, period)); + } + + public synchronized void updateTimerAsync(long delay, long period) + throws IllegalArgumentException, IllegalStateException { + tasks.add(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::update, delay, period)); + } + + public synchronized void cancel() { + tasks.forEach(t -> t.cancel()); + tasks.clear(); + } + + public void update() { + Objects.requireNonNull(updater, "Please use new AutoUpdatedObject(Runnable) or override the run method."); + updater.run(); + } + +} diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/ThreadUtil.java b/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/SchedulerUtil.java similarity index 92% rename from Paper/src/main/java/fr/pandacube/lib/paper/util/ThreadUtil.java rename to Paper/src/main/java/fr/pandacube/lib/paper/scheduler/SchedulerUtil.java index e58aff8..46259e4 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/ThreadUtil.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/SchedulerUtil.java @@ -1,4 +1,4 @@ -package fr.pandacube.lib.paper.util; +package fr.pandacube.lib.paper.scheduler; import java.util.concurrent.Callable; @@ -7,7 +7,7 @@ import org.bukkit.Bukkit; import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.paper.PandaLibPaper; -public class ThreadUtil { +public class SchedulerUtil { public static void runOnServerThread(Runnable task) { if (Bukkit.isPrimaryThread())