command system
This commit is contained in:
parent
a8f6af2b37
commit
a891ab4d3a
11 changed files with 361 additions and 87 deletions
|
@ -2,8 +2,11 @@ package game.command;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import game.block.Block;
|
||||
import game.entity.Entity;
|
||||
|
@ -15,14 +18,17 @@ import game.world.BlockPos;
|
|||
import game.world.State;
|
||||
import game.world.Vec3;
|
||||
import game.world.WorldPos;
|
||||
import game.world.WorldServer;
|
||||
|
||||
public class ScriptArgs {
|
||||
private final String command;
|
||||
private final Map<String, ScriptArg> arguments;
|
||||
private final ScriptEnvironment env;
|
||||
|
||||
protected ScriptArgs(Map<String, ScriptArg> arguments, String command) {
|
||||
protected ScriptArgs(Map<String, ScriptArg> arguments, String command, ScriptEnvironment env) {
|
||||
this.arguments = arguments;
|
||||
this.command = command;
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
private static <T> String joinArgs(Iterable<T> iter) {
|
||||
|
@ -37,7 +43,7 @@ public class ScriptArgs {
|
|||
|
||||
public static ScriptArgs parseArgs(ScriptEnvironment env, String str, String[] argv, CachedExecutable cached) {
|
||||
Map<String, Parameter> parameters = cached.getParameters();
|
||||
List<Parameter> positionals = cached.getPositionals();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.getPositionals());
|
||||
// String[] argv = ArgumentParser.splitString(str);
|
||||
Map<String, ScriptArg> args = Maps.newHashMap();
|
||||
int ppos = 0;
|
||||
|
@ -75,7 +81,7 @@ public class ScriptArgs {
|
|||
int nargs = param.getParsers().size();
|
||||
// if(!pos)
|
||||
// z += 1;
|
||||
if(z + (pos ? 0 : 1) + nargs >= argv.length)
|
||||
if(z + (pos ? 0 : 1) + nargs > argv.length)
|
||||
if(nargs == 1 && param.getName().equals(param.getParsers().get(0).getName()))
|
||||
throw new ScriptException("Position %d: Argument '%s' benötigt einen Parameter", z, param.getName());
|
||||
else
|
||||
|
@ -109,7 +115,7 @@ public class ScriptArgs {
|
|||
if(!pos)
|
||||
inputs[0] = arg;
|
||||
args.put(param.getName(), new ScriptArg(param, z, inputs, params));
|
||||
z += nargs;
|
||||
z += nargs - (pos ? 1 : 0);
|
||||
}
|
||||
for(Parameter param : parameters.values()) {
|
||||
if(!args.containsKey(param.getName())) {
|
||||
|
@ -126,7 +132,83 @@ public class ScriptArgs {
|
|||
args.put(param.getName(), new ScriptArg(param, -1, null, params));
|
||||
}
|
||||
}
|
||||
return new ScriptArgs(args, str);
|
||||
return new ScriptArgs(args, str, env);
|
||||
}
|
||||
|
||||
private static String[] getParam(ScriptEnvironment env, String[] argv, CachedExecutable cached) {
|
||||
Map<String, Parameter> parameters = cached.getParameters();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.getPositionals());
|
||||
Set<String> args = Sets.newHashSet();
|
||||
int ppos = 0;
|
||||
boolean parse = true;
|
||||
for(int z = 1; z < argv.length; z++) {
|
||||
String arg = argv[z];
|
||||
Parameter param = null;
|
||||
boolean pos = false;
|
||||
if(z == argv.length - 1) {
|
||||
if(ppos < positionals.size()) {
|
||||
param = positionals.get(ppos);
|
||||
if(param.getParsers().isEmpty()) // np
|
||||
return null;
|
||||
return param.getParsers().get(0).getCompletions(env);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if(parse && arg.equals("--")) {
|
||||
parse = false;
|
||||
continue;
|
||||
}
|
||||
else if(parse && (arg.startsWith("--") || (arg.startsWith("-") && arg.length() == 2))) {
|
||||
param = parameters.get(arg.substring(arg.startsWith("--") ? 2 : 1));
|
||||
if(param != null && param.isPositional() && !args.contains(param.getName())) {
|
||||
for(int n = 0; n < positionals.size(); n++) {
|
||||
if(param == positionals.get(n)) {
|
||||
positionals.remove(n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(ppos < positionals.size()) {
|
||||
param = positionals.get(ppos++);
|
||||
pos = true;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
if(param == null || args.contains(param.getName()))
|
||||
return null;
|
||||
int nargs = param.getParsers().size();
|
||||
// if(z + (pos ? 0 : 1) + nargs > argv.length - 1) {
|
||||
// return param.getParsers().get(argv.length - z).getCompletions(env);
|
||||
// }
|
||||
int apos = 0;
|
||||
for(int n = pos ? 0 : 1; n < nargs + (pos ? 0 : 1); n++) {
|
||||
if(z + n == argv.length - 1)
|
||||
return param.getParsers().get(apos).getCompletions(env);
|
||||
String par = argv[z + n];
|
||||
if(parse && (par.startsWith("--") || (par.startsWith("-") && par.length() == 2)))
|
||||
return null;
|
||||
apos += 1;
|
||||
}
|
||||
args.add(param.getName());
|
||||
z += nargs - (pos ? 1 : 0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String[] parseComplete(ScriptEnvironment env, String[] argv, CachedExecutable cached) {
|
||||
String[] comp = getParam(env, argv, cached);
|
||||
if(comp == null || comp.length == 0) {
|
||||
Set<String> params = Sets.newTreeSet();
|
||||
for(String param : cached.getParameters().keySet()) {
|
||||
params.add(param.length() == 1 ? "-" + param : ("--" + param));
|
||||
}
|
||||
comp = params.toArray(new String[params.size()]);
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
||||
public ScriptArg getArg(String name) {
|
||||
|
@ -137,7 +219,15 @@ public class ScriptArgs {
|
|||
return this.arguments.containsKey(name);
|
||||
}
|
||||
|
||||
protected <T> T getDefault(String name, String par, T def) {
|
||||
public boolean has(String name, String par) {
|
||||
return this.arguments.containsKey(name) && this.arguments.get(name).getValues().containsKey(par);
|
||||
}
|
||||
|
||||
public boolean has(String name) {
|
||||
return this.has(name, name);
|
||||
}
|
||||
|
||||
public <T> T getDefault(String name, String par, T def) {
|
||||
ScriptArg arg = this.arguments.get(name);
|
||||
if(arg == null)
|
||||
return def;
|
||||
|
@ -145,15 +235,15 @@ public class ScriptArgs {
|
|||
return value == null ? def : (T)value;
|
||||
}
|
||||
|
||||
protected <T> T getDefault(String name, T def) {
|
||||
public <T> T getDefault(String name, T def) {
|
||||
return this.getDefault(name, name, def);
|
||||
}
|
||||
|
||||
protected <T> T getUnchecked(String name, String par) {
|
||||
public <T> T getUnchecked(String name, String par) {
|
||||
return this.getDefault(name, par, null);
|
||||
}
|
||||
|
||||
protected <T> T getUnchecked(String name) {
|
||||
public <T> T getUnchecked(String name) {
|
||||
return this.getDefault(name, null);
|
||||
}
|
||||
|
||||
|
@ -309,4 +399,8 @@ public class ScriptArgs {
|
|||
public Vec3 getVector(String name) {
|
||||
return new Vec3(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"));
|
||||
}
|
||||
|
||||
public WorldServer getWorld(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue