diff --git a/BetterStaffChat-bungeecord/pom.xml b/BetterStaffChat-bungeecord/pom.xml index dff7b4f..dc5aab0 100644 --- a/BetterStaffChat-bungeecord/pom.xml +++ b/BetterStaffChat-bungeecord/pom.xml @@ -25,7 +25,7 @@ dev.austech BetterStaffChat - 1.0.1 + 1.0.2 BetterStaffChat-bungeecord diff --git a/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/BetterStaffChatBungeeCord.java b/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/BetterStaffChatBungeeCord.java index c82ac6a..118de6b 100644 --- a/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/BetterStaffChatBungeeCord.java +++ b/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/BetterStaffChatBungeeCord.java @@ -58,6 +58,7 @@ public final class BetterStaffChatBungeeCord extends Plugin { public void onEnable() { instance = this; + TextUtil.init(false, this); Config.load(); if (getConfig().getBoolean("check-for-updates")) diff --git a/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/listener/PlayerListener.java b/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/listener/PlayerListener.java index bf9f498..161b87a 100644 --- a/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/listener/PlayerListener.java +++ b/BetterStaffChat-bungeecord/src/main/java/dev/austech/betterstaffchat/bungeecord/listener/PlayerListener.java @@ -70,6 +70,7 @@ public void onPlayerChat(ChatEvent event) { @EventHandler public void onPlayerPostLogin(PostLoginEvent event) { if (event.getPlayer().hasPermission("betterstaffchat.messages.join") && !BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.join").equals("")) { + if (event.getPlayer().getServer() == null) return; BetterStaffChatBungeeCord.getInstance().getProxy().getScheduler().schedule(BetterStaffChatBungeeCord.getInstance(), () -> { String message = TextUtil.colorize( BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.join") @@ -94,6 +95,7 @@ public void onPlayerPostLogin(PostLoginEvent event) { @EventHandler public void onPlayerDisconnect(PlayerDisconnectEvent event) { if (event.getPlayer().hasPermission("betterstaffchat.messages.leave") && !BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.leave").equals("")) { + if (event.getPlayer().getServer() == null) return; BetterStaffChatBungeeCord.getInstance().getProxy().getScheduler().schedule(BetterStaffChatBungeeCord.getInstance(), () -> { String message = TextUtil.colorize( BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.leave") diff --git a/BetterStaffChat-common/pom.xml b/BetterStaffChat-common/pom.xml index b46e1bf..dd2bbf8 100644 --- a/BetterStaffChat-common/pom.xml +++ b/BetterStaffChat-common/pom.xml @@ -25,7 +25,7 @@ dev.austech BetterStaffChat - 1.0.1 + 1.0.2 BetterStaffChat-common diff --git a/BetterStaffChat-common/src/main/java/dev/austech/betterstaffchat/common/util/TextUtil.java b/BetterStaffChat-common/src/main/java/dev/austech/betterstaffchat/common/util/TextUtil.java index afc7491..d62a684 100644 --- a/BetterStaffChat-common/src/main/java/dev/austech/betterstaffchat/common/util/TextUtil.java +++ b/BetterStaffChat-common/src/main/java/dev/austech/betterstaffchat/common/util/TextUtil.java @@ -22,8 +22,24 @@ import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.TextComponent; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + @UtilityClass public final class TextUtil { + private String serverVersion = ""; + + public void init(boolean spigot, Object plugin) { + if (!spigot) { + serverVersion = "new enough"; + } else { + try { + serverVersion = (String) plugin.getClass().getMethod("getVersion").invoke(plugin); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + } /** * Colors a specific message using the ChatColor API. @@ -31,7 +47,29 @@ public final class TextUtil { * @return Colored string */ public String colorize(String string) { - return ChatColor.translateAlternateColorCodes('&', string); + if (serverVersion.isEmpty()) { + throw new IllegalArgumentException("Server version has not been set."); + } + if (serverVersion.equals("new enough") || serverVersion.contains("1.16") || serverVersion.contains("1.17")) // TODO: Make this dynamic later + return ChatColor.translateAlternateColorCodes('&', colorizeRgb(string)); + else return ChatColor.translateAlternateColorCodes('&', string); + } + + /* Pulled right from https://www.spigotmc.org/threads/hex-chat-class.449300/ */ + public String colorizeRgb(String string) { + final Pattern hexPattern = Pattern.compile("&#" + "([A-Fa-f0-9]{6})" + ""); + Matcher matcher = hexPattern.matcher(string); + StringBuffer buffer = new StringBuffer(string.length() + 4 * 8); + while (matcher.find()) + { + String group = matcher.group(1); + matcher.appendReplacement(buffer, ChatColor.COLOR_CHAR + "x" + + ChatColor.COLOR_CHAR + group.charAt(0) + ChatColor.COLOR_CHAR + group.charAt(1) + + ChatColor.COLOR_CHAR + group.charAt(2) + ChatColor.COLOR_CHAR + group.charAt(3) + + ChatColor.COLOR_CHAR + group.charAt(4) + ChatColor.COLOR_CHAR + group.charAt(5) + ); + } + return matcher.appendTail(buffer).toString(); } public TextComponent colorizeToComponent(String string) { diff --git a/BetterStaffChat-package/pom.xml b/BetterStaffChat-package/pom.xml index c18ffdd..0f563a9 100644 --- a/BetterStaffChat-package/pom.xml +++ b/BetterStaffChat-package/pom.xml @@ -23,7 +23,7 @@ BetterStaffChat dev.austech - 1.0.1 + 1.0.2 4.0.0 diff --git a/BetterStaffChat-spigot/pom.xml b/BetterStaffChat-spigot/pom.xml index db5dd16..cff4da7 100644 --- a/BetterStaffChat-spigot/pom.xml +++ b/BetterStaffChat-spigot/pom.xml @@ -25,7 +25,7 @@ dev.austech BetterStaffChat - 1.0.1 + 1.0.2 BetterStaffChat-spigot diff --git a/BetterStaffChat-spigot/src/main/java/dev/austech/betterstaffchat/spigot/BetterStaffChatSpigot.java b/BetterStaffChat-spigot/src/main/java/dev/austech/betterstaffchat/spigot/BetterStaffChatSpigot.java index 0f2ee69..ce79f1b 100644 --- a/BetterStaffChat-spigot/src/main/java/dev/austech/betterstaffchat/spigot/BetterStaffChatSpigot.java +++ b/BetterStaffChat-spigot/src/main/java/dev/austech/betterstaffchat/spigot/BetterStaffChatSpigot.java @@ -36,7 +36,6 @@ import lombok.Setter; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Activity; -import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; @@ -47,7 +46,6 @@ import java.io.File; import java.util.ArrayList; import java.util.UUID; -import java.util.concurrent.TimeUnit; public final class BetterStaffChatSpigot extends JavaPlugin { @@ -62,6 +60,7 @@ public final class BetterStaffChatSpigot extends JavaPlugin { public void onEnable() { instance = this; + TextUtil.init(true, this); Config.load(); if (getConfig().getBoolean("check-for-updates")) @@ -134,6 +133,10 @@ public void logPrefixDebug(String string) { if (getConfig().getBoolean("debug")) Bukkit.getConsoleSender().sendMessage("[BetterStaffChat] Debug - " + TextUtil.colorize(string)); } + public String getVersion() { + return Bukkit.getVersion(); + } + @Override public void onDisable() { if (isDiscordEnabled()) (getJda()).shutdown(); diff --git a/pom.xml b/pom.xml index ed5a2f1..1e448cc 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ dev.austech BetterStaffChat - 1.0.1 + 1.0.2 pom