Tweaked implementation from PR from https://bitbucket.org/Rodentman87/

This commit is contained in:
Aust1n46 2020-08-28 17:32:02 -05:00
parent 1e29fe0300
commit d860c1553e
9 changed files with 74 additions and 4 deletions

View File

@ -27,6 +27,9 @@ public class Me extends MineverseCommand {
if(sender instanceof Player && MineverseChatAPI.getMineverseChatPlayer((Player) sender).hasFilter()) {
msg = Format.FilterChat(msg);
}
if(sender.hasPermission("venturechat.color.legacy")) {
msg = Format.FormatStringLegacyColor(msg);
}
if(sender.hasPermission("venturechat.color"))
msg = Format.FormatStringColor(msg);
if(sender.hasPermission("venturechat.format"))

View File

@ -285,6 +285,9 @@ public class Party extends MineverseCommand {
if(mcp.hasFilter()) {
msg = Format.FilterChat(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
msg = Format.FormatStringLegacyColor(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
msg = Format.FormatStringColor(msg);
}

View File

@ -55,6 +55,9 @@ public class Message extends MineverseCommand {
if(mcp.hasFilter()) {
msg = Format.FilterChat(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
msg = Format.FormatStringLegacyColor(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
msg = Format.FormatStringColor(msg);
}
@ -122,6 +125,9 @@ public class Message extends MineverseCommand {
if(mcp.hasFilter()) {
msg = Format.FilterChat(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
msg = Format.FormatStringLegacyColor(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
msg = Format.FormatStringColor(msg);
}

View File

@ -93,6 +93,9 @@ public class Reply extends MineverseCommand {
if(mcp.hasFilter()) {
msg = Format.FilterChat(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
msg = Format.FormatStringLegacyColor(msg);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
msg = Format.FormatStringColor(msg);
}

View File

@ -113,6 +113,9 @@ public class ChatListener implements Listener {
if(mcp.hasFilter()) {
filtered = Format.FilterChat(filtered);
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
filtered = Format.FormatStringLegacyColor(filtered);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
filtered = Format.FormatStringColor(filtered);
}
@ -177,6 +180,9 @@ public class ChatListener implements Listener {
if(mcp.hasFilter()) {
filtered = Format.FilterChat(filtered);
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
filtered = Format.FormatStringLegacyColor(filtered);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
filtered = Format.FormatStringColor(filtered);
}
@ -463,6 +469,9 @@ public class ChatListener implements Listener {
}
}
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
chat = Format.FormatStringLegacyColor(chat);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
chat = Format.FormatStringColor(chat);
}

View File

@ -127,8 +127,15 @@ public class CommandListener implements CommandExecutor, Listener {
}
if(send.length() > 0) send = send.substring(1);
s = Format.FormatStringAll(s);
if(mcp.getPlayer().hasPermission("venturechat.color")) send = Format.FormatStringColor(send);
if(mcp.getPlayer().hasPermission("venturechat.format")) send = Format.FormatString(send);
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
send = Format.FormatStringLegacyColor(send);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
send = Format.FormatStringColor(send);
}
if(mcp.getPlayer().hasPermission("venturechat.format")) {
send = Format.FormatString(send);
}
if(s.startsWith("Command:")) {
mcp.getPlayer().chat(s.substring(9).replace("$", send));
event.setCancelled(true);

View File

@ -19,6 +19,9 @@ public class SignListener implements Listener {
MineverseChatPlayer mcp = MineverseChatAPI.getMineverseChatPlayer(event.getPlayer());
for(int a = 0; a < event.getLines().length; a++) {
String line = event.getLine(a);
if(mcp.getPlayer().hasPermission("venturechat.color.legacy")) {
line = Format.FormatStringLegacyColor(line);
}
if(mcp.getPlayer().hasPermission("venturechat.color")) {
line = Format.FormatStringColor(line);
}

View File

@ -30,7 +30,7 @@ public class Format {
public static final int HEX_COLOR_CODE_LENGTH = 14;
public static final String DEFAULT_COLOR_CODE = String.valueOf(ChatColor.WHITE);
public static final String HEX_COLOR_CODE_PREFIX = "#";
public static final String BUKKIT_COLOR_CODE_PREFIX = "§";
public static final String BUKKIT_COLOR_CODE_PREFIX = "\u00A7";
public static final String BUKKIT_HEX_COLOR_CODE_PREFIX = "x";
public static String convertToJson(MineverseChatPlayer sender, String format, String chat) {
@ -438,6 +438,7 @@ public class Format {
}
protected static Pattern chatColorPattern = Pattern.compile("(?i)&([0-9])");
protected static Pattern legacyChatFormatPattern = Pattern.compile("(?<!(&x(&[a-fA-F0-9]){5}))(?<!(&x(&[a-fA-F0-9]){4}))(?<!(&x(&[a-fA-F0-9]){3}))(?<!(&x(&[a-fA-F0-9]){2}))(?<!(&x(&[a-fA-F0-9]){1}))(?<!(&x))(&)([0-9a-fA-F])");
public static String FormatStringColor(String string) {
String allFormated = string;
@ -456,6 +457,22 @@ public class Format {
allFormated = convertHexColorCodeStringToBukkitColorCodeString(allFormated);
return allFormated;
}
public static String FormatStringLegacyColor(String string) {
String allFormated = string;
allFormated = legacyChatFormatPattern.matcher(allFormated).replaceAll("\u00A7$13");
allFormated = allFormated.replaceAll(BUKKIT_COLOR_CODE_PREFIX + "[aA]", BUKKIT_COLOR_CODE_PREFIX + "a");
allFormated = allFormated.replaceAll(BUKKIT_COLOR_CODE_PREFIX + "[bB]", BUKKIT_COLOR_CODE_PREFIX + "b");
allFormated = allFormated.replaceAll(BUKKIT_COLOR_CODE_PREFIX + "[cC]", BUKKIT_COLOR_CODE_PREFIX + "c");
allFormated = allFormated.replaceAll(BUKKIT_COLOR_CODE_PREFIX + "[dD]", BUKKIT_COLOR_CODE_PREFIX + "d");
allFormated = allFormated.replaceAll(BUKKIT_COLOR_CODE_PREFIX + "[eE]", BUKKIT_COLOR_CODE_PREFIX + "e");
allFormated = allFormated.replaceAll(BUKKIT_COLOR_CODE_PREFIX + "[fF]", BUKKIT_COLOR_CODE_PREFIX + "f");
allFormated = allFormated.replaceAll("%", "\\%");
return allFormated;
}
public static String FormatString(String string) {
String allFormated = string;
@ -544,4 +561,4 @@ public class Format {
public static int currentTimeMillis() {
return (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
}
}
}

View File

@ -1,5 +1,6 @@
package mineverse.Aust1n46.chat.utilities;
import static mineverse.Aust1n46.chat.utilities.Format.BUKKIT_COLOR_CODE_PREFIX;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -137,4 +138,22 @@ public class FormatTest {
String result = Format.convertHexColorCodeStringToBukkitColorCodeString(input);
assertEquals(expectedResult, result);
}
@Test
public void testConvertLegacyColorCodeStringToBukkitColorCodeString() {
String input = "Hello &cThere Austin";
String expectedResult = "Hello " + BUKKIT_COLOR_CODE_PREFIX + "cThere Austin";
String result = Format.FormatStringLegacyColor(input);
assertEquals(expectedResult, result);
}
@Test
public void testRemoveSpigoteHexColorCodeFromStringWithLegacyFormatting() {
String input = "&x&f&f&f&f&f&fHello There Austin";
String expectedResult = "&x&f&f&f&f&f&fHello There Austin";
String result = Format.FormatStringLegacyColor(input);
assertEquals(expectedResult, result);
}
}