more fixes, formatting, expand control over format, fix dupe

This commit is contained in:
Aikar
2017-08-08 22:08:05 -04:00
parent 3cdd47942d
commit 75579fe649
9 changed files with 63 additions and 15 deletions
@@ -71,7 +71,7 @@ public class BukkitCommandManager extends CommandManager<CommandSender, ChatColo
this.formatters.put(MessageType.ERROR, defaultFormatter = 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));
this.formatters.put(MessageType.HELP, new BukkitMessageFormatter(ChatColor.BLUE, ChatColor.GREEN, ChatColor.YELLOW));
this.formatters.put(MessageType.HELP, new BukkitMessageFormatter(ChatColor.AQUA, ChatColor.GREEN, ChatColor.YELLOW));
Bukkit.getPluginManager().registerEvents(new ACFBukkitListener(plugin), plugin);
getLocales(); // auto load locales
}
@@ -51,7 +51,7 @@ public class BungeeCommandManager extends CommandManager<CommandSender, ChatColo
this.formatters.put(MessageType.ERROR, defaultFormatter = 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));
this.formatters.put(MessageType.HELP, new BungeeMessageFormatter(ChatColor.BLUE, ChatColor.GREEN, ChatColor.YELLOW));
this.formatters.put(MessageType.HELP, new BungeeMessageFormatter(ChatColor.AQUA, ChatColor.GREEN, ChatColor.YELLOW));
getLocales(); // auto load locales
}
@@ -44,7 +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("<c(?<color>\\d+)>(?<msg>.+?)</c\\1>", Pattern.CASE_INSENSITIVE);
public static final Pattern FORMATTER = Pattern.compile("<c(?<color>\\d+)>(?<msg>.*?)</c\\1>", Pattern.CASE_INSENSITIVE);
public static final Pattern I18N_STRING = Pattern.compile("\\{@@(?<key>.+?)}", Pattern.CASE_INSENSITIVE);
@@ -365,6 +365,22 @@ public final class ACFUtil {
return total.toString().trim();
}
public static String ltrim(String s) {
int i = 0;
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
i++;
}
return s.substring(i);
}
public static String rtrim(String s) {
int i = s.length()-1;
while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
i--;
}
return s.substring(0,i+1);
}
public static List<String> enumNames(Enum<?>[] values) {
return Stream.of(values).map(Enum::name).collect(Collectors.toList());
}
@@ -516,6 +516,10 @@ public abstract class BaseCommand {
return manager.generateCommandHelp();
}
public void showCommandHelp() {
getCommandHelp().showHelp();
}
public void help(Object issuer, String[] args) {
help(manager.getCommandIssuer(issuer), args);
}
@@ -23,7 +23,9 @@
package co.aikar.commands;
import co.aikar.locales.MessageKeyProvider;
import com.google.common.collect.SetMultimap;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@@ -38,14 +40,16 @@ public class CommandHelp {
this.issuer = issuer;
SetMultimap<String, RegisteredCommand> subCommands = rootCommand.getSubCommands();
Set<RegisteredCommand> seen = new HashSet<>();
subCommands.entries().forEach(e -> {
String key = e.getKey();
if (key.equals("__default") || key.equals("__unknown")){
return;
}
RegisteredCommand regCommand = e.getValue();
if (regCommand.hasPermission(issuer)) {
if (regCommand.hasPermission(issuer) && !seen.contains(regCommand)) {
this.helpEntries.add(new HelpEntry(regCommand));
seen.add(regCommand);
}
});
}
@@ -55,17 +59,36 @@ public class CommandHelp {
}
public void showHelp() {
showHelp(issuer);
showHelp(issuer, MessageKeys.HELP_FORMAT);
}
public void showHelp(CommandIssuer issuer) {
getHelpEntries().forEach(e -> issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_FORMAT,
//{command} {parameters} {seperator} {description}
showHelp(issuer, MessageKeys.HELP_FORMAT);
}
public void showHelp(CommandIssuer issuer, MessageKeyProvider format) {
getHelpEntries().forEach(e -> {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, format, getFormatReplacements(e));
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
});
}
/**
* Override this to control replacements
* @param e
* @return
*/
@NotNull
public String[] getFormatReplacements(HelpEntry e) {
//{command} {parameters} {seperator} {description}
return new String[] {
"{command}", e.getCommand(),
"{parameters}", e.getParameterSyntax(),
"{seperator}", e.getDescription().isEmpty() ? "" : " - ",
"{seperator}", e.getDescription().isEmpty() ? "" : "-",
"{description}", e.getDescription()
));
};
}
public List<HelpEntry> getHelpEntries() {
@@ -220,6 +220,14 @@ public abstract class CommandManager <I, FT, F extends MessageFormatter<FT>> {
}
public void sendMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) {
String message = formatMessage(issuer, type, key, replacements);
for (String msg : ACFPatterns.NEWLINE.split(message)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public String formatMessage(CommandIssuer issuer, MessageType type, MessageKeyProvider key, String... replacements) {
String message = getLocales().getMessage(issuer, key.getMessageKey());
if (replacements.length > 0) {
message = ACFUtil.replaceStrings(message, replacements);
@@ -231,10 +239,7 @@ public abstract class CommandManager <I, FT, F extends MessageFormatter<FT>> {
if (formatter != null) {
message = formatter.format(message);
}
for (String msg : ACFPatterns.NEWLINE.split(message)) {
issuer.sendMessageInternal(msg);
}
return message;
}
public Locale getIssuerLocale(CommandIssuer issuer) {
+1 -1
View File
@@ -33,4 +33,4 @@ acf-core.must_be_min_length = Error: Must be at least {min} characters long.
acf-core.must_be_max_length = Error: Must be less than {max} characters long.
acf-core.not_allowed_on_console = Error: Console may not execute this command.
acf-core.could_not_find_player = Error: Could not find a player by the name: <c2>{search}</c2>
acf-core.help_format = {command} <c2>{parameters}</c2> <c3>{seperator} {description}</c3>
acf-core.help_format = <c1>{command}</c1> <c2>{parameters}</c2> <c3>{seperator} {description}</c3>
@@ -58,7 +58,7 @@ public class SpongeCommandManager extends CommandManager<CommandSource, TextColo
this.formatters.put(MessageType.ERROR, defaultFormatter = 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));
this.formatters.put(MessageType.HELP, new SpongeMessageFormatter(TextColors.BLUE, TextColors.GREEN, TextColors.YELLOW));
this.formatters.put(MessageType.HELP, new SpongeMessageFormatter(TextColors.AQUA, TextColors.GREEN, TextColors.YELLOW));
getLocales(); // auto load locales
}