From ec5c90ecc8dca2590b70fd3c9ce2c0b53dc8a6b0 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 15 Apr 2017 01:45:30 +0200 Subject: [PATCH] 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 --- Notes.txt | 36 ++++++++++ .../buttondevteam/chat/ChatProcessing.java | 11 ++- .../commands/ucmds/admin/DebugCommand.java | 10 +-- .../chat/formatting/ChatFormatter.java | 72 +++++++++++++++---- .../chat/formatting/ChatFormatterBuilder.java | 54 ++++++++++++-- .../chat/formatting/TellrawPart.java | 63 ++++++++++------ .../buttondevteam/chat/ChatFormatTest.java | 20 +++--- 7 files changed, 200 insertions(+), 66 deletions(-) create mode 100644 Notes.txt diff --git a/Notes.txt b/Notes.txt new file mode 100644 index 0000000..78af086 --- /dev/null +++ b/Notes.txt @@ -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* diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 24a782d..2cb3b89 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -53,16 +53,15 @@ public class ChatProcessing { } 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()); - 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()); - 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()); commonFormatters.add(ESCAPE_FORMATTER); - // URLs + Rainbow text - commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined) - .setOpenlink("$1").setRange(true).build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setUnderlined(true).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(CONSOLE_PING_PATTERN).setColor(Color.Aqua) .setOnmatch((String match) -> { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/DebugCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/DebugCommand.java index 6c03561..8cd99db 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/DebugCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/DebugCommand.java @@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender; import buttondevteam.chat.PluginMain; public class DebugCommand extends AdminCommandBase { - private static boolean DebugMode = false; + public static boolean DebugMode = false; @Override public String[] GetHelpText(String alias) { @@ -20,13 +20,15 @@ public class DebugCommand extends AdminCommandBase { @Override public boolean OnCommand(CommandSender sender, String alias, String[] args) { - sender.sendMessage("§eDebug mode " - + ((DebugMode = !DebugMode) ? "§aenabled." : "§cdisabled.")); + sender.sendMessage("§eDebug mode " + ((DebugMode = !DebugMode) ? "§aenabled." : "§cdisabled.")); return true; } public static void SendDebugMessage(String message) { if (DebugMode) - PluginMain.Instance.getLogger().info(message); + if (PluginMain.Instance != null) + PluginMain.Instance.getLogger().info(message); + else + System.out.println(message); } } diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index e7d587a..e9fae16 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -15,7 +15,11 @@ import buttondevteam.lib.chat.*; public final class ChatFormatter { 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 Function onmatch; private String openlink; @@ -24,14 +28,23 @@ public final class ChatFormatter { private short removecharpos = -1; private boolean isrange; - public ChatFormatter(Pattern regex, Format format, Color color, Function onmatch, String openlink, - Priority priority, short removecharcount, short removecharpos, boolean isrange) { + public ChatFormatter(Pattern regex, boolean italic, boolean bold, boolean underlined, boolean strikethrough, + boolean obfuscated, Color color, Function onmatch, String openlink, Priority priority, + short removecharcount, short removecharpos, boolean isrange) { + super(); 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.onmatch = onmatch; this.openlink = openlink; - this.priority = Priority.High; + if (priority == null) + this.priority = Priority.Normal; + else + this.priority = priority; this.removecharcount = removecharcount; this.removecharpos = removecharpos; this.isrange = isrange; @@ -67,6 +80,8 @@ public final class ChatFormatter { ArrayList combined = new ArrayList<>(); Map nextSection = new HashMap<>(); boolean escaped = false; + int takenStart = -1, takenEnd = -1; + ChatFormatter takenFormatter = null; 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 final FormattedSection section = sections.get(i); @@ -79,16 +94,31 @@ public final class ChatFormatter { continue; } 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))) { 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 combined.add(s); - DebugCommand.SendDebugMessage("Finished section: " + s); //TODO: Remove smaller sections from IsRange sections + DebugCommand.SendDebugMessage("Finished section: " + s); } else { DebugCommand.SendDebugMessage("Adding next section: " + section); nextSection.put(section.Formatters.get(0), section); } + DebugCommand + .SendDebugMessage("New area taken: (" + takenStart + "-" + takenEnd + ") " + takenFormatter); } else { DebugCommand.SendDebugMessage("Skipping section: " + section); escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' @@ -196,7 +226,7 @@ public final class ChatFormatter { originaltext = textsb.toString(); DebugCommand.SendDebugMessage("Section text: " + originaltext); Color color = null; - int format = 0; + boolean bold = false, italic = false, underlined = false, strikethrough = false, obfuscated = false; String openlink = null; section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { @@ -205,8 +235,16 @@ public final class ChatFormatter { originaltext = formatter.onmatch.apply(originaltext); if (formatter.color != null) color = formatter.color; - if (formatter.format != null) - format = formatter.format.getFlag(); // TODO: Fix + if (formatter.bold) + 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) openlink = formatter.openlink; } @@ -214,8 +252,11 @@ public final class ChatFormatter { newtp.setText(originaltext); if (color != null) newtp.setColor(color); - if (format != 0) - newtp.setFormat(format); + newtp.setBold(bold); + newtp.setItalic(italic); + newtp.setUnderlined(underlined); + newtp.setStrikethrough(strikethrough); + newtp.setObfuscated(obfuscated); if (openlink != null && openlink.length() > 0) { newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL, (section.Matches.size() > 0 ? openlink.replace("$1", section.Matches.get(0)) : openlink))) @@ -228,7 +269,10 @@ public final class ChatFormatter { @Override public String toString() { - return new StringBuilder("F(").append(color).append(", ").append(format).append(", ").append(openlink) - .append(", ").append(priority).append(", ").append(regex).append(")").toString(); + return new StringBuilder("F(").append(color).append(", ") + .append((bold ? "bold" : "") + (italic ? "italic" : "") + (underlined ? "underlined" : "") + + (strikethrough ? "strikethrough" : "") + (obfuscated ? "obfuscated" : "")) + .append(", ").append(openlink).append(", ").append(priority).append(", ").append(regex).append(")") + .toString(); } } diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java index a835d7f..ab7474c 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java @@ -7,7 +7,11 @@ import buttondevteam.lib.chat.*; public class ChatFormatterBuilder { 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 Function onmatch; private String openlink; @@ -17,8 +21,8 @@ public class ChatFormatterBuilder { private boolean range = false; public ChatFormatter build() { - return new ChatFormatter(regex, format, color, onmatch, openlink, priority, removecharcount, removecharpos, - range); + return new ChatFormatter(regex, italic, bold, underlined, strikethrough, obfuscated, color, onmatch, openlink, + priority, removecharcount, removecharpos, range); } public Pattern getRegex() { @@ -30,12 +34,48 @@ public class ChatFormatterBuilder { return this; } - public Format getFormat() { - return format; + public boolean isItalic() { + return italic; } - public ChatFormatterBuilder setFormat(Format format) { - this.format = format; + public ChatFormatterBuilder setItalic(boolean italic) { + 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; } diff --git a/src/main/java/buttondevteam/chat/formatting/TellrawPart.java b/src/main/java/buttondevteam/chat/formatting/TellrawPart.java index 4caacb1..0e7dc72 100644 --- a/src/main/java/buttondevteam/chat/formatting/TellrawPart.java +++ b/src/main/java/buttondevteam/chat/formatting/TellrawPart.java @@ -6,11 +6,9 @@ import java.util.List; import buttondevteam.lib.chat.*; -@SuppressWarnings("unused") public final class TellrawPart implements Serializable { private static final long serialVersionUID = 4125357644462144024L; private Color color; - private transient int format; private boolean italic; private boolean bold; private boolean underlined; @@ -34,29 +32,48 @@ public final class TellrawPart implements Serializable { return this; } - public int getFormat() { - return format; + public boolean isItalic() { + return italic; } - 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 & Format.Italic.getFlag()) != 0) - this.italic = true; - else if ((format & Format.Bold.getFlag()) != 0) - this.bold = true; - else if ((format & Format.Underlined.getFlag()) != 0) - this.underlined = true; - else if ((format & Format.Strikethrough.getFlag()) != 0) - this.strikethrough = true; - else if ((format & Format.Obfuscated.getFlag()) != 0) - this.obfuscated = true; - else - throw new UnsupportedOperationException("Trying to set to an unknown format!"); + public TellrawPart setItalic(boolean italic) { + this.italic = italic; + return this; + } + + public boolean isBold() { + return bold; + } + + public TellrawPart setBold(boolean bold) { + this.bold = bold; + return this; + } + + public boolean isUnderlined() { + return underlined; + } + + 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; } diff --git a/src/test/java/buttondevteam/chat/ChatFormatTest.java b/src/test/java/buttondevteam/chat/ChatFormatTest.java index be0982e..2d8a38e 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatTest.java +++ b/src/test/java/buttondevteam/chat/ChatFormatTest.java @@ -5,31 +5,27 @@ import org.bukkit.command.CommandSender; import org.junit.Test; import org.mockito.Mockito; +import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.chat.formatting.ChatFormatter; import buttondevteam.chat.formatting.TellrawPart; import buttondevteam.core.TestPrepare; import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Color; -import buttondevteam.lib.chat.Format; import junit.framework.TestCase; public class ChatFormatTest extends TestCase { @Test public void test() { - // fail("Not yet implemented"); TestPrepare.PrepareServer(); final CommandSender sender = Mockito.mock(CommandSender.class); - testMessage(sender, "*test*", new TellrawPart("test").setFormat(Format.Italic.getFlag()).setColor(Color.White)); - testMessage(sender, "**test**", new TellrawPart("test").setFormat(Format.Bold.getFlag()).setColor(Color.White)); - testMessage(sender, "***test***", new TellrawPart("test") - .setFormat(Format.Bold.getFlag() | Format.Italic.getFlag()).setColor(Color.White)); + 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") - .setFormat(Format.Bold.getFlag() | Format.Italic.getFlag() | Format.Underlined.getFlag()) - .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)); + 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)); } void testMessage(final CommandSender sender, final String message, TellrawPart... extras) {