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
@@ -28,9 +28,11 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class BukkitCommandIssuer implements CommandIssuer {
private final BukkitCommandManager manager;
private final CommandSender sender;
BukkitCommandIssuer(CommandSender sender) {
BukkitCommandIssuer(BukkitCommandManager manager, CommandSender sender) {
this.manager = manager;
this.sender = sender;
}
@@ -47,14 +49,7 @@ public class BukkitCommandIssuer implements CommandIssuer {
@Override
public void sendMessage(MessageType type, String message) {
switch (type) {
case ERROR:
case SYNTAX:
sender.sendMessage(ChatColor.RED + ACFBukkitUtil.color(message));
break;
default:
sender.sendMessage(ChatColor.YELLOW + ACFBukkitUtil.color(message));
}
sender.sendMessage(ACFBukkitUtil.color(format(manager, type, message)));
}
@Override
@@ -27,6 +27,7 @@ import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
import co.aikar.timings.lib.MCTiming;
import co.aikar.timings.lib.TimingManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
@@ -89,6 +90,9 @@ public class BukkitCommandManager extends CommandManager {
ACFUtil.sneaky(e);
}
this.commandMap = commandMap;
this.formatters.put(MessageType.ERROR, new BukkitMessageFormatter(ChatColor.RED, ChatColor.YELLOW, ChatColor.RED));
this.formatters.put(MessageType.SYNTAX, new BukkitMessageFormatter(ChatColor.YELLOW, ChatColor.GREEN, ChatColor.WHITE));
this.formatters.put(MessageType.INFO, new BukkitMessageFormatter(ChatColor.BLUE, ChatColor.DARK_GREEN, ChatColor.GREEN));
Bukkit.getPluginManager().registerEvents(new ACFBukkitListener(plugin), plugin);
}
@@ -184,7 +188,7 @@ public class BukkitCommandManager extends CommandManager {
if (!(issuer instanceof CommandSender)) {
throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
}
return new BukkitCommandIssuer((CommandSender) issuer);
return new BukkitCommandIssuer(this, (CommandSender) issuer);
}
@Override
@@ -0,0 +1,58 @@
/*
* 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 org.bukkit.ChatColor;
public class BukkitMessageFormatter implements MessageFormatter {
private final ChatColor color1;
private final ChatColor color2;
private final ChatColor color3;
public BukkitMessageFormatter(ChatColor color1) {
this(color1, color1);
}
public BukkitMessageFormatter(ChatColor color1, ChatColor color2) {
this(color1, color2, color2);
}
public BukkitMessageFormatter(ChatColor color1, ChatColor color2, ChatColor color3) {
this.color1 = color1;
this.color2 = color2;
this.color3 = color3;
}
@Override
public String c1(String message) {
return color1 + message;
}
@Override
public String c2(String message) {
return color2 + message;
}
@Override
public String c3(String message) {
return color3 + message;
}
}
@@ -51,12 +51,12 @@ public class BukkitRootCommand extends Command implements RootCommand {
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return tabComplete(new BukkitCommandIssuer(sender), alias, args);
return tabComplete(new BukkitCommandIssuer(manager, sender), alias, args);
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
return execute(new BukkitCommandIssuer(sender), commandLabel, args);
return execute(new BukkitCommandIssuer(manager, sender), commandLabel, args);
}
private List<String> tabComplete(CommandIssuer sender, String alias, String[] args) throws IllegalArgumentException {
@@ -23,21 +23,23 @@
package co.aikar.commands;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
public class BungeeCommandIssuer implements CommandIssuer{
private final BungeeCommandManager manager;
private final CommandSender sender;
BungeeCommandIssuer(CommandSender sender) {
BungeeCommandIssuer(BungeeCommandManager manager, CommandSender sender) {
this.manager = manager;
this.sender = sender;
}
@Override
public <T> T getIssuer() {
//noinspection unchecked
return (T) sender;
}
@@ -48,14 +50,8 @@ public class BungeeCommandIssuer implements CommandIssuer{
@Override
public void sendMessage(MessageType type, String message) {
switch (type) {
case ERROR:
case SYNTAX:
sender.sendMessage(new TextComponent(ChatColor.RED + ACFBungeeUtil.color(message)));
break;
default:
sender.sendMessage(new TextComponent(ChatColor.YELLOW + ACFBungeeUtil.color(message)));
}
message = format(manager, type, message);
sender.sendMessage(new TextComponent(ACFBungeeUtil.color(message)));
}
@Override
@@ -24,6 +24,7 @@
package co.aikar.commands;
import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;
@@ -46,6 +47,9 @@ public class BungeeCommandManager extends CommandManager {
public BungeeCommandManager(Plugin plugin) {
this.plugin = plugin;
this.formatters.put(MessageType.ERROR, new BungeeMessageFormatter(ChatColor.RED, ChatColor.YELLOW, ChatColor.RED));
this.formatters.put(MessageType.SYNTAX, new BungeeMessageFormatter(ChatColor.YELLOW, ChatColor.GREEN, ChatColor.WHITE));
this.formatters.put(MessageType.INFO, new BungeeMessageFormatter(ChatColor.BLUE, ChatColor.DARK_GREEN, ChatColor.GREEN));
}
public Plugin getPlugin() {
@@ -97,7 +101,7 @@ public class BungeeCommandManager extends CommandManager {
if (!(issuer instanceof CommandSender)) {
throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
}
return new BungeeCommandIssuer((CommandSender) issuer);
return new BungeeCommandIssuer(this, (CommandSender) issuer);
}
@Override
@@ -107,6 +111,7 @@ public class BungeeCommandManager extends CommandManager {
@Override
public <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
//noinspection unchecked
return (R) new BungeeCommandExecutionContext(command, parameter, sender, args, i, passedArgs);
}
@@ -0,0 +1,35 @@
package co.aikar.commands;
import net.md_5.bungee.api.ChatColor;
public class BungeeMessageFormatter implements MessageFormatter {
private final ChatColor color1;
private final ChatColor color2;
private final ChatColor color3;
public BungeeMessageFormatter(ChatColor color1) {
this(color1, color1);
}
public BungeeMessageFormatter(ChatColor color1, ChatColor color2) {
this(color1, color2, color2);
}
public BungeeMessageFormatter(ChatColor color1, ChatColor color2, ChatColor color3) {
this.color1 = color1;
this.color2 = color2;
this.color3 = color3;
}
@Override
public String c1(String message) {
return color1 + message;
}
@Override
public String c2(String message) {
return color2 + message;
}
@Override
public String c3(String message) {
return color3 + message;
}
}
@@ -77,7 +77,7 @@ public class BungeeRootCommand extends Command implements RootCommand, TabExecut
@Override
public void execute(CommandSender sender, String[] args) {
execute(new BungeeCommandIssuer(sender), getName(), args);
execute(new BungeeCommandIssuer(manager, sender), getName(), args);
}
private void execute(CommandIssuer sender, String commandLabel, String[] args) {
@@ -95,7 +95,7 @@ public class BungeeRootCommand extends Command implements RootCommand, TabExecut
@Override
public Iterable<String> onTabComplete(CommandSender commandSender, String[] strings) {
return onTabComplete(new BungeeCommandIssuer(commandSender), getName(), strings);
return onTabComplete(new BungeeCommandIssuer(manager, commandSender), getName(), strings);
}
private List<String> onTabComplete(CommandIssuer sender, String alias, String[] args) throws IllegalArgumentException {
@@ -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();
}
}
@@ -26,15 +26,16 @@ package co.aikar.commands;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.serializer.TextSerializers;
public class SpongeCommandIssuer implements CommandIssuer {
private final SpongeCommandManager manager;
private final CommandSource source;
SpongeCommandIssuer(final CommandSource source) {
SpongeCommandIssuer(SpongeCommandManager manager, final CommandSource source) {
this.manager = manager;
this.source = source;
}
@@ -51,14 +52,8 @@ public class SpongeCommandIssuer implements CommandIssuer {
@Override
public void sendMessage(MessageType type, String message) {
switch (type) {
case ERROR:
case SYNTAX:
this.source.sendMessage(Text.of(TextColors.RED, TextSerializers.LEGACY_FORMATTING_CODE.stripCodes(message)));
break;
default:
this.source.sendMessage(Text.of(TextColors.YELLOW, TextSerializers.LEGACY_FORMATTING_CODE.stripCodes(message)));
}
message = format(manager, type, message);
this.source.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message));
}
@Override
@@ -30,6 +30,7 @@ import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.text.format.TextColors;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
@@ -49,6 +50,10 @@ public class SpongeCommandManager extends CommandManager {
public SpongeCommandManager(PluginContainer plugin) {
this.plugin = plugin;
this.commandTiming = Timings.of(plugin, "Commands");
this.formatters.put(MessageType.ERROR, new SpongeMessageFormatter(TextColors.RED, TextColors.YELLOW, TextColors.RED));
this.formatters.put(MessageType.SYNTAX, new SpongeMessageFormatter(TextColors.YELLOW, TextColors.GREEN, TextColors.WHITE));
this.formatters.put(MessageType.INFO, new SpongeMessageFormatter(TextColors.BLUE, TextColors.DARK_GREEN, TextColors.GREEN));
}
@Override
@@ -106,11 +111,12 @@ public class SpongeCommandManager extends CommandManager {
if (!(issuer instanceof CommandSource)) {
throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
}
return new SpongeCommandIssuer((CommandSource) issuer);
return new SpongeCommandIssuer(this, (CommandSource) issuer);
}
@Override
public <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
//noinspection unchecked
return (R) new SpongeCommandExecutionContext(command, parameter, sender, args, i, passedArgs);
}
@@ -0,0 +1,42 @@
package co.aikar.commands;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.serializer.TextSerializers;
public class SpongeMessageFormatter implements MessageFormatter {
private final TextColor color1;
private final TextColor color2;
private final TextColor color3;
public SpongeMessageFormatter(TextColor color1) {
this(color1, color1);
}
public SpongeMessageFormatter(TextColor color1, TextColor color2) {
this(color1, color2, color2);
}
public SpongeMessageFormatter(TextColor color1, TextColor color2, TextColor color3) {
this.color1 = color1;
this.color2 = color2;
this.color3 = color3;
}
@Override
public String c1(String message) {
return convert(color1, message);
}
@Override
public String c2(String message) {
return convert(color2, message);
}
@Override
public String c3(String message) {
return convert(color3, message);
}
private String convert(TextColor color, String message) {
return TextSerializers.LEGACY_FORMATTING_CODE.serialize(Text.of(color, message));
}
}
@@ -58,7 +58,7 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if(this.execute(new SpongeCommandIssuer(source), this.name, arguments.split(" "))) {
if(this.execute(new SpongeCommandIssuer(manager, source), this.name, arguments.split(" "))) {
return CommandResult.success();
}
return CommandResult.empty();
@@ -66,7 +66,7 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> location) throws CommandException {
return tabComplete(new SpongeCommandIssuer(source), this.name, arguments.split(" "));
return tabComplete(new SpongeCommandIssuer(manager, source), this.name, arguments.split(" "));
}
@Override