From 12b1a45558c7267d4d8cebf4e79d340a9b666aac Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 9 Jun 2017 12:28:52 +0200 Subject: [PATCH] Progressing with processing --- .../buttondevteam/chat/ChatProcessing.java | 2 +- .../chat/formatting/ChatFormatter.java | 38 +++++++++++-------- .../buttondevteam/chat/ChatFormatTest.java | 4 +- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 84282a2..c245189 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -50,7 +50,7 @@ public class ChatProcessing { static { commonFormatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setBold(true) - .setRemoveCharCount((short) 2).setRange(true).build()); + .setRemoveCharCount((short) 2).setRange(true).setPriority(Priority.High).build()); commonFormatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setItalic(true) .setRemoveCharCount((short) 1).setRange(true).build()); commonFormatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setUnderlined(true) diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index b901565..37f5c77 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -67,12 +67,10 @@ public final class ChatFormatter { sections.add(section); } } - sections.sort((s1, s2) -> { - if (s1.Start == s2.Start) - return Integer.compare(s1.End, s2.End); - else - return Integer.compare(s1.Start, s2.Start); - }); + sections.sort((s1, s2) -> s1.Start == s2.Start + ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(), + s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End) + : Integer.compare(s1.Start, s2.Start)); ArrayList combined = new ArrayList<>(); Map nextSection = new HashMap<>(); boolean escaped = false; @@ -90,8 +88,16 @@ public final class ChatFormatter { continue; } if (!escaped) { - if (section.Start >= takenStart && section.Start <= takenEnd) { - if (section.RemCharFromStart <= takenEnd - takenStart) { + if (combined.stream().anyMatch(s -> s.IsRange && (s.Start == section.Start + || (s.Start < section.Start ? s.End >= section.Start : s.Start <= section.End)))) { + DebugCommand.SendDebugMessage("Range " + section + " overlaps with a combined section, ignoring."); + continue; + } + if (section.Start == takenStart || (section.Start > takenStart && section.Start < takenEnd)) { + /* + * if (nextSection.containsKey(section.Formatters.get(0)) ? section.RemCharFromStart <= takenEnd - takenStart : section.RemCharFromStart > takenEnd - takenStart) { + */ + if (section.RemCharFromStart < takenEnd - takenStart) { System.out.println("Lose: " + section); System.out.println("And win: " + takenFormatter); continue; // The current section loses @@ -106,7 +112,7 @@ public final class ChatFormatter { if (nextSection.containsKey(section.Formatters.get(0))) { FormattedSection s = nextSection.remove(section.Formatters.get(0)); s.End = section.Start + section.RemCharFromStart - 1; - s.IsRange = false; // IsRange means it's a 1 long section indicating a start or an end + // 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 { @@ -176,7 +182,8 @@ public final class ChatFormatter { found = true; } for (int j = i - 1; j <= i + 1; j++) { - if (j < sections.size() && sections.get(j).End < sections.get(j).Start) { + if (j < sections.size() && sections.get(j).End - sections.get(j).RemCharFromEnd < sections.get(j).Start + + sections.get(j).RemCharFromStart) { DebugCommand.SendDebugMessage("Removing section: " + sections.get(j)); sections.remove(j); found = true; @@ -188,12 +195,10 @@ public final class ChatFormatter { 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); - }); + sections.sort((s1, s2) -> s1.Start == s2.Start + ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(), + s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End) + : Integer.compare(s1.Start, s2.Start)); } else cont = false; } @@ -203,6 +208,7 @@ public final class ChatFormatter { DebugCommand.SendDebugMessage("Applying section: " + section); String originaltext; int start = section.Start + section.RemCharFromStart, end = section.End + 1 - section.RemCharFromEnd; // TODO: RemCharPos + DebugCommand.SendDebugMessage("Start: " + start + " - End: " + end); StringBuilder textsb = new StringBuilder(str.substring(start, end)); originaltext = textsb.toString(); DebugCommand.SendDebugMessage("Section text: " + originaltext); diff --git a/src/test/java/buttondevteam/chat/ChatFormatTest.java b/src/test/java/buttondevteam/chat/ChatFormatTest.java index e7fabfe..2d8a38e 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatTest.java +++ b/src/test/java/buttondevteam/chat/ChatFormatTest.java @@ -21,11 +21,11 @@ public class ChatFormatTest extends TestCase { DebugCommand.DebugMode = true; testMessage(sender, "*test*", new TellrawPart("test").setItalic(true).setColor(Color.White)); testMessage(sender, "**test**", new TellrawPart("test").setBold(true).setColor(Color.White)); - /*testMessage(sender, "***test***", new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White)); + testMessage(sender, "***test***", new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White)); testMessage(sender, "***_test_***", new TellrawPart("test").setBold(true).setItalic(true).setUnderlined(true).setColor(Color.White)); testMessage(sender, "***_~~test~~_***", new TellrawPart("test").setBold(true).setItalic(true) - .setUnderlined(true).setStrikethrough(true).setColor(Color.White));*/ + .setUnderlined(true).setStrikethrough(true).setColor(Color.White)); } void testMessage(final CommandSender sender, final String message, TellrawPart... extras) {