Fixed bold formatting, other fixes

JUnit is awesome
Diffing the expected and actual result in a small fraction of the time
needed to load the server
This commit is contained in:
Norbi Peti 2017-04-15 01:45:30 +02:00
parent 758332faf6
commit ec5c90ecc8
7 changed files with 200 additions and 66 deletions

36
Notes.txt Normal file
View file

@ -0,0 +1,36 @@
Expected:
***test***
||- ||-
||: bold
-: italic
Actual:
***test***
||- ||-
-|| -||
- -
nextSection:
*: italic(0)
**:
Either italic(0), bold(0) - Delete italic
bold(0), italic(1) - Delete italic
bold(0)
Or bold(0), italic(0) - Delete italic?
italic, italic - 0-length section as result, delete?
takenStart, takenEnd
because it's ordered, the indexes will be either the same or ascending
^^ Implemented
**test**
^ ^ <-- !
start end
RemChar: 2
tes*

View file

@ -53,16 +53,15 @@ public class ChatProcessing {
} }
static { static {
commonFormatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(Format.Bold) commonFormatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setBold(true)
.setRemoveCharCount((short) 2).setRange(true).build()); .setRemoveCharCount((short) 2).setRange(true).build());
commonFormatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(Format.Italic) commonFormatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setItalic(true)
.setRemoveCharCount((short) 1).setRange(true).build()); .setRemoveCharCount((short) 1).setRange(true).build());
commonFormatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setFormat(Format.Underlined) commonFormatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setUnderlined(true)
.setRemoveCharCount((short) 1).setRange(true).build()); .setRemoveCharCount((short) 1).setRange(true).build());
commonFormatters.add(ESCAPE_FORMATTER); commonFormatters.add(ESCAPE_FORMATTER);
// URLs + Rainbow text commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setUnderlined(true).setOpenlink("$1")
commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined) .setRange(true).build());
.setOpenlink("$1").setRange(true).build());
commonFormatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature commonFormatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature
commonFormatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) commonFormatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua)
.setOnmatch((String match) -> { .setOnmatch((String match) -> {

View file

@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender;
import buttondevteam.chat.PluginMain; import buttondevteam.chat.PluginMain;
public class DebugCommand extends AdminCommandBase { public class DebugCommand extends AdminCommandBase {
private static boolean DebugMode = false; public static boolean DebugMode = false;
@Override @Override
public String[] GetHelpText(String alias) { public String[] GetHelpText(String alias) {
@ -20,13 +20,15 @@ public class DebugCommand extends AdminCommandBase {
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) { public boolean OnCommand(CommandSender sender, String alias, String[] args) {
sender.sendMessage("§eDebug mode " sender.sendMessage("§eDebug mode " + ((DebugMode = !DebugMode) ? "§aenabled." : "§cdisabled."));
+ ((DebugMode = !DebugMode) ? "§aenabled." : "§cdisabled."));
return true; return true;
} }
public static void SendDebugMessage(String message) { public static void SendDebugMessage(String message) {
if (DebugMode) if (DebugMode)
PluginMain.Instance.getLogger().info(message); if (PluginMain.Instance != null)
PluginMain.Instance.getLogger().info(message);
else
System.out.println(message);
} }
} }

View file

@ -15,7 +15,11 @@ import buttondevteam.lib.chat.*;
public final class ChatFormatter { public final class ChatFormatter {
private Pattern regex; private Pattern regex;
private Format format; private boolean italic;
private boolean bold;
private boolean underlined;
private boolean strikethrough;
private boolean obfuscated;
private Color color; private Color color;
private Function<String, String> onmatch; private Function<String, String> onmatch;
private String openlink; private String openlink;
@ -24,14 +28,23 @@ public final class ChatFormatter {
private short removecharpos = -1; private short removecharpos = -1;
private boolean isrange; private boolean isrange;
public ChatFormatter(Pattern regex, Format format, Color color, Function<String, String> onmatch, String openlink, public ChatFormatter(Pattern regex, boolean italic, boolean bold, boolean underlined, boolean strikethrough,
Priority priority, short removecharcount, short removecharpos, boolean isrange) { boolean obfuscated, Color color, Function<String, String> onmatch, String openlink, Priority priority,
short removecharcount, short removecharpos, boolean isrange) {
super();
this.regex = regex; this.regex = regex;
this.format = format; this.italic = italic;
this.bold = bold;
this.underlined = underlined;
this.strikethrough = strikethrough;
this.obfuscated = obfuscated;
this.color = color; this.color = color;
this.onmatch = onmatch; this.onmatch = onmatch;
this.openlink = openlink; this.openlink = openlink;
this.priority = Priority.High; if (priority == null)
this.priority = Priority.Normal;
else
this.priority = priority;
this.removecharcount = removecharcount; this.removecharcount = removecharcount;
this.removecharpos = removecharpos; this.removecharpos = removecharpos;
this.isrange = isrange; this.isrange = isrange;
@ -67,6 +80,8 @@ public final class ChatFormatter {
ArrayList<FormattedSection> combined = new ArrayList<>(); ArrayList<FormattedSection> combined = new ArrayList<>();
Map<ChatFormatter, FormattedSection> nextSection = new HashMap<>(); Map<ChatFormatter, FormattedSection> nextSection = new HashMap<>();
boolean escaped = false; boolean escaped = false;
int takenStart = -1, takenEnd = -1;
ChatFormatter takenFormatter = null;
for (int i = 0; i < sections.size(); i++) { for (int i = 0; i < sections.size(); i++) {
// Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true // Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true
final FormattedSection section = sections.get(i); final FormattedSection section = sections.get(i);
@ -79,16 +94,31 @@ public final class ChatFormatter {
continue; continue;
} }
if (!escaped) { if (!escaped) {
if (section.Start >= takenStart && section.Start <= takenEnd) {
if (section.RemCharFromStart <= takenEnd - takenStart) {
System.out.println("Lose: " + section);
System.out.println("And win: " + takenFormatter);
continue; // The current section loses
}
nextSection.remove(takenFormatter); // The current section wins
System.out.println("Win: " + section);
System.out.println("And lose: " + takenFormatter);
}
takenStart = section.Start;
takenEnd = section.Start + section.RemCharFromStart;
takenFormatter = section.Formatters.get(0);
if (nextSection.containsKey(section.Formatters.get(0))) { if (nextSection.containsKey(section.Formatters.get(0))) {
FormattedSection s = nextSection.remove(section.Formatters.get(0)); FormattedSection s = nextSection.remove(section.Formatters.get(0));
s.End = section.Start; 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); combined.add(s);
DebugCommand.SendDebugMessage("Finished section: " + s); //TODO: Remove smaller sections from IsRange sections DebugCommand.SendDebugMessage("Finished section: " + s);
} else { } else {
DebugCommand.SendDebugMessage("Adding next section: " + section); DebugCommand.SendDebugMessage("Adding next section: " + section);
nextSection.put(section.Formatters.get(0), section); nextSection.put(section.Formatters.get(0), section);
} }
DebugCommand
.SendDebugMessage("New area taken: (" + takenStart + "-" + takenEnd + ") " + takenFormatter);
} else { } else {
DebugCommand.SendDebugMessage("Skipping section: " + section); DebugCommand.SendDebugMessage("Skipping section: " + section);
escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*'
@ -196,7 +226,7 @@ public final class ChatFormatter {
originaltext = textsb.toString(); originaltext = textsb.toString();
DebugCommand.SendDebugMessage("Section text: " + originaltext); DebugCommand.SendDebugMessage("Section text: " + originaltext);
Color color = null; Color color = null;
int format = 0; boolean bold = false, italic = false, underlined = false, strikethrough = false, obfuscated = false;
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) {
@ -205,8 +235,16 @@ public final class ChatFormatter {
originaltext = formatter.onmatch.apply(originaltext); originaltext = formatter.onmatch.apply(originaltext);
if (formatter.color != null) if (formatter.color != null)
color = formatter.color; color = formatter.color;
if (formatter.format != null) if (formatter.bold)
format = formatter.format.getFlag(); // TODO: Fix bold = true;
if (formatter.italic)
italic = true;
if (formatter.underlined)
underlined = true;
if (formatter.strikethrough)
strikethrough = true;
if (formatter.obfuscated)
obfuscated = true;
if (formatter.openlink != null) if (formatter.openlink != null)
openlink = formatter.openlink; openlink = formatter.openlink;
} }
@ -214,8 +252,11 @@ public final class ChatFormatter {
newtp.setText(originaltext); newtp.setText(originaltext);
if (color != null) if (color != null)
newtp.setColor(color); newtp.setColor(color);
if (format != 0) newtp.setBold(bold);
newtp.setFormat(format); newtp.setItalic(italic);
newtp.setUnderlined(underlined);
newtp.setStrikethrough(strikethrough);
newtp.setObfuscated(obfuscated);
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,
(section.Matches.size() > 0 ? openlink.replace("$1", section.Matches.get(0)) : openlink))) (section.Matches.size() > 0 ? openlink.replace("$1", section.Matches.get(0)) : openlink)))
@ -228,7 +269,10 @@ public final class ChatFormatter {
@Override @Override
public String toString() { public String toString() {
return new StringBuilder("F(").append(color).append(", ").append(format).append(", ").append(openlink) return new StringBuilder("F(").append(color).append(", ")
.append(", ").append(priority).append(", ").append(regex).append(")").toString(); .append((bold ? "bold" : "") + (italic ? "italic" : "") + (underlined ? "underlined" : "")
+ (strikethrough ? "strikethrough" : "") + (obfuscated ? "obfuscated" : ""))
.append(", ").append(openlink).append(", ").append(priority).append(", ").append(regex).append(")")
.toString();
} }
} }

View file

@ -7,7 +7,11 @@ import buttondevteam.lib.chat.*;
public class ChatFormatterBuilder { public class ChatFormatterBuilder {
private Pattern regex; private Pattern regex;
private Format format; private boolean italic;
private boolean bold;
private boolean underlined;
private boolean strikethrough;
private boolean obfuscated;
private Color color; private Color color;
private Function<String, String> onmatch; private Function<String, String> onmatch;
private String openlink; private String openlink;
@ -17,8 +21,8 @@ public class ChatFormatterBuilder {
private boolean range = false; private boolean range = false;
public ChatFormatter build() { public ChatFormatter build() {
return new ChatFormatter(regex, format, color, onmatch, openlink, priority, removecharcount, removecharpos, return new ChatFormatter(regex, italic, bold, underlined, strikethrough, obfuscated, color, onmatch, openlink,
range); priority, removecharcount, removecharpos, range);
} }
public Pattern getRegex() { public Pattern getRegex() {
@ -30,12 +34,48 @@ public class ChatFormatterBuilder {
return this; return this;
} }
public Format getFormat() { public boolean isItalic() {
return format; return italic;
} }
public ChatFormatterBuilder setFormat(Format format) { public ChatFormatterBuilder setItalic(boolean italic) {
this.format = format; this.italic = italic;
return this;
}
public boolean isBold() {
return bold;
}
public ChatFormatterBuilder setBold(boolean bold) {
this.bold = bold;
return this;
}
public boolean isUnderlined() {
return underlined;
}
public ChatFormatterBuilder setUnderlined(boolean underlined) {
this.underlined = underlined;
return this;
}
public boolean isStrikethrough() {
return strikethrough;
}
public ChatFormatterBuilder setStrikethrough(boolean strikethrough) {
this.strikethrough = strikethrough;
return this;
}
public boolean isObfuscated() {
return obfuscated;
}
public ChatFormatterBuilder setObfuscated(boolean obfuscated) {
this.obfuscated = obfuscated;
return this; return this;
} }

View file

@ -6,11 +6,9 @@ import java.util.List;
import buttondevteam.lib.chat.*; import buttondevteam.lib.chat.*;
@SuppressWarnings("unused")
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 Color color; private Color color;
private transient int format;
private boolean italic; private boolean italic;
private boolean bold; private boolean bold;
private boolean underlined; private boolean underlined;
@ -34,29 +32,48 @@ public final class TellrawPart implements Serializable {
return this; return this;
} }
public int getFormat() { public boolean isItalic() {
return format; return italic;
} }
public TellrawPart setFormat(int format) { public TellrawPart setItalic(boolean italic) {
this.format = format; this.italic = italic;
this.italic = false; return this;
this.bold = false; }
this.underlined = false;
this.strikethrough = false; public boolean isBold() {
this.obfuscated = false; return bold;
if ((format & Format.Italic.getFlag()) != 0) }
this.italic = true;
else if ((format & Format.Bold.getFlag()) != 0) public TellrawPart setBold(boolean bold) {
this.bold = true; this.bold = bold;
else if ((format & Format.Underlined.getFlag()) != 0) return this;
this.underlined = true; }
else if ((format & Format.Strikethrough.getFlag()) != 0)
this.strikethrough = true; public boolean isUnderlined() {
else if ((format & Format.Obfuscated.getFlag()) != 0) return underlined;
this.obfuscated = true; }
else
throw new UnsupportedOperationException("Trying to set to an unknown format!"); public TellrawPart setUnderlined(boolean underlined) {
this.underlined = underlined;
return this;
}
public boolean isStrikethrough() {
return strikethrough;
}
public TellrawPart setStrikethrough(boolean strikethrough) {
this.strikethrough = strikethrough;
return this;
}
public boolean isObfuscated() {
return obfuscated;
}
public TellrawPart setObfuscated(boolean obfuscated) {
this.obfuscated = obfuscated;
return this; return this;
} }

View file

@ -5,31 +5,27 @@ import org.bukkit.command.CommandSender;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
import buttondevteam.chat.formatting.ChatFormatter; import buttondevteam.chat.formatting.ChatFormatter;
import buttondevteam.chat.formatting.TellrawPart; import buttondevteam.chat.formatting.TellrawPart;
import buttondevteam.core.TestPrepare; import buttondevteam.core.TestPrepare;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.Format;
import junit.framework.TestCase; import junit.framework.TestCase;
public class ChatFormatTest extends TestCase { public class ChatFormatTest extends TestCase {
@Test @Test
public void test() { public void test() {
// fail("Not yet implemented");
TestPrepare.PrepareServer(); TestPrepare.PrepareServer();
final CommandSender sender = Mockito.mock(CommandSender.class); final CommandSender sender = Mockito.mock(CommandSender.class);
testMessage(sender, "*test*", new TellrawPart("test").setFormat(Format.Italic.getFlag()).setColor(Color.White)); DebugCommand.DebugMode = true;
testMessage(sender, "**test**", new TellrawPart("test").setFormat(Format.Bold.getFlag()).setColor(Color.White)); testMessage(sender, "*test*", new TellrawPart("test").setItalic(true).setColor(Color.White));
testMessage(sender, "***test***", new TellrawPart("test") testMessage(sender, "**test**", new TellrawPart("test").setBold(true).setColor(Color.White));
.setFormat(Format.Bold.getFlag() | Format.Italic.getFlag()).setColor(Color.White)); testMessage(sender, "***test***", new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White));
testMessage(sender, "***_test_***", testMessage(sender, "***_test_***",
new TellrawPart("test") new TellrawPart("test").setBold(true).setItalic(true).setUnderlined(true).setColor(Color.White));
.setFormat(Format.Bold.getFlag() | Format.Italic.getFlag() | Format.Underlined.getFlag()) testMessage(sender, "***_~~test~~_***", new TellrawPart("test").setBold(true).setItalic(true)
.setColor(Color.White)); .setUnderlined(true).setStrikethrough(true).setColor(Color.White));
testMessage(sender, "***_~~test~~_***",
new TellrawPart("test").setFormat(Format.Bold.getFlag() | Format.Italic.getFlag()
| Format.Underlined.getFlag() | Format.Strikethrough.getFlag()).setColor(Color.White));
} }
void testMessage(final CommandSender sender, final String message, TellrawPart... extras) { void testMessage(final CommandSender sender, final String message, TellrawPart... extras) {