Current progress on updating the help PR

This commit is contained in:
Aikar
2017-08-08 20:30:40 -04:00
parent 88a170cb58
commit 57a41c7540
12 changed files with 89 additions and 106 deletions
@@ -1,41 +0,0 @@
/*
* 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.ArrayList;
import java.util.List;
public class BukkitCommandHelp extends CommandHelp {
protected BukkitCommandHelp(CommandManager manager, BaseCommand cmd) {
super(manager, cmd);
}
@Override
void renderHelp(CommandIssuer issuer) {
List<String> rendered = new ArrayList<>();
getCommandHelp().forEach(h -> rendered.add("/" + h.getCommand() + " " + h.getSyntax() + ((h.getHelpText() != null && !h.getHelpText().isEmpty() ? " - " + h.getHelpText() : ""))));
rendered.forEach(issuer::sendMessage);
}
}
@@ -267,11 +267,7 @@ public class BukkitCommandManager extends CommandManager<CommandSender, ChatColo
}
@Override
CommandHelp getHelp(String command) {
RootCommand cmd = obtainRootCommand(command);
BaseCommand defCommand = cmd.getDefCommand();
if (defCommand == null)
return null;
return new BukkitCommandHelp(this, defCommand);
public CommandHelp generateCommandHelp(BaseCommand command) {
return new CommandHelp(this, command, getCurrentCommandOperationContext());
}
}
@@ -23,6 +23,8 @@
package co.aikar.commands;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -31,7 +33,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class BukkitRootCommand extends Command implements RootCommand {
@@ -39,7 +40,7 @@ public class BukkitRootCommand extends Command implements RootCommand {
private final BukkitCommandManager manager;
private final String name;
private BaseCommand defCommand;
private Map<String, BaseCommand> subCommands = new HashMap<>();
private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
private List<BaseCommand> children = new ArrayList<>();
boolean isRegistered = false;
@@ -73,9 +74,9 @@ public class BukkitRootCommand extends Command implements RootCommand {
private boolean execute(CommandIssuer sender, String commandLabel, String[] args) {
for (int i = args.length; i >= 0; i--) {
String checkSub = StringUtils.join(args, " ", 0, i).toLowerCase();
BaseCommand subHandler = this.subCommands.get(checkSub);
if (subHandler != null) {
subHandler.execute(sender, commandLabel, args);
Set<RegisteredCommand> registeredCommands = this.subCommands.get(checkSub);
if (!registeredCommands.isEmpty()) {
registeredCommands.iterator().next().scope.execute(sender, commandLabel, args);
return true;
}
}
@@ -100,7 +101,7 @@ public class BukkitRootCommand extends Command implements RootCommand {
}
@Override
public Map<String, BaseCommand> getSubCommands() {
public SetMultimap<String, RegisteredCommand> getSubCommands() {
return this.subCommands;
}
@@ -512,8 +512,8 @@ public abstract class BaseCommand {
return false;
}
CommandHelp getCommandHelp(){
return manager.getHelp(this.getExecCommandLabel());
public CommandHelp getCommandHelp(){
return manager.generateCommandHelp(this);
}
public void help(Object issuer, String[] args) {
@@ -27,37 +27,58 @@ import com.google.common.collect.SetMultimap;
import java.util.*;
public abstract class CommandHelp {
private BaseCommand command;
public class CommandHelp {
private final CommandManager manager;
private final List<HelpEntry> helpEntries;
private final CommandOperationContext currentContext;
protected CommandHelp(CommandManager manager, BaseCommand command) {
this.command = command;
protected CommandHelp(CommandManager manager, BaseCommand command, CommandOperationContext currentContext) {
this.manager = manager;
this.currentContext = currentContext;
List<HelpEntry> entries = new ArrayList<>();
for (RootCommand root : command.registeredCommands.values()) {
SetMultimap<String, RegisteredCommand> subCommands = root.getSubCommands();
subCommands.entries().forEach(e -> {
if (e.getKey().equals("__default") || e.getKey().equals("__unknown")){
return;
}
RegisteredCommand regCommand = e.getValue();
entries.add(new HelpEntry(regCommand));
});
}
this.helpEntries = entries;
}
abstract void renderHelp(CommandIssuer issuer);
public CommandManager getManager() {
return manager;
}
public void showHelp() {
showHelp(currentContext.getCommandIssuer());
}
Collection<HelpEntry> getCommandHelp() {
SetMultimap<String, RegisteredCommand> subCommands = command.subCommands;
Set<HelpEntry> help = new HashSet<>();
List<String> used = new ArrayList<>();
subCommands.entries().forEach(e -> {
if(e.getKey().equals("__default") || e.getKey().equals("__unknown")){
return;
}
RegisteredCommand regCommand = e.getValue();
if(!used.contains(regCommand.getCommand())) {
help.add(new HelpEntry(regCommand));
used.add(regCommand.getCommand());
}
public void showHelp(CommandIssuer issuer) {
getHelpEntries().forEach(h -> {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_FORMAT,
//{command} {parameters} {seperator} {helptext}
"{command}", h.getCommand(),
"{parameters}", h.getParameterSyntax(),
"{seperator}", h.getHelpText().isEmpty() ? "" : " - ",
"{helptext}", h.getHelpText()
);
});
return help;
}
public BaseCommand getCommand() {
return command;
}
public List<HelpEntry> getHelpEntries() {
return helpEntries;
}
public CommandOperationContext getCurrentContext() {
return currentContext;
}
}
@@ -106,9 +106,7 @@ public abstract class CommandManager <I, FT, F extends MessageFormatter<FT>> {
*/
public abstract CommandCompletions<?> getCommandCompletions();
CommandHelp getHelp(String command){
throw new IllegalStateException("Not implemented yet.");
}
public abstract CommandHelp generateCommandHelp(BaseCommand command);
/**
* Registers a command with ACF
@@ -33,11 +33,11 @@ public class HelpEntry {
public String getCommand(){
return this.command.getCommand();
return "/" + this.command.command;
}
public String getSyntax(){
return this.command.getSyntaxText();
public String getParameterSyntax(){
return this.command.syntaxText;
}
public String getHelpText(){
@@ -25,6 +25,7 @@ package co.aikar.commands;
import co.aikar.locales.LocaleManager;
import co.aikar.locales.MessageKey;
import co.aikar.locales.MessageKeyProvider;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import org.jetbrains.annotations.NotNull;
@@ -130,11 +131,12 @@ public class Locales {
return this.localeManager.addMessage(locale, key, message);
}
public String getMessage(CommandIssuer issuer, MessageKey key) {
String message = this.localeManager.getMessage(issuer, key);
public String getMessage(CommandIssuer issuer, MessageKeyProvider key) {
final MessageKey msgKey = key.getMessageKey();
String message = this.localeManager.getMessage(issuer, msgKey);
if (message == null) {
manager.log(LogLevel.ERROR, "Missing Language Key: " + key.getKey());
message = "<MISSING_LANGUAGE_KEY:" + key.getKey() + ">";
manager.log(LogLevel.ERROR, "Missing Language Key: " + msgKey.getKey());
message = "<MISSING_LANGUAGE_KEY:" + msgKey.getKey() + ">";
}
return message;
}
@@ -43,7 +43,8 @@ public enum MessageKeys implements MessageKeyProvider {
MUST_BE_MIN_LENGTH,
MUST_BE_MAX_LENGTH,
NOT_ALLOWED_ON_CONSOLE,
COULD_NOT_FIND_PLAYER;
COULD_NOT_FIND_PLAYER,
HELP_FORMAT;
private final MessageKey key = MessageKey.of("acf-core." + this.name().toLowerCase());
public MessageKey getMessageKey() {
@@ -29,10 +29,10 @@ import java.util.concurrent.atomic.AtomicInteger;
public class MessageType {
private static final AtomicInteger counter = new AtomicInteger(1);
public static MessageType INFO = new MessageType();
public static MessageType SYNTAX = new MessageType();
public static MessageType ERROR = new MessageType();
public static final MessageType INFO = new MessageType();
public static final MessageType SYNTAX = new MessageType();
public static final MessageType ERROR = new MessageType();
public static final MessageType HELP = new MessageType();
private final int id = counter.getAndIncrement();
@@ -23,31 +23,35 @@
package co.aikar.commands;
import java.util.HashMap;
import com.google.common.collect.SetMultimap;
import java.util.List;
import java.util.Map;
import java.util.Set;
interface RootCommand {
void addChild(BaseCommand command);
CommandManager getManager();
default Map<String, BaseCommand> getSubCommands(){
return new HashMap<>(0);
}
SetMultimap<String, RegisteredCommand> getSubCommands();
String getCommandName();
default void addChildShared(List<BaseCommand> children, Map<String, BaseCommand> subCommands, BaseCommand command) {
command.subCommands.keySet().forEach(key -> {
default void addChildShared(List<BaseCommand> children, SetMultimap<String, RegisteredCommand> subCommands, BaseCommand command) {
command.subCommands.entries().forEach(e -> {
String key = e.getKey();
RegisteredCommand registeredCommand = e.getValue();
if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.UNKNOWN)) {
return;
}
BaseCommand regged = subCommands.get(key);
if (regged != null) {
this.getManager().log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + getCommandName() + " - but it is already defined in " + regged.getName());
this.getManager().log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this.");
return;
Set<RegisteredCommand> registered = subCommands.get(key);
if (!registered.isEmpty()) {
BaseCommand prevBase = registered.iterator().next().scope;
if (prevBase != registeredCommand.scope) {
this.getManager().log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + getCommandName() + " - but it is already defined in " + prevBase.getName());
this.getManager().log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this.");
return;
}
}
subCommands.put(key, command);
subCommands.put(key, registeredCommand);
});
children.add(command);
+1
View File
@@ -33,3 +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} {parameters} {seperator} {helptext}