command system
This commit is contained in:
parent
a8f6af2b37
commit
a891ab4d3a
11 changed files with 361 additions and 87 deletions
|
@ -24,6 +24,7 @@ import com.google.common.util.concurrent.ListenableFutureTask;
|
||||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
|
|
||||||
import game.color.TextColor;
|
import game.color.TextColor;
|
||||||
|
import game.command.ScriptEnvironment;
|
||||||
import game.dimension.Dimension;
|
import game.dimension.Dimension;
|
||||||
import game.dimension.Space;
|
import game.dimension.Space;
|
||||||
import game.entity.Entity;
|
import game.entity.Entity;
|
||||||
|
@ -107,6 +108,7 @@ public final class Server implements Runnable, IThreadListener {
|
||||||
private final List<WorldServer> ticked = Lists.<WorldServer>newArrayList();
|
private final List<WorldServer> ticked = Lists.<WorldServer>newArrayList();
|
||||||
private final List<Dimension> unload = Lists.<Dimension>newArrayList();
|
private final List<Dimension> unload = Lists.<Dimension>newArrayList();
|
||||||
private final Map<String, Position> warps = Maps.<String, Position>newTreeMap();
|
private final Map<String, Position> warps = Maps.<String, Position>newTreeMap();
|
||||||
|
private final ScriptEnvironment scriptEnv = new ScriptEnvironment(this);
|
||||||
// private final String owner;
|
// private final String owner;
|
||||||
private final File playerDir;
|
private final File playerDir;
|
||||||
private final File baseDir;
|
private final File baseDir;
|
||||||
|
@ -150,9 +152,9 @@ public final class Server implements Runnable, IThreadListener {
|
||||||
// this.port = port;
|
// this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public String getOwner() {
|
public ScriptEnvironment getScriptEnvironment() {
|
||||||
// return this.owner;
|
return this.scriptEnv;
|
||||||
// }
|
}
|
||||||
|
|
||||||
public boolean isStarted() {
|
public boolean isStarted() {
|
||||||
return this.started;
|
return this.started;
|
||||||
|
|
|
@ -2,10 +2,14 @@ package game.command;
|
||||||
|
|
||||||
import game.dimension.Dimension;
|
import game.dimension.Dimension;
|
||||||
import game.init.UniverseRegistry;
|
import game.init.UniverseRegistry;
|
||||||
|
import game.world.Position;
|
||||||
|
|
||||||
public class DimensionParser extends NonDefaultingParser {
|
public class DimensionParser extends CompletingParser {
|
||||||
public DimensionParser(String name) {
|
private final boolean useSender;
|
||||||
|
|
||||||
|
public DimensionParser(String name, boolean useSender) {
|
||||||
super(name);
|
super(name);
|
||||||
|
this.useSender = useSender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object parse(ScriptEnvironment env, String input) {
|
public Object parse(ScriptEnvironment env, String input) {
|
||||||
|
@ -24,6 +28,15 @@ public class DimensionParser extends NonDefaultingParser {
|
||||||
return dim;
|
return dim;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getDefault(ScriptEnvironment env) {
|
||||||
|
if(!this.useSender)
|
||||||
|
return null;
|
||||||
|
Position pos = env.getExecutor().getExecPos();
|
||||||
|
return pos == null ? null : UniverseRegistry.getDimension(pos.dim);
|
||||||
|
// if(dim == null)
|
||||||
|
// throw new ScriptException("Unbekannte Dimension '%s'");
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getCompletions(ScriptEnvironment env) {
|
public String[] getCompletions(ScriptEnvironment env) {
|
||||||
return UniverseRegistry.getWorldNames().toArray(new String[UniverseRegistry.getWorldNames().size()]);
|
return UniverseRegistry.getWorldNames().toArray(new String[UniverseRegistry.getWorldNames().size()]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,26 @@
|
||||||
package game.command;
|
package game.command;
|
||||||
|
|
||||||
|
import game.world.Position;
|
||||||
|
|
||||||
public class DoubleParser extends DefaultingParser {
|
public class DoubleParser extends DefaultingParser {
|
||||||
|
public static enum DefType {
|
||||||
|
X, Y, Z, YAW, PITCH;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final DefType defType;
|
||||||
private final Double min;
|
private final Double min;
|
||||||
private final Double max;
|
private final Double max;
|
||||||
|
|
||||||
public DoubleParser(String name, Double def, Double min, Double max, Object ... completions) {
|
public DoubleParser(String name, Double def, Double min, Double max, Object ... completions) {
|
||||||
super(name, def, completions);
|
super(name, def, completions);
|
||||||
|
this.defType = null;
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoubleParser(String name, DefType type, Double min, Double max, Object ... completions) {
|
||||||
|
super(name, null, completions);
|
||||||
|
this.defType = type;
|
||||||
this.min = min;
|
this.min = min;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
@ -30,4 +45,21 @@ public class DoubleParser extends DefaultingParser {
|
||||||
throw new ScriptException("Die Zahl darf höchstens %f betragen, habe %f", this.max, value);
|
throw new ScriptException("Die Zahl darf höchstens %f betragen, habe %f", this.max, value);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double getDefault(ScriptEnvironment env) {
|
||||||
|
Position pos = this.defType == null ? null : env.getExecutor().getExecPos();
|
||||||
|
switch(this.defType) {
|
||||||
|
case X:
|
||||||
|
return pos == null ? null : pos.x;
|
||||||
|
case Y:
|
||||||
|
return pos == null ? null : pos.y;
|
||||||
|
case Z:
|
||||||
|
return pos == null ? null : pos.z;
|
||||||
|
case YAW:
|
||||||
|
return pos == null ? null : (double)pos.yaw;
|
||||||
|
case PITCH:
|
||||||
|
return pos == null ? null : (double)pos.pitch;
|
||||||
|
}
|
||||||
|
return (Double)super.getDefault(env);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ public class Parameter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPositional() {
|
public boolean isPositional() {
|
||||||
return this.position > 0;
|
return this.position >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPosition() {
|
public int getPosition() {
|
||||||
|
|
|
@ -2,8 +2,11 @@ package game.command;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.Maps;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
import game.block.Block;
|
import game.block.Block;
|
||||||
import game.entity.Entity;
|
import game.entity.Entity;
|
||||||
|
@ -15,14 +18,17 @@ import game.world.BlockPos;
|
||||||
import game.world.State;
|
import game.world.State;
|
||||||
import game.world.Vec3;
|
import game.world.Vec3;
|
||||||
import game.world.WorldPos;
|
import game.world.WorldPos;
|
||||||
|
import game.world.WorldServer;
|
||||||
|
|
||||||
public class ScriptArgs {
|
public class ScriptArgs {
|
||||||
private final String command;
|
private final String command;
|
||||||
private final Map<String, ScriptArg> arguments;
|
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.arguments = arguments;
|
||||||
this.command = command;
|
this.command = command;
|
||||||
|
this.env = env;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> String joinArgs(Iterable<T> iter) {
|
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) {
|
public static ScriptArgs parseArgs(ScriptEnvironment env, String str, String[] argv, CachedExecutable cached) {
|
||||||
Map<String, Parameter> parameters = cached.getParameters();
|
Map<String, Parameter> parameters = cached.getParameters();
|
||||||
List<Parameter> positionals = cached.getPositionals();
|
List<Parameter> positionals = Lists.newArrayList(cached.getPositionals());
|
||||||
// String[] argv = ArgumentParser.splitString(str);
|
// String[] argv = ArgumentParser.splitString(str);
|
||||||
Map<String, ScriptArg> args = Maps.newHashMap();
|
Map<String, ScriptArg> args = Maps.newHashMap();
|
||||||
int ppos = 0;
|
int ppos = 0;
|
||||||
|
@ -75,7 +81,7 @@ public class ScriptArgs {
|
||||||
int nargs = param.getParsers().size();
|
int nargs = param.getParsers().size();
|
||||||
// if(!pos)
|
// if(!pos)
|
||||||
// z += 1;
|
// 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()))
|
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());
|
throw new ScriptException("Position %d: Argument '%s' benötigt einen Parameter", z, param.getName());
|
||||||
else
|
else
|
||||||
|
@ -109,7 +115,7 @@ public class ScriptArgs {
|
||||||
if(!pos)
|
if(!pos)
|
||||||
inputs[0] = arg;
|
inputs[0] = arg;
|
||||||
args.put(param.getName(), new ScriptArg(param, z, inputs, params));
|
args.put(param.getName(), new ScriptArg(param, z, inputs, params));
|
||||||
z += nargs;
|
z += nargs - (pos ? 1 : 0);
|
||||||
}
|
}
|
||||||
for(Parameter param : parameters.values()) {
|
for(Parameter param : parameters.values()) {
|
||||||
if(!args.containsKey(param.getName())) {
|
if(!args.containsKey(param.getName())) {
|
||||||
|
@ -126,7 +132,83 @@ public class ScriptArgs {
|
||||||
args.put(param.getName(), new ScriptArg(param, -1, null, params));
|
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) {
|
public ScriptArg getArg(String name) {
|
||||||
|
@ -137,7 +219,15 @@ public class ScriptArgs {
|
||||||
return this.arguments.containsKey(name);
|
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);
|
ScriptArg arg = this.arguments.get(name);
|
||||||
if(arg == null)
|
if(arg == null)
|
||||||
return def;
|
return def;
|
||||||
|
@ -145,15 +235,15 @@ public class ScriptArgs {
|
||||||
return value == null ? def : (T)value;
|
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);
|
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);
|
return this.getDefault(name, par, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T getUnchecked(String name) {
|
public <T> T getUnchecked(String name) {
|
||||||
return this.getDefault(name, null);
|
return this.getDefault(name, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,4 +399,8 @@ public class ScriptArgs {
|
||||||
public Vec3 getVector(String name) {
|
public Vec3 getVector(String name) {
|
||||||
return new Vec3(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"));
|
return new Vec3(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WorldServer getWorld(String name) {
|
||||||
|
return this.getUnchecked(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import com.google.common.collect.Maps;
|
||||||
|
|
||||||
import game.Server;
|
import game.Server;
|
||||||
import game.color.TextColor;
|
import game.color.TextColor;
|
||||||
|
import game.command.commands.CommandSpawn;
|
||||||
|
|
||||||
public class ScriptEnvironment {
|
public class ScriptEnvironment {
|
||||||
private final Server server;
|
private final Server server;
|
||||||
|
@ -20,6 +21,7 @@ public class ScriptEnvironment {
|
||||||
|
|
||||||
public ScriptEnvironment(Server server) {
|
public ScriptEnvironment(Server server) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
|
this.registerDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerExecutable(Executable executable) {
|
public void registerExecutable(Executable executable) {
|
||||||
|
@ -107,8 +109,35 @@ public class ScriptEnvironment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void complete(String cmd, ScriptExecutor exec) {
|
public List<String> complete(String cmd, ScriptExecutor exec) {
|
||||||
|
List<String> list = Lists.newArrayList();
|
||||||
|
try {
|
||||||
|
String[][] cmds = ArgumentParser.splitString(cmd.endsWith(" ") ? cmd + "END" : cmd);
|
||||||
|
if(cmds.length == 0)
|
||||||
|
return list;
|
||||||
|
String[] argv = cmds[cmds.length - 1];
|
||||||
|
if(argv.length == 0)
|
||||||
|
return list;
|
||||||
|
String[] comp;
|
||||||
|
if(argv.length > 1) {
|
||||||
|
CachedExecutable cached = this.executables.get(argv[0]);
|
||||||
|
if(cached == null)
|
||||||
|
return list;
|
||||||
|
comp = ScriptArgs.parseComplete(this, argv, cached);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
comp = this.executables.keySet().toArray(new String[this.executables.keySet().size()]);
|
||||||
|
}
|
||||||
|
String param = cmd.endsWith(" ") ? "" : argv[argv.length - 1];
|
||||||
|
for(String cmp : comp) {
|
||||||
|
if(cmp.regionMatches(true, 0, param, 0, param.length()))
|
||||||
|
list.add(cmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Throwable t) {
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerDefaults() {
|
public void registerDefaults() {
|
||||||
|
@ -127,5 +156,7 @@ public class ScriptEnvironment {
|
||||||
return ScriptEnvironment.this.previousOutput == null ? null : ScriptEnvironment.this.previousOutput.toString();
|
return ScriptEnvironment.this.previousOutput == null ? null : ScriptEnvironment.this.previousOutput.toString();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.registerExecutable(new CommandSpawn());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package game.command;
|
package game.command;
|
||||||
|
|
||||||
|
import game.world.Position;
|
||||||
|
|
||||||
public interface ScriptExecutor {
|
public interface ScriptExecutor {
|
||||||
void logConsole(String msg);
|
void logConsole(String msg);
|
||||||
String getExecId();
|
String getExecId();
|
||||||
String getExecName();
|
String getExecName();
|
||||||
|
Position getExecPos();
|
||||||
|
|
||||||
default void logConsole(String fmt, Object ... args) {
|
default void logConsole(String fmt, Object ... args) {
|
||||||
this.logConsole(String.format(fmt, args));
|
this.logConsole(String.format(fmt, args));
|
||||||
|
|
|
@ -4,32 +4,37 @@ import java.util.List;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import game.Server;
|
|
||||||
import game.dimension.Dimension;
|
import game.dimension.Dimension;
|
||||||
import game.world.WorldServer;
|
import game.world.WorldServer;
|
||||||
|
|
||||||
public class WorldParser extends DimensionParser {
|
public class WorldParser extends DimensionParser {
|
||||||
private final boolean loadedOnly;
|
private final boolean loadedOnly;
|
||||||
private final Server server;
|
|
||||||
|
|
||||||
public WorldParser(String name, Server server, boolean loadedOnly) {
|
public WorldParser(String name, boolean loadedOnly, boolean useSender) {
|
||||||
super(name);
|
super(name, useSender);
|
||||||
this.server = server;
|
|
||||||
this.loadedOnly = loadedOnly;
|
this.loadedOnly = loadedOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WorldServer parse(ScriptEnvironment env, String input) {
|
public WorldServer parse(ScriptEnvironment env, String input) {
|
||||||
Dimension dim = (Dimension)super.parse(env, input);
|
Dimension dim = (Dimension)super.parse(env, input);
|
||||||
WorldServer world = this.loadedOnly ? this.server.getWorldNoLoad(dim.getDimensionId()) : this.server.getWorld(dim.getDimensionId());
|
WorldServer world = this.loadedOnly ? env.getServer().getWorldNoLoad(dim.getDimensionId()) : env.getServer().getWorld(dim.getDimensionId());
|
||||||
if(world == null)
|
if(world == null)
|
||||||
throw new ScriptException("Dimension '%s' ist nicht geladen", dim.getFormattedName(false));
|
throw new ScriptException("Dimension '%s' ist nicht geladen", dim.getFormattedName(false));
|
||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WorldServer getDefault(ScriptEnvironment env) {
|
||||||
|
Dimension dim = (Dimension)super.getDefault(env);
|
||||||
|
return this.loadedOnly ? env.getServer().getWorldNoLoad(dim.getDimensionId()) : env.getServer().getWorld(dim.getDimensionId());
|
||||||
|
// if(world == null)
|
||||||
|
// throw new ScriptException("Dimension '%s' ist nicht geladen", dim.getFormattedName(false));
|
||||||
|
// return world;
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getCompletions(ScriptEnvironment env) {
|
public String[] getCompletions(ScriptEnvironment env) {
|
||||||
if(this.loadedOnly) {
|
if(this.loadedOnly) {
|
||||||
List<String> loaded = Lists.newArrayList();
|
List<String> loaded = Lists.newArrayList();
|
||||||
for(WorldServer world : this.server.getWorlds()) {
|
for(WorldServer world : env.getServer().getWorlds()) {
|
||||||
loaded.add(world.dimension.getDimensionName());
|
loaded.add(world.dimension.getDimensionName());
|
||||||
}
|
}
|
||||||
return loaded.toArray(new String[loaded.size()]);
|
return loaded.toArray(new String[loaded.size()]);
|
||||||
|
|
57
java/src/game/command/commands/CommandSpawn.java
Normal file
57
java/src/game/command/commands/CommandSpawn.java
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package game.command.commands;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
|
import game.command.DoubleParser;
|
||||||
|
import game.command.DoubleParser.DefType;
|
||||||
|
import game.command.EnumParser;
|
||||||
|
import game.command.IntParser;
|
||||||
|
import game.command.ScriptArgs;
|
||||||
|
import game.command.ScriptEnvironment;
|
||||||
|
import game.command.ScriptException;
|
||||||
|
import game.command.ScriptExecutable;
|
||||||
|
import game.command.WorldParser;
|
||||||
|
import game.entity.Entity;
|
||||||
|
import game.entity.types.EntityLiving;
|
||||||
|
import game.init.EntityRegistry;
|
||||||
|
import game.world.Vec3;
|
||||||
|
import game.world.World;
|
||||||
|
import game.world.WorldServer;
|
||||||
|
|
||||||
|
public class CommandSpawn extends ScriptExecutable {
|
||||||
|
public CommandSpawn() {
|
||||||
|
super("spawn");
|
||||||
|
Set<String> names = Sets.newTreeSet();
|
||||||
|
for(Class<? extends Entity> clazz : EntityRegistry.getAllClasses()) {
|
||||||
|
names.add(EntityRegistry.getEntityString(clazz));
|
||||||
|
}
|
||||||
|
this.addParameter("type", new EnumParser("type", null, (Object[])names.toArray(new String[names.size()])));
|
||||||
|
this.setParamsOptional();
|
||||||
|
this.addParameter("position", new DoubleParser("x", DefType.X, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE), new DoubleParser("y", DefType.Y, 0.0, (double)World.HEIGHT), new DoubleParser("z", DefType.Z, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE));
|
||||||
|
this.addParameter("dim", new WorldParser("dim", false, true));
|
||||||
|
|
||||||
|
this.addParameter("noinit", 'n');
|
||||||
|
this.addParameter("count", 'c', new IntParser("count", false, 1, 1, 1024));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object exec(ScriptEnvironment env, ScriptArgs args) {
|
||||||
|
String type = args.getString("type");
|
||||||
|
WorldServer world = args.getWorld("dim");
|
||||||
|
Vec3 pos = args.getVector("position");
|
||||||
|
int count = args.getInt("count");
|
||||||
|
boolean init = !args.has("noinit");
|
||||||
|
for(int z = 0; z < count; z++) {
|
||||||
|
Entity entity = EntityRegistry.createEntityByName(type, world);
|
||||||
|
if(entity == null)
|
||||||
|
throw new ScriptException("Objekt konnte nicht erzeugt werden");
|
||||||
|
entity.setLocationAndAngles(pos.xCoord, pos.yCoord, pos.zCoord, world.rand.floatv() * 360.0f, 0.0f);
|
||||||
|
if(init && (entity instanceof EntityLiving))
|
||||||
|
((EntityLiving)entity).onInitialSpawn(null);
|
||||||
|
world.spawnEntityInWorld(entity);
|
||||||
|
}
|
||||||
|
env.getExecutor().logConsole("%s%s bei %d, %d, %d in %s erschaffen", count == 1 ? "" : (count + "x "), EntityRegistry.getEntityName(type), (int)pos.xCoord, (int)pos.yCoord, (int)pos.zCoord, world.dimension.getFormattedName(false));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2670,4 +2670,8 @@ public abstract class Entity
|
||||||
return null;
|
return null;
|
||||||
return ItemRegistry.getRegisteredItem(id.toLowerCase() + "_spawner");
|
return ItemRegistry.getRegisteredItem(id.toLowerCase() + "_spawner");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Position getPos() {
|
||||||
|
return new Position(this.posX, this.posY, this.posZ, this.rotYaw, this.rotPitch, this.worldObj.dimension.getDimensionId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ import game.clipboard.Rotation;
|
||||||
import game.clipboard.RotationValue;
|
import game.clipboard.RotationValue;
|
||||||
import game.clipboard.Vector;
|
import game.clipboard.Vector;
|
||||||
import game.color.TextColor;
|
import game.color.TextColor;
|
||||||
|
import game.command.ScriptExecutor;
|
||||||
import game.dimension.Dimension;
|
import game.dimension.Dimension;
|
||||||
import game.entity.Entity;
|
import game.entity.Entity;
|
||||||
import game.entity.animal.EntityHorse;
|
import game.entity.animal.EntityHorse;
|
||||||
|
@ -125,7 +126,7 @@ import game.world.WorldServer;
|
||||||
import io.netty.util.concurrent.Future;
|
import io.netty.util.concurrent.Future;
|
||||||
import io.netty.util.concurrent.GenericFutureListener;
|
import io.netty.util.concurrent.GenericFutureListener;
|
||||||
|
|
||||||
public class NetHandlerPlayServer extends NetHandler implements ICrafting
|
public class NetHandlerPlayServer extends NetHandler implements ICrafting, ScriptExecutor
|
||||||
{
|
{
|
||||||
private static enum EditAction {
|
private static enum EditAction {
|
||||||
SELECT("Auswahlmodus"), COPYPASTE("Kopiermodus"), TRANSFORM("Drehmodus");
|
SELECT("Auswahlmodus"), COPYPASTE("Kopiermodus"), TRANSFORM("Drehmodus");
|
||||||
|
@ -1630,7 +1631,7 @@ public class NetHandlerPlayServer extends NetHandler implements ICrafting
|
||||||
private boolean setVar(String line) {
|
private boolean setVar(String line) {
|
||||||
if(!this.isAdmin())
|
if(!this.isAdmin())
|
||||||
return false;
|
return false;
|
||||||
if(line.length() < 2) {
|
if(line.length() < 1) {
|
||||||
for(Entry<String, Config.Value> entry : Config.VARS.entrySet()) {
|
for(Entry<String, Config.Value> entry : Config.VARS.entrySet()) {
|
||||||
Config.Value cvar = entry.getValue();
|
Config.Value cvar = entry.getValue();
|
||||||
String v = cvar.getValue();
|
String v = cvar.getValue();
|
||||||
|
@ -1953,6 +1954,31 @@ public class NetHandlerPlayServer extends NetHandler implements ICrafting
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void runCommand(String command) {
|
||||||
|
this.server.getScriptEnvironment().execute(command, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> completeCommand(String command) {
|
||||||
|
return this.server.getScriptEnvironment().complete(command, this);
|
||||||
|
// return Lists.newArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void logConsole(String msg) {
|
||||||
|
this.addFeed(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExecId() {
|
||||||
|
return this.user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExecName() {
|
||||||
|
return this.entity != null ? this.entity.getCommandName() : this.user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position getExecPos() {
|
||||||
|
return this.entity != null ? this.entity.getPos() : null;
|
||||||
|
}
|
||||||
|
|
||||||
public void processMessage(CPacketMessage packetIn)
|
public void processMessage(CPacketMessage packetIn)
|
||||||
{
|
{
|
||||||
|
@ -1967,7 +1993,7 @@ public class NetHandlerPlayServer extends NetHandler implements ICrafting
|
||||||
switch(packetIn.getType()) {
|
switch(packetIn.getType()) {
|
||||||
case COMMAND:
|
case COMMAND:
|
||||||
if(!this.teleport(msg) && !this.setVar(msg) && !this.setAdmin(msg))
|
if(!this.teleport(msg) && !this.setVar(msg) && !this.setAdmin(msg))
|
||||||
this.addFeed(TextColor.RED + "Befehl wurde nicht gefunden");
|
this.runCommand(msg); // this.addFeed(TextColor.RED + "Befehl wurde nicht gefunden");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CHAT:
|
case CHAT:
|
||||||
|
@ -1990,6 +2016,73 @@ public class NetHandlerPlayServer extends NetHandler implements ICrafting
|
||||||
throw new IllegalArgumentException("Ungültiger Nachrichten-Typ!");
|
throw new IllegalArgumentException("Ungültiger Nachrichten-Typ!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Iterable<String> getWarpList(char pre) {
|
||||||
|
switch(pre) {
|
||||||
|
case '+':
|
||||||
|
return Lists.newArrayList(this.server.getUsers());
|
||||||
|
case '@':
|
||||||
|
List<String> warps = Lists.newArrayList("spawn");
|
||||||
|
for(String warp : this.server.getWarps().keySet()) {
|
||||||
|
warps.add(warp);
|
||||||
|
}
|
||||||
|
return warps;
|
||||||
|
case '*':
|
||||||
|
return UniverseRegistry.getWorldNames();
|
||||||
|
}
|
||||||
|
return Lists.newArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> getVarList() {
|
||||||
|
List<String> list = Lists.newArrayList(Config.VARS.keySet());
|
||||||
|
list.add("time");
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getVarCompletion(String var) {
|
||||||
|
Config.Value v = Config.VARS.get(var);
|
||||||
|
if(v == null)
|
||||||
|
return Lists.newArrayList();
|
||||||
|
if(v.type == ValueType.BOOLEAN)
|
||||||
|
return Boolean.parseBoolean(v.getValue()) ? Lists.newArrayList("false", "true") : Lists.newArrayList("true", "false");
|
||||||
|
else if(v.type == ValueType.INTEGER || v.type == ValueType.FLOAT)
|
||||||
|
return Lists.newArrayList(v.def);
|
||||||
|
return Lists.newArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void processComplete(CPacketComplete packetIn)
|
||||||
|
{
|
||||||
|
NetHandler.checkThread(packetIn, this, this.server);
|
||||||
|
if(packetIn.getMessage().startsWith(" ")) {
|
||||||
|
this.entity.connection.sendPacket(new S3APacketTabComplete(new String[0]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<String> list = Lists.<String>newArrayList(); // Command.getCompletions(this.playerEntity, packetIn.getMessage());
|
||||||
|
|
||||||
|
// for (String s : )
|
||||||
|
// {
|
||||||
|
// list.add(s);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if(list == null) {
|
||||||
|
// list = Lists.<String>newArrayList();
|
||||||
|
String s = packetIn.getMessage();
|
||||||
|
char pre = s.startsWith("#") || s.startsWith("!") || s.startsWith("+") || s.startsWith("*") || s.startsWith("@") ? s.charAt(0) : 0;
|
||||||
|
s = pre == 0 ? s : s.substring(1);
|
||||||
|
String[] argv = s.split(" ", -1);
|
||||||
|
s = argv[argv.length - 1];
|
||||||
|
Iterable<String> res = pre == '#' && (Config.teleportAllowed || this.isAdmin()) ? (argv.length == 5 ? UniverseRegistry.getWorldNames() : Lists.newArrayList()) : ((pre == '+' || pre == '*' || pre == '@') && (Config.teleportAllowed || this.isAdmin()) ? (argv.length == 1 ? this.getWarpList(pre) : Lists.newArrayList()) : (pre == '!' && this.isAdmin() ? this.server.getAllUsernames() : ((this.isAdmin() ? (argv.length == 1 ? getVarList() : (argv.length == 2 ? (argv[0].equals("time") ? Lists.newArrayList("day", "night", "noon", "midnight", "sunrise", "sunset") : getVarCompletion(argv[0])) : Lists.newArrayList())) : Lists.newArrayList()))));
|
||||||
|
for(String s1 : res) {
|
||||||
|
if(s1.regionMatches(true, 0, s, 0, s.length()))
|
||||||
|
list.add((argv.length == 1 && pre != 0 ? pre : "") + s1);
|
||||||
|
}
|
||||||
|
// if(list.isEmpty()) {
|
||||||
|
list.addAll(this.completeCommand(packetIn.getMessage()));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
this.entity.connection.sendPacket(new S3APacketTabComplete((String[])list.toArray(new String[list.size()])));
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isFinite(double value) {
|
private static boolean isFinite(double value) {
|
||||||
return Double.NEGATIVE_INFINITY < value & value < Double.POSITIVE_INFINITY;
|
return Double.NEGATIVE_INFINITY < value & value < Double.POSITIVE_INFINITY;
|
||||||
|
@ -2902,66 +2995,6 @@ public class NetHandlerPlayServer extends NetHandler implements ICrafting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Iterable<String> getWarpList(char pre) {
|
|
||||||
switch(pre) {
|
|
||||||
case '+':
|
|
||||||
return Lists.newArrayList(this.server.getUsers());
|
|
||||||
case '@':
|
|
||||||
List<String> warps = Lists.newArrayList("spawn");
|
|
||||||
for(String warp : this.server.getWarps().keySet()) {
|
|
||||||
warps.add(warp);
|
|
||||||
}
|
|
||||||
return warps;
|
|
||||||
case '*':
|
|
||||||
return UniverseRegistry.getWorldNames();
|
|
||||||
}
|
|
||||||
return Lists.newArrayList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> getVarList() {
|
|
||||||
List<String> list = Lists.newArrayList(Config.VARS.keySet());
|
|
||||||
list.add("time");
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> getVarCompletion(String var) {
|
|
||||||
Config.Value v = Config.VARS.get(var);
|
|
||||||
if(v == null)
|
|
||||||
return Lists.newArrayList();
|
|
||||||
if(v.type == ValueType.BOOLEAN)
|
|
||||||
return Boolean.parseBoolean(v.getValue()) ? Lists.newArrayList("false", "true") : Lists.newArrayList("true", "false");
|
|
||||||
else if(v.type == ValueType.INTEGER || v.type == ValueType.FLOAT)
|
|
||||||
return Lists.newArrayList(v.def);
|
|
||||||
return Lists.newArrayList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void processComplete(CPacketComplete packetIn)
|
|
||||||
{
|
|
||||||
NetHandler.checkThread(packetIn, this, this.server);
|
|
||||||
List<String> list = Lists.<String>newArrayList(); // Command.getCompletions(this.playerEntity, packetIn.getMessage());
|
|
||||||
|
|
||||||
// for (String s : )
|
|
||||||
// {
|
|
||||||
// list.add(s);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if(list == null) {
|
|
||||||
// list = Lists.<String>newArrayList();
|
|
||||||
String s = packetIn.getMessage();
|
|
||||||
char pre = s.startsWith("#") || s.startsWith("!") || s.startsWith("+") || s.startsWith("*") || s.startsWith("@") ? s.charAt(0) : 0;
|
|
||||||
s = pre == 0 ? s : s.substring(1);
|
|
||||||
String[] argv = s.split(" ", -1);
|
|
||||||
s = argv[argv.length - 1];
|
|
||||||
Iterable<String> res = pre == '#' && (Config.teleportAllowed || this.isAdmin()) ? (argv.length == 5 ? UniverseRegistry.getWorldNames() : Lists.newArrayList()) : ((pre == '+' || pre == '*' || pre == '@') && (Config.teleportAllowed || this.isAdmin()) ? (argv.length == 1 ? this.getWarpList(pre) : Lists.newArrayList()) : (pre == '!' && this.isAdmin() ? this.server.getAllUsernames() : ((this.isAdmin() ? (argv.length == 1 ? getVarList() : (argv.length == 2 ? (argv[0].equals("time") ? Lists.newArrayList("day", "night", "noon", "midnight", "sunrise", "sunset") : getVarCompletion(argv[0])) : Lists.newArrayList())) : Lists.newArrayList()))));
|
|
||||||
for(String s1 : res) {
|
|
||||||
if(s1.regionMatches(true, 0, s, 0, s.length()))
|
|
||||||
list.add((argv.length == 1 && pre != 0 ? pre : "") + s1);
|
|
||||||
}
|
|
||||||
// }
|
|
||||||
|
|
||||||
this.entity.connection.sendPacket(new S3APacketTabComplete((String[])list.toArray(new String[list.size()])));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean respawnPlayer() {
|
public boolean respawnPlayer() {
|
||||||
if(this.entity.getHealth() > 0)
|
if(this.entity.getHealth() > 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue