Use regex for checking for the presence of plugin tags

This commit is contained in:
games647 2019-05-04 16:42:33 +02:00
parent 7ca5986b76
commit a974dfd859
No known key found for this signature in database
GPG Key ID: BFC68C8708713A88
4 changed files with 35 additions and 11 deletions

View File

@ -37,11 +37,6 @@ public class ColorPluginAppender extends ColorAppender {
@Override
public LogEvent onAppend(LogEvent logEvent) {
String oldMessage = logEvent.getMessage().getFormattedMessage();
if (logEvent.getLoggerName().isEmpty()) {
// ignore non logging messages like command output
return logEvent;
}
String prefix = '[' + logEvent.getLoggerName() + "] ";
//PaperSpigot append prefix

View File

@ -27,11 +27,6 @@ public class ColorPluginAppender extends ColorAppender {
public LogEvent onAppend(LogEvent logEvent) {
String message = logEvent.getMessage().getFormattedMessage();
String loggerName = logEvent.getLoggerName();
if (logEvent.getLoggerName().isEmpty()) {
// ignore non logging messages like command output
return logEvent;
}
//old message + potential prefix and color codes
StringBuilder msgBuilder = new StringBuilder(message.length() + loggerName.length() + 10);
if (!PROXY_PREFIX.equals(loggerName)) {

View File

@ -10,6 +10,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Attribute;
@ -21,7 +22,10 @@ public class CommonFormatter {
//copied from AnsiEscape in order to provide compatibility with older Minecraft versions
private static final String CSI = "\u001b[";
private static final char SUFFIX = 'm';
private final String reset = Ansi.ansi().a(Ansi.Attribute.RESET).toString();
private static final Pattern TAG_PATTERN = Pattern.compile("^\\[.+\\].*$");
private final String reset = Ansi.ansi().a(Attribute.RESET).toString();
private final Set<String> ignoreMessages;
private final boolean truncateColor;
@ -63,6 +67,9 @@ public class CommonFormatter {
public String colorizePluginTag(String message) {
String newMessage = message;
if (!TAG_PATTERN.matcher(message).matches()) {
return newMessage;
}
String startingColorCode = "";
if (message.startsWith(CSI)) {

View File

@ -39,6 +39,33 @@ public class CommonFormatterTest {
assertThat(formatter.colorizePluginTag("[TestPlugin] msg"), is(expected));
}
@Test
public void testColorizePluginTagNotPresentRight() {
loadPluginColors();
// unmodified
String msg = "[TestPlugin msg";
assertThat(formatter.colorizePluginTag(msg), is(msg));
}
@Test
public void testColorizePluginTagNotPresentLeft() {
loadPluginColors();
// unmodified
String msg = "TestPlugin] msg";
assertThat(formatter.colorizePluginTag(msg), is(msg));
}
@Test
public void testColorizePluginTagWrongOrder() {
loadPluginColors();
// unmodified
String msg = "]TestPlugin[ msg";
assertThat(formatter.colorizePluginTag(msg), is(msg));
}
@Test
public void testColorizeNameDefault() {
loadPluginColors();