Added RemCharPos support

This commit is contained in:
Norbi Peti 2016-10-02 12:14:54 +02:00
parent c4e22a779e
commit a173cdefb2
3 changed files with 61 additions and 39 deletions

View file

@ -1,6 +1,7 @@
package buttondevteam.chat.formatting; package buttondevteam.chat.formatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -63,22 +64,22 @@ public final class ChatFormatter {
if (sections.size() < 2) if (sections.size() < 2)
break; break;
DebugCommand.SendDebugMessage("i: " + i); DebugCommand.SendDebugMessage("i: " + i);
if (sections.get(i - 1).Start == sections.get(i).Start && sections.get(i - 1).End == sections.get(i).End) { FormattedSection firstSection = sections.get(i - 1);
DebugCommand.SendDebugMessage("Combining sections " + sections.get(i - 1) + " and " + sections.get(i)); DebugCommand.SendDebugMessage("Combining sections " + firstSection + " and " + sections.get(i));
sections.get(i - 1).Formatters.addAll(sections.get(i).Formatters); if (firstSection.Start == sections.get(i).Start && firstSection.End == sections.get(i).End) {
sections.get(i - 1).Matches.addAll(sections.get(i).Matches); firstSection.Formatters.addAll(sections.get(i).Formatters);
if (sections.get(i - 1).RemCharFromStart < sections.get(i).RemCharFromStart) firstSection.Matches.addAll(sections.get(i).Matches);
sections.get(i - 1).RemCharFromStart = sections.get(i).RemCharFromStart; if (firstSection.RemCharFromStart < sections.get(i).RemCharFromStart)
if (sections.get(i - 1).RemCharFromEnd < sections.get(i).RemCharFromEnd) firstSection.RemCharFromStart = sections.get(i).RemCharFromStart;
sections.get(i - 1).RemCharFromEnd = sections.get(i).RemCharFromEnd; if (firstSection.RemCharFromEnd < sections.get(i).RemCharFromEnd)
DebugCommand.SendDebugMessage("To section " + sections.get(i - 1)); firstSection.RemCharFromEnd = sections.get(i).RemCharFromEnd;
firstSection.RemCharPos.addAll(sections.get(i).RemCharPos);
DebugCommand.SendDebugMessage("To section " + firstSection);
sections.remove(i); sections.remove(i);
found = true; found = true;
} else if (sections.get(i - 1).End > sections.get(i).Start } else if (firstSection.End > sections.get(i).Start && firstSection.Start < sections.get(i).End) {
&& sections.get(i - 1).Start < sections.get(i).End) { int origend = firstSection.End;
DebugCommand.SendDebugMessage("Combining sections " + sections.get(i - 1) + " and " + sections.get(i)); firstSection.End = sections.get(i).Start - 1;
int origend = sections.get(i - 1).End;
sections.get(i - 1).End = sections.get(i).Start - 1;
int origend2 = sections.get(i).End; int origend2 = sections.get(i).End;
boolean switchends; boolean switchends;
if (switchends = origend2 < origend) { if (switchends = origend2 < origend) {
@ -86,28 +87,38 @@ public final class ChatFormatter {
origend = origend2; origend = origend2;
origend2 = tmp; origend2 = tmp;
} }
FormattedSection section = new FormattedSection(sections.get(i - 1).Formatters, sections.get(i).Start, FormattedSection section = new FormattedSection(firstSection.Formatters, sections.get(i).Start, origend,
origend, sections.get(i - 1).Matches, sections.get(i).RemCharFromStart, firstSection.Matches, sections.get(i).RemCharFromStart, firstSection.RemCharFromEnd,
sections.get(i - 1).RemCharFromEnd, sections.get(i - 1).RemCharPos); // TODO: RemCharPos Collections.emptyList());
section.Formatters.addAll(sections.get(i).Formatters); // TODO: Add remove counts to every part, then check if they have duplicates section.Formatters.addAll(sections.get(i).Formatters);
section.Matches.addAll(sections.get(i).Matches); // TODO: Clean section.Matches.addAll(sections.get(i).Matches); // TODO: Clean
sections.add(i, section); sections.add(i, section);
nextindex++; nextindex++;
FormattedSection thirdFormattedSection = sections.get(i + 1); FormattedSection thirdFormattedSection = sections.get(i + 1);
if (switchends) { // Use the properties of the first section not the second one if (switchends) { // Use the properties of the first section not the second one
thirdFormattedSection.Formatters.clear(); thirdFormattedSection.Formatters.clear();
thirdFormattedSection.Formatters.addAll(sections.get(i - 1).Formatters); thirdFormattedSection.Formatters.addAll(firstSection.Formatters);
thirdFormattedSection.Matches.clear(); thirdFormattedSection.Matches.clear();
thirdFormattedSection.Matches.addAll(sections.get(i - 1).Matches); thirdFormattedSection.Matches.addAll(firstSection.Matches);
short remchar = section.RemCharFromEnd; short remchar = section.RemCharFromEnd;
section.RemCharFromEnd = thirdFormattedSection.RemCharFromEnd; section.RemCharFromEnd = thirdFormattedSection.RemCharFromEnd;
thirdFormattedSection.RemCharFromEnd = remchar; thirdFormattedSection.RemCharFromEnd = remchar;
} }
sections.get(i - 1).RemCharFromEnd = 0; firstSection.RemCharFromEnd = 0;
thirdFormattedSection.RemCharFromStart = 0; thirdFormattedSection.RemCharFromStart = 0;
thirdFormattedSection.Start = origend + 1; thirdFormattedSection.Start = origend + 1;
thirdFormattedSection.End = origend2; 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(" 2:" + section + "");
DebugCommand.SendDebugMessage(" 3:" + thirdFormattedSection); DebugCommand.SendDebugMessage(" 3:" + thirdFormattedSection);
found = true; found = true;
@ -140,10 +151,14 @@ public final class ChatFormatter {
DebugCommand.SendDebugMessage("Applying section: " + section); DebugCommand.SendDebugMessage("Applying section: " + section);
String originaltext; String originaltext;
int start = section.Start + section.RemCharFromStart, end = section.End + 1 - section.RemCharFromEnd; // TODO: RemCharPos int start = section.Start + section.RemCharFromStart, end = section.End + 1 - section.RemCharFromEnd; // TODO: RemCharPos
originaltext = str.substring(start, end); StringBuilder textsb = new StringBuilder(str.substring(start, end));
DebugCommand.SendDebugMessage("Originaltext: " + originaltext); 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; Color color = null;
Format format = null; int format = 0;
String openlink = null; String openlink = null;
section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority));
for (ChatFormatter formatter : section.Formatters) { for (ChatFormatter formatter : section.Formatters) {
@ -153,7 +168,7 @@ public final class ChatFormatter {
if (formatter.color != null) if (formatter.color != null)
color = formatter.color; color = formatter.color;
if (formatter.format != null) if (formatter.format != null)
format = formatter.format; format = formatter.format.flag; //TODO: Fix
if (formatter.openlink != null) if (formatter.openlink != null)
openlink = formatter.openlink; openlink = formatter.openlink;
} }
@ -161,7 +176,7 @@ public final class ChatFormatter {
newtp.setText(originaltext); newtp.setText(originaltext);
if (color != null) if (color != null)
newtp.setColor(color); newtp.setColor(color);
if (format != null) if (format != 0)
newtp.setFormat(format); newtp.setFormat(format);
if (openlink != null && openlink.length() > 0) { if (openlink != null && openlink.length() > 0) {
newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL, newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL,
@ -187,12 +202,19 @@ public final class ChatFormatter {
Format(String name) { Format(String name) {
this.name = name; this.name = name;
this.flag = 1 << this.ordinal();
} }
@Override @Override
public String getName() { public String getName() {
return name; return name;
} }
private final int flag;
public int getFlag() {
return flag;
}
} }
public enum Color implements TellrawSerializableEnum { public enum Color implements TellrawSerializableEnum {

View file

@ -10,7 +10,7 @@ class FormattedSection {
ArrayList<String> Matches = new ArrayList<String>(); ArrayList<String> Matches = new ArrayList<String>();
short RemCharFromStart; short RemCharFromStart;
short RemCharFromEnd; short RemCharFromEnd;
int RemCharPos; ArrayList<Integer> RemCharPos = new ArrayList<Integer>();
FormattedSection(ChatFormatter formatter, int start, int end, ArrayList<String> matches, short remcharfromstart, FormattedSection(ChatFormatter formatter, int start, int end, ArrayList<String> matches, short remcharfromstart,
short remcharfromend, int remcharpos) { short remcharfromend, int remcharpos) {
@ -20,18 +20,18 @@ class FormattedSection {
Matches.addAll(matches); Matches.addAll(matches);
RemCharFromStart = remcharfromstart; RemCharFromStart = remcharfromstart;
RemCharFromEnd = remcharfromend; RemCharFromEnd = remcharfromend;
RemCharPos = remcharpos; RemCharPos.add(remcharpos);
} }
FormattedSection(Collection<ChatFormatter> formatters, int start, int end, ArrayList<String> matches, FormattedSection(Collection<ChatFormatter> formatters, int start, int end, ArrayList<String> matches,
short remcharfromstart, short remcharfromend, int remcharpos) { short remcharfromstart, short remcharfromend, Collection<Integer> remcharpos) {
Start = start; Start = start;
End = end; End = end;
Formatters.addAll(formatters); Formatters.addAll(formatters);
Matches.addAll(matches); Matches.addAll(matches);
RemCharFromStart = remcharfromstart; RemCharFromStart = remcharfromstart;
RemCharFromEnd = remcharfromend; RemCharFromEnd = remcharfromend;
RemCharPos = remcharpos; RemCharPos.addAll(remcharpos);
} }
@Override @Override

View file

@ -8,7 +8,7 @@ import java.util.List;
public final class TellrawPart implements Serializable { public final class TellrawPart implements Serializable {
private static final long serialVersionUID = 4125357644462144024L; private static final long serialVersionUID = 4125357644462144024L;
private ChatFormatter.Color color; private ChatFormatter.Color color;
private transient ChatFormatter.Format format; private transient int format;
private boolean italic; private boolean italic;
private boolean bold; private boolean bold;
private boolean underlined; private boolean underlined;
@ -32,26 +32,26 @@ public final class TellrawPart implements Serializable {
return this; return this;
} }
public ChatFormatter.Format getFormat() { public int getFormat() {
return format; return format;
} }
public TellrawPart setFormat(ChatFormatter.Format format) { public TellrawPart setFormat(int format) {
this.format = format; this.format = format;
this.italic = false; this.italic = false;
this.bold = false; this.bold = false;
this.underlined = false; this.underlined = false;
this.strikethrough = false; this.strikethrough = false;
this.obfuscated = false; this.obfuscated = false;
if (format.equals(ChatFormatter.Format.Italic)) if ((format & ChatFormatter.Format.Italic.getFlag()) != 0)
this.italic = true; this.italic = true;
else if (format.equals(ChatFormatter.Format.Bold)) else if ((format & ChatFormatter.Format.Bold.getFlag()) != 0)
this.bold = true; this.bold = true;
else if (format.equals(ChatFormatter.Format.Underlined)) else if ((format & ChatFormatter.Format.Underlined.getFlag()) != 0)
this.underlined = true; this.underlined = true;
else if (format.equals(ChatFormatter.Format.Strikethrough)) else if ((format & ChatFormatter.Format.Strikethrough.getFlag()) != 0)
this.strikethrough = true; this.strikethrough = true;
else if (format.equals(ChatFormatter.Format.Obfuscated)) else if ((format & ChatFormatter.Format.Obfuscated.getFlag()) != 0)
this.obfuscated = true; this.obfuscated = true;
else else
throw new UnsupportedOperationException("Trying to set to an unknown format!"); throw new UnsupportedOperationException("Trying to set to an unknown format!");