diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java index 9b62ed436f..9193020f8d 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java @@ -40,7 +40,7 @@ public final class CraftChatMessage { } private static class StringMessage { - private static final Pattern INCREMENTAL_PATTERN = Pattern.compile("(" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + "[0-9a-fk-or])|(\\n)|((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))", Pattern.CASE_INSENSITIVE); + private static final Pattern INCREMENTAL_PATTERN = Pattern.compile("(" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + "[0-9a-fk-or])|(\\n)|(?i)((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))"); private final List list = new ArrayList(); private IChatBaseComponent currentChatComponent = new ChatComponentText(""); diff --git a/src/test/java/org/bukkit/ChatTest.java b/src/test/java/org/bukkit/ChatTest.java index 444ce6eb89..41c4b04599 100644 --- a/src/test/java/org/bukkit/ChatTest.java +++ b/src/test/java/org/bukkit/ChatTest.java @@ -1,10 +1,13 @@ package org.bukkit; import net.minecraft.server.EnumChatFormat; +import net.minecraft.server.IChatBaseComponent; import org.bukkit.craftbukkit.util.CraftChatMessage; import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class ChatTest { @Test @@ -19,4 +22,28 @@ public class ChatTest { Assert.assertEquals(format, CraftChatMessage.getColor(CraftChatMessage.getColor(format))); } } + + @Test + public void testColorConversion() { + String test = String.format("%1$sA%1$sa%1$sB%1$sb%1$sC%1$sc%1$sD%1$sd%1$sE%1$se%1$sZ%1$sz%1$s", ChatColor.COLOR_CHAR); + IChatBaseComponent name = CraftChatMessage.fromStringOrNull(test); + assertEquals(test.replace(String.valueOf(ChatColor.COLOR_CHAR), ""), + CraftChatMessage.fromComponent(name).replace(String.valueOf(ChatColor.COLOR_CHAR), "")); + } + + @Test + public void testURLJsonConversion() { + IChatBaseComponent[] components; + components = CraftChatMessage.fromString("https://spigotmc.org/test Test Message"); + assertEquals("TextComponent{text='', siblings=[TextComponent{text='https://spigotmc.org/test', siblings=[], style=Style{hasParent=true, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=ClickEvent{action=OPEN_URL, value='https://spigotmc.org/test'}, hoverEvent=null, insertion=null}}, TextComponent{text=' Test Message', siblings=[], style=Style{hasParent=true, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}], style=Style{hasParent=false, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}", + components[0].toString()); + + components = CraftChatMessage.fromString("123 " + ChatColor.GOLD + "https://spigotmc.org " + ChatColor.BOLD + "test"); + assertEquals("TextComponent{text='', siblings=[TextComponent{text='123 ', siblings=[], style=Style{hasParent=true, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}, TextComponent{text='https://spigotmc.org', siblings=[], style=Style{hasParent=true, color=§6, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=ClickEvent{action=OPEN_URL, value='https://spigotmc.org'}, hoverEvent=null, insertion=null}}, TextComponent{text=' ', siblings=[], style=Style{hasParent=true, color=§6, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}, TextComponent{text='test', siblings=[], style=Style{hasParent=true, color=§6, bold=true, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}], style=Style{hasParent=false, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}", + components[0].toString()); + + components = CraftChatMessage.fromString("multiCase http://SpigotMC.ORg/SpOngeBobMeEMeGoESHeRE"); + assertEquals("TextComponent{text='', siblings=[TextComponent{text='multiCase ', siblings=[], style=Style{hasParent=true, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}, TextComponent{text='http://SpigotMC.ORg/SpOngeBobMeEMeGoESHeRE', siblings=[], style=Style{hasParent=true, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=ClickEvent{action=OPEN_URL, value='http://SpigotMC.ORg/SpOngeBobMeEMeGoESHeRE'}, hoverEvent=null, insertion=null}}], style=Style{hasParent=false, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null, insertion=null}}", + components[0].toString()); + } }