From a173cdefb2d9a542076acc2fed2f0c27f76b224c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 2 Oct 2016 12:14:54 +0200 Subject: [PATCH] Added RemCharPos support --- .../chat/formatting/ChatFormatter.java | 76 ++++++++++++------- .../chat/formatting/FormattedSection.java | 8 +- .../chat/formatting/TellrawPart.java | 16 ++-- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 0c35f03..e640a14 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -1,6 +1,7 @@ package buttondevteam.chat.formatting; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.function.Function; import java.util.regex.Matcher; @@ -63,22 +64,22 @@ public final class ChatFormatter { if (sections.size() < 2) break; DebugCommand.SendDebugMessage("i: " + i); - if (sections.get(i - 1).Start == sections.get(i).Start && sections.get(i - 1).End == sections.get(i).End) { - DebugCommand.SendDebugMessage("Combining sections " + sections.get(i - 1) + " and " + sections.get(i)); - sections.get(i - 1).Formatters.addAll(sections.get(i).Formatters); - sections.get(i - 1).Matches.addAll(sections.get(i).Matches); - if (sections.get(i - 1).RemCharFromStart < sections.get(i).RemCharFromStart) - sections.get(i - 1).RemCharFromStart = sections.get(i).RemCharFromStart; - if (sections.get(i - 1).RemCharFromEnd < sections.get(i).RemCharFromEnd) - sections.get(i - 1).RemCharFromEnd = sections.get(i).RemCharFromEnd; - DebugCommand.SendDebugMessage("To section " + sections.get(i - 1)); + 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 (sections.get(i - 1).End > sections.get(i).Start - && sections.get(i - 1).Start < sections.get(i).End) { - DebugCommand.SendDebugMessage("Combining sections " + sections.get(i - 1) + " and " + sections.get(i)); - int origend = sections.get(i - 1).End; - sections.get(i - 1).End = sections.get(i).Start - 1; + } 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) { @@ -86,28 +87,38 @@ public final class ChatFormatter { origend = origend2; origend2 = tmp; } - FormattedSection section = new FormattedSection(sections.get(i - 1).Formatters, sections.get(i).Start, - origend, sections.get(i - 1).Matches, sections.get(i).RemCharFromStart, - sections.get(i - 1).RemCharFromEnd, sections.get(i - 1).RemCharPos); // TODO: RemCharPos - section.Formatters.addAll(sections.get(i).Formatters); // TODO: Add remove counts to every part, then check if they have duplicates + 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(sections.get(i - 1).Formatters); + thirdFormattedSection.Formatters.addAll(firstSection.Formatters); thirdFormattedSection.Matches.clear(); - thirdFormattedSection.Matches.addAll(sections.get(i - 1).Matches); + thirdFormattedSection.Matches.addAll(firstSection.Matches); short remchar = section.RemCharFromEnd; section.RemCharFromEnd = thirdFormattedSection.RemCharFromEnd; thirdFormattedSection.RemCharFromEnd = remchar; } - sections.get(i - 1).RemCharFromEnd = 0; + firstSection.RemCharFromEnd = 0; thirdFormattedSection.RemCharFromStart = 0; thirdFormattedSection.Start = origend + 1; thirdFormattedSection.End = origend2; - DebugCommand.SendDebugMessage("To sections 1:" + sections.get(i - 1) + ""); + 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; @@ -140,10 +151,14 @@ 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 - originaltext = str.substring(start, end); - DebugCommand.SendDebugMessage("Originaltext: " + originaltext); + StringBuilder textsb = new StringBuilder(str.substring(start, end)); + for (int x = 0; x < section.RemCharPos.size(); x++) + if (section.RemCharPos.get(x) != -1) + textsb.deleteCharAt(section.RemCharPos.get(x)); + originaltext = textsb.toString(); + DebugCommand.SendDebugMessage("Section text: " + originaltext); Color color = null; - Format format = null; + int format = 0; String openlink = null; section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { @@ -153,7 +168,7 @@ public final class ChatFormatter { if (formatter.color != null) color = formatter.color; if (formatter.format != null) - format = formatter.format; + format = formatter.format.flag; //TODO: Fix if (formatter.openlink != null) openlink = formatter.openlink; } @@ -161,7 +176,7 @@ public final class ChatFormatter { newtp.setText(originaltext); if (color != null) newtp.setColor(color); - if (format != null) + if (format != 0) newtp.setFormat(format); if (openlink != null && openlink.length() > 0) { newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL, @@ -187,12 +202,19 @@ public final class ChatFormatter { Format(String name) { this.name = name; + this.flag = 1 << this.ordinal(); } @Override public String getName() { return name; } + + private final int flag; + + public int getFlag() { + return flag; + } } public enum Color implements TellrawSerializableEnum { diff --git a/src/main/java/buttondevteam/chat/formatting/FormattedSection.java b/src/main/java/buttondevteam/chat/formatting/FormattedSection.java index b78433d..4d2f209 100644 --- a/src/main/java/buttondevteam/chat/formatting/FormattedSection.java +++ b/src/main/java/buttondevteam/chat/formatting/FormattedSection.java @@ -10,7 +10,7 @@ class FormattedSection { ArrayList Matches = new ArrayList(); short RemCharFromStart; short RemCharFromEnd; - int RemCharPos; + ArrayList RemCharPos = new ArrayList(); FormattedSection(ChatFormatter formatter, int start, int end, ArrayList matches, short remcharfromstart, short remcharfromend, int remcharpos) { @@ -20,18 +20,18 @@ class FormattedSection { Matches.addAll(matches); RemCharFromStart = remcharfromstart; RemCharFromEnd = remcharfromend; - RemCharPos = remcharpos; + RemCharPos.add(remcharpos); } FormattedSection(Collection formatters, int start, int end, ArrayList matches, - short remcharfromstart, short remcharfromend, int remcharpos) { + short remcharfromstart, short remcharfromend, Collection remcharpos) { Start = start; End = end; Formatters.addAll(formatters); Matches.addAll(matches); RemCharFromStart = remcharfromstart; RemCharFromEnd = remcharfromend; - RemCharPos = remcharpos; + RemCharPos.addAll(remcharpos); } @Override diff --git a/src/main/java/buttondevteam/chat/formatting/TellrawPart.java b/src/main/java/buttondevteam/chat/formatting/TellrawPart.java index d71f3e4..c9145dc 100644 --- a/src/main/java/buttondevteam/chat/formatting/TellrawPart.java +++ b/src/main/java/buttondevteam/chat/formatting/TellrawPart.java @@ -8,7 +8,7 @@ import java.util.List; public final class TellrawPart implements Serializable { private static final long serialVersionUID = 4125357644462144024L; private ChatFormatter.Color color; - private transient ChatFormatter.Format format; + private transient int format; private boolean italic; private boolean bold; private boolean underlined; @@ -32,26 +32,26 @@ public final class TellrawPart implements Serializable { return this; } - public ChatFormatter.Format getFormat() { + public int getFormat() { return format; } - public TellrawPart setFormat(ChatFormatter.Format format) { + public TellrawPart setFormat(int format) { this.format = format; this.italic = false; this.bold = false; this.underlined = false; this.strikethrough = false; this.obfuscated = false; - if (format.equals(ChatFormatter.Format.Italic)) + if ((format & ChatFormatter.Format.Italic.getFlag()) != 0) this.italic = true; - else if (format.equals(ChatFormatter.Format.Bold)) + else if ((format & ChatFormatter.Format.Bold.getFlag()) != 0) this.bold = true; - else if (format.equals(ChatFormatter.Format.Underlined)) + else if ((format & ChatFormatter.Format.Underlined.getFlag()) != 0) this.underlined = true; - else if (format.equals(ChatFormatter.Format.Strikethrough)) + else if ((format & ChatFormatter.Format.Strikethrough.getFlag()) != 0) this.strikethrough = true; - else if (format.equals(ChatFormatter.Format.Obfuscated)) + else if ((format & ChatFormatter.Format.Obfuscated.getFlag()) != 0) this.obfuscated = true; else throw new UnsupportedOperationException("Trying to set to an unknown format!");