help command

This commit is contained in:
Sen 2025-03-25 10:13:15 +01:00
parent fc8603497a
commit fd84384f10
3 changed files with 68 additions and 0 deletions

View file

@ -86,4 +86,8 @@ public class CachedExecutable {
public String getName() {
return this.name;
}
public String toString() {
return this.name;
}
}

View file

@ -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));
}
}

View 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;
}
}