From bda72749cd06499f7bc0d749806c19a4089d4eef Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 24 Sep 2016 17:31:55 +0200 Subject: [PATCH] Probably fixed #46 --- .../buttondevteam/chat/ChatProcessing.java | 5 +++ .../chat/formatting/ChatFormatter.java | 31 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index a8ada45..c4a002c 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -45,6 +45,7 @@ public class ChatProcessing { // Returns e.setCancelled public static boolean ProcessChat(CommandSender sender, String message) { + long processstart = System.nanoTime(); if (PlayerListener.essentials == null) PlayerListener.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); Player player = (sender instanceof Player ? (Player) sender : null); @@ -230,7 +231,9 @@ public class ChatProcessing { + PlayerListener.AlphaDeaths : ""))))))); json.addExtra(new TellrawPart("> ")); + long combinetime = System.nanoTime(); ChatFormatter.Combine(formatters, formattedmessage, json); + combinetime = System.nanoTime() - combinetime; Gson gson = new GsonBuilder() .registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum()) .registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection()) @@ -399,6 +402,8 @@ public class ChatProcessing { .sendMessage(String.format("[%s] <%s%s> %s", currentchannel.DisplayName, (player != null ? player.getDisplayName() : sender.getName()), (mp != null ? mp.GetFormattedFlair() : ""), message)); + DebugCommand.SendDebugMessage("-- Full ChatProcessing time: " + (System.nanoTime() - processstart)); + DebugCommand.SendDebugMessage("-- ChatFormatter.Combine time: " + combinetime); return true; } } diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 684492a..f0e6fe4 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -35,6 +35,7 @@ public final class ChatFormatter { * This method assumes that there is always a global formatter */ ArrayList sections = new ArrayList(); + List removecharpositions = new ArrayList(); for (ChatFormatter formatter : formatters) { Matcher matcher = formatter.regex.matcher(str); while (matcher.find()) { @@ -47,6 +48,12 @@ public final class ChatFormatter { DebugCommand.SendDebugMessage("First group: " + groups.get(0)); FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups); sections.add(section); + if (formatter.removecharcount != 0) { + removecharpositions.add(section.Start + formatter.removecharcount); + removecharpositions.add(section.End - formatter.removecharcount); + } + if (formatter.removecharpos != -1) + removecharpositions.add(section.Start + (int) formatter.removecharpos); } } sections.sort((s1, s2) -> { @@ -55,6 +62,7 @@ public final class ChatFormatter { else return Integer.compare(s1.Start, s2.Start); }); + removecharpositions.sort(null); boolean cont = true; boolean found = false; for (int i = 1; cont;) { @@ -124,15 +132,25 @@ public final class ChatFormatter { cont = false; } } + int nextremcharpospos = 0; for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i); DebugCommand.SendDebugMessage("Applying section: " + section); - String originaltext = str.substring(section.Start, section.End + 1); + int nextremcharpos = -1; + if (removecharpositions.size() > nextremcharpospos) + nextremcharpos = removecharpositions.get(nextremcharpospos); + String originaltext; + int start = section.Start, end = section.End + 1; + if (nextremcharpos == section.Start) + start++; + if (nextremcharpos == section.End) + end--; + originaltext = str.substring(start, end); + nextremcharpospos++; DebugCommand.SendDebugMessage("Originaltext: " + originaltext); Color color = null; Format format = null; String openlink = null; - List removecharpositions = new ArrayList(); section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { DebugCommand.SendDebugMessage("Applying formatter: " + formatter); @@ -144,16 +162,9 @@ public final class ChatFormatter { format = formatter.format; if (formatter.openlink != null) openlink = formatter.openlink; - if (formatter.removecharcount != 0) { - removecharpositions.add(section.Start + formatter.removecharcount); // TODO: Do this before combining the sections - removecharpositions.add(section.End - formatter.removecharcount); - } - if (formatter.removecharpos != -1) - removecharpositions.add((int) formatter.removecharpos); } TellrawPart newtp = new TellrawPart(""); - StringBuilder origtextsb = new StringBuilder(originaltext); // TODO - newtp.setText(origtextsb.toString()); + newtp.setText(originaltext); if (color != null) newtp.setColor(color); if (format != null)