Implement Chat Formatting per platform

This commit is contained in:
Aikar
2017-06-27 00:24:05 -04:00
parent 2e493fcb49
commit cd109eb266
17 changed files with 250 additions and 45 deletions
@@ -44,6 +44,7 @@ final class ACFPatterns {
public static final Pattern VALID_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]{2,16}$");
public static final Pattern NON_PRINTABLE_CHARACTERS = Pattern.compile("[^\\x20-\\x7F]");
public static final Pattern EQUALS = Pattern.compile("=");
public static final Pattern FORMATTER = Pattern.compile("<(?<type>c1|c2|c3)>(?<msg>.+?)</(c1|c2|c3)>", Pattern.CASE_INSENSITIVE);
@@ -499,7 +499,7 @@ public abstract class BaseCommand {
}
public void showSyntax(CommandIssuer issuer, RegisteredCommand<?> cmd) {
issuer.sendMessage(MessageType.SYNTAX, "&cUsage: /" + cmd.command + " " + cmd.syntaxText);
issuer.sendMessage(MessageType.SYNTAX, "Usage: <c2>/" + cmd.command + "</c2> <c3>" + cmd.syntaxText + "</c3>");
}
public boolean hasPermission(Object issuer) {
@@ -53,4 +53,11 @@ public interface CommandIssuer {
boolean hasPermission(String permission);
void sendMessage(MessageType type, String message);
default String format(CommandManager manager, MessageType type, String message) {
MessageFormatter formatter = manager.formatters.get(type);
if (formatter != null) {
message = formatter.format(message);
}
return message;
}
}
@@ -25,6 +25,7 @@ package co.aikar.commands;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -37,6 +38,13 @@ abstract class CommandManager {
protected CommandReplacements replacements = new CommandReplacements(this);
protected Locales locales = new Locales(this);
protected ExceptionHandler defaultExceptionHandler = null;
protected EnumMap<MessageType, MessageFormatter> formatters = new EnumMap<>(MessageType.class);
{
MessageFormatter plain = message -> message;
formatters.put(MessageType.INFO, plain);
formatters.put(MessageType.SYNTAX, plain);
formatters.put(MessageType.ERROR, plain);
}
/**
* Gets the command contexts manager
@@ -107,13 +115,12 @@ abstract class CommandManager {
return new RegisteredCommand(command, cmdName, method, prefSubCommand);
}
/**
* Sets the default {@link ExceptionHandler} that is called when an exception occurs while executing a command, if the command doesn't have it's own exception handler registered.
*
* @param exceptionHandler the handler that should handle uncaught exceptions
*/
public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler){
public void setDefaultExceptionHandler(ExceptionHandler exceptionHandler) {
defaultExceptionHandler = exceptionHandler;
}
@@ -126,11 +133,11 @@ abstract class CommandManager {
return defaultExceptionHandler;
}
protected boolean handleUncaughtException(BaseCommand scope, RegisteredCommand registeredCommand, CommandIssuer sender, List<String> args, Throwable t){
protected boolean handleUncaughtException(BaseCommand scope, RegisteredCommand registeredCommand, CommandIssuer sender, List<String> args, Throwable t) {
boolean result = false;
if(scope.getExceptionHandler() != null){
if (scope.getExceptionHandler() != null) {
result = scope.getExceptionHandler().execute(scope, registeredCommand, sender, args, t);
}else if(defaultExceptionHandler != null){
} else if (defaultExceptionHandler != null) {
result = defaultExceptionHandler.execute(scope, registeredCommand, sender, args, t);
}
return result;
@@ -143,7 +150,6 @@ abstract class CommandManager {
if (replacements.length > 0) {
message = ACFUtil.replaceStrings(message, replacements);
}
// TODO: Colors?
issuer.sendMessage(type, message);
}
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package co.aikar.commands;
import java.util.regex.Matcher;
public interface MessageFormatter {
String c1(String message);
default String c2(String message) {
return c1(message);
}
default String c3(String message) {
return c2(message);
}
default String format(String message) {
Matcher matcher = ACFPatterns.FORMATTER.matcher(message);
StringBuffer sb = new StringBuffer(message.length());
while (matcher.find()) {
String type = matcher.group("type");
String msg = matcher.group("msg");
switch (type.toLowerCase()) {
case "c3": msg = c3(msg); break;
case "c2": msg = c2(msg); break;
default: msg = c1(msg); break;
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(msg + c1("")));
}
matcher.appendTail(sb);
return c1("") + sb.toString();
}
}