diff --git a/src/mineverse/Aust1n46/chat/utilities/Format.java b/src/mineverse/Aust1n46/chat/utilities/Format.java index a136deb..6d15ff8 100644 --- a/src/mineverse/Aust1n46/chat/utilities/Format.java +++ b/src/mineverse/Aust1n46/chat/utilities/Format.java @@ -1,6 +1,5 @@ package mineverse.Aust1n46.chat.utilities; -//import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; @@ -22,17 +21,24 @@ import mineverse.Aust1n46.chat.api.MineverseChatPlayer; import mineverse.Aust1n46.chat.json.JsonFormat; import mineverse.Aust1n46.chat.versions.VersionHandler; -//This class is where all formatting methods are stored. -public class Format { +/** + * Class containing chat formatting methods. + */ +public class Format { private static MineverseChat plugin = MineverseChat.getInstance(); - + public static final int LEGACY_COLOR_CODE_LENGTH = 2; 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 = "\u00A7"; + public static final char BUKKIT_COLOR_CODE_PREFIX_CHAR = '\u00A7'; public static final String BUKKIT_HEX_COLOR_CODE_PREFIX = "x"; - + + private static final Pattern LEGACY_CHAT_COLOR_DIGITS_PATTERN = Pattern.compile("&([0-9])"); + private static final Pattern LEGACY_CHAT_COLOR_PATTERN = Pattern.compile( + "(? a + 13) { - if(String.valueOf(ch[a + 3]).matches("[0123456789abcdefABCDEF]") && String.valueOf(ch[a + 5]).matches("[0123456789abcdefABCDEF]") && String.valueOf(ch[a + 7]).matches("[0123456789abcdefABCDEF]") && String.valueOf(ch[a + 9]).matches("[0123456789abcdefABCDEF]") && String.valueOf(ch[a + 11]).matches("[0123456789abcdefABCDEF]") && String.valueOf(ch[a + 13]).matches("[0123456789abcdefABCDEF]") && ch[a + 2] == '§' && ch[a + 4] == '§' && ch[a + 6] == '§' && ch[a + 8] == '§' && ch[a + 10] == '§' && ch[a + 12] == '§') { - ts = String.valueOf(ch[a]) + ch[a + 1] + ch[a + 2] + ch[a + 3] + ch[a + 4] + ch[a + 5] + ch[a + 6] + ch[a + 7] + ch[a + 8] + ch[a + 9] + ch[a + 10] + ch[a + 11] + ch[a + 12] + ch[a + 13]; + a++; + } else if (ch[a + 1] == 'x' && ch[a] == BUKKIT_COLOR_CODE_PREFIX_CHAR) { + if (ch.length > a + 13) { + if (String.valueOf(ch[a + 3]).matches("[0123456789abcdefABCDEF]") + && String.valueOf(ch[a + 5]).matches("[0123456789abcdefABCDEF]") + && String.valueOf(ch[a + 7]).matches("[0123456789abcdefABCDEF]") + && String.valueOf(ch[a + 9]).matches("[0123456789abcdefABCDEF]") + && String.valueOf(ch[a + 11]).matches("[0123456789abcdefABCDEF]") + && String.valueOf(ch[a + 13]).matches("[0123456789abcdefABCDEF]") + && ch[a + 2] == BUKKIT_COLOR_CODE_PREFIX_CHAR && ch[a + 4] == BUKKIT_COLOR_CODE_PREFIX_CHAR + && ch[a + 6] == BUKKIT_COLOR_CODE_PREFIX_CHAR && ch[a + 8] == BUKKIT_COLOR_CODE_PREFIX_CHAR + && ch[a + 10] == BUKKIT_COLOR_CODE_PREFIX_CHAR + && ch[a + 12] == BUKKIT_COLOR_CODE_PREFIX_CHAR) { + ts = String.valueOf(ch[a]) + ch[a + 1] + ch[a + 2] + ch[a + 3] + ch[a + 4] + ch[a + 5] + + ch[a + 6] + ch[a + 7] + ch[a + 8] + ch[a + 9] + ch[a + 10] + ch[a + 11] + ch[a + 12] + + ch[a + 13]; a += 13; } } @@ -187,11 +217,11 @@ public class Format { } return ts; } - + private static String convertToJsonColors(String s) { return convertToJsonColors(s, ""); } - + private static String convertToJsonColors(String s, String extensions) { String remaining = s; String temp = ""; @@ -207,18 +237,20 @@ public class Format { String previousColor = ""; int colorLength = LEGACY_COLOR_CODE_LENGTH; do { - if(remaining.length() < LEGACY_COLOR_CODE_LENGTH) { + if (remaining.length() < LEGACY_COLOR_CODE_LENGTH) { temp = "{\"text\":\"" + remaining + "\"},"; break; } modifier = ""; - indexColor = remaining.indexOf(BUKKIT_COLOR_CODE_PREFIX); - previousColor = color; - + indexColor = remaining.indexOf(BUKKIT_COLOR_CODE_PREFIX); + previousColor = color; + color = remaining.substring(1, indexColor + LEGACY_COLOR_CODE_LENGTH); - if(color.equals(BUKKIT_HEX_COLOR_CODE_PREFIX)) { - if(remaining.length() >= HEX_COLOR_CODE_LENGTH) { - color = HEX_COLOR_CODE_PREFIX + remaining.substring(LEGACY_COLOR_CODE_LENGTH, indexColor + HEX_COLOR_CODE_LENGTH).replace(BUKKIT_COLOR_CODE_PREFIX, ""); + if (color.equals(BUKKIT_HEX_COLOR_CODE_PREFIX)) { + if (remaining.length() >= HEX_COLOR_CODE_LENGTH) { + color = HEX_COLOR_CODE_PREFIX + + remaining.substring(LEGACY_COLOR_CODE_LENGTH, indexColor + HEX_COLOR_CODE_LENGTH) + .replace(BUKKIT_COLOR_CODE_PREFIX, ""); colorLength = HEX_COLOR_CODE_LENGTH; bold = false; obfuscated = false; @@ -226,133 +258,154 @@ public class Format { strikethrough = false; underlined = false; } - } - else if(!color.matches("[0123456789abcdefABCDEF]")) { - switch(color) { - case "l": - case "L": { - bold = true; - break; - } - case "k": - case "K": { - obfuscated = true; - break; - } - case "o": - case "O": { - italic = true; - break; - } - case "m": - case "M": { - strikethrough = true; - break; - } - case "n": - case "N": { - underlined = true; - break; - } - case "r": - case "R": { - bold = false; - obfuscated = false; - italic = false; - strikethrough = false; - underlined = false; - color = "f"; - break; - } + } else if (!color.matches("[0123456789abcdefABCDEF]")) { + switch (color) { + case "l": + case "L": { + bold = true; + break; } - if(!color.equals("f")) - color = previousColor; - if(color.length() == 0) + case "k": + case "K": { + obfuscated = true; + break; + } + case "o": + case "O": { + italic = true; + break; + } + case "m": + case "M": { + strikethrough = true; + break; + } + case "n": + case "N": { + underlined = true; + break; + } + case "r": + case "R": { + bold = false; + obfuscated = false; + italic = false; + strikethrough = false; + underlined = false; color = "f"; - } - else { + break; + } + } + if (!color.equals("f")) + color = previousColor; + if (color.length() == 0) + color = "f"; + } else { bold = false; obfuscated = false; italic = false; strikethrough = false; underlined = false; - } - if(bold) - modifier += ",\"bold\":\"true\""; - if(obfuscated) - modifier += ",\"obfuscated\":\"true\""; - if(italic) - modifier += ",\"italic\":\"true\""; - if(underlined) - modifier += ",\"underlined\":\"true\""; - if(strikethrough) - modifier += ",\"strikethrough\":\"true\""; + } + if (bold) + modifier += ",\"bold\":\"true\""; + if (obfuscated) + modifier += ",\"obfuscated\":\"true\""; + if (italic) + modifier += ",\"italic\":\"true\""; + if (underlined) + modifier += ",\"underlined\":\"true\""; + if (strikethrough) + modifier += ",\"strikethrough\":\"true\""; remaining = remaining.substring(colorLength); colorLength = LEGACY_COLOR_CODE_LENGTH; indexNextColor = remaining.indexOf(BUKKIT_COLOR_CODE_PREFIX); - if(indexNextColor == -1) { + if (indexNextColor == -1) { indexNextColor = remaining.length(); } - temp += "{\"text\":\"" + remaining.substring(0, indexNextColor) + "\",\"color\":\"" + hexidecimalToJsonColorRGB(color) + "\"" + modifier + extensions + "},"; + temp += "{\"text\":\"" + remaining.substring(0, indexNextColor) + "\",\"color\":\"" + + hexidecimalToJsonColorRGB(color) + "\"" + modifier + extensions + "},"; remaining = remaining.substring(indexNextColor); - } - while(remaining.length() > 1 && indexColor != -1); - if(temp.length() > 1) + } while (remaining.length() > 1 && indexColor != -1); + if (temp.length() > 1) temp = temp.substring(0, temp.length() - 1); return temp; } - + private static String hexidecimalToJsonColorRGB(String c) { - if(c.length() == 1) { - switch(c) { - case "0": return "black"; - case "1": return "dark_blue"; - case "2": return "dark_green"; - case "3": return "dark_aqua"; - case "4": return "dark_red"; - case "5": return "dark_purple"; - case "6": return "gold"; - case "7": return "gray"; - case "8": return "dark_gray"; - case "9": return "blue"; - case "a": - case "A": return "green"; - case "b": - case "B": return "aqua"; - case "c": - case "C": return "red"; - case "d": - case "D": return "light_purple"; - case "e": - case "E": return "yellow"; - case "f": - case "F": return "white"; - default: return "white"; + if (c.length() == 1) { + switch (c) { + case "0": + return "black"; + case "1": + return "dark_blue"; + case "2": + return "dark_green"; + case "3": + return "dark_aqua"; + case "4": + return "dark_red"; + case "5": + return "dark_purple"; + case "6": + return "gold"; + case "7": + return "gray"; + case "8": + return "dark_gray"; + case "9": + return "blue"; + case "a": + case "A": + return "green"; + case "b": + case "B": + return "aqua"; + case "c": + case "C": + return "red"; + case "d": + case "D": + return "light_purple"; + case "e": + case "E": + return "yellow"; + case "f": + case "F": + return "white"; + default: + return "white"; } } - if(isValidHexColor(c)) { + if (isValidHexColor(c)) { return c; } return "white"; } - + public static String convertPlainTextToJson(String s, boolean convertURL) { - if(convertURL) { + if (convertURL) { return "[" + Format.convertLinks(s) + "]"; - } - else { - return "[" + convertToJsonColors("§f" + s) + "]"; + } else { + return "[" + convertToJsonColors(DEFAULT_COLOR_CODE + s) + "]"; } } - + public static String formatModerationGUI(String json, Player player, String sender, String channelName, int hash) { - if(player.hasPermission("venturechat.gui")) { + if (player.hasPermission("venturechat.gui")) { json = json.substring(0, json.length() - 1); - json += "," + Format.convertToJsonColors(Format.FormatStringAll(plugin.getConfig().getString("guiicon")), ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/vchatgui " + sender + " " + channelName + " " + hash +"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[" + Format.convertToJsonColors(Format.FormatStringAll(plugin.getConfig().getString("guitext"))) + "]}}") + "]"; + json += "," + Format.convertToJsonColors(Format.FormatStringAll(plugin.getConfig().getString("guiicon")), + ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/vchatgui " + sender + " " + channelName + + " " + hash + + "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[" + + Format.convertToJsonColors( + Format.FormatStringAll(plugin.getConfig().getString("guitext"))) + + "]}}") + + "]"; } return json; } - + public static PacketContainer createPacketPlayOutChat(String json) { WrappedChatComponent component = WrappedChatComponent.fromJson(json); PacketContainer container = new PacketContainer(PacketType.Play.Server.CHAT); @@ -360,150 +413,173 @@ public class Format { container.getChatComponents().write(0, component); return container; } - + public static PacketContainer createPacketPlayOutChat(WrappedChatComponent component) { PacketContainer container = new PacketContainer(PacketType.Play.Server.CHAT); container.getModifier().writeDefaults(); container.getChatComponents().write(0, component); return container; } - + public static void sendPacketPlayOutChat(Player player, PacketContainer packet) { try { ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); - } - catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } } - - public static String toPlainText(Object o, Class c) { + + public static String toPlainText(Object o, Class c) { List finalList = new ArrayList<>(); StringBuilder stringbuilder = new StringBuilder(); try { splitComponents(finalList, o, c); - for(Object component : finalList) { - if(VersionHandler.is1_7_10()) { + for (Object component : finalList) { + if (VersionHandler.is1_7_10()) { stringbuilder.append((String) component.getClass().getMethod("e").invoke(component)); - } - else { + } else { stringbuilder.append((String) component.getClass().getMethod("getText").invoke(component)); } } - } - catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } -// if(plugin.getConfig().getString("loglevel", "info").equals("debug")) { -// System.out.println("my string"); -// System.out.println("my string"); -// System.out.println("my string"); -// System.out.println("my string"); -// System.out.println("my string"); -// System.out.println(stringbuilder.toString()); -// } + // if(plugin.getConfig().getString("loglevel", "info").equals("debug")) { + // System.out.println("my string"); + // System.out.println("my string"); + // System.out.println("my string"); + // System.out.println("my string"); + // System.out.println("my string"); + // System.out.println(stringbuilder.toString()); + // } return stringbuilder.toString(); } - + private static void splitComponents(List finalList, Object o, Class c) throws Exception { -// if(plugin.getConfig().getString("loglevel", "info").equals("debug")) { -// for(Method m : c.getMethods()) { -// System.out.println(m.getName()); -// } -// } - if(VersionHandler.is1_7() || VersionHandler.is1_8() || VersionHandler.is1_9() || VersionHandler.is1_10() || VersionHandler.is1_11() || VersionHandler.is1_12() || VersionHandler.is1_13() || (VersionHandler.is1_14() && !VersionHandler.is1_14_4())) { + // if(plugin.getConfig().getString("loglevel", "info").equals("debug")) { + // for(Method m : c.getMethods()) { + // System.out.println(m.getName()); + // } + // } + if (VersionHandler.is1_7() || VersionHandler.is1_8() || VersionHandler.is1_9() || VersionHandler.is1_10() + || VersionHandler.is1_11() || VersionHandler.is1_12() || VersionHandler.is1_13() + || (VersionHandler.is1_14() && !VersionHandler.is1_14_4())) { ArrayList list = (ArrayList) c.getMethod("a").invoke(o, new Object[0]); - for(Object component : list) { + for (Object component : list) { ArrayList innerList = (ArrayList) c.getMethod("a").invoke(component, new Object[0]); - if(innerList.size() > 0) { + if (innerList.size() > 0) { splitComponents(finalList, component, c); - } - else { + } else { finalList.add(component); } } - } - else { + } else { ArrayList list = (ArrayList) c.getMethod("getSiblings").invoke(o, new Object[0]); - for(Object component : list) { + for (Object component : list) { ArrayList innerList = (ArrayList) c.getMethod("getSiblings").invoke(component, new Object[0]); - if(innerList.size() > 0) { + if (innerList.size() > 0) { splitComponents(finalList, component, c); - } - else { + } else { finalList.add(component); } } } } - - protected static Pattern chatColorPattern = Pattern.compile("(?i)&([0-9])"); - protected static Pattern legacyChatFormatPattern = Pattern.compile("(? filters = plugin.getConfig().getStringList("filters"); - for(String s : filters) { + for (String s : filters) { t = 0; String[] pparse = new String[2]; pparse[0] = " "; pparse[1] = " "; StringTokenizer st = new StringTokenizer(s, ","); - while(st.hasMoreTokens()) { - if(t < 2) { + while (st.hasMoreTokens()) { + if (t < 2) { pparse[t++] = st.nextToken(); } } @@ -511,49 +587,50 @@ public class Format { } return msg; } - + public static boolean isValidColor(String color) { Boolean bFound = false; - for(ChatColor bkColors : ChatColor.values()) { - if(color.equalsIgnoreCase(bkColors.name())) { + for (ChatColor bkColors : ChatColor.values()) { + if (color.equalsIgnoreCase(bkColors.name())) { bFound = true; } } return bFound; } - + public static boolean isValidHexColor(String color) { Pattern pattern = Pattern.compile("(^#[0-9a-fA-F]{6}\\b)"); Matcher matcher = pattern.matcher(color); return matcher.find(); } - + public static String convertHexColorCodeToBukkitColorCode(String color) { StringBuilder bukkitColorCode = new StringBuilder(BUKKIT_COLOR_CODE_PREFIX + BUKKIT_HEX_COLOR_CODE_PREFIX); - for(int a = 1; a < color.length(); a++) { + for (int a = 1; a < color.length(); a++) { bukkitColorCode.append(BUKKIT_COLOR_CODE_PREFIX + color.charAt(a)); } return bukkitColorCode.toString().toLowerCase(); } - + public static String convertHexColorCodeStringToBukkitColorCodeString(String string) { Pattern pattern = Pattern.compile("(#[0-9a-fA-F]{6})"); Matcher matcher = pattern.matcher(string); - while(matcher.find()) { + while (matcher.find()) { int indexStart = matcher.start(); int indexEnd = matcher.end(); - String hexColor = string.substring(indexStart, indexEnd); + String hexColor = string.substring(indexStart, indexEnd); String bukkitColor = convertHexColorCodeToBukkitColorCode(hexColor); string = string.replaceAll(hexColor, bukkitColor); matcher.reset(string); } return string; } - + public static String escapeAllRegex(String input) { - return input.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(").replace(")", "\\)").replace("|", "\\|").replace("+", "\\+").replace("*", "\\*"); + return input.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(") + .replace(")", "\\)").replace("|", "\\|").replace("+", "\\+").replace("*", "\\*"); } - + public static boolean underlineURLs() { return plugin.getConfig().getBoolean("underlineurls", true); } diff --git a/test/mineverse/Aust1n46/chat/utilities/FormatTest.java b/test/mineverse/Aust1n46/chat/utilities/FormatTest.java index 7df6516..e7dfb25 100644 --- a/test/mineverse/Aust1n46/chat/utilities/FormatTest.java +++ b/test/mineverse/Aust1n46/chat/utilities/FormatTest.java @@ -20,29 +20,32 @@ import org.powermock.modules.junit4.PowerMockRunner; import mineverse.Aust1n46.chat.MineverseChat; +/** + * Tests {@link Format}. + */ @RunWith(PowerMockRunner.class) -@PrepareForTest( { MineverseChat.class }) +@PrepareForTest({ MineverseChat.class }) public class FormatTest { - + private MineverseChat mockPlugin; private FileConfiguration mockConfig; - + private List filters; - + @Before public void setUp() { - filters = new ArrayList(); - filters.add("ass,donut"); - + filters = new ArrayList(); + filters.add("ass,donut"); + mockPlugin = Mockito.mock(MineverseChat.class); mockConfig = Mockito.mock(FileConfiguration.class); - + PowerMockito.mockStatic(MineverseChat.class); PowerMockito.when(MineverseChat.getInstance()).thenReturn(mockPlugin); - Mockito.when(mockPlugin.getConfig()).thenReturn(mockConfig); - Mockito.when(mockConfig.getStringList("filters")).thenReturn(filters); + Mockito.when(mockPlugin.getConfig()).thenReturn(mockConfig); + Mockito.when(mockConfig.getStringList("filters")).thenReturn(filters); } - + @After public void tearDown() { mockPlugin = null; @@ -52,95 +55,111 @@ public class FormatTest { @Test public void testGetLastCodeSingleColor() { - String input = "§cHello"; - String expectedResult = "§c"; - + String input = BUKKIT_COLOR_CODE_PREFIX + "cHello"; + String expectedResult = BUKKIT_COLOR_CODE_PREFIX + "c"; + String result = Format.getLastCode(input); - + assertEquals(expectedResult, result); } - + @Test public void testGetLastCodeColorAfterFormat() { - String input = "§o§cHello"; - String expectedResult = "§c"; - + String input = BUKKIT_COLOR_CODE_PREFIX + "o" + BUKKIT_COLOR_CODE_PREFIX + "cHello"; + String expectedResult = BUKKIT_COLOR_CODE_PREFIX + "c"; + String result = Format.getLastCode(input); - + assertEquals(expectedResult, result); } - + @Test public void testGetLastCodeColorBeforeFormat() { - String input = "§c§oHello"; - String expectedResult = "§c§o"; - + String input = BUKKIT_COLOR_CODE_PREFIX + "c" + BUKKIT_COLOR_CODE_PREFIX + "oHello"; + String expectedResult = BUKKIT_COLOR_CODE_PREFIX + "c" + BUKKIT_COLOR_CODE_PREFIX + "o"; + String result = Format.getLastCode(input); - + assertEquals(expectedResult, result); } - + @Test - public void testFilterChat() { + public void testFilterChat() { String test = "I am an ass"; String expectedResult = "I am an donut"; - + String result = Format.FilterChat(test); assertEquals(expectedResult, result); } - + @Test - public void testIsValidColor() { + public void testIsValidColor() { String color = "red"; - + boolean result = Format.isValidColor(color); assertTrue(result); } - + @Test public void testIsInvalidColor() { String color = "randomString"; - + boolean result = Format.isValidColor(color); assertFalse(result); } - + @Test public void testIsValidHexColor() { String hexColor = "#ff00ff"; - + boolean result = Format.isValidHexColor(hexColor); assertTrue(result); } - + @Test public void testIsInvalidHexColor() { String hexColor = "#random"; - + boolean result = Format.isValidHexColor(hexColor); assertFalse(result); } - + @Test public void testConvertHexColorCodeToBukkitColorCode() { String hexColor = "#ff00ff"; - String expectedResult = "§x§f§f§0§0§f§f"; - + String expectedResult = BUKKIT_COLOR_CODE_PREFIX + "x" + BUKKIT_COLOR_CODE_PREFIX + "f" + + BUKKIT_COLOR_CODE_PREFIX + "f" + BUKKIT_COLOR_CODE_PREFIX + "0" + BUKKIT_COLOR_CODE_PREFIX + "0" + + BUKKIT_COLOR_CODE_PREFIX + "f" + BUKKIT_COLOR_CODE_PREFIX + "f"; + String result = Format.convertHexColorCodeToBukkitColorCode(hexColor); assertEquals(expectedResult, result); } - + @Test public void testConvertHexColorCodeStringToBukkitColorCodeString() { - String input = "#ff00ffHello§cThere#00ff00Austin"; - String expectedResult = "§x§f§f§0§0§f§fHello§cThere§x§0§0§f§f§0§0Austin"; - + String input = "#ff00ffHello" + BUKKIT_COLOR_CODE_PREFIX + "cThere#00ff00Austin"; + String expectedResult = BUKKIT_COLOR_CODE_PREFIX + "x" + BUKKIT_COLOR_CODE_PREFIX + "f" + + BUKKIT_COLOR_CODE_PREFIX + "f" + BUKKIT_COLOR_CODE_PREFIX + "0" + BUKKIT_COLOR_CODE_PREFIX + "0" + + BUKKIT_COLOR_CODE_PREFIX + "f" + BUKKIT_COLOR_CODE_PREFIX + "fHello" + BUKKIT_COLOR_CODE_PREFIX + + "cThere" + BUKKIT_COLOR_CODE_PREFIX + "x" + BUKKIT_COLOR_CODE_PREFIX + "0" + BUKKIT_COLOR_CODE_PREFIX + + "0" + BUKKIT_COLOR_CODE_PREFIX + "f" + BUKKIT_COLOR_CODE_PREFIX + "f" + BUKKIT_COLOR_CODE_PREFIX + "0" + + BUKKIT_COLOR_CODE_PREFIX + "0Austin"; + String result = Format.convertHexColorCodeStringToBukkitColorCodeString(input); assertEquals(expectedResult, result); } - + @Test - public void testConvertLegacyColorCodeStringToBukkitColorCodeString() { + public void testFormatStringLegacyColor_NoColorCode() { + String input = "Hello There Austin"; + String expectedResult = "Hello There Austin"; + + String result = Format.FormatStringLegacyColor(input); + assertEquals(expectedResult, result); + } + + @Test + public void testFormatStringLegacyColor_LegacyCodeOnly() { String input = "Hello &cThere Austin"; String expectedResult = "Hello " + BUKKIT_COLOR_CODE_PREFIX + "cThere Austin"; @@ -149,11 +168,20 @@ public class FormatTest { } @Test - public void testRemoveSpigoteHexColorCodeFromStringWithLegacyFormatting() { + public void testFormatStringLegacyColor_SpigotHexCodeOnly() { 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); } + + @Test + public void testFormatStringLegacyColor_BothColorCodes() { + String input = "&x&f&f&f&f&f&f&cHello There Austin"; + String expectedResult = "&x&f&f&f&f&f&f" + BUKKIT_COLOR_CODE_PREFIX + "cHello There Austin"; + + String result = Format.FormatStringLegacyColor(input); + assertEquals(expectedResult, result); + } }