Generify help generation

This commit is contained in:
mrkirby153
2017-07-18 19:41:45 -07:00
parent 1d5b0f086d
commit 5be72c40e2
8 changed files with 166 additions and 29 deletions
@@ -0,0 +1,41 @@
/*
* 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);
}
}
@@ -26,7 +26,6 @@ package co.aikar.commands;
import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
import co.aikar.timings.lib.MCTiming;
import co.aikar.timings.lib.TimingManager;
import com.google.common.collect.SetMultimap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
@@ -43,7 +42,6 @@ import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -279,23 +277,11 @@ public class BukkitCommandManager extends CommandManager {
}
@Override
List<String> getHelp(String command) {
BukkitRootCommand cmd = (BukkitRootCommand) obtainRootCommand(command);
CommandHelp getHelp(String command) {
RootCommand cmd = obtainRootCommand(command);
BaseCommand defCommand = cmd.getDefCommand();
if (defCommand instanceof ForwardingCommand) {
command = defCommand.commandName;
ForwardingCommand fwdCmd = (ForwardingCommand) defCommand;
defCommand = fwdCmd.getCommand();
}
SetMultimap<String, RegisteredCommand> cmds = defCommand.subCommands;
List<String> help = new ArrayList<>();
String finalCommand = command;
cmds.entries().forEach(e -> {
if (e.getKey().equals("__default"))
return;
help.add("/" + finalCommand + " " + e.getKey() + " " + e.getValue().syntaxText + (
(e.getValue().helpText != null && !e.getValue().helpText.isEmpty())?" - " + e.getValue().helpText: ""));
});
return help;
if (defCommand == null)
return null;
return new BukkitCommandHelp(this, defCommand);
}
}
@@ -104,7 +104,8 @@ public class BukkitRootCommand extends Command implements RootCommand {
return this.subCommands;
}
BaseCommand getDefCommand(){
@Override
public BaseCommand getDefCommand(){
return defCommand;
}
}
@@ -110,12 +110,7 @@ public abstract class BaseCommand {
}
this.commandName = cmd != null ? cmd : self.getSimpleName().toLowerCase();
Description descAnn = self.getAnnotation(Description.class);
if(descAnn != null && !descAnn.value().isEmpty()){
this.description = descAnn.value();
} else {
this.description = this.commandName + " commands";
}
this.description = this.commandName + " commands";
this.usageMessage = "/" + this.commandName;
final CommandPermission perm = self.getAnnotation(CommandPermission.class);
@@ -517,7 +512,7 @@ public abstract class BaseCommand {
return false;
}
List<String> getHelp(){
CommandHelp getCommandHelp(){
return manager.getHelp(this.getExecCommandLabel());
}
@@ -0,0 +1,63 @@
/*
* 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 com.google.common.collect.SetMultimap;
import java.util.*;
public abstract class CommandHelp {
private BaseCommand command;
protected CommandHelp(CommandManager manager, BaseCommand command) {
this.command = command;
}
abstract void renderHelp(CommandIssuer issuer);
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());
}
});
return help;
}
public BaseCommand getCommand() {
return command;
}
}
@@ -83,8 +83,8 @@ public abstract class CommandManager {
*/
public abstract CommandCompletions<?> getCommandCompletions();
List<String> getHelp(String command){
return new ArrayList<>(0);
CommandHelp getHelp(String command){
throw new IllegalStateException("Not implemented yet.");
}
/**
@@ -194,6 +194,7 @@ public abstract class CommandManager {
return getLocales().getDefaultLocale();
}
public CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args) {
return new CommandOperationContext(
this,
@@ -0,0 +1,46 @@
/*
* 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;
public class HelpEntry {
private final RegisteredCommand command;
public HelpEntry(RegisteredCommand command) {
this.command = command;
}
public String getCommand(){
return this.command.getCommand();
}
public String getSyntax(){
return this.command.getSyntaxText();
}
public String getHelpText(){
return this.command.helpText;
}
}
@@ -52,4 +52,8 @@ interface RootCommand {
children.add(command);
}
default BaseCommand getDefCommand(){
return null;
}
}