Fix all remaining issues with the chat processing

Checking regex formatters first, then string, then range
Fix section combining
24/24
#71
This commit is contained in:
Norbi Peti 2020-02-13 17:20:17 +01:00
parent 4d0c7c170e
commit 4952923f9b
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 25 additions and 15 deletions

View file

@ -68,7 +68,6 @@ public class ChatProcessing {
return "@console"; return "@console";
}).build(), true, "@console"), }).build(), true, "@console"),
new RegexMatchProvider("hashtag", HASHTAG_PATTERN, FormatSettings.builder().color(Color.Blue).openlink("https://twitter.com/hashtag/$1").build()),
new StringMatchProvider("cyan", FormatSettings.builder().color(Color.Aqua).build(), true, "cyan"), // #55 new StringMatchProvider("cyan", FormatSettings.builder().color(Color.Aqua).build(), true, "cyan"), // #55
new RangeMatchProvider("code", "`", FormatSettings.builder().color(Color.DarkGray).build()), new RangeMatchProvider("code", "`", FormatSettings.builder().color(Color.DarkGray).build()),
new RegexMatchProvider("maskedLink", MASKED_LINK_PATTERN, FormatSettings.builder().underlined(true) new RegexMatchProvider("maskedLink", MASKED_LINK_PATTERN, FormatSettings.builder().underlined(true)
@ -80,6 +79,7 @@ public class ChatProcessing {
return text; return text;
}).build()), }).build()),
new RegexMatchProvider("url", URL_PATTERN, FormatSettings.builder().underlined(true).openlink("$1").build()), new RegexMatchProvider("url", URL_PATTERN, FormatSettings.builder().underlined(true).openlink("$1").build()),
new RegexMatchProvider("hashtag", HASHTAG_PATTERN, FormatSettings.builder().color(Color.Blue).openlink("https://twitter.com/hashtag/$1").build()),
new StringMatchProvider("someone", FormatSettings.builder().color(Color.Aqua) new StringMatchProvider("someone", FormatSettings.builder().color(Color.Aqua)
.onmatch((match, builder, section) -> { .onmatch((match, builder, section) -> {
if (Bukkit.getOnlinePlayers().size() == 0) return match; if (Bukkit.getOnlinePlayers().size() == 0) return match;

View file

@ -42,7 +42,13 @@ public final class ChatFormatter {
escapeThings(str, excluded, remchars); escapeThings(str, excluded, remchars);
createSections(formatters, str, sections, excluded, remchars, defaults); sections.add(new FormattedSection(defaults, 0, str.length() - 1, Collections.emptyList())); //Add entire message
var providers = formatters.stream().filter(mp -> mp instanceof RegexMatchProvider).collect(Collectors.toList());
createSections(providers, str, sections, excluded, remchars, defaults);
providers = formatters.stream().filter(mp -> mp instanceof StringMatchProvider).collect(Collectors.toList());
createSections(providers, str, sections, excluded, remchars, defaults);
providers = formatters.stream().filter(mp -> mp instanceof RangeMatchProvider).collect(Collectors.toList());
createSections(providers, str, sections, excluded, remchars, defaults);
sortSections(sections); sortSections(sections);
header("Section combining"); header("Section combining");
@ -66,7 +72,6 @@ public final class ChatFormatter {
private static void createSections(List<MatchProviderBase> formatters, String str, ArrayList<FormattedSection> sections, private static void createSections(List<MatchProviderBase> formatters, String str, ArrayList<FormattedSection> sections,
ArrayList<int[]> excludedAreas, ArrayList<int[]> removedCharacters, FormatSettings defaults) { ArrayList<int[]> excludedAreas, ArrayList<int[]> removedCharacters, FormatSettings defaults) {
sections.add(new FormattedSection(defaults, 0, str.length() - 1, Collections.emptyList())); //Add entire message
formatters.forEach(MatchProviderBase::reset); //Reset state information, as we aren't doing deep cloning formatters.forEach(MatchProviderBase::reset); //Reset state information, as we aren't doing deep cloning
while (formatters.size() > 0) { while (formatters.size() > 0) {
for (var iterator = formatters.iterator(); iterator.hasNext(); ) { for (var iterator = formatters.iterator(); iterator.hasNext(); ) {
@ -91,7 +96,8 @@ public final class ChatFormatter {
{ {
FormattedSection firstSect = sections.get(i - 1); FormattedSection firstSect = sections.get(i - 1);
FormattedSection lastSect = sections.get(i); FormattedSection lastSect = sections.get(i);
if (firstSect.Start > lastSect.Start) { //The first can't start later if (firstSect.Start > lastSect.Start //The first can't start later
|| (firstSect.Start == lastSect.Start && firstSect.End < lastSect.End)) {
var section = firstSect; var section = firstSect;
firstSect = lastSect; firstSect = lastSect;
lastSect = section; lastSect = section;
@ -112,18 +118,23 @@ public final class ChatFormatter {
i = 0; i = 0;
sortSections(sections); sortSections(sections);
continue; continue;
} else if (firstSection.End > lastSection.Start && firstSection.Start < lastSection.End) { } else if (firstSection.End >= lastSection.Start && firstSection.Start <= lastSection.End) {
int origend2 = firstSection.End; int firstSectEnd = firstSection.End;
firstSection.End = lastSection.Start - 1; firstSection.End = lastSection.Start - 1;
int origend = lastSection.End; int lastSectEnd = lastSection.End;
FormattedSection section = new FormattedSection(firstSection.Settings, lastSection.Start, origend, FormattedSection section = new FormattedSection(firstSection.Settings, lastSection.Start, lastSectEnd,
firstSection.Matches); firstSection.Matches);
section.Settings.copyFrom(lastSection.Settings); section.Settings.copyFrom(lastSection.Settings);
section.Matches.addAll(lastSection.Matches); section.Matches.addAll(lastSection.Matches);
sections.add(i, section); sections.add(i, section);
lastSection.Start = origend + 1; if (firstSectEnd > lastSection.End) { //Copy first section info to last as the lastSection initially cuts the firstSection in half
lastSection.End = origend2; lastSection.Settings = FormatSettings.builder().build();
lastSection.Settings.copyFrom(firstSection.Settings);
}
lastSection.Start = lastSectEnd + 1;
lastSection.End = firstSectEnd;
Predicate<FormattedSection> removeIfNeeded = s -> { Predicate<FormattedSection> removeIfNeeded = s -> {
if (s.Start < 0 || s.End < 0 || s.Start > s.End) { if (s.Start < 0 || s.End < 0 || s.Start > s.End) {

View file

@ -20,7 +20,9 @@ public abstract class MatchProviderBase implements MatchProvider {
public abstract FormattedSection getNextSection(String message, ArrayList<int[]> ignoredAreas, ArrayList<int[]> removedCharacters); public abstract FormattedSection getNextSection(String message, ArrayList<int[]> ignoredAreas, ArrayList<int[]> removedCharacters);
@Override @Override
public abstract String toString(); public String toString() {
return name;
}
protected abstract void resetSubclass(); protected abstract void resetSubclass();

View file

@ -6,7 +6,6 @@ import lombok.ToString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ToString
public class RangeMatchProvider extends MatchProviderBase { public class RangeMatchProvider extends MatchProviderBase {
private final String pattern; private final String pattern;
@ToString.Exclude @ToString.Exclude

View file

@ -8,7 +8,6 @@ import java.util.ArrayList;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ToString
public class RegexMatchProvider extends MatchProviderBase { public class RegexMatchProvider extends MatchProviderBase {
private final Pattern pattern; private final Pattern pattern;
@ToString.Exclude @ToString.Exclude

View file

@ -7,7 +7,6 @@ import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ToString
public class StringMatchProvider extends MatchProviderBase { public class StringMatchProvider extends MatchProviderBase {
private final String[] strings; private final String[] strings;
@ToString.Exclude @ToString.Exclude

View file

@ -62,7 +62,7 @@ public class ChatFormatIT {
list.add(new ChatFormatIT(sender, "*https://norbipeti.github.io/ heh*", new TellrawPart("https://norbipeti.github.io/").setItalic(true).setUnderlined(true) list.add(new ChatFormatIT(sender, "*https://norbipeti.github.io/ heh*", new TellrawPart("https://norbipeti.github.io/").setItalic(true).setUnderlined(true)
.setHoverEvent(TellrawEvent.create(HoverAction.SHOW_TEXT, .setHoverEvent(TellrawEvent.create(HoverAction.SHOW_TEXT,
new TellrawPart("Click to open").setColor(Color.Blue))) new TellrawPart("Click to open").setColor(Color.Blue)))
.setClickEvent(TellrawEvent.create(ClickAction.OPEN_URL, "https://norbipeti.github.io/")), new TellrawPart(" heh").setItalic(true))); .setClickEvent(TellrawEvent.create(ClickAction.OPEN_URL, "https://norbipeti.github.io/")).setColor(Color.White), new TellrawPart(" heh").setItalic(true).setColor(Color.White)));
list.add(new ChatFormatIT(sender, "*test _test_ test*", new TellrawPart("test test test").setItalic(true).setColor(Color.White))); list.add(new ChatFormatIT(sender, "*test _test_ test*", new TellrawPart("test test test").setItalic(true).setColor(Color.White)));
list.add(new ChatFormatIT(sender, "*test __test__ test*", new TellrawPart("test ").setItalic(true).setColor(Color.White), list.add(new ChatFormatIT(sender, "*test __test__ test*", new TellrawPart("test ").setItalic(true).setColor(Color.White),
new TellrawPart("test").setItalic(true).setUnderlined(true).setColor(Color.White), new TellrawPart(" test").setItalic(true).setColor(Color.White))); new TellrawPart("test").setItalic(true).setUnderlined(true).setColor(Color.White), new TellrawPart(" test").setItalic(true).setColor(Color.White)));