help command
This commit is contained in:
parent
fc8603497a
commit
fd84384f10
3 changed files with 68 additions and 0 deletions
|
@ -86,4 +86,8 @@ public class CachedExecutable {
|
|||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import com.google.common.collect.Maps;
|
|||
|
||||
import game.Server;
|
||||
import game.color.TextColor;
|
||||
import game.command.commands.CommandHelp;
|
||||
import game.command.commands.CommandSpawn;
|
||||
import game.log.Log;
|
||||
|
||||
|
@ -62,6 +63,10 @@ public class ScriptEnvironment {
|
|||
return this.server;
|
||||
}
|
||||
|
||||
public Map<String, CachedExecutable> getExecutables() {
|
||||
return this.executables;
|
||||
}
|
||||
|
||||
public ScriptExecutor getExecutor() {
|
||||
return this.currentExecutor;
|
||||
}
|
||||
|
@ -191,5 +196,7 @@ public class ScriptEnvironment {
|
|||
});
|
||||
|
||||
this.registerExecutable(new CommandSpawn());
|
||||
|
||||
this.registerExecutable(new CommandHelp(this));
|
||||
}
|
||||
}
|
||||
|
|
57
java/src/game/command/commands/CommandHelp.java
Normal file
57
java/src/game/command/commands/CommandHelp.java
Normal file
|
@ -0,0 +1,57 @@
|
|||
package game.command.commands;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import game.command.ArgumentParser;
|
||||
import game.command.CachedExecutable;
|
||||
import game.command.Parameter;
|
||||
import game.command.ScriptEnvironment;
|
||||
import game.command.ScriptExecutable;
|
||||
import game.command.ScriptExecutor;
|
||||
import game.util.Util;
|
||||
|
||||
public class CommandHelp extends ScriptExecutable {
|
||||
public CommandHelp(ScriptEnvironment env) {
|
||||
super("help");
|
||||
|
||||
this.setParamsOptional();
|
||||
this.addEnum("command", CachedExecutable.class, env.getExecutables().values());
|
||||
}
|
||||
|
||||
public Object exec(ScriptEnvironment env, ScriptExecutor exec, CachedExecutable command) {
|
||||
if(command == null) {
|
||||
for(CachedExecutable cmd : env.getExecutables().values()) {
|
||||
this.exec(env, exec, cmd);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
List<String> list = Lists.newArrayList();
|
||||
for(Entry<String, Parameter> entry : command.getParameters().entrySet()) {
|
||||
Parameter param = entry.getValue();
|
||||
if(entry.getKey().length() == 1 && !param.isPositional() && param.hasShortName()) {
|
||||
list.add("-" + param.getShortName() + (param.getParsers().isEmpty() ? " (" + param.getName() + ")" : (param.getParsers().size() == 1 &&
|
||||
param.getParsers().get(0).getName().equals(param.getName()) ? " <" + param.getName() + ">" :
|
||||
" (" + param.getName() + ")" + Util.buildLines(" ", new Function<ArgumentParser, String>() {
|
||||
public String apply(ArgumentParser parser) {
|
||||
return "<" + parser.getName() + ">";
|
||||
}
|
||||
}, param.getParsers()))));
|
||||
}
|
||||
}
|
||||
for(Parameter param : command.getPositionals()) {
|
||||
list.add((param.isRequired() ? "<" : "[") + (param.getParsers().size() == 1 &&
|
||||
param.getParsers().get(0).getName().equals(param.getName()) ? param.getName() :
|
||||
"(" + param.getName() + ") " + Util.buildLines(" ", new Function<ArgumentParser, String>() {
|
||||
public String apply(ArgumentParser parser) {
|
||||
return "<" + parser.getName() + ">";
|
||||
}
|
||||
}, param.getParsers())) + (param.isRequired() ? ">" : "]"));
|
||||
}
|
||||
exec.logConsole("%s %s", command.getName(), Util.buildLines(" ", list));
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue