0

Improve comments/strip ChatColors

This commit is contained in:
Ronald Jack Jenkins Jr
2017-08-08 20:06:17 -04:00
parent a16b336404
commit ebcf0b6132
6 changed files with 73 additions and 26 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
Copyright (C) 2016 Ronald Jack Jenkins Jr. Copyright (C) 2016-2017 Ronald Jack Jenkins Jr., SLF4Bukkit contributors.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Ronald Jack Jenkins Jr. * Copyright (C) 2016-2017 Ronald Jack Jenkins Jr., SLF4Bukkit contributors.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -25,28 +25,20 @@ import org.fusesource.jansi.Ansi.Attribute;
import java.util.Map; import java.util.Map;
/** /**
* Maps {@link ChatColor} values to their Jansi equivalents. * Maps {@link ChatColor} values to their JAnsi equivalents.
*
* <p>This class might not always be instantiable as Jansi might not be present at runtime.</p>
* *
* @author Ronald Jack Jenkins Jr. * @author Ronald Jack Jenkins Jr.
*/ */
final class AnsiColorMapper implements ColorMapper { final class AnsiColorMapper implements ColorMapper {
/** /**
* Creates a new instance. * No-op constructor. Callers must catch {@code Throwable} and handle the
* scenario in which JAnsi is not available by substituting another
* {@link ColorMapper} implementation.
* *
* <p>This class relies on Jansi. If this dependency is not present at runtime, it cannot be initialized as the JVM * @throws Throwable if JAnsi is not present at runtime.
* raises a {@code Throwable} due to missing dependencies.</p>
*
* <p>In this case callers of this constructor should fall back to an alternative {@code ColorMapper}
* implementation.</p>
*
* @throws Throwable if Jansi is not present at runtime
*/ */
AnsiColorMapper() throws Throwable { AnsiColorMapper() throws Throwable {}
//see javadoc
}
// @formatter:off // @formatter:off
private final Map<ChatColor, String> MAP = ImmutableMap.<ChatColor, String>builder() private final Map<ChatColor, String> MAP = ImmutableMap.<ChatColor, String>builder()

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016 Ronald Jack Jenkins Jr. * Copyright (C) 2016-2017 Ronald Jack Jenkins Jr., SLF4Bukkit contributors.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -19,13 +19,15 @@ package info.ronjenkins.slf4bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
/** /**
* Utility class that maps {@link ChatColor} values to their equivalents, * Implementations of this interface map {@link ChatColor} values to their
* so that messages logged to the console are formatted correctly. * equivalents, based on what the current Bukkit implementation supports.
*
* @author Ronald Jack Jenkins Jr.
*/ */
public interface ColorMapper { public interface ColorMapper {
/** /**
* Translates {@link ChatColor} directives to their equivalents. * Translates {@link ChatColor} directives to their string equivalents.
* *
* @param input * @param input
* null is coerced to the empty string. * null is coerced to the empty string.

View File

@@ -1,16 +1,33 @@
/*
* Copyright (C) 2017 TheE, Ronald Jack Jenkins Jr, SLF4Bukkit contributors.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package info.ronjenkins.slf4bukkit; package info.ronjenkins.slf4bukkit;
/** /**
* Creates {@code ColorMapper} instances. * Creates the {@link ColorMapper} instance that best suits the capabilities of
* the current Bukkit environment.
* *
* @see ColorMapper * @author TheE
*/ */
public final class ColorMapperFactory { public final class ColorMapperFactory {
/** /**
* Creates an new {@code ColorMapper} instance. * Creates an new {@code ColorMapper} instance.
* *
* @return a new instance * @return never null.
*/ */
public static ColorMapper create() { public static ColorMapper create() {
try { try {

View File

@@ -1,12 +1,40 @@
/*
* Copyright (C) 2017 TheE, Ronald Jack Jenkins Jr, SLF4Bukkit contributors.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package info.ronjenkins.slf4bukkit; package info.ronjenkins.slf4bukkit;
import org.bukkit.ChatColor;
/** /**
* Does not do any mapping but simply returns the given string or, if {@code null} is given, an empty string. * Strips all {@link ChatColor}s from the input string.
*
* @author TheE
*/ */
final class NotSupportedColorMapper implements ColorMapper { final class NotSupportedColorMapper implements ColorMapper {
@Override @Override
public String map(final String input) { public String map(final String input) {
return input == null ? "" : input; if (input == null) {
return "";
}
String output = input;
for (final ChatColor chatColor : ChatColor.values()) {
output = output.replace(chatColor.toString(), "");
}
return output;
} }
} }

View File

@@ -4,7 +4,7 @@
*/ */
/* /*
* Portions of this file are * Portions of this file are
* Copyright (C) 2016 Ronald Jack Jenkins Jr. * Copyright (C) 2016-2017 Ronald Jack Jenkins Jr., SLF4Bukkit contributors.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -181,6 +181,14 @@ import com.google.common.collect.ImmutableMap;
* of markers does not affect whether or not a given logging level is enabled. * of markers does not affect whether or not a given logging level is enabled.
* </p> * </p>
* *
* <p>
* When executed on a Bukkit implementation that does not contain the JAnsi
* library (e.g. PaperSpigot), all color-related functionality is silently
* ignored. Any messages logged in such an environment by SLF4Bukkit will have
* any {@link ChatColor} values stripped. SLF4Bukkit does not emit any warnings
* when executed in an environment where JAnsi is not available.
* </p>
*
* @author Ceki G&uuml;lc&uuml; * @author Ceki G&uuml;lc&uuml;
* @author <a href="mailto:sanders@apache.org">Scott Sanders</a> * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
* @author Rod Waldhoff * @author Rod Waldhoff