From e68d229674d4f6853a2255a5d1d5542765db3aa8 Mon Sep 17 00:00:00 2001 From: TheE Date: Sat, 24 Jun 2017 20:34:42 +0200 Subject: [PATCH 01/17] Use Spigot's Maven repository --- pom.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 04f4734..7c4e0d7 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,12 @@ repo + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + @@ -425,4 +431,4 @@ - \ No newline at end of file + From 50c130303453a393b1a312b8d0ebce67b9cbb658 Mon Sep 17 00:00:00 2001 From: TheE Date: Sat, 15 Jul 2017 00:28:56 +0200 Subject: [PATCH 02/17] Only use ColorMapper if jansi is present, ignore it if not This change allows slf4bukkit to run on Bukkit implementations without jansi by making ColorMapper optional: If it cannot be initialized, colors are not mapped. Instead this may (or may not) be done by the Bukkit implementation, that alos handles the logging system everything the output of slf4bukkit is to. --- .../ronjenkins/slf4bukkit/ColorMapper.java | 46 +++++++++++++++---- .../org/slf4j/impl/BukkitLoggerAdapter.java | 41 ++++++++++++----- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java index 506b490..31bc706 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java @@ -16,13 +16,13 @@ */ package info.ronjenkins.slf4bukkit; -import java.util.Map; +import com.google.common.collect.ImmutableMap; import org.bukkit.ChatColor; import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi.Attribute; -import com.google.common.collect.ImmutableMap; +import java.util.Map; /** * Utility class that maps {@link ChatColor} values to their JAnsi equivalents, @@ -32,8 +32,11 @@ import com.google.common.collect.ImmutableMap; */ public final class ColorMapper { + private ColorMapper() { + } + // @formatter:off - private static final Map MAP = ImmutableMap.builder() + private final Map MAP = ImmutableMap.builder() .put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()) .put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()) .put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()) @@ -59,20 +62,47 @@ public final class ColorMapper { .build(); // @formatter:on + /** + * Attempts to create a new ColorMapper. + * + * @return a new ColorMapper instance + * @throws UnsupportedByBukkitImplementationException if colorMapping is not supported on the BukkitImplementation on + * which this method is called + */ + public static ColorMapper create() throws UnsupportedByBukkitImplementationException { + try { + return new ColorMapper(); + } catch (Throwable e) { + throw new UnsupportedByBukkitImplementationException(e); + } + + } + /** * Translates {@link ChatColor} directives to their JAnsi equivalents. * - * @param input - * null is coerced to the empty string. + * @param input null is coerced to the empty string. * @return never null. */ - public static String map(final String input) { - if (input == null) { return ""; } + public String map(final String input) { + if (input == null) { + return ""; + } String output = input; - for (final Map.Entry mapping : ColorMapper.MAP.entrySet()) { + for (final Map.Entry mapping : MAP.entrySet()) { output = output.replace(mapping.getKey().toString(), mapping.getValue()); } return output; } + /** + * Thrown when an operation is not supported by the Bukkit implementation the operation runs on. + */ + public static class UnsupportedByBukkitImplementationException extends Exception { + + private UnsupportedByBukkitImplementationException(Throwable e) { + super(e); + } + } + } diff --git a/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java b/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java index 9ffb5e9..50d3bb2 100644 --- a/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java +++ b/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java @@ -44,17 +44,11 @@ */ package org.slf4j.impl; +import com.google.common.collect.ImmutableMap; + import info.ronjenkins.slf4bukkit.ColorMapper; import info.ronjenkins.slf4bukkit.ColorMarker; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.apache.commons.lang.exception.ExceptionUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -67,7 +61,13 @@ import org.slf4j.helpers.FormattingTuple; import org.slf4j.helpers.MessageFormatter; import org.yaml.snakeyaml.Yaml; -import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** *

@@ -219,11 +219,22 @@ public final class BukkitLoggerAdapter implements Logger { private final String name; // The short name of this simple log instance private transient String shortLogName = null; + private final ThreadLocal mapper; // NOTE: BukkitPluginLoggerAdapter constructor should have only package access // so that only BukkitPluginLoggerFactory be able to create one. BukkitLoggerAdapter(final String name) { this.name = name; + this.mapper = new ThreadLocal() { + @Override + protected ColorMapper initialValue() { + try { + return ColorMapper.create(); + } catch (ColorMapper.UnsupportedByBukkitImplementationException ignore) { + return null; + } + } + }; } /** @@ -352,7 +363,7 @@ public final class BukkitLoggerAdapter implements Logger { * * @param property * the config property where the map exists. - * @param defaultValue + * @param defaultValues * the fallback values returned by this method. * @return never null, always contains one mapping for each {@link Level}, and * contains no null keys/values. Equal to {@code defaultValue} if the @@ -1043,6 +1054,14 @@ public final class BukkitLoggerAdapter implements Logger { // Log the message. logger.log(BukkitLoggerAdapter.slf4jLevelIntToBukkitJULLevel(level), - ColorMapper.map(buf.toString())); + mapColors(buf.toString())); + } + + private String mapColors(String input) { + ColorMapper colorMapper = mapper.get(); + if (colorMapper != null) { + return colorMapper.map(input); + } + return input; } } From aa00ff8bd129981c7cbd741889f66d1dfd83c2e6 Mon Sep 17 00:00:00 2001 From: TheE Date: Sat, 15 Jul 2017 00:43:45 +0200 Subject: [PATCH 03/17] Change group-id to 'io.github.mywarp' to prevent confusion --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7c4e0d7..0372f32 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ 4.0.0 - info.ronjenkins + io.github.mywarp slf4bukkit 0.1.5-SNAPSHOT SLF4Bukkit From 59473992c4032226ab023715059e140ff12f1176 Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Sun, 30 Jul 2017 23:41:04 -0400 Subject: [PATCH 04/17] Push release commit back to master --- deploy.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index d1859fb..d8f6106 100644 --- a/deploy.sh +++ b/deploy.sh @@ -14,7 +14,8 @@ git config --global user.name "Ronald Jack Jenkins Jr." POM_VERSION=$(cat .version | xargs) git add pom.xml README.md -git commit -m "Committing release $POM_VERSION" +git commit -m "Committing release $POM_VERSION [skip ci]" +git push git tag "$POM_VERSION" git push origin "$POM_VERSION" From 3ab68c1e370cb1631b26899bb1541b62589f557c Mon Sep 17 00:00:00 2001 From: TheE Date: Mon, 31 Jul 2017 16:16:40 +0200 Subject: [PATCH 05/17] Revert "Change group-id to 'io.github.mywarp' to prevent confusion" This reverts commit aa00ff8bd129981c7cbd741889f66d1dfd83c2e6. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 0372f32..7c4e0d7 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ 4.0.0 - io.github.mywarp + info.ronjenkins slf4bukkit 0.1.5-SNAPSHOT SLF4Bukkit From 08dc4dbaa7ebb137846e70a7fa5fe93860aaae0d Mon Sep 17 00:00:00 2001 From: TheE Date: Mon, 31 Jul 2017 16:17:07 +0200 Subject: [PATCH 06/17] Revert "Only use ColorMapper if jansi is present, ignore it if not" This reverts commit 50c130303453a393b1a312b8d0ebce67b9cbb658. --- .../ronjenkins/slf4bukkit/ColorMapper.java | 46 ++++--------------- .../org/slf4j/impl/BukkitLoggerAdapter.java | 41 +++++------------ 2 files changed, 19 insertions(+), 68 deletions(-) diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java index 31bc706..506b490 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java @@ -16,13 +16,13 @@ */ package info.ronjenkins.slf4bukkit; -import com.google.common.collect.ImmutableMap; +import java.util.Map; import org.bukkit.ChatColor; import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi.Attribute; -import java.util.Map; +import com.google.common.collect.ImmutableMap; /** * Utility class that maps {@link ChatColor} values to their JAnsi equivalents, @@ -32,11 +32,8 @@ import java.util.Map; */ public final class ColorMapper { - private ColorMapper() { - } - // @formatter:off - private final Map MAP = ImmutableMap.builder() + private static final Map MAP = ImmutableMap.builder() .put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()) .put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()) .put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()) @@ -62,47 +59,20 @@ public final class ColorMapper { .build(); // @formatter:on - /** - * Attempts to create a new ColorMapper. - * - * @return a new ColorMapper instance - * @throws UnsupportedByBukkitImplementationException if colorMapping is not supported on the BukkitImplementation on - * which this method is called - */ - public static ColorMapper create() throws UnsupportedByBukkitImplementationException { - try { - return new ColorMapper(); - } catch (Throwable e) { - throw new UnsupportedByBukkitImplementationException(e); - } - - } - /** * Translates {@link ChatColor} directives to their JAnsi equivalents. * - * @param input null is coerced to the empty string. + * @param input + * null is coerced to the empty string. * @return never null. */ - public String map(final String input) { - if (input == null) { - return ""; - } + public static String map(final String input) { + if (input == null) { return ""; } String output = input; - for (final Map.Entry mapping : MAP.entrySet()) { + for (final Map.Entry mapping : ColorMapper.MAP.entrySet()) { output = output.replace(mapping.getKey().toString(), mapping.getValue()); } return output; } - /** - * Thrown when an operation is not supported by the Bukkit implementation the operation runs on. - */ - public static class UnsupportedByBukkitImplementationException extends Exception { - - private UnsupportedByBukkitImplementationException(Throwable e) { - super(e); - } - } - } diff --git a/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java b/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java index 50d3bb2..9ffb5e9 100644 --- a/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java +++ b/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java @@ -44,11 +44,17 @@ */ package org.slf4j.impl; -import com.google.common.collect.ImmutableMap; - import info.ronjenkins.slf4bukkit.ColorMapper; import info.ronjenkins.slf4bukkit.ColorMarker; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.apache.commons.lang.exception.ExceptionUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -61,13 +67,7 @@ import org.slf4j.helpers.FormattingTuple; import org.slf4j.helpers.MessageFormatter; import org.yaml.snakeyaml.Yaml; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.google.common.collect.ImmutableMap; /** *

@@ -219,22 +219,11 @@ public final class BukkitLoggerAdapter implements Logger { private final String name; // The short name of this simple log instance private transient String shortLogName = null; - private final ThreadLocal mapper; // NOTE: BukkitPluginLoggerAdapter constructor should have only package access // so that only BukkitPluginLoggerFactory be able to create one. BukkitLoggerAdapter(final String name) { this.name = name; - this.mapper = new ThreadLocal() { - @Override - protected ColorMapper initialValue() { - try { - return ColorMapper.create(); - } catch (ColorMapper.UnsupportedByBukkitImplementationException ignore) { - return null; - } - } - }; } /** @@ -363,7 +352,7 @@ public final class BukkitLoggerAdapter implements Logger { * * @param property * the config property where the map exists. - * @param defaultValues + * @param defaultValue * the fallback values returned by this method. * @return never null, always contains one mapping for each {@link Level}, and * contains no null keys/values. Equal to {@code defaultValue} if the @@ -1054,14 +1043,6 @@ public final class BukkitLoggerAdapter implements Logger { // Log the message. logger.log(BukkitLoggerAdapter.slf4jLevelIntToBukkitJULLevel(level), - mapColors(buf.toString())); - } - - private String mapColors(String input) { - ColorMapper colorMapper = mapper.get(); - if (colorMapper != null) { - return colorMapper.map(input); - } - return input; + ColorMapper.map(buf.toString())); } } From 183c533c9f44420b2759e4a8d45481dfd02276aa Mon Sep 17 00:00:00 2001 From: TheE Date: Mon, 31 Jul 2017 16:17:39 +0200 Subject: [PATCH 07/17] Revert "Use Spigot's Maven repository" This reverts commit e68d229674d4f6853a2255a5d1d5542765db3aa8. --- pom.xml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 7c4e0d7..04f4734 100644 --- a/pom.xml +++ b/pom.xml @@ -50,12 +50,6 @@ repo - - - spigot-repo - https://hub.spigotmc.org/nexus/content/groups/public/ - - @@ -431,4 +425,4 @@ - + \ No newline at end of file From c88eab84a808dd8b5c5f77ec2194b43bfd7cd42d Mon Sep 17 00:00:00 2001 From: TheE Date: Mon, 31 Jul 2017 16:43:19 +0200 Subject: [PATCH 08/17] Only use ColorMapper if jAnsi is present, ignore it if not This change allows slf4bukkit to run on Bukkit implementations without jansi by making ColorMapper an interface. Instances are created by using a ColorMappingFactory: If the Bukkit implementation bundles jAnsi, an AnsiColorMapper is returned that maps ChatColors to their ansi equivalents; if the Bukkit implementation does not bundle jAnsi, a NotSupportedColorMapper is returned that returns strings as is. --- .../slf4bukkit/AnsiColorMapper.java | 80 +++++++++++++++++++ .../ronjenkins/slf4bukkit/ColorMapper.java | 50 +----------- .../slf4bukkit/ColorMapperFactory.java | 23 ++++++ .../slf4bukkit/NotSupportedColorMapper.java | 12 +++ .../org/slf4j/impl/BukkitLoggerAdapter.java | 4 +- 5 files changed, 122 insertions(+), 47 deletions(-) create mode 100644 src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java create mode 100644 src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java create mode 100644 src/main/java/info/ronjenkins/slf4bukkit/NotSupportedColorMapper.java diff --git a/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java b/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java new file mode 100644 index 0000000..6ced4e0 --- /dev/null +++ b/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2016 Ronald Jack Jenkins Jr. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.ronjenkins.slf4bukkit; + +import com.google.common.collect.ImmutableMap; + +import org.bukkit.ChatColor; +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.Ansi.Attribute; + +import java.util.Map; + +/** + * Maps {@link ChatColor} values to their JAnsi equivalents. + * + *

This class might not always be instantiable as jansi might not be present at runtime.

+ * + * @author Ronald Jack Jenkins Jr. + */ +final class AnsiColorMapper implements ColorMapper { + + AnsiColorMapper() throws Throwable { + // Calling this constructor could result in a Throwable because JAnsi, which is required to execute code + // in this class, might not be present at runtime, thus expected classes are not found. + } + + // @formatter:off + private final Map MAP = ImmutableMap.builder() + .put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()) + .put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()) + .put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()) + .put(ChatColor.DARK_AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString()) + .put(ChatColor.DARK_RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString()) + .put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString()) + .put(ChatColor.GOLD, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString()) + .put(ChatColor.GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString()) + .put(ChatColor.DARK_GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString()) + .put(ChatColor.BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString()) + .put(ChatColor.GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString()) + .put(ChatColor.AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString()) + .put(ChatColor.RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).bold().toString()) + .put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString()) + .put(ChatColor.YELLOW, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString()) + .put(ChatColor.WHITE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString()) + .put(ChatColor.MAGIC, Ansi.ansi().a(Attribute.BLINK_SLOW).toString()) + .put(ChatColor.BOLD, Ansi.ansi().a(Attribute.UNDERLINE_DOUBLE).toString()) + .put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Attribute.STRIKETHROUGH_ON).toString()) + .put(ChatColor.UNDERLINE, Ansi.ansi().a(Attribute.UNDERLINE).toString()) + .put(ChatColor.ITALIC, Ansi.ansi().a(Attribute.ITALIC).toString()) + .put(ChatColor.RESET, Ansi.ansi().a(Attribute.RESET).toString()) + .build(); + // @formatter:on + + @Override + public String map(final String input) { + if (input == null) { + return ""; + } + String output = input; + for (final Map.Entry mapping : MAP.entrySet()) { + output = output.replace(mapping.getKey().toString(), mapping.getValue()); + } + return output; + } + +} diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java index 506b490..bafaccb 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapper.java @@ -16,63 +16,21 @@ */ package info.ronjenkins.slf4bukkit; -import java.util.Map; - import org.bukkit.ChatColor; -import org.fusesource.jansi.Ansi; -import org.fusesource.jansi.Ansi.Attribute; - -import com.google.common.collect.ImmutableMap; /** - * Utility class that maps {@link ChatColor} values to their JAnsi equivalents, + * Utility class that maps {@link ChatColor} values to their equivalents, * so that messages logged to the console are formatted correctly. - * - * @author Ronald Jack Jenkins Jr. */ -public final class ColorMapper { - - // @formatter:off - private static final Map MAP = ImmutableMap.builder() - .put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()) - .put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()) - .put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()) - .put(ChatColor.DARK_AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString()) - .put(ChatColor.DARK_RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString()) - .put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString()) - .put(ChatColor.GOLD, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString()) - .put(ChatColor.GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString()) - .put(ChatColor.DARK_GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString()) - .put(ChatColor.BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString()) - .put(ChatColor.GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString()) - .put(ChatColor.AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString()) - .put(ChatColor.RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).bold().toString()) - .put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString()) - .put(ChatColor.YELLOW, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString()) - .put(ChatColor.WHITE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString()) - .put(ChatColor.MAGIC, Ansi.ansi().a(Attribute.BLINK_SLOW).toString()) - .put(ChatColor.BOLD, Ansi.ansi().a(Attribute.UNDERLINE_DOUBLE).toString()) - .put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Attribute.STRIKETHROUGH_ON).toString()) - .put(ChatColor.UNDERLINE, Ansi.ansi().a(Attribute.UNDERLINE).toString()) - .put(ChatColor.ITALIC, Ansi.ansi().a(Attribute.ITALIC).toString()) - .put(ChatColor.RESET, Ansi.ansi().a(Attribute.RESET).toString()) - .build(); - // @formatter:on +public interface ColorMapper { /** - * Translates {@link ChatColor} directives to their JAnsi equivalents. + * Translates {@link ChatColor} directives to their equivalents. * * @param input * null is coerced to the empty string. * @return never null. */ - public static String map(final String input) { - if (input == null) { return ""; } - String output = input; - for (final Map.Entry mapping : ColorMapper.MAP.entrySet()) { - output = output.replace(mapping.getKey().toString(), mapping.getValue()); - } - return output; - } + String map(String input); } diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java new file mode 100644 index 0000000..67a0c58 --- /dev/null +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java @@ -0,0 +1,23 @@ +package info.ronjenkins.slf4bukkit; + +/** + * Creates {@code ColorMapper} instances. + * + * @see ColorMapper + */ +public final class ColorMapperFactory { + + /** + * Creates an new {@code ColorMapper} instance. + * + * @return a new instance + */ + public static ColorMapper create() { + try { + return new AnsiColorMapper(); + } catch (Throwable throwable) { + return new NotSupportedColorMapper(); + } + } + +} diff --git a/src/main/java/info/ronjenkins/slf4bukkit/NotSupportedColorMapper.java b/src/main/java/info/ronjenkins/slf4bukkit/NotSupportedColorMapper.java new file mode 100644 index 0000000..d67fda4 --- /dev/null +++ b/src/main/java/info/ronjenkins/slf4bukkit/NotSupportedColorMapper.java @@ -0,0 +1,12 @@ +package info.ronjenkins.slf4bukkit; + +/** + * Does not do any mapping but simply returns the given string or, if {@code null} is given, an empty string. + */ +final class NotSupportedColorMapper implements ColorMapper { + + @Override + public String map(final String input) { + return input == null ? "" : input; + } +} diff --git a/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java b/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java index 9ffb5e9..1e53511 100644 --- a/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java +++ b/src/main/java/org/slf4j/impl/BukkitLoggerAdapter.java @@ -45,6 +45,7 @@ package org.slf4j.impl; import info.ronjenkins.slf4bukkit.ColorMapper; +import info.ronjenkins.slf4bukkit.ColorMapperFactory; import info.ronjenkins.slf4bukkit.ColorMarker; import java.io.IOException; @@ -218,6 +219,7 @@ public final class BukkitLoggerAdapter implements Logger { // The logger name. private final String name; // The short name of this simple log instance + private final ColorMapper mapper = ColorMapperFactory.create(); private transient String shortLogName = null; // NOTE: BukkitPluginLoggerAdapter constructor should have only package access @@ -1043,6 +1045,6 @@ public final class BukkitLoggerAdapter implements Logger { // Log the message. logger.log(BukkitLoggerAdapter.slf4jLevelIntToBukkitJULLevel(level), - ColorMapper.map(buf.toString())); + mapper.map(buf.toString())); } } From fd336c1ce058360bedea5352a746cd08042e43ec Mon Sep 17 00:00:00 2001 From: TheE Date: Wed, 2 Aug 2017 00:45:46 +0200 Subject: [PATCH 09/17] Replace comment with javadoc --- .../ronjenkins/slf4bukkit/AnsiColorMapper.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java b/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java index 6ced4e0..b6a50eb 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/AnsiColorMapper.java @@ -25,17 +25,27 @@ import org.fusesource.jansi.Ansi.Attribute; import java.util.Map; /** - * Maps {@link ChatColor} values to their JAnsi equivalents. + * Maps {@link ChatColor} values to their Jansi equivalents. * - *

This class might not always be instantiable as jansi might not be present at runtime.

+ *

This class might not always be instantiable as Jansi might not be present at runtime.

* * @author Ronald Jack Jenkins Jr. */ final class AnsiColorMapper implements ColorMapper { + /** + * Creates a new instance. + * + *

This class relies on Jansi. If this dependency is not present at runtime, it cannot be initialized as the JVM + * raises a {@code Throwable} due to missing dependencies.

+ * + *

In this case callers of this constructor should fall back to an alternative {@code ColorMapper} + * implementation.

+ * + * @throws Throwable if Jansi is not present at runtime + */ AnsiColorMapper() throws Throwable { - // Calling this constructor could result in a Throwable because JAnsi, which is required to execute code - // in this class, might not be present at runtime, thus expected classes are not found. + //see javadoc } // @formatter:off From b28d49092ec1a54ac9db180138ba483980a9a665 Mon Sep 17 00:00:00 2001 From: TheE Date: Wed, 2 Aug 2017 17:03:35 +0200 Subject: [PATCH 10/17] Make jansi an optional dependency in pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index f27acd1..4d95eff 100644 --- a/pom.xml +++ b/pom.xml @@ -73,6 +73,7 @@ jansi 1.11 provided + true
From ebcf0b6132c6397d2e9d08ab01a61e030d3c13f7 Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Tue, 8 Aug 2017 20:06:17 -0400 Subject: [PATCH 11/17] Improve comments/strip ChatColors --- pom.xml | 2 +- .../slf4bukkit/AnsiColorMapper.java | 22 ++++--------- .../ronjenkins/slf4bukkit/ColorMapper.java | 10 +++--- .../slf4bukkit/ColorMapperFactory.java | 23 +++++++++++-- .../slf4bukkit/NotSupportedColorMapper.java | 32 +++++++++++++++++-- .../org/slf4j/impl/BukkitLoggerAdapter.java | 10 +++++- 6 files changed, 73 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 4d95eff..456c8d5 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ @@ -42,3 +42,4 @@ If you wish to use [SLF4J](http://slf4j.org) in your Bukkit plugin, or if your p + SLF4Bukkit issues `ChatColor.RESET` after every log message, so you don't have to worry about resetting after each message. + You can use the [ColorString](${project.url}/apidocs/info/ronjenkins/slf4bukkit/ColorString.html) class to easily create colored log messages. + For a consistent user experience, it's recommended that you perform all logging via SLF4Bukkit and not use `Plugin.getLogger()`. + + Any color-related features are silently stripped/ignored when SLF4Bukkit runs on a Bukkit implementation where JAnsi is not available (e.g. PaperSpigot). diff --git a/src/site/markdown/devs.md b/src/site/markdown/devs.md index 5539dd4..02d3f23 100644 --- a/src/site/markdown/devs.md +++ b/src/site/markdown/devs.md @@ -1,15 +1,15 @@ -## Copyright (C) 2016 Ronald Jack Jenkins Jr. -## +## Copyright (C) 2016-2017 Ronald Jack Jenkins Jr. +## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program. If not, see . #set($h1 = '#') @@ -41,3 +41,4 @@ If you wish to use [SLF4J](http://slf4j.org) in your Bukkit plugin, or if your p + SLF4Bukkit issues `ChatColor.RESET` after every log message, so you don't have to worry about resetting after each message. + You can use the [ColorString](apidocs/info/ronjenkins/slf4bukkit/ColorString.html) class to easily create colored log messages. + For a consistent user experience, it's recommended that you perform all logging via SLF4Bukkit and not use `Plugin.getLogger()`. + + Any color-related features are silently stripped/ignored when SLF4Bukkit runs on a Bukkit implementation where JAnsi is not available (e.g. PaperSpigot). diff --git a/src/site/markdown/index.md b/src/site/markdown/index.md index 91d0b15..12159f7 100644 --- a/src/site/markdown/index.md +++ b/src/site/markdown/index.md @@ -1,15 +1,15 @@ -## Copyright (C) 2016 Ronald Jack Jenkins Jr. -## +## Copyright (C) 2016-2017 Ronald Jack Jenkins Jr. +## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program. If not, see . #set($h1 = '#') @@ -43,19 +43,19 @@ slf4j: # # If not specified or given an invalid value, defaults to "info". defaultLogLevel: info - + # Shows an "[SLF4J]" header for every message logged through SLF4Bukkit. # # If not specified or given an invalid value, defaults to "false". showHeader: false - + # Shows the full logger name (e.g. "info.ronjenkins.bukkit.MyPlugin"), # wrapped in curly braces. # # If not specified or given an invalid value, defaults to "false". If true, # this overrides "slf4j.showShortLogName". showLogName: false - + # Shows the short logger name, wrapped in curly braces. The short logger name # is the short Java package name format (e.g. a logger named # "info.ronjenkins.bukkit.MyPlugin" would have a short name of @@ -64,13 +64,13 @@ slf4j: # If not specified or given an invalid value, defaults to "true". If # "slf4j.showLogName" is true, this option is ignored. showShortLogName: true - + # Shows the name of the logging thread, wrapped in brackets. You probably # don't want this information unless you're helping troubleshoot a plugin. # # If not specified or given an invalid value, defaults to "false". showThreadName: false - + # This section controls default colors for logging levels. Each entry in this # section maps one of SLF4J's logging levels to one of SLF4Bukkit's # ColorMarker values. The possible keys (levels) in this section are the @@ -106,16 +106,20 @@ slf4j: # YELLOW # WHITE # NONE (default console color) + # + # If you are running this plugin on a Bukkit implementation that does not + # include the JAnsi library (e.g. PaperSpigot), none of these configuration + # values will be honored and all log output will have no colors. colors: error: RED warn: YELLOW info: NONE debug: NONE trace: NONE - + # This section controls logging levels for individual loggers. log: - + # For each element in this section, the key is the full logger name and the # value is the logging level for that logger. Possible logging levels are # the same as what's available for the "slf4j.defaultLogLevel" property From 07986618eee565b9b364071eed9f3178b05540be Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Tue, 8 Aug 2017 20:20:53 -0400 Subject: [PATCH 13/17] Remove useless TODOs --- src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java index 4428819..c54d7b2 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java @@ -68,7 +68,6 @@ public enum ColorMarker implements Marker { */ @Override public boolean contains(final Marker other) { - // TODO Auto-generated method stub return false; } @@ -119,7 +118,6 @@ public enum ColorMarker implements Marker { */ @Override public boolean hasReferences() { - // TODO Auto-generated method stub return false; } From c030eee392971780c6a40c7ecde144de95e9ea14 Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Tue, 8 Aug 2017 20:29:14 -0400 Subject: [PATCH 14/17] Correct whitespace --- src/github/README.md | 4 ++-- src/site/markdown/devs.md | 6 +++--- src/site/markdown/index.md | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/github/README.md b/src/github/README.md index fe733b7..e600ec9 100644 --- a/src/github/README.md +++ b/src/github/README.md @@ -5,12 +5,12 @@ it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . --> diff --git a/src/site/markdown/devs.md b/src/site/markdown/devs.md index 02d3f23..e97c5e4 100644 --- a/src/site/markdown/devs.md +++ b/src/site/markdown/devs.md @@ -1,15 +1,15 @@ ## Copyright (C) 2016-2017 Ronald Jack Jenkins Jr. -## +## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program. If not, see . #set($h1 = '#') diff --git a/src/site/markdown/index.md b/src/site/markdown/index.md index 12159f7..a099248 100644 --- a/src/site/markdown/index.md +++ b/src/site/markdown/index.md @@ -1,15 +1,15 @@ ## Copyright (C) 2016-2017 Ronald Jack Jenkins Jr. -## +## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program. If not, see . #set($h1 = '#') @@ -43,19 +43,19 @@ slf4j: # # If not specified or given an invalid value, defaults to "info". defaultLogLevel: info - + # Shows an "[SLF4J]" header for every message logged through SLF4Bukkit. # # If not specified or given an invalid value, defaults to "false". showHeader: false - + # Shows the full logger name (e.g. "info.ronjenkins.bukkit.MyPlugin"), # wrapped in curly braces. # # If not specified or given an invalid value, defaults to "false". If true, # this overrides "slf4j.showShortLogName". showLogName: false - + # Shows the short logger name, wrapped in curly braces. The short logger name # is the short Java package name format (e.g. a logger named # "info.ronjenkins.bukkit.MyPlugin" would have a short name of @@ -64,13 +64,13 @@ slf4j: # If not specified or given an invalid value, defaults to "true". If # "slf4j.showLogName" is true, this option is ignored. showShortLogName: true - + # Shows the name of the logging thread, wrapped in brackets. You probably # don't want this information unless you're helping troubleshoot a plugin. # # If not specified or given an invalid value, defaults to "false". showThreadName: false - + # This section controls default colors for logging levels. Each entry in this # section maps one of SLF4J's logging levels to one of SLF4Bukkit's # ColorMarker values. The possible keys (levels) in this section are the @@ -116,10 +116,10 @@ slf4j: info: NONE debug: NONE trace: NONE - + # This section controls logging levels for individual loggers. log: - + # For each element in this section, the key is the full logger name and the # value is the logging level for that logger. Possible logging levels are # the same as what's available for the "slf4j.defaultLogLevel" property From 6eeea9c12a62c51167b754a7c8cb604a3034052f Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Tue, 8 Aug 2017 20:44:49 -0400 Subject: [PATCH 15/17] Allow GMSP_OAUTHTOKEN to be missing --- env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/env.sh b/env.sh index 228337a..813a317 100644 --- a/env.sh +++ b/env.sh @@ -1,2 +1,2 @@ #!/bin/bash -env | grep GMSP_OAUTHTOKEN >> .env +env | grep GMSP_OAUTHTOKEN >> .env || true From 3ed1c8c5f8e4d2f769cc53e62982c8a2e08a2056 Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Tue, 8 Aug 2017 20:50:19 -0400 Subject: [PATCH 16/17] Make ColorMapperFactory uninstantiable --- .../java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java index 7e9d201..e40f042 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMapperFactory.java @@ -24,6 +24,8 @@ package info.ronjenkins.slf4bukkit; */ public final class ColorMapperFactory { + private ColorMapperFactory() {} + /** * Creates an new {@code ColorMapper} instance. * From 16defaf9d1b88dd1ab11cec8f43f350a48031497 Mon Sep 17 00:00:00 2001 From: Ronald Jack Jenkins Jr Date: Tue, 8 Aug 2017 20:57:01 -0400 Subject: [PATCH 17/17] Add more color comments --- src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java | 5 +++++ src/main/java/info/ronjenkins/slf4bukkit/ColorString.java | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java index c54d7b2..e5f3768 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorMarker.java @@ -26,6 +26,11 @@ import org.slf4j.Marker; * SLF4J markers that map to a subset of {@link ChatColor}s. These markers never * contain any references (other markers). * + *

+ * This class does not depend on JAnsi, so it is safe to use even in + * environments where JAnsi is not available (e.g. PaperSpigot). + *

+ * * @author Ronald Jack Jenkins Jr. */ public enum ColorMarker implements Marker { diff --git a/src/main/java/info/ronjenkins/slf4bukkit/ColorString.java b/src/main/java/info/ronjenkins/slf4bukkit/ColorString.java index d0981c9..786174d 100644 --- a/src/main/java/info/ronjenkins/slf4bukkit/ColorString.java +++ b/src/main/java/info/ronjenkins/slf4bukkit/ColorString.java @@ -22,6 +22,13 @@ package info.ronjenkins.slf4bukkit; * current value and then continue adding content to this object. This class is * thread-safe. * + *

+ * Plugins can use this class even if they will be executed in environments + * where JAnsi is not available (e.g. PaperSpigot) because all colors are + * stripped when the message is logged. This class does not depend on JAnsi, + * so it is safe to use in such environments. + *

+ * * @author Ronald Jack Jenkins Jr. */ public final class ColorString {