From 08dc4dbaa7ebb137846e70a7fa5fe93860aaae0d Mon Sep 17 00:00:00 2001 From: TheE Date: Mon, 31 Jul 2017 16:17:07 +0200 Subject: [PATCH] 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())); } }