From b33d0685c1ceac4cb159bf348aa0175766f5b837 Mon Sep 17 00:00:00 2001 From: thmsdy Date: Wed, 27 Jan 2021 12:58:16 -0600 Subject: [PATCH] Add hex code support --- dependency-reduced-pom.xml | 4 +- pom.xml | 4 +- src/main/java/com/fpghoti/fpchatx/FPChat.java | 2 +- .../com/fpghoti/fpchatx/badge/BadgeList.java | 3 ++ .../{customcodes => chat}/BubbleCode.java | 2 +- .../com/fpghoti/fpchatx/chat/ChatChannel.java | 8 ++-- .../fpchatx/{customcodes => chat}/Codify.java | 31 +----------- .../com/fpghoti/fpchatx/chat/HexColor.java | 48 +++++++++++++++++++ .../com/fpghoti/fpchatx/chat/PrepareChat.java | 11 ++--- .../command/commands/BadgeListCommand.java | 20 ++++---- .../com/fpghoti/fpchatx/player/FPlayer.java | 8 ++-- .../java/com/fpghoti/fpchatx/util/Util.java | 7 ++- src/main/resources/plugin.yml | 2 +- 13 files changed, 88 insertions(+), 62 deletions(-) rename src/main/java/com/fpghoti/fpchatx/{customcodes => chat}/BubbleCode.java (95%) rename src/main/java/com/fpghoti/fpchatx/{customcodes => chat}/Codify.java (69%) create mode 100644 src/main/java/com/fpghoti/fpchatx/chat/HexColor.java diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index e188008..e8d22c4 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.fpghoti FPChat - 1.1.2 + 1.1.4 src/main/java @@ -57,7 +57,7 @@ org.spigotmc spigot-api - 1.16.3-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided diff --git a/pom.xml b/pom.xml index faedda5..e68c05d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.fpghoti FPChat - 1.1.3 + 1.1.4 spigot-repo @@ -65,7 +65,7 @@ org.spigotmc spigot-api - 1.16.3-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided diff --git a/src/main/java/com/fpghoti/fpchatx/FPChat.java b/src/main/java/com/fpghoti/fpchatx/FPChat.java index 6a707aa..7410e26 100644 --- a/src/main/java/com/fpghoti/fpchatx/FPChat.java +++ b/src/main/java/com/fpghoti/fpchatx/FPChat.java @@ -191,7 +191,7 @@ public class FPChat extends JavaPlugin { } public void log(Level level, String msg) { - log.log(level, "[FPChat] " + msg.replaceAll("§[0-9A-FK-OR]", "")); + log.log(level, msg.replaceAll("§[0-9A-FK-OR]", "")); } diff --git a/src/main/java/com/fpghoti/fpchatx/badge/BadgeList.java b/src/main/java/com/fpghoti/fpchatx/badge/BadgeList.java index 728bb1d..edb8471 100644 --- a/src/main/java/com/fpghoti/fpchatx/badge/BadgeList.java +++ b/src/main/java/com/fpghoti/fpchatx/badge/BadgeList.java @@ -112,6 +112,9 @@ public class BadgeList implements Iterable{ } public Badge getIndex(int index) { + if(list.size() <= 0) { + return null; + } return list.get(index); } diff --git a/src/main/java/com/fpghoti/fpchatx/customcodes/BubbleCode.java b/src/main/java/com/fpghoti/fpchatx/chat/BubbleCode.java similarity index 95% rename from src/main/java/com/fpghoti/fpchatx/customcodes/BubbleCode.java rename to src/main/java/com/fpghoti/fpchatx/chat/BubbleCode.java index 1273186..b083480 100644 --- a/src/main/java/com/fpghoti/fpchatx/customcodes/BubbleCode.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/BubbleCode.java @@ -1,4 +1,4 @@ -package com.fpghoti.fpchatx.customcodes; +package com.fpghoti.fpchatx.chat; public class BubbleCode { diff --git a/src/main/java/com/fpghoti/fpchatx/chat/ChatChannel.java b/src/main/java/com/fpghoti/fpchatx/chat/ChatChannel.java index 1fcc170..bfc3e35 100644 --- a/src/main/java/com/fpghoti/fpchatx/chat/ChatChannel.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/ChatChannel.java @@ -11,8 +11,6 @@ import org.bukkit.entity.Player; import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.config.ChannelFile; -import com.fpghoti.fpchatx.customcodes.BubbleCode; -import com.fpghoti.fpchatx.customcodes.Codify; import com.fpghoti.fpchatx.permission.Permission; import com.fpghoti.fpchatx.player.FPlayer; @@ -309,11 +307,11 @@ public abstract class ChatChannel { } if(Permission.canUseColor(p)){ String last = ChatFilter.filter(filler + msg); - last = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(last)); - finalMessage = ChatColor.translateAlternateColorCodes('&', stf + badges + header + last); + last = BubbleCode.bubblecode(Permission.canBubbleCode(p), ChatColor.translateAlternateColorCodes('&', HexColor.formatHex(last))); + finalMessage = HexColor.formatHex(stf + badges + header + last); }else{ String newmsg = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('§', ChatFilter.filter(filler + msg))); - finalMessage = stf + badges + Codify.removeBubbles(header + newmsg); + finalMessage = stf + badges + Codify.removeBubbles(HexColor.formatHex(header) + newmsg); } return finalMessage; } diff --git a/src/main/java/com/fpghoti/fpchatx/customcodes/Codify.java b/src/main/java/com/fpghoti/fpchatx/chat/Codify.java similarity index 69% rename from src/main/java/com/fpghoti/fpchatx/customcodes/Codify.java rename to src/main/java/com/fpghoti/fpchatx/chat/Codify.java index cae99bb..9f7082c 100644 --- a/src/main/java/com/fpghoti/fpchatx/customcodes/Codify.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/Codify.java @@ -1,35 +1,6 @@ -package com.fpghoti.fpchatx.customcodes; +package com.fpghoti.fpchatx.chat; public class Codify { - - public static String changeFormatSign(String str) { - String nstr = str; - nstr = nstr.replace("&0", "§0"); - nstr = nstr.replace("&1", "§1"); - nstr = nstr.replace("&2", "§2"); - nstr = nstr.replace("&3", "§3"); - nstr = nstr.replace("&4", "§4"); - nstr = nstr.replace("&5", "§5"); - nstr = nstr.replace("&6", "§6"); - nstr = nstr.replace("&7", "§7"); - nstr = nstr.replace("&8", "§8"); - nstr = nstr.replace("&9", "§9"); - nstr = nstr.replace("&a", "§a"); - nstr = nstr.replace("&b", "§b"); - nstr = nstr.replace("&c", "§c"); - nstr = nstr.replace("&d", "§d"); - nstr = nstr.replace("&e", "§e"); - nstr = nstr.replace("&f", "§f"); - nstr = nstr.replace("&k", "§k"); - nstr = nstr.replace("&l", "§l"); - nstr = nstr.replace("&m", "§m"); - nstr = nstr.replace("&n", "§n"); - nstr = nstr.replace("&o", "§o"); - nstr = nstr.replace("&r", "§r"); - //nstr = nstr.replace("&w", "§w"); - //nstr = nstr.replace("&x", "§x"); - return nstr; - } public static String removeBubbles(String str) { String nstr = str; diff --git a/src/main/java/com/fpghoti/fpchatx/chat/HexColor.java b/src/main/java/com/fpghoti/fpchatx/chat/HexColor.java new file mode 100644 index 0000000..3afd63b --- /dev/null +++ b/src/main/java/com/fpghoti/fpchatx/chat/HexColor.java @@ -0,0 +1,48 @@ +package com.fpghoti.fpchatx.chat; + +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.fpghoti.fpchatx.util.Util; + +import net.md_5.bungee.api.ChatColor; + +public class HexColor { + + private static final Pattern pattern = Pattern.compile("(? 1){ + return true; + }; + return ver >= 16; + } + + public static String formatHex(String message) { + if(!allowHex()) { + return message; + } + HashMap toReplace = new HashMap(); + Matcher matcher = pattern.matcher(message); + while (matcher.find()) { + String hex = message.substring(matcher.start(), matcher.end()); + if(isHexCode(hex)) { + toReplace.put(hex, "" + ChatColor.of(hex)); + } + } + for(String hex : toReplace.keySet()) { + message = message.replace(hex, toReplace.get(hex)); + } + return message; + } + + public static boolean isHexCode(String code) { + Pattern pattern = Pattern.compile("#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})"); + Matcher matcher = pattern.matcher(code); + return matcher.matches(); + } +} \ No newline at end of file diff --git a/src/main/java/com/fpghoti/fpchatx/chat/PrepareChat.java b/src/main/java/com/fpghoti/fpchatx/chat/PrepareChat.java index 79c0135..8d6504d 100644 --- a/src/main/java/com/fpghoti/fpchatx/chat/PrepareChat.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/PrepareChat.java @@ -1,12 +1,11 @@ package com.fpghoti.fpchatx.chat; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.entity.Player; import com.fpghoti.fpchatx.FPChat; -import com.fpghoti.fpchatx.customcodes.BubbleCode; -import com.fpghoti.fpchatx.customcodes.Codify; import com.fpghoti.fpchatx.permission.Permission; import com.fpghoti.fpchatx.player.FPlayer; import com.fpghoti.fpchatx.util.VaultUtil; @@ -20,7 +19,7 @@ public class PrepareChat { public static String swapPlaceholders(FPlayer p, String recipient, String msg, boolean in) { return swapPlaceholders(p,null,msg,recipient,in); } - + public static String swapPlaceholders(FPlayer p, String msg) { return swapPlaceholders(p,null,msg,"",false); } @@ -38,14 +37,14 @@ public class PrepareChat { String groupSuffix = ""; World world = sender.getWorld(); prefix = VaultUtil.chat.getPlayerPrefix(sender); - prefix = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(prefix)).replaceAll("&([0-9A-FK-OR])", "§$1").replace("&", "§"); + prefix = BubbleCode.bubblecode(Permission.canBubbleCode(p), ChatColor.translateAlternateColorCodes('&', prefix)); suffix = VaultUtil.chat.getPlayerSuffix(sender); - suffix = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(suffix)).replaceAll("&([0-9A-FK-OR])", "§$1").replace("&", "§"); + suffix = BubbleCode.bubblecode(Permission.canBubbleCode(p), ChatColor.translateAlternateColorCodes('&', suffix)); group = VaultUtil.permission.getPrimaryGroup(sender); groupPrefix = VaultUtil.chat.getGroupPrefix(world, group).replace("&", "§"); groupSuffix = VaultUtil.chat.getGroupSuffix(world, group).replace("&", "§"); String displayName = sender.getDisplayName(); - displayName = BubbleCode.bubblecode(Permission.canBubbleCode(p), Codify.changeFormatSign(displayName)).replaceAll("&([0-9A-FK-OR])", "§$1").replace("&", "§"); + displayName = BubbleCode.bubblecode(Permission.canBubbleCode(p), ChatColor.translateAlternateColorCodes('&', displayName)); String format = ""; if(channel != null) { if(Permission.isDistinguished(p)) { diff --git a/src/main/java/com/fpghoti/fpchatx/command/commands/BadgeListCommand.java b/src/main/java/com/fpghoti/fpchatx/command/commands/BadgeListCommand.java index 988738e..081e0e2 100644 --- a/src/main/java/com/fpghoti/fpchatx/command/commands/BadgeListCommand.java +++ b/src/main/java/com/fpghoti/fpchatx/command/commands/BadgeListCommand.java @@ -66,16 +66,18 @@ public class BadgeListCommand extends Commands { int index = (pg - 1) * 8 + i; if (index < list.getListSize()) { Badge badge = list.getIndex(index); - int id = badge.getId(); - String name = badge.getName(); - String contents = badge.getContents(); - String perm = badge.getPerm(); - String msg = ChatColor.DARK_AQUA + "ID: " + ChatColor.AQUA + id + ChatColor.DARK_AQUA + " - " + ChatColor.RESET + contents + - ChatColor.RESET + ChatColor.DARK_AQUA + " - " + ChatColor.WHITE + ChatColor.BOLD + name; - if(Permission.isAdmin(p)) { - msg = msg + " " + ChatColor.YELLOW + perm; + if(badge != null) { + int id = badge.getId(); + String name = badge.getName(); + String contents = badge.getContents(); + String perm = badge.getPerm(); + String msg = ChatColor.DARK_AQUA + "ID: " + ChatColor.AQUA + id + ChatColor.DARK_AQUA + " - " + ChatColor.RESET + contents + + ChatColor.RESET + ChatColor.DARK_AQUA + " - " + ChatColor.WHITE + ChatColor.BOLD + name; + if(Permission.isAdmin(p)) { + msg = msg + " " + ChatColor.YELLOW + perm; + } + FPlayer.plainMsg(p, msg); } - FPlayer.plainMsg(p, msg); } } diff --git a/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java b/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java index 22c16e1..372bd1e 100644 --- a/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java +++ b/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java @@ -16,14 +16,14 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.badge.BadgeData; +import com.fpghoti.fpchatx.chat.BubbleCode; import com.fpghoti.fpchatx.chat.ChatChannel; import com.fpghoti.fpchatx.chat.ChatFilter; +import com.fpghoti.fpchatx.chat.Codify; import com.fpghoti.fpchatx.chat.PrepareChat; import com.fpghoti.fpchatx.chat.StandardChannel; import com.fpghoti.fpchatx.chat.TempChannel; import com.fpghoti.fpchatx.config.PlayerFile; -import com.fpghoti.fpchatx.customcodes.BubbleCode; -import com.fpghoti.fpchatx.customcodes.Codify; import com.fpghoti.fpchatx.permission.Permission; import com.fpghoti.fpchatx.util.Util; import com.fpghoti.fpchatx.util.VaultUtil; @@ -550,7 +550,7 @@ public class FPlayer { } if(Permission.canPMColor(from)){ String last = ChatFilter.filter(filler + msg); - last = BubbleCode.bubblecode(Permission.canBubbleCode(from), Codify.changeFormatSign(last)); + last = BubbleCode.bubblecode(Permission.canBubbleCode(from), ChatColor.translateAlternateColorCodes('&', last)); finalMessage = stf + header + last; }else{ String newmsg = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('§', ChatFilter.filter(filler + msg))); @@ -584,7 +584,7 @@ public class FPlayer { } if(Permission.canPMColor(this)){ String last = ChatFilter.filter(filler + msg); - last = BubbleCode.bubblecode(Permission.canBubbleCode(this), Codify.changeFormatSign(last)); + last = BubbleCode.bubblecode(Permission.canBubbleCode(this), ChatColor.translateAlternateColorCodes('&', last)); finalMessage = stf + header + last; }else{ String newmsg = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('§', ChatFilter.filter(filler + msg))); diff --git a/src/main/java/com/fpghoti/fpchatx/util/Util.java b/src/main/java/com/fpghoti/fpchatx/util/Util.java index d127f7e..381d9c4 100644 --- a/src/main/java/com/fpghoti/fpchatx/util/Util.java +++ b/src/main/java/com/fpghoti/fpchatx/util/Util.java @@ -7,7 +7,12 @@ import org.bukkit.entity.Player; public class Util { - public Player playerGet(String uuid){ + public static String getGameVersion() { + String version = Bukkit.getVersion().split("MC: ")[1].replace(")", ""); + return version; + } + + public static Player playerGet(String uuid){ UUID id = UUID.fromString(uuid); Player p = Bukkit.getPlayer(id); return p; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 5daf0f7..548d823 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ version: ${project.version} main: com.fpghoti.fpchatx.FPChat description: Chat plugin for 1.12+ depend: [Vault] -loadbefore: [Essentials] +loadbefore: [Essentials,ChatManager] commands: fpc: aliases: [fpchat,ch]