Added support for uppercase characters in color and hex codes

This commit is contained in:
Aust1n46 2020-07-14 17:52:19 -05:00
parent 8feeba0a78
commit b0cf4d997d
3 changed files with 78 additions and 25 deletions

View File

@ -462,7 +462,7 @@ public class ChatListener implements Listener {
chat = curColor + chat;
}
String globalJSON = Format.convertToJson(mcp, format, chat);
String globalJSON = Format.convertToJson(mcp, format, chat);
String consoleChat = format + chat;
String message = consoleChat.replaceAll("(§([a-z0-9]))", "");
int hash = message.hashCode();

View File

@ -167,17 +167,17 @@ public class Format {
String ts = "";
char[] ch = s.toCharArray();
for(int a = 0; a < s.length() - 1; a ++) {
if(String.valueOf(ch[a + 1]).matches("[lkomn]") && ch[a] == '§') {
if(String.valueOf(ch[a + 1]).matches("[lkomnLKOMN]") && ch[a] == '§') {
ts += String.valueOf(ch[a]) + ch[a + 1];
a ++;
}
else if(String.valueOf(ch[a + 1]).matches("[0123456789abcdefr]") && ch[a] == '§') {
else if(String.valueOf(ch[a + 1]).matches("[0123456789abcdefrABCDEFR]") && ch[a] == '§') {
ts = String.valueOf(ch[a]) + ch[a + 1];
a ++;
}
else if(ch[a + 1] == 'x' && ch[a] == '§') {
if(ch.length > a + 13) {
if(String.valueOf(ch[a + 3]).matches("[0123456789abcdef]") && String.valueOf(ch[a + 5]).matches("[0123456789abcdef]") && String.valueOf(ch[a + 7]).matches("[0123456789abcdef]") && String.valueOf(ch[a + 9]).matches("[0123456789abcdef]") && String.valueOf(ch[a + 11]).matches("[0123456789abcdef]") && String.valueOf(ch[a + 13]).matches("[0123456789abcdef]") && ch[a + 2] == '§' && ch[a + 4] == '§' && ch[a + 6] == '§' && ch[a + 8] == '§' && ch[a + 10] == '§' && ch[a + 12] == '§') {
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 += 13;
}
@ -226,29 +226,35 @@ public class Format {
underlined = false;
}
}
else if(!color.matches("[0123456789abcdef]")) {
else if(!color.matches("[0123456789abcdefABCDEF]")) {
switch(color) {
case "l": {
case "l":
case "L": {
bold = true;
break;
}
case "k": {
case "k":
case "K": {
obfuscated = true;
break;
}
case "o": {
case "o":
case "O": {
italic = true;
break;
}
case "m": {
case "m":
case "M": {
strikethrough = true;
break;
}
case "n": {
case "n":
case "N": {
underlined = true;
break;
}
case "r": {
case "r":
case "R": {
bold = false;
obfuscated = false;
italic = false;
@ -308,15 +314,25 @@ public class Format {
case "7": return "gray";
case "8": return "dark_gray";
case "9": return "blue";
case "a": return "green";
case "b": return "aqua";
case "c": return "red";
case "d": return "light_purple";
case "e": return "yellow";
case "f": return "white";
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";
}
}
return c;
if(isValidHexColor(c)) {
return c;
}
return "white";
}
public static String convertPlainTextToJson(String s, boolean convertURL) {

View File

@ -1,11 +1,14 @@
package mineverse.Aust1n46.chat.utilities;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -53,7 +56,7 @@ public class FormatTest {
String result = Format.getLastCode(input);
Assert.assertEquals(expectedResult, result);
assertEquals(expectedResult, result);
}
@Test
@ -63,7 +66,7 @@ public class FormatTest {
String result = Format.getLastCode(input);
Assert.assertEquals(expectedResult, result);
assertEquals(expectedResult, result);
}
@Test
@ -73,7 +76,7 @@ public class FormatTest {
String result = Format.getLastCode(input);
Assert.assertEquals(expectedResult, result);
assertEquals(expectedResult, result);
}
@Test
@ -82,7 +85,7 @@ public class FormatTest {
String expectedResult = "I am an donut";
String result = Format.FilterChat(test);
Assert.assertEquals(expectedResult, result);
assertEquals(expectedResult, result);
}
@Test
@ -90,7 +93,7 @@ public class FormatTest {
String color = "red";
boolean result = Format.isValidColor(color);
Assert.assertTrue(result);
assertTrue(result);
}
@Test
@ -98,6 +101,40 @@ public class FormatTest {
String color = "randomString";
boolean result = Format.isValidColor(color);
Assert.assertFalse(result);
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 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 result = Format.convertHexColorCodeStringToBukkitColorCodeString(input);
assertEquals(expectedResult, result);
}
}