From e68d229674d4f6853a2255a5d1d5542765db3aa8 Mon Sep 17 00:00:00 2001 From: TheE Date: Sat, 24 Jun 2017 20:34:42 +0200 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 3ab68c1e370cb1631b26899bb1541b62589f557c Mon Sep 17 00:00:00 2001 From: TheE Date: Mon, 31 Jul 2017 16:16:40 +0200 Subject: [PATCH 4/9] 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 5/9] 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 6/9] 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 7/9] 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 8/9] 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 9/9] 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