From 08640f404d5a850bf0e24a0a6e9f3234def75180 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 17 Dec 2016 00:37:04 +0100 Subject: [PATCH 1/9] Started working on the chat formatter --- .../buttondevteam/chat/ChatProcessing.java | 13 ++- .../chat/formatting/ChatFormatter.java | 100 ++---------------- .../chat/formatting/FormattedSection.java | 9 +- 3 files changed, 21 insertions(+), 101 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 4ff2106..3d03c9c 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -33,15 +33,15 @@ import buttondevteam.chat.listener.PlayerListener; import buttondevteam.lib.chat.*; public class ChatProcessing { - private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\([\\*\\_\\\\])"); + private static final Pattern NULL_MENTION_PATTERN = Pattern.compile("null"); + private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\"); private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console")); private static final Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+)"); private static final Pattern URL_PATTERN = Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)"); private static final Pattern ENTIRE_MESSAGE_PATTERN = Pattern.compile(".+"); - private static final Pattern UNDERLINED_PATTERN = Pattern.compile("(? { diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 0594c93..7e52bc6 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -19,9 +19,10 @@ public final class ChatFormatter { private Priority priority; private short removecharcount = 0; private short removecharpos = -1; + private boolean isrange; public ChatFormatter(Pattern regex, Format format, Color color, Function onmatch, String openlink, - Priority priority, short removecharcount, short removecharpos) { + Priority priority, short removecharcount, short removecharpos, boolean isrange) { this.regex = regex; this.format = format; this.color = color; @@ -30,6 +31,7 @@ public final class ChatFormatter { this.priority = Priority.High; this.removecharcount = removecharcount; this.removecharpos = removecharpos; + this.isrange = isrange; } public static void Combine(List formatters, String str, TellrawPart tp) { @@ -48,7 +50,8 @@ public final class ChatFormatter { if (groups.size() > 0) DebugCommand.SendDebugMessage("First group: " + groups.get(0)); FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups, - formatter.removecharcount, formatter.removecharcount, formatter.removecharpos); + formatter.removecharcount, formatter.removecharcount, formatter.removecharpos, + formatter.isrange); sections.add(section); } } @@ -58,94 +61,9 @@ public final class ChatFormatter { else return Integer.compare(s1.Start, s2.Start); }); - boolean cont = true; - boolean found = false; - for (int i = 1; cont;) { - int nextindex = i + 1; - if (sections.size() < 2) - break; - DebugCommand.SendDebugMessage("i: " + i); - FormattedSection firstSection = sections.get(i - 1); - DebugCommand.SendDebugMessage("Combining sections " + firstSection + " and " + sections.get(i)); - if (firstSection.Start == sections.get(i).Start && firstSection.End == sections.get(i).End) { - firstSection.Formatters.addAll(sections.get(i).Formatters); - firstSection.Matches.addAll(sections.get(i).Matches); - if (firstSection.RemCharFromStart < sections.get(i).RemCharFromStart) - firstSection.RemCharFromStart = sections.get(i).RemCharFromStart; - if (firstSection.RemCharFromEnd < sections.get(i).RemCharFromEnd) - firstSection.RemCharFromEnd = sections.get(i).RemCharFromEnd; - firstSection.RemCharPos.addAll(sections.get(i).RemCharPos); - DebugCommand.SendDebugMessage("To section " + firstSection); - sections.remove(i); - found = true; - } else if (firstSection.End > sections.get(i).Start && firstSection.Start < sections.get(i).End) { - int origend = firstSection.End; - firstSection.End = sections.get(i).Start - 1; - int origend2 = sections.get(i).End; - boolean switchends; - if (switchends = origend2 < origend) { - int tmp = origend; - origend = origend2; - origend2 = tmp; - } - FormattedSection section = new FormattedSection(firstSection.Formatters, sections.get(i).Start, origend, - firstSection.Matches, sections.get(i).RemCharFromStart, firstSection.RemCharFromEnd, - Collections.emptyList()); - section.Formatters.addAll(sections.get(i).Formatters); - section.Matches.addAll(sections.get(i).Matches); // TODO: Clean - sections.add(i, section); - nextindex++; - FormattedSection thirdFormattedSection = sections.get(i + 1); - if (switchends) { // Use the properties of the first section not the second one - thirdFormattedSection.Formatters.clear(); - thirdFormattedSection.Formatters.addAll(firstSection.Formatters); - thirdFormattedSection.Matches.clear(); - thirdFormattedSection.Matches.addAll(firstSection.Matches); - short remchar = section.RemCharFromEnd; - section.RemCharFromEnd = thirdFormattedSection.RemCharFromEnd; - thirdFormattedSection.RemCharFromEnd = remchar; - } - firstSection.RemCharFromEnd = 0; - thirdFormattedSection.RemCharFromStart = 0; - thirdFormattedSection.Start = origend + 1; - thirdFormattedSection.End = origend2; - for (int x = 0; x < firstSection.RemCharPos.size(); x++) { - if (firstSection.RemCharPos.get(x) > firstSection.End) { - if (firstSection.RemCharPos.get(x) > section.End) - thirdFormattedSection.RemCharPos.add( - firstSection.RemCharPos.get(x) - thirdFormattedSection.Start + firstSection.Start); - else - section.RemCharPos.add(firstSection.RemCharPos.get(x) - section.Start + firstSection.Start); - firstSection.RemCharPos.remove(x--); - } - } - DebugCommand.SendDebugMessage("To sections 1:" + firstSection + ""); - DebugCommand.SendDebugMessage(" 2:" + section + ""); - DebugCommand.SendDebugMessage(" 3:" + thirdFormattedSection); - found = true; - } - for (int j = i - 1; j <= i + 1; j++) { - if (j < sections.size() && sections.get(j).End < sections.get(j).Start) { - DebugCommand.SendDebugMessage("Removing section: " + sections.get(j)); - sections.remove(j); - found = true; - } - } - i = nextindex - 1; - i++; - if (i >= sections.size()) { - if (found) { - i = 1; - found = false; - sections.sort((s1, s2) -> { - if (s1.Start == s2.Start) - return Integer.compare(s1.End, s2.End); - else - return Integer.compare(s1.Start, s2.Start); - }); - } else - cont = false; - } + List combined = new ArrayList<>(); + for (int i = 0; i < sections.size(); i++) { + // Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true } for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i); @@ -169,7 +87,7 @@ public final class ChatFormatter { if (formatter.color != null) color = formatter.color; if (formatter.format != null) - format = formatter.format.getFlag(); //TODO: Fix + format = formatter.format.getFlag(); // TODO: Fix if (formatter.openlink != null) openlink = formatter.openlink; } diff --git a/src/main/java/buttondevteam/chat/formatting/FormattedSection.java b/src/main/java/buttondevteam/chat/formatting/FormattedSection.java index 4d2f209..2891d21 100644 --- a/src/main/java/buttondevteam/chat/formatting/FormattedSection.java +++ b/src/main/java/buttondevteam/chat/formatting/FormattedSection.java @@ -11,9 +11,10 @@ class FormattedSection { short RemCharFromStart; short RemCharFromEnd; ArrayList RemCharPos = new ArrayList(); + boolean IsRange; FormattedSection(ChatFormatter formatter, int start, int end, ArrayList matches, short remcharfromstart, - short remcharfromend, int remcharpos) { + short remcharfromend, int remcharpos, boolean isrange) { Start = start; End = end; Formatters.add(formatter); @@ -21,10 +22,11 @@ class FormattedSection { RemCharFromStart = remcharfromstart; RemCharFromEnd = remcharfromend; RemCharPos.add(remcharpos); + IsRange = isrange; } FormattedSection(Collection formatters, int start, int end, ArrayList matches, - short remcharfromstart, short remcharfromend, Collection remcharpos) { + short remcharfromstart, short remcharfromend, Collection remcharpos, boolean isrange) { Start = start; End = end; Formatters.addAll(formatters); @@ -32,6 +34,7 @@ class FormattedSection { RemCharFromStart = remcharfromstart; RemCharFromEnd = remcharfromend; RemCharPos.addAll(remcharpos); + IsRange = isrange; } @Override @@ -39,6 +42,6 @@ class FormattedSection { return new StringBuilder("Section(").append(Start).append(", ").append(End).append(", formatters: ") .append(Formatters.toString()).append(", matches: ").append(Matches.toString()).append(", RemChars: ") .append(RemCharFromStart).append(", ").append(RemCharFromEnd).append(", ").append(RemCharPos) - .append(")").toString(); + .append(", ").append(IsRange).append(")").toString(); } } \ No newline at end of file -- 2.30.2 From f39ed23699d855ab07591433b5fe021feff3a973 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 18 Dec 2016 13:33:04 +0100 Subject: [PATCH 2/9] Started formatter and escape logic --- .../buttondevteam/chat/ChatProcessing.java | 69 ++++++++++--------- .../chat/formatting/ChatFormatter.java | 15 ++++ 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 3d03c9c..3bdbd5b 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -2,6 +2,7 @@ package buttondevteam.chat; import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.regex.Pattern; import org.bukkit.Bukkit; @@ -46,6 +47,39 @@ public class ChatProcessing { "dark_purple" }; // TODO private static boolean pingedconsole = false; + private static ArrayList commonFormatters = new ArrayList<>(); + + public static final ChatFormatter ESCAPE_FORMATTER = new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN) + .setRemoveCharPos((short) 0).build(); + + private ChatProcessing() { + } + + static { + commonFormatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(Format.Bold) + .setRemoveCharCount((short) 2).build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(Format.Italic) + .setRemoveCharCount((short) 1).build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setFormat(Format.Underlined) + .setRemoveCharCount((short) 1).build()); + commonFormatters.add(ESCAPE_FORMATTER); + // URLs + Rainbow text + commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined) + .setOpenlink("$1").build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature + commonFormatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) + .setOnmatch((String match) -> { + if (!pingedconsole) { + System.out.print("\007"); + pingedconsole = true; // Will set it to false in ProcessChat + } + return match; + }).setPriority(Priority.High).build()); + + commonFormatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue) + .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build()); + } + // Returns e.setCancelled public static boolean ProcessChat(Channel channel, CommandSender sender, String message) { long processstart = System.nanoTime(); @@ -84,7 +118,8 @@ public class ChatProcessing { } Channel currentchannel = channel; - ArrayList formatters = new ArrayList(); + @SuppressWarnings("unchecked") + ArrayList formatters = (ArrayList) commonFormatters.clone(); Color colormode = currentchannel.color; if (mp != null && mp.OtherColorMode != null) @@ -102,17 +137,6 @@ public class ChatProcessing { String suggestmsg = formattedmessage; - formatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(Format.Bold) - .setRemoveCharCount((short) 2).build()); - formatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(Format.Italic) - .setRemoveCharCount((short) 1).build()); - formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setFormat(Format.Underlined) - .setRemoveCharCount((short) 1).build()); - formatters.add(new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN).setRemoveCharPos((short) 0).build()); - - // URLs + Rainbow text - formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined).setOpenlink("$1") - .build()); if (PluginMain.GetPlayers().size() > 0) { StringBuilder namesb = new StringBuilder(); namesb.append("(?i)("); @@ -130,8 +154,6 @@ public class ChatProcessing { nicksb.deleteCharAt(nicksb.length() - 1); nicksb.append(")"); - formatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(namesb.toString())).setColor(Color.Aqua) .setOnmatch((String match) -> { Player p = Bukkit.getPlayer(match); @@ -172,24 +194,7 @@ public class ChatProcessing { }).setPriority(Priority.High).build()); } - pingedconsole = false; - formatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) - .setOnmatch((String match) -> { - if (!pingedconsole) { - System.out.print("\007"); - pingedconsole = true; - } - return match; - }).setPriority(Priority.High).build()); - - formatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue) - .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build()); - - /* - * if (!hadurls) { if (formattedmessage.matches("(?i).*" + Pattern.quote("@console") + ".*")) { formattedmessage = formattedmessage.replaceAll( "(?i)" + Pattern.quote("@console"), - * "§b@console§r"); formattedmessage = formattedmessage .replaceAll( "(?i)" + Pattern.quote("@console"), String.format( - * "\",\"color\":\"%s\"},{\"text\":\"§b@console§r\",\"color\":\"blue\"},{\"text\":\"" , colormode)); System.out.println("\007"); } } - */ + pingedconsole = false; // Will set it to true onmatch (static constructor) TellrawPart json = new TellrawPart(""); if (mp != null && mp.ChatOnly) { diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 7e52bc6..37d2196 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -2,11 +2,14 @@ package buttondevteam.chat.formatting; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; +import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.lib.chat.*; @@ -62,8 +65,20 @@ public final class ChatFormatter { return Integer.compare(s1.Start, s2.Start); }); List combined = new ArrayList<>(); + Map nextSection = new HashMap<>(); + boolean escaped = false; for (int i = 0; i < sections.size(); i++) { // Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true + final FormattedSection section = sections.get(i); + if (!section.IsRange) { + escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER); + continue; // TODO: Escape \ and check and fix escape logic + } + if (nextSection.containsKey(section.Formatters.get(0)) && !escaped) { + FormattedSection s = nextSection.remove(section.Formatters.get(0)); + s.End = section.Start; + combined.add(s); + } } for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i); -- 2.30.2 From 3ab97276f6799098c57e77635a03aa4bc4d15419 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 19 Dec 2016 22:33:11 +0100 Subject: [PATCH 3/9] Finished IsRange --- .../java/buttondevteam/chat/ChatProcessing.java | 15 +++++---------- .../chat/formatting/ChatFormatterBuilder.java | 13 ++++++++++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 3bdbd5b..2c18920 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -2,7 +2,6 @@ package buttondevteam.chat; import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.regex.Pattern; import org.bukkit.Bukkit; @@ -21,11 +20,7 @@ import com.palmergames.bukkit.towny.object.Town; import buttondevteam.chat.commands.UnlolCommand; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; -import buttondevteam.chat.formatting.ChatFormatter; -import buttondevteam.chat.formatting.ChatFormatterBuilder; -import buttondevteam.chat.formatting.TellrawEvent; -import buttondevteam.chat.formatting.TellrawPart; -import buttondevteam.chat.formatting.TellrawSerializer; +import buttondevteam.chat.formatting.*; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.chat.Channel; @@ -57,15 +52,15 @@ public class ChatProcessing { static { commonFormatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(Format.Bold) - .setRemoveCharCount((short) 2).build()); + .setRemoveCharCount((short) 2).setRange(true).build()); commonFormatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(Format.Italic) - .setRemoveCharCount((short) 1).build()); + .setRemoveCharCount((short) 1).setRange(true).build()); commonFormatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setFormat(Format.Underlined) - .setRemoveCharCount((short) 1).build()); + .setRemoveCharCount((short) 1).setRange(true).build()); commonFormatters.add(ESCAPE_FORMATTER); // URLs + Rainbow text commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined) - .setOpenlink("$1").build()); + .setOpenlink("$1").setRange(true).build()); commonFormatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature commonFormatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) .setOnmatch((String match) -> { diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java index 2277b10..a835d7f 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java @@ -14,9 +14,11 @@ public class ChatFormatterBuilder { private Priority priority; private short removecharcount = 0; private short removecharpos = -1; + private boolean range = false; public ChatFormatter build() { - return new ChatFormatter(regex, format, color, onmatch, openlink, priority, removecharcount, removecharpos); + return new ChatFormatter(regex, format, color, onmatch, openlink, priority, removecharcount, removecharpos, + range); } public Pattern getRegex() { @@ -100,4 +102,13 @@ public class ChatFormatterBuilder { this.removecharpos = removecharpos; return this; } + + public boolean isRange() { + return range; + } + + public ChatFormatterBuilder setRange(boolean range) { + this.range = range; + return this; + } } -- 2.30.2 From 94252e446dba38b92e26605fe62bd74fbcc6482b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 27 Dec 2016 22:42:21 +0100 Subject: [PATCH 4/9] Progressing... Slowly... --- .../buttondevteam/chat/formatting/ChatFormatter.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 37d2196..6c50f77 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -64,7 +64,7 @@ public final class ChatFormatter { else return Integer.compare(s1.Start, s2.Start); }); - List combined = new ArrayList<>(); + ArrayList combined = new ArrayList<>(); Map nextSection = new HashMap<>(); boolean escaped = false; for (int i = 0; i < sections.size(); i++) { @@ -72,14 +72,20 @@ public final class ChatFormatter { final FormattedSection section = sections.get(i); if (!section.IsRange) { escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER); + combined.add(section); continue; // TODO: Escape \ and check and fix escape logic - } + } // TODO: Actually combine overlapping sections if (nextSection.containsKey(section.Formatters.get(0)) && !escaped) { FormattedSection s = nextSection.remove(section.Formatters.get(0)); + DebugCommand.SendDebugMessage("Finishing section: " + s); s.End = section.Start; combined.add(s); + } else { + DebugCommand.SendDebugMessage("Adding next section: " + section); + nextSection.put(section.Formatters.get(0), section); } } + sections = combined; for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i); DebugCommand.SendDebugMessage("Applying section: " + section); -- 2.30.2 From eba73d6db4b1a2113ecbe02e3087202fb8e8e86b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 28 Dec 2016 01:12:10 +0100 Subject: [PATCH 5/9] Probably fixed escape logic --- .../chat/formatting/ChatFormatter.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 6c50f77..3c7170d 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -71,15 +71,18 @@ public final class ChatFormatter { // Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true final FormattedSection section = sections.get(i); if (!section.IsRange) { - escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER); + escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second combined.add(section); - continue; // TODO: Escape \ and check and fix escape logic + continue; } // TODO: Actually combine overlapping sections - if (nextSection.containsKey(section.Formatters.get(0)) && !escaped) { - FormattedSection s = nextSection.remove(section.Formatters.get(0)); - DebugCommand.SendDebugMessage("Finishing section: " + s); - s.End = section.Start; - combined.add(s); + if (nextSection.containsKey(section.Formatters.get(0))) { + if (!escaped) { + FormattedSection s = nextSection.remove(section.Formatters.get(0)); + s.End = section.Start; + combined.add(s); + DebugCommand.SendDebugMessage("Finished section: " + s); + } else + escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' } else { DebugCommand.SendDebugMessage("Adding next section: " + section); nextSection.put(section.Formatters.get(0), section); -- 2.30.2 From 0f74ad4cb160738b5708912723168b03224ef107 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Dec 2016 19:33:23 +0100 Subject: [PATCH 6/9] Fixed escape logic even more and readded combine --- .../chat/formatting/ChatFormatter.java | 109 ++++++++++++++++-- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 3c7170d..ac6772f 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -72,23 +72,114 @@ public final class ChatFormatter { final FormattedSection section = sections.get(i); if (!section.IsRange) { escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second - combined.add(section); + if (!escaped) // Don't add the escape character + combined.add(section); continue; } // TODO: Actually combine overlapping sections - if (nextSection.containsKey(section.Formatters.get(0))) { - if (!escaped) { + if (!escaped) { + if (nextSection.containsKey(section.Formatters.get(0))) { FormattedSection s = nextSection.remove(section.Formatters.get(0)); s.End = section.Start; + s.IsRange = false; // IsRange means it's a 1 long section indicating a start or an end combined.add(s); DebugCommand.SendDebugMessage("Finished section: " + s); - } else - escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' - } else { - DebugCommand.SendDebugMessage("Adding next section: " + section); - nextSection.put(section.Formatters.get(0), section); - } + } else { + DebugCommand.SendDebugMessage("Adding next section: " + section); + nextSection.put(section.Formatters.get(0), section); + } + } else + escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' } sections = combined; + boolean cont = true; + boolean found = false; + for (int i = 1; cont;) { + int nextindex = i + 1; + if (sections.size() < 2) + break; + DebugCommand.SendDebugMessage("i: " + i); + FormattedSection firstSection = sections.get(i - 1); + DebugCommand.SendDebugMessage("Combining sections " + firstSection + " and " + sections.get(i)); + if (firstSection.Start == sections.get(i).Start && firstSection.End == sections.get(i).End) { + firstSection.Formatters.addAll(sections.get(i).Formatters); + firstSection.Matches.addAll(sections.get(i).Matches); + if (firstSection.RemCharFromStart < sections.get(i).RemCharFromStart) + firstSection.RemCharFromStart = sections.get(i).RemCharFromStart; + if (firstSection.RemCharFromEnd < sections.get(i).RemCharFromEnd) + firstSection.RemCharFromEnd = sections.get(i).RemCharFromEnd; + firstSection.RemCharPos.addAll(sections.get(i).RemCharPos); + DebugCommand.SendDebugMessage("To section " + firstSection); + sections.remove(i); + found = true; + } else if (firstSection.End > sections.get(i).Start && firstSection.Start < sections.get(i).End) { + int origend = firstSection.End; + firstSection.End = sections.get(i).Start - 1; + int origend2 = sections.get(i).End; + boolean switchends; + if (switchends = origend2 < origend) { + int tmp = origend; + origend = origend2; + origend2 = tmp; + } + FormattedSection section = new FormattedSection(firstSection.Formatters, sections.get(i).Start, origend, + firstSection.Matches, sections.get(i).RemCharFromStart, firstSection.RemCharFromEnd, + Collections.emptyList(), false); + section.Formatters.addAll(sections.get(i).Formatters); + section.Matches.addAll(sections.get(i).Matches); // TODO: Clean + sections.add(i, section); + nextindex++; + FormattedSection thirdFormattedSection = sections.get(i + 1); + if (switchends) { // Use the properties of the first section not the second one + thirdFormattedSection.Formatters.clear(); + thirdFormattedSection.Formatters.addAll(firstSection.Formatters); + thirdFormattedSection.Matches.clear(); + thirdFormattedSection.Matches.addAll(firstSection.Matches); + short remchar = section.RemCharFromEnd; + section.RemCharFromEnd = thirdFormattedSection.RemCharFromEnd; + thirdFormattedSection.RemCharFromEnd = remchar; + } + firstSection.RemCharFromEnd = 0; + thirdFormattedSection.RemCharFromStart = 0; + thirdFormattedSection.Start = origend + 1; + thirdFormattedSection.End = origend2; + for (int x = 0; x < firstSection.RemCharPos.size(); x++) { + if (firstSection.RemCharPos.get(x) > firstSection.End) { + if (firstSection.RemCharPos.get(x) > section.End) + thirdFormattedSection.RemCharPos.add( + firstSection.RemCharPos.get(x) - thirdFormattedSection.Start + firstSection.Start); + else + section.RemCharPos.add(firstSection.RemCharPos.get(x) - section.Start + firstSection.Start); + firstSection.RemCharPos.remove(x--); + } + } + DebugCommand.SendDebugMessage("To sections 1:" + firstSection + ""); + DebugCommand.SendDebugMessage(" 2:" + section + ""); + DebugCommand.SendDebugMessage(" 3:" + thirdFormattedSection); + found = true; + } + for (int j = i - 1; j <= i + 1; j++) { + if (j < sections.size() && sections.get(j).End < sections.get(j).Start) { + DebugCommand.SendDebugMessage("Removing section: " + sections.get(j)); + sections.remove(j); + found = true; + } + } + i = nextindex - 1; + i++; + if (i >= sections.size()) { + if (found) { + i = 1; + found = false; + sections.sort((s1, s2) -> { + if (s1.Start == s2.Start) + return Integer.compare(s1.End, s2.End); + else + return Integer.compare(s1.Start, s2.Start); + }); + } else + cont = false; + } + } for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i); DebugCommand.SendDebugMessage("Applying section: " + section); -- 2.30.2 From 2594721152fb02db6259d687eea7581b18d8fc6f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Dec 2016 21:22:34 +0100 Subject: [PATCH 7/9] Fixes! Escape logic again... - Made the escape chars disappear - Fixed empty nickname list matching empty strings --- .../buttondevteam/chat/ChatProcessing.java | 57 ++++++++++--------- .../chat/formatting/ChatFormatter.java | 12 ++-- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 14a1933..c795b7c 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -135,6 +135,7 @@ public class ChatProcessing { namesb.append(")"); StringBuilder nicksb = new StringBuilder(); nicksb.append("(?i)("); + boolean addNickFormatter = false; { final int size = Bukkit.getOnlinePlayers().size(); int index = 0; @@ -144,6 +145,7 @@ public class ChatProcessing { nicksb.append(nick); if (index < size - 1) { nicksb.append("|"); + addNickFormatter = true; } } index++; @@ -169,26 +171,27 @@ public class ChatProcessing { return color + p.getName() + "§r"; }).setPriority(Priority.High).build()); - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())).setColor(Color.Aqua) - .setOnmatch((String match) -> { - if (PlayerListener.nicknames.containsKey(match)) { - Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match)); - if (p == null) { - PluginMain.Instance.getLogger().warning( - "Error: Can't find player nicknamed " + match + " but was reported as online."); - return "§c" + match + "§r"; + if (addNickFormatter) + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())) + .setColor(Color.Aqua).setOnmatch((String match) -> { + if (PlayerListener.nicknames.containsKey(match)) { + Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match)); + if (p == null) { + PluginMain.Instance.getLogger().warning("Error: Can't find player nicknamed " + + match + " but was reported as online."); + return "§c" + match + "§r"; + } + if (PlayerListener.NotificationSound == null) + p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); + else + p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, + (float) PlayerListener.NotificationPitch); + return PluginMain.essentials.getUser(p).getNickname(); } - if (PlayerListener.NotificationSound == null) - p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); - else - p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, - (float) PlayerListener.NotificationPitch); - return PluginMain.essentials.getUser(p).getNickname(); - } - Bukkit.getServer().getLogger().warning( - "Player nicknamed " + match + " not found in nickname map but was reported as online."); - return "§c" + match + "§r"; - }).setPriority(Priority.High).build()); + Bukkit.getServer().getLogger().warning("Player nicknamed " + match + + " not found in nickname map but was reported as online."); + return "§c" + match + "§r"; + }).setPriority(Priority.High).build()); } pingedconsole = false; // Will set it to true onmatch (static constructor) @@ -198,15 +201,16 @@ public class ChatProcessing { json.addExtra(new TellrawPart("[C]").setHoverEvent( TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, "Chat only"))); } - final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + currentchannel.DisplayName) - + "]" + (mp != null && !mp.RPMode ? "[OOC]" : ""); + final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + + currentchannel.DisplayName) + "]" + (mp != null && !mp.RPMode ? "[OOC]" : ""); json.addExtra( - new TellrawPart(channelidentifier).setHoverEvent( + new TellrawPart(channelidentifier) + .setHoverEvent( TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, new TellrawPart((sender instanceof IDiscordSender ? "From Discord\n" : "") + "Copy message").setColor(Color.Blue))) - .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, - TellrawEvent.ClickAction.SUGGEST_COMMAND, suggestmsg))); + .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, + TellrawEvent.ClickAction.SUGGEST_COMMAND, suggestmsg))); json.addExtra(new TellrawPart(" <")); json.addExtra( new TellrawPart( @@ -381,9 +385,8 @@ public class ChatProcessing { player.sendMessage("§cAn error occured while sending the message."); return true; } - PluginMain.Instance.getServer().getConsoleSender() - .sendMessage(String.format("%s <%s> %s", channelidentifier, - (player != null ? player.getDisplayName() : sender.getName()), message)); + PluginMain.Instance.getServer().getConsoleSender().sendMessage(String.format("%s <%s> %s", channelidentifier, + (player != null ? player.getDisplayName() : sender.getName()), message)); DebugCommand.SendDebugMessage( "-- Full ChatProcessing time: " + (System.nanoTime() - processstart) / 1000000f + " ms"); DebugCommand.SendDebugMessage("-- ChatFormatter.Combine time: " + combinetime / 1000000f + " ms"); diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index ac6772f..7baf4ae 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -72,8 +72,10 @@ public final class ChatFormatter { final FormattedSection section = sections.get(i); if (!section.IsRange) { escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second - if (!escaped) // Don't add the escape character - combined.add(section); + if (escaped) // Don't add the escape character + section.RemCharFromStart = 1; + combined.add(section); + DebugCommand.SendDebugMessage("Added " + (!escaped ? "not " : "") + "escaped section: " + section); continue; } // TODO: Actually combine overlapping sections if (!escaped) { @@ -87,8 +89,10 @@ public final class ChatFormatter { DebugCommand.SendDebugMessage("Adding next section: " + section); nextSection.put(section.Formatters.get(0), section); } - } else + } else { + DebugCommand.SendDebugMessage("Skipping section: " + section); escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' + } } sections = combined; boolean cont = true; @@ -225,6 +229,6 @@ public final class ChatFormatter { @Override public String toString() { return new StringBuilder("F(").append(color).append(", ").append(format).append(", ").append(openlink) - .append(", ").append(priority).append(")").toString(); + .append(", ").append(priority).append(", ").append(regex).append(")").toString(); } } -- 2.30.2 From 7de0ecc87d5d741219cba581629ddf9e2c5286c7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Dec 2016 21:50:53 +0100 Subject: [PATCH 8/9] Fixed small bug - SHRUG WORKS --- src/main/java/buttondevteam/chat/ChatProcessing.java | 3 +-- src/main/java/buttondevteam/chat/formatting/ChatFormatter.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index c795b7c..9532163 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -44,8 +44,7 @@ public class ChatProcessing { private static ArrayList commonFormatters = new ArrayList<>(); - public static final ChatFormatter ESCAPE_FORMATTER = new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN) - .setRemoveCharPos((short) 0).build(); + public static final ChatFormatter ESCAPE_FORMATTER = new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN).build(); private ChatProcessing() { } diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 7baf4ae..ce58598 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -77,7 +77,7 @@ public final class ChatFormatter { combined.add(section); DebugCommand.SendDebugMessage("Added " + (!escaped ? "not " : "") + "escaped section: " + section); continue; - } // TODO: Actually combine overlapping sections + } if (!escaped) { if (nextSection.containsKey(section.Formatters.get(0))) { FormattedSection s = nextSection.remove(section.Formatters.get(0)); -- 2.30.2 From d0e038cdd3b042f30e85d6263e29e570300f9b2a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Dec 2016 22:11:26 +0100 Subject: [PATCH 9/9] Added #55 --- src/main/java/buttondevteam/chat/ChatProcessing.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 9532163..8cf9b81 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -30,6 +30,7 @@ import buttondevteam.lib.chat.*; public class ChatProcessing { private static final Pattern NULL_MENTION_PATTERN = Pattern.compile("null"); + private static final Pattern CYAN_PATTERN = Pattern.compile("cyan"); private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\"); private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console")); private static final Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+)"); @@ -72,6 +73,7 @@ public class ChatProcessing { commonFormatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue) .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(CYAN_PATTERN).setColor(Color.Aqua).build()); // #55 } // Returns e.setCancelled for custom event -- 2.30.2