From 07a023375225a86a9c423902e317596d227df59c Mon Sep 17 00:00:00 2001 From: Ismael Hanbel Date: Sun, 16 Jun 2024 19:30:15 +0200 Subject: [PATCH] Add minimessage support for tab, and few improvements --- .../sternalboard/commands/SternalCommand.java | 8 ++- .../managers/tab/TabExecutor.java | 6 +-- .../managers/tab/list/LegacyTabExecutor.java | 25 +++------ .../managers/tab/list/ModernTabExecutor.java | 26 +++------ .../sternalboard/utils/color/BukkitColor.java | 11 ---- .../sternalboard/utils/color/BungeeColor.java | 54 ------------------- .../utils/color/ColorHandler.java | 30 ----------- .../utils/placeholders/PlaceholderParser.java | 12 ----- 8 files changed, 26 insertions(+), 146 deletions(-) delete mode 100644 plugin/src/main/java/com/xism4/sternalboard/utils/color/BukkitColor.java delete mode 100644 plugin/src/main/java/com/xism4/sternalboard/utils/color/BungeeColor.java delete mode 100644 plugin/src/main/java/com/xism4/sternalboard/utils/color/ColorHandler.java delete mode 100644 plugin/src/main/java/com/xism4/sternalboard/utils/placeholders/PlaceholderParser.java diff --git a/plugin/src/main/java/com/xism4/sternalboard/commands/SternalCommand.java b/plugin/src/main/java/com/xism4/sternalboard/commands/SternalCommand.java index 764541b..807c9d6 100644 --- a/plugin/src/main/java/com/xism4/sternalboard/commands/SternalCommand.java +++ b/plugin/src/main/java/com/xism4/sternalboard/commands/SternalCommand.java @@ -93,9 +93,15 @@ private void toggleSubcommand(CommandSender sender) { private void reloadSubcommand(CommandSender sender) { if (sender.hasPermission("sternalboard.reload")){ plugin.getRawConfig().reload(); + this.config = plugin.getConfig(); - plugin.setAnimateScoreboard(config.getBoolean("settings.animated")); + + plugin.setAnimateScoreboard( + config.getBoolean("settings.animated") + ); + plugin.getScoreboardManager().reload(); + if (plugin.isAnimationEnabled()) { plugin.getRawAnimConfig().reload(); if (plugin.getAnimationManager() != null) { diff --git a/plugin/src/main/java/com/xism4/sternalboard/managers/tab/TabExecutor.java b/plugin/src/main/java/com/xism4/sternalboard/managers/tab/TabExecutor.java index dd2868b..d54fcab 100644 --- a/plugin/src/main/java/com/xism4/sternalboard/managers/tab/TabExecutor.java +++ b/plugin/src/main/java/com/xism4/sternalboard/managers/tab/TabExecutor.java @@ -1,16 +1,16 @@ package com.xism4.sternalboard.managers.tab; +import com.xism4.sternalboard.SternalBoardPlugin; import com.xism4.sternalboard.managers.tab.list.LegacyTabExecutor; import com.xism4.sternalboard.managers.tab.list.ModernTabExecutor; import com.xism4.sternalboard.utils.GameVersion; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; public abstract class TabExecutor { private static TabExecutor instance = null; - private static TabExecutor getInstance(JavaPlugin plugin) { + private static TabExecutor getInstance(SternalBoardPlugin plugin) { if (instance == null) { if (GameVersion.isGreaterEqualThan(GameVersion.v1_13_R1)) { instance = new ModernTabExecutor( @@ -27,7 +27,7 @@ private static TabExecutor getInstance(JavaPlugin plugin) { public abstract void sendTab(Player player, String header, String footer); - public static void sendTabList(JavaPlugin plugin, Player player, String header, String footer) { + public static void sendTabList(SternalBoardPlugin plugin, Player player, String header, String footer) { getInstance(plugin).sendTab( player, header, footer ); diff --git a/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/LegacyTabExecutor.java b/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/LegacyTabExecutor.java index d98f18b..4322c07 100644 --- a/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/LegacyTabExecutor.java +++ b/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/LegacyTabExecutor.java @@ -1,15 +1,14 @@ package com.xism4.sternalboard.managers.tab.list; +import com.xism4.sternalboard.SternalBoardPlugin; import com.xism4.sternalboard.managers.tab.TabExecutor; -import com.xism4.sternalboard.utils.color.ColorHandler; -import com.xism4.sternalboard.utils.placeholders.PlaceholderParser; +import com.xism4.sternalboard.utils.TextUtils; import me.blueslime.nmshandlerapi.SpecifiedClass; import me.blueslime.nmshandlerapi.method.MethodContainer; import me.blueslime.nmshandlerapi.method.MethodData; import me.blueslime.nmshandlerapi.utils.presets.Presets; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; import javax.annotation.Nonnull; import java.lang.reflect.Field; @@ -43,10 +42,10 @@ public class LegacyTabExecutor extends TabExecutor { private Method playerHandler; - private final JavaPlugin plugin; + private final SternalBoardPlugin sternalBoardPlugin; - public LegacyTabExecutor(JavaPlugin plugin) { - this.plugin = plugin; + public LegacyTabExecutor(SternalBoardPlugin sternalBoardPlugin) { + this.sternalBoardPlugin = sternalBoardPlugin; generate(); } @@ -96,17 +95,9 @@ public void sendTab(Player player, String headerText, String footerText) { String tempHeader; String tempFooter; - if (plugin.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) { - tempHeader = ColorHandler.convert( - PlaceholderParser.parse(player, check(headerText)) - ); - tempFooter = ColorHandler.convert( - PlaceholderParser.parse(player, check(footerText)) - ); - } else { - tempHeader = ColorHandler.convert(check(headerText)); - tempFooter = ColorHandler.convert(check(footerText)); - } + tempHeader = TextUtils.processPlaceholders(sternalBoardPlugin, player, check(headerText) + ); + tempFooter = TextUtils.processPlaceholders(sternalBoardPlugin, player, check(footerText)); try { Object header = buildComponent(tempHeader); diff --git a/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/ModernTabExecutor.java b/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/ModernTabExecutor.java index 057082a..f0d3916 100644 --- a/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/ModernTabExecutor.java +++ b/plugin/src/main/java/com/xism4/sternalboard/managers/tab/list/ModernTabExecutor.java @@ -1,34 +1,24 @@ package com.xism4.sternalboard.managers.tab.list; +import com.xism4.sternalboard.SternalBoardPlugin; import com.xism4.sternalboard.managers.tab.TabExecutor; -import com.xism4.sternalboard.utils.color.ColorHandler; -import com.xism4.sternalboard.utils.placeholders.PlaceholderParser; +import com.xism4.sternalboard.utils.TextUtils; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; public class ModernTabExecutor extends TabExecutor { - private final JavaPlugin plugin; + private final SternalBoardPlugin plugin; + private TextUtils textUtils; - public ModernTabExecutor(JavaPlugin plugin) { + public ModernTabExecutor(SternalBoardPlugin plugin) { this.plugin = plugin; } @Override public void sendTab(Player player, String header, String footer) { - if (plugin.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) { - player.setPlayerListHeaderFooter( - ColorHandler.convert( - PlaceholderParser.parse(player, check(header)) - ), - ColorHandler.convert( - PlaceholderParser.parse(player, check(footer)) - ) - ); - return; - } + plugin.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI"); player.setPlayerListHeaderFooter( - ColorHandler.convert(check(header)), - ColorHandler.convert(check(footer)) + TextUtils.processPlaceholders(plugin, player, check(header)), + TextUtils.processPlaceholders(plugin, player, check(footer)) ); } } diff --git a/plugin/src/main/java/com/xism4/sternalboard/utils/color/BukkitColor.java b/plugin/src/main/java/com/xism4/sternalboard/utils/color/BukkitColor.java deleted file mode 100644 index ba2583f..0000000 --- a/plugin/src/main/java/com/xism4/sternalboard/utils/color/BukkitColor.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.xism4.sternalboard.utils.color; - -import org.bukkit.ChatColor; - -public class BukkitColor extends ColorHandler { - @Override - public String execute(String text) { - return ChatColor.translateAlternateColorCodes('&', text); - } -} - diff --git a/plugin/src/main/java/com/xism4/sternalboard/utils/color/BungeeColor.java b/plugin/src/main/java/com/xism4/sternalboard/utils/color/BungeeColor.java deleted file mode 100644 index 198c3da..0000000 --- a/plugin/src/main/java/com/xism4/sternalboard/utils/color/BungeeColor.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.xism4.sternalboard.utils.color; - - -import net.md_5.bungee.api.ChatColor; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class BungeeColor extends ColorHandler { - private Method COLORIZE_METHOD; - - public BungeeColor() { - try { - COLORIZE_METHOD = ChatColor.class.getDeclaredMethod("of", String.class); - } catch (Exception ignored) { - COLORIZE_METHOD = null; - } - } - - public String execute(String message) { - Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}"); - Matcher matcher = pattern.matcher(message); - - while (matcher.find()) { - String code = message.substring(matcher.start(), matcher.end()); - if (COLORIZE_METHOD != null) { - try { - ChatColor color = (ChatColor) COLORIZE_METHOD.invoke( - ChatColor.WHITE, - code - ); - - message = message.replace( - code, - String.valueOf(color) - ); - - matcher = pattern.matcher( - message - ); - } catch (IllegalAccessException | InvocationTargetException e ) { - return message.replace( - code, - "" - ); - } - } - } - return ChatColor.translateAlternateColorCodes('&', message); - } -} - diff --git a/plugin/src/main/java/com/xism4/sternalboard/utils/color/ColorHandler.java b/plugin/src/main/java/com/xism4/sternalboard/utils/color/ColorHandler.java deleted file mode 100644 index e4cab39..0000000 --- a/plugin/src/main/java/com/xism4/sternalboard/utils/color/ColorHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.xism4.sternalboard.utils.color; - -public abstract class ColorHandler { - - private static ColorHandler INSTANCE = null; - - public static ColorHandler get() { - if (INSTANCE == null) { - INSTANCE = create(); - } - return INSTANCE; - } - - private static ColorHandler create() { - try { - Class.forName("net.md_5.bungee.api.ChatColor"); - - return new BungeeColor(); - } catch (ClassNotFoundException e) { - return new BukkitColor(); - } - } - - public abstract String execute(String text); - - public static String convert(String text) { - return get().execute(text); - } - -} \ No newline at end of file diff --git a/plugin/src/main/java/com/xism4/sternalboard/utils/placeholders/PlaceholderParser.java b/plugin/src/main/java/com/xism4/sternalboard/utils/placeholders/PlaceholderParser.java deleted file mode 100644 index 852ea0a..0000000 --- a/plugin/src/main/java/com/xism4/sternalboard/utils/placeholders/PlaceholderParser.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.xism4.sternalboard.utils.placeholders; - -import me.clip.placeholderapi.PlaceholderAPI; -import org.bukkit.entity.Player; - -public class PlaceholderParser { - public static String parse(Player player, String message) { - return PlaceholderAPI.setPlaceholders(player, message); - } -} - -