gui and text drawing, gui misc
This commit is contained in:
parent
c906760bd4
commit
4ec8affe85
37 changed files with 799 additions and 646 deletions
7
java/src/game/command/ArgCombiner.java
Normal file
7
java/src/game/command/ArgCombiner.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package game.command;
|
||||
|
||||
public interface ArgCombiner<T> {
|
||||
Object combine(T[] values);
|
||||
Class<?> getTypeClass();
|
||||
Class<T> getInputClass();
|
||||
}
|
|
@ -86,6 +86,7 @@ public abstract class ArgumentParser {
|
|||
public abstract Object parse(ScriptEnvironment env, String input);
|
||||
public abstract Object getDefault(ScriptEnvironment env);
|
||||
public abstract String[] getCompletions(ScriptEnvironment env);
|
||||
public abstract Class<?> getTypeClass();
|
||||
|
||||
public final String getName() {
|
||||
return this.name;
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
package game.command;
|
||||
|
||||
public class BooleanParser extends EnumParser {
|
||||
public class BooleanParser extends EnumParser<Boolean> {
|
||||
public BooleanParser(String name, Boolean def) {
|
||||
super(name, def, true, false);
|
||||
super(name, Boolean.class, def, true, false);
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return this.hasDefault() ? boolean.class : Boolean.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package game.command;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -11,12 +12,14 @@ public class CachedExecutable {
|
|||
private final String name;
|
||||
private final Map<String, Parameter> parameters;
|
||||
private final List<Parameter> positionals;
|
||||
private final Method method;
|
||||
|
||||
protected CachedExecutable(Executable executable, Map<String, Parameter> parameters, List<Parameter> positionals) {
|
||||
protected CachedExecutable(Executable executable, Map<String, Parameter> parameters, List<Parameter> positionals, Method method) {
|
||||
this.executable = executable;
|
||||
this.parameters = parameters;
|
||||
this.positionals = positionals;
|
||||
this.name = executable.getName();
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public static CachedExecutable cacheExecutable(Executable executable) {
|
||||
|
@ -39,13 +42,39 @@ public class CachedExecutable {
|
|||
throw new NullPointerException("positions[" + z + "]");
|
||||
positionals.add(positions[z]);
|
||||
}
|
||||
return new CachedExecutable(executable, parameters, positionals);
|
||||
List<Class<?>> classes = Lists.newArrayList(ScriptEnvironment.class, ScriptExecutor.class);
|
||||
for(Parameter param : executable.getParamList()) {
|
||||
ArgCombiner<?> combiner = param.getCombiner();
|
||||
if(combiner != null) {
|
||||
classes.add(combiner.getTypeClass());
|
||||
continue;
|
||||
}
|
||||
if(param.getParsers().isEmpty()) {
|
||||
classes.add(boolean.class);
|
||||
continue;
|
||||
}
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
classes.add(parser.getTypeClass());
|
||||
}
|
||||
}
|
||||
Method method;
|
||||
try {
|
||||
method = executable.getClass().getDeclaredMethod("exec", classes.toArray(new Class<?>[classes.size()]));
|
||||
}
|
||||
catch(NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return new CachedExecutable(executable, parameters, positionals, method);
|
||||
}
|
||||
|
||||
public Executable getExecutable() {
|
||||
return this.executable;
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
public Map<String, Parameter> getParameters() {
|
||||
return this.parameters;
|
||||
}
|
||||
|
|
|
@ -11,4 +11,8 @@ public abstract class DefaultingParser extends CompletingParser {
|
|||
public Object getDefault(ScriptEnvironment env) {
|
||||
return this.def;
|
||||
}
|
||||
|
||||
protected final boolean hasDefault() {
|
||||
return this.def != null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,4 +40,8 @@ public class DimensionParser extends CompletingParser {
|
|||
public String[] getCompletions(ScriptEnvironment env) {
|
||||
return UniverseRegistry.getWorldNames().toArray(new String[UniverseRegistry.getWorldNames().size()]);
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return Dimension.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,4 +62,8 @@ public class DoubleParser extends DefaultingParser {
|
|||
}
|
||||
return (Double)super.getDefault(env);
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return this.hasDefault() ? double.class : Double.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,10 @@ import java.util.Map;
|
|||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class EnumParser extends DefaultingParser {
|
||||
private final Map<String, Object> lookup = Maps.newHashMap();
|
||||
private final Object[] selections;
|
||||
public class EnumParser<T> extends DefaultingParser {
|
||||
private final Class<T> clazz;
|
||||
private final Map<String, T> lookup = Maps.newHashMap();
|
||||
private final T[] selections;
|
||||
|
||||
private static <T> String joinArgs(T[] iter) {
|
||||
StringBuilder sb = new StringBuilder("'");
|
||||
|
@ -18,16 +19,17 @@ public class EnumParser extends DefaultingParser {
|
|||
return sb.append("'").toString();
|
||||
}
|
||||
|
||||
public EnumParser(String name, Object def, Object ... selections) {
|
||||
public EnumParser(String name, Class<T> clazz, T def, T ... selections) {
|
||||
super(name, def, selections);
|
||||
this.clazz = clazz;
|
||||
this.selections = selections;
|
||||
for(Object o : selections) {
|
||||
for(T o : selections) {
|
||||
this.lookup.put(o.toString().toLowerCase(), o);
|
||||
}
|
||||
}
|
||||
|
||||
public Object parse(ScriptEnvironment env, String input) {
|
||||
Object value = this.lookup.get(input.toLowerCase());
|
||||
public T parse(ScriptEnvironment env, String input) {
|
||||
T value = this.lookup.get(input.toLowerCase());
|
||||
if(value != null)
|
||||
return value;
|
||||
int id = -1;
|
||||
|
@ -41,4 +43,8 @@ public class EnumParser extends DefaultingParser {
|
|||
joinArgs(this.selections));
|
||||
return this.selections[id];
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return this.clazz;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Executable {
|
||||
Object exec(ScriptEnvironment env, ScriptArgs args);
|
||||
// Object exec(ScriptEnvironment env, ScriptArgs args);
|
||||
Map<String, Parameter> getParameters();
|
||||
List<Parameter> getParamList();
|
||||
String getName();
|
||||
}
|
||||
|
|
|
@ -32,4 +32,8 @@ public class IntParser extends DefaultingParser {
|
|||
throw new ScriptException("Die Zahl darf höchstens %d betragen, habe %d", this.max, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return this.hasDefault() ? int.class : Integer.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,4 +30,8 @@ public class LongParser extends DefaultingParser {
|
|||
throw new ScriptException("Die Zahl darf höchstens %d betragen, habe %d", this.max, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return this.hasDefault() ? long.class : Long.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,13 +8,15 @@ public class Parameter {
|
|||
private final boolean required;
|
||||
private final int position;
|
||||
private final List<ArgumentParser> parsers;
|
||||
private final ArgCombiner<?> combiner;
|
||||
|
||||
public Parameter(String name, char shortName, int position, boolean required, List<ArgumentParser> parsers) {
|
||||
public Parameter(String name, char shortName, int position, boolean required, List<ArgumentParser> parsers, ArgCombiner<?> combiner) {
|
||||
this.name = name;
|
||||
this.shortName = shortName;
|
||||
this.position = position;
|
||||
this.required = required;
|
||||
this.parsers = parsers;
|
||||
this.combiner = combiner;
|
||||
}
|
||||
|
||||
public boolean isPositional() {
|
||||
|
@ -29,6 +31,10 @@ public class Parameter {
|
|||
return this.parsers;
|
||||
}
|
||||
|
||||
public ArgCombiner<?> getCombiner() {
|
||||
return this.combiner;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return this.required;
|
||||
}
|
||||
|
|
|
@ -8,19 +8,6 @@ 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;
|
||||
import game.entity.npc.EntityNPC;
|
||||
import game.entity.types.EntityLiving;
|
||||
import game.item.Item;
|
||||
import game.item.ItemStack;
|
||||
import game.nbt.NBTTagCompound;
|
||||
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;
|
||||
|
@ -126,6 +113,9 @@ public class ScriptArgs {
|
|||
throw new ScriptException("Argument '%s' muss angegeben werden", param.getName());
|
||||
}
|
||||
}
|
||||
else if(param.getParsers().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> params = Maps.newHashMapWithExpectedSize(param.getParsers().size());
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
params.put(parser.getName(), parser.getDefault(env));
|
||||
|
@ -200,212 +190,222 @@ public class ScriptArgs {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static String[] parseComplete(ScriptEnvironment env, String[] argv, CachedExecutable cached) {
|
||||
public static String[] parseComplete(ScriptEnvironment env, String[] argv, CachedExecutable cached, String last) {
|
||||
String[] comp = getParam(env, argv, cached);
|
||||
if(comp == null || comp.length == 0) {
|
||||
Set<String> params = Sets.newTreeSet();
|
||||
boolean all = last.startsWith("--");
|
||||
for(String param : cached.getParameters().keySet()) {
|
||||
params.add(param.length() == 1 ? "-" + param : ("--" + param));
|
||||
if(all || param.length() == 1)
|
||||
params.add(param.length() == 1 ? "-" + param : ("--" + param));
|
||||
}
|
||||
comp = params.toArray(new String[params.size()]);
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
||||
public ScriptArg getArg(String name) {
|
||||
return this.arguments.get(name);
|
||||
}
|
||||
// public ScriptArg getArg(String name) {
|
||||
// return this.arguments.get(name);
|
||||
// }
|
||||
|
||||
public boolean hasArg(String name) {
|
||||
return this.arguments.containsKey(name);
|
||||
}
|
||||
|
||||
public boolean has(String name, String par) {
|
||||
return this.arguments.containsKey(name) && this.arguments.get(name).getValues().containsKey(par);
|
||||
}
|
||||
// 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 boolean has(String name) {
|
||||
// return this.has(name, name);
|
||||
// }
|
||||
|
||||
public <T> T getDefault(String name, String par, T def) {
|
||||
// public <T> T getDefault(String name, String par, T def) {
|
||||
// ScriptArg arg = this.arguments.get(name);
|
||||
// if(arg == null)
|
||||
// return def;
|
||||
// Object value = arg.getValues().get(par);
|
||||
// return value == null ? def : (T)value;
|
||||
// }
|
||||
//
|
||||
// public <T> T getDefault(String name, T def) {
|
||||
// return this.getDefault(name, name, def);
|
||||
// }
|
||||
//
|
||||
// public <T> T getUnchecked(String name, String par) {
|
||||
// return this.getDefault(name, par, null);
|
||||
// }
|
||||
|
||||
public <T> T getUnchecked(String name, String par) {
|
||||
ScriptArg arg = this.arguments.get(name);
|
||||
if(arg == null)
|
||||
return def;
|
||||
return null;
|
||||
Object value = arg.getValues().get(par);
|
||||
return value == null ? def : (T)value;
|
||||
return value == null ? null : (T)value;
|
||||
}
|
||||
|
||||
public <T> T getDefault(String name, T def) {
|
||||
return this.getDefault(name, name, def);
|
||||
}
|
||||
|
||||
public <T> T getUnchecked(String name, String par) {
|
||||
return this.getDefault(name, par, null);
|
||||
}
|
||||
|
||||
public <T> T getUnchecked(String name) {
|
||||
return this.getDefault(name, null);
|
||||
}
|
||||
|
||||
public boolean getBool(String name, boolean def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public boolean getBool(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public <T extends Enum<?>> T getEnum(String name, T def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public <T extends Enum<?>> T getEnum(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public int getInt(String name, int def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public int getInt(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public long getLong(String name, long def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public long getLong(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public double getDouble(String name, double def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public double getDouble(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public String getString(String name, String def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public String getString(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public String[] getStrings(String name, String[] def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public String[] getStrings(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public Entity getEntity(String name, Entity def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public Entity getEntity(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public EntityLiving getLiving(String name, EntityLiving def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public EntityLiving getLiving(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public EntityNPC getNpc(String name, EntityNPC def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public EntityNPC getNpc(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public Block getBlock(String name, Block def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public Block getBlock(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public State getState(String name, State def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public State getState(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public Item getItem(String name, Item def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public Item getItem(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public ItemStack getStack(String name, ItemStack def) {
|
||||
return this.getDefault(name, def);
|
||||
}
|
||||
|
||||
public ItemStack getStack(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
|
||||
public BlockPos getColumnPos(String name, BlockPos def) {
|
||||
return this.hasArg(name) ? this.getColumnPos(name) : def;
|
||||
}
|
||||
|
||||
public BlockPos getColumnPos(String name) {
|
||||
return new BlockPos(this.getUnchecked(name, "x"), 0, this.getUnchecked(name, "z"));
|
||||
}
|
||||
|
||||
public BlockPos getBlockPos(String name, BlockPos def) {
|
||||
return this.hasArg(name) ? this.getBlockPos(name) : def;
|
||||
}
|
||||
|
||||
public BlockPos getBlockPos(String name) {
|
||||
return new BlockPos(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"));
|
||||
}
|
||||
|
||||
public WorldPos getWorldPos(String name, WorldPos def) {
|
||||
return this.hasArg(name) ? this.getWorldPos(name) : def;
|
||||
}
|
||||
|
||||
public WorldPos getWorldPos(String name) {
|
||||
return new WorldPos(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"),
|
||||
this.getUnchecked(name, "dim"));
|
||||
}
|
||||
|
||||
public Vec3 getVector2D(String name, Vec3 def) {
|
||||
return this.hasArg(name) ? this.getVector2D(name) : def;
|
||||
}
|
||||
|
||||
public Vec3 getVector2D(String name) {
|
||||
return new Vec3(this.getUnchecked(name, "x"), 0.0, this.getUnchecked(name, "z"));
|
||||
}
|
||||
|
||||
public Vec3 getVector(String name, Vec3 def) {
|
||||
return this.hasArg(name) ? this.getVector(name) : def;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public NBTTagCompound getTag(String name) {
|
||||
return this.getUnchecked(name);
|
||||
}
|
||||
// public <T> T getUnchecked(String name) {
|
||||
// return this.getDefault(name, null);
|
||||
// }
|
||||
//
|
||||
// public boolean getBool(String name, boolean def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public boolean getBool(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public <T extends Enum<?>> T getEnum(String name, T def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public <T extends Enum<?>> T getEnum(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public int getInt(String name, int def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public int getInt(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public long getLong(String name, long def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public long getLong(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public double getDouble(String name, double def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public double getDouble(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public String getString(String name, String def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public String getString(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public String[] getStrings(String name, String[] def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public String[] getStrings(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public Entity getEntity(String name, Entity def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public Entity getEntity(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public EntityLiving getLiving(String name, EntityLiving def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public EntityLiving getLiving(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public EntityNPC getNpc(String name, EntityNPC def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public EntityNPC getNpc(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public Block getBlock(String name, Block def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public Block getBlock(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public State getState(String name, State def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public State getState(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public Item getItem(String name, Item def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public Item getItem(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public ItemStack getStack(String name, ItemStack def) {
|
||||
// return this.getDefault(name, def);
|
||||
// }
|
||||
//
|
||||
// public ItemStack getStack(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
//
|
||||
// public BlockPos getColumnPos(String name, BlockPos def) {
|
||||
// return this.hasArg(name) ? this.getColumnPos(name) : def;
|
||||
// }
|
||||
//
|
||||
// public BlockPos getColumnPos(String name) {
|
||||
// return new BlockPos(this.getUnchecked(name, "x"), 0, this.getUnchecked(name, "z"));
|
||||
// }
|
||||
//
|
||||
// public BlockPos getBlockPos(String name, BlockPos def) {
|
||||
// return this.hasArg(name) ? this.getBlockPos(name) : def;
|
||||
// }
|
||||
//
|
||||
// public BlockPos getBlockPos(String name) {
|
||||
// return new BlockPos(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"));
|
||||
// }
|
||||
//
|
||||
// public WorldPos getWorldPos(String name, WorldPos def) {
|
||||
// return this.hasArg(name) ? this.getWorldPos(name) : def;
|
||||
// }
|
||||
//
|
||||
// public WorldPos getWorldPos(String name) {
|
||||
// return new WorldPos(this.getUnchecked(name, "x"), this.getUnchecked(name, "y"), this.getUnchecked(name, "z"),
|
||||
// this.getUnchecked(name, "dim"));
|
||||
// }
|
||||
//
|
||||
// public Vec3 getVector2D(String name, Vec3 def) {
|
||||
// return this.hasArg(name) ? this.getVector2D(name) : def;
|
||||
// }
|
||||
//
|
||||
// public Vec3 getVector2D(String name) {
|
||||
// return new Vec3(this.getUnchecked(name, "x"), 0.0, this.getUnchecked(name, "z"));
|
||||
// }
|
||||
//
|
||||
// public Vec3 getVector(String name, Vec3 def) {
|
||||
// return this.hasArg(name) ? this.getVector(name) : def;
|
||||
// }
|
||||
//
|
||||
// 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);
|
||||
// }
|
||||
//
|
||||
// public NBTTagCompound getTag(String name) {
|
||||
// return this.getUnchecked(name);
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package game.command;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
@ -10,6 +12,7 @@ import com.google.common.collect.Maps;
|
|||
import game.Server;
|
||||
import game.color.TextColor;
|
||||
import game.command.commands.CommandSpawn;
|
||||
import game.log.Log;
|
||||
|
||||
public class ScriptEnvironment {
|
||||
private final Server server;
|
||||
|
@ -80,7 +83,36 @@ public class ScriptEnvironment {
|
|||
if(cached == null)
|
||||
throw new ScriptException("Skript '%s' existiert nicht", argv[0]);
|
||||
ScriptArgs args = ScriptArgs.parseArgs(this, cmd, argv, cached);
|
||||
o = cached.getExecutable().exec(this, args);
|
||||
List<Object> params = Lists.newArrayList(this, this.currentExecutor);
|
||||
for(Parameter param : cached.getExecutable().getParamList()) {
|
||||
ArgCombiner combiner = param.getCombiner();
|
||||
if(combiner != null) {
|
||||
Object[] data = (Object[])Array.newInstance(combiner.getInputClass(), param.getParsers().size());
|
||||
for(int z = 0; z < data.length; z++) {
|
||||
data[z] = args.getUnchecked(param.getName(), param.getParsers().get(z).getName());
|
||||
if(data[z] == null) {
|
||||
data = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
params.add(data == null ? null : combiner.combine(data));
|
||||
continue;
|
||||
}
|
||||
if(param.getParsers().isEmpty()) {
|
||||
params.add(args.hasArg(param.getName()));
|
||||
continue;
|
||||
}
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
params.add(args.getUnchecked(param.getName(), parser.getName()));
|
||||
}
|
||||
}
|
||||
try {
|
||||
o = cached.getMethod().invoke(cached.getExecutable(), params.toArray(new Object[params.size()]));
|
||||
}
|
||||
catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// o = cached.getExecutable().exec(this, args);
|
||||
if(setPrev)
|
||||
this.previousOutput = o;
|
||||
}
|
||||
|
@ -102,6 +134,7 @@ public class ScriptEnvironment {
|
|||
}
|
||||
catch(Throwable t) {
|
||||
exec.logConsole(TextColor.RED + "Fehler: %s", t.getMessage());
|
||||
Log.CONSOLE.error(t, "Fehler beim Ausführen von Befehl '%s'", cmd);
|
||||
}
|
||||
finally {
|
||||
this.currentExecutor = null;
|
||||
|
@ -118,17 +151,17 @@ public class ScriptEnvironment {
|
|||
String[] argv = cmds[cmds.length - 1];
|
||||
if(argv.length == 0)
|
||||
return list;
|
||||
String param = cmd.endsWith(" ") ? "" : argv[argv.length - 1];
|
||||
String[] comp;
|
||||
if(argv.length > 1) {
|
||||
CachedExecutable cached = this.executables.get(argv[0]);
|
||||
if(cached == null)
|
||||
return list;
|
||||
comp = ScriptArgs.parseComplete(this, argv, cached);
|
||||
comp = ScriptArgs.parseComplete(this, argv, cached, param);
|
||||
}
|
||||
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);
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
package game.command;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import game.command.DoubleParser.DefType;
|
||||
import game.world.Vec3;
|
||||
import game.world.World;
|
||||
|
||||
public abstract class ScriptExecutable implements Executable {
|
||||
private final String name;
|
||||
private final Map<String, Parameter> parameters = Maps.newHashMap();
|
||||
private final List<Parameter> argList = Lists.newArrayList();
|
||||
|
||||
private int parPos = 0;
|
||||
private boolean parReq = true;
|
||||
|
@ -25,16 +29,28 @@ public abstract class ScriptExecutable implements Executable {
|
|||
return this;
|
||||
}
|
||||
|
||||
protected ScriptExecutable addParameter(String name, ArgumentParser ... parsers) {
|
||||
this.parameters.put(name, new Parameter(name, (char)0, this.parPos++, this.parReq, Lists.newArrayList(parsers)));
|
||||
protected ScriptExecutable addParameter(String name, ArgCombiner<?> combiner, ArgumentParser ... parsers) {
|
||||
Parameter param = new Parameter(name, (char)0, this.parPos++, this.parReq, Lists.newArrayList(parsers), combiner);
|
||||
this.parameters.put(name, param);
|
||||
this.argList.add(param);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected ScriptExecutable addParameter(String name, char shortName, ArgumentParser ... parsers) {
|
||||
this.parameters.put(name, new Parameter(name, shortName, -1, false, Lists.newArrayList(parsers)));
|
||||
protected ScriptExecutable addParameter(String name, char shortName, ArgCombiner<?> combiner, ArgumentParser ... parsers) {
|
||||
Parameter param = new Parameter(name, shortName, -1, false, Lists.newArrayList(parsers), combiner);
|
||||
this.parameters.put(name, param);
|
||||
this.argList.add(param);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected ScriptExecutable addParameter(String name, ArgumentParser ... parsers) {
|
||||
return this.addParameter(name, null, parsers);
|
||||
}
|
||||
|
||||
protected ScriptExecutable addParameter(String name, char shortName, ArgumentParser ... parsers) {
|
||||
return this.addParameter(name, shortName, null, parsers);
|
||||
}
|
||||
|
||||
protected ScriptExecutable addParameter(ArgumentParser parser) {
|
||||
return this.addParameter(parser.getName(), parser);
|
||||
}
|
||||
|
@ -44,7 +60,22 @@ public abstract class ScriptExecutable implements Executable {
|
|||
}
|
||||
|
||||
protected ScriptExecutable addVector(String name, boolean defaulted) {
|
||||
return this.addParameter(name, new DoubleParser("x", defaulted ? DefType.X : null, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE), new DoubleParser("y", defaulted ? DefType.Y : null, 0.0, (double)World.HEIGHT), new DoubleParser("z", defaulted ? DefType.Z : null, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE));
|
||||
return this.addParameter(name, new ArgCombiner<Double>() {
|
||||
public Vec3 combine(Double[] values) {
|
||||
return new Vec3(values[0], values[1], values[2]);
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return Vec3.class;
|
||||
}
|
||||
|
||||
public Class<Double> getInputClass() {
|
||||
return Double.class;
|
||||
}
|
||||
},
|
||||
new DoubleParser("x", defaulted ? DefType.X : null, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE),
|
||||
new DoubleParser("y", defaulted ? DefType.Y : null, 0.0, (double)World.HEIGHT),
|
||||
new DoubleParser("z", defaulted ? DefType.Z : null, (double)(-World.MAX_SIZE), (double)World.MAX_SIZE));
|
||||
}
|
||||
|
||||
protected ScriptExecutable addWorld(String name, boolean defaulted) {
|
||||
|
@ -63,24 +94,24 @@ public abstract class ScriptExecutable implements Executable {
|
|||
return this.addParameter(name, shortName);
|
||||
}
|
||||
|
||||
protected ScriptExecutable addFlag(String name) {
|
||||
return this.addParameter(name, name.charAt(0));
|
||||
// protected ScriptExecutable addFlag(String name) {
|
||||
// return this.addParameter(name, name.charAt(0));
|
||||
// }
|
||||
|
||||
protected <T> ScriptExecutable addEnum(String name, T def, Class<T> clazz, T ... values) {
|
||||
return this.addParameter(new EnumParser<T>(name, clazz, def, values));
|
||||
}
|
||||
|
||||
protected ScriptExecutable addEnum(String name, Object ... values) {
|
||||
return this.addParameter(new EnumParser(name, null, values));
|
||||
protected <T> ScriptExecutable addEnum(String name, Class<T> clazz, T ... values) {
|
||||
return this.addEnum(name, null, clazz, values);
|
||||
}
|
||||
|
||||
protected ScriptExecutable addEnumDef(String name, Object def, Object ... values) {
|
||||
return this.addParameter(new EnumParser(name, null, values));
|
||||
protected <T> ScriptExecutable addEnum(String name, T def, Class<T> clazz, Collection<T> values) {
|
||||
return this.addEnum(name, def, clazz, values.toArray((T[])Array.newInstance(clazz, values.size())));
|
||||
}
|
||||
|
||||
protected ScriptExecutable addEnum(String name, Collection<?> values) {
|
||||
return this.addEnum(name, (Object[])values.toArray(new String[values.size()]));
|
||||
}
|
||||
|
||||
protected ScriptExecutable addEnumDef(String name, Object def, Collection<?> values) {
|
||||
return this.addEnumDef(name, def, (Object[])values.toArray(new String[values.size()]));
|
||||
protected <T> ScriptExecutable addEnum(String name, Class<T> clazz, Collection<T> values) {
|
||||
return this.addEnum(name, null, clazz, values);
|
||||
}
|
||||
|
||||
protected ScriptExecutable addTag(String name) {
|
||||
|
@ -91,14 +122,19 @@ public abstract class ScriptExecutable implements Executable {
|
|||
return this.addParameter(shortName, new TagParser(name, null));
|
||||
}
|
||||
|
||||
public Object exec(ScriptEnvironment env, ScriptArgs args) {
|
||||
return null;
|
||||
}
|
||||
// public Object exec(ScriptEnvironment env, ScriptArgs args) {
|
||||
// this.getClass().getDeclaredMethod(name, null)
|
||||
// return null;
|
||||
// }
|
||||
|
||||
public Map<String, Parameter> getParameters() {
|
||||
return this.parameters;
|
||||
}
|
||||
|
||||
public List<Parameter> getParamList() {
|
||||
return this.argList;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package game.command;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import game.util.CharValidator;
|
||||
|
||||
public class StringParser extends DefaultingParser {
|
||||
private final boolean allowEmpty;
|
||||
private final Integer minLength;
|
||||
private final Integer maxLength;
|
||||
private final Predicate<Character> validator;
|
||||
private final CharValidator validator;
|
||||
|
||||
public StringParser(String name, String def, boolean allowEmpty, Integer minLength, Integer maxLength, Predicate<Character> validator,
|
||||
public StringParser(String name, String def, boolean allowEmpty, Integer minLength, Integer maxLength, CharValidator validator,
|
||||
Object ... completions) {
|
||||
super(name, def, completions);
|
||||
this.allowEmpty = allowEmpty;
|
||||
|
@ -34,6 +34,12 @@ public class StringParser extends DefaultingParser {
|
|||
else
|
||||
throw new ScriptException("Die Zeichenkette darf höchstens %d Zeichen lang sein, habe %d ('%s')",
|
||||
this.maxLength, input.length(), input);
|
||||
if(this.validator != null && !this.validator.valid(input))
|
||||
throw new ScriptException("Die Zeichenkette '%s' enthält ungültige Zeichen", input);
|
||||
return input;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,4 +19,8 @@ public class TagParser extends DefaultingParser {
|
|||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return NBTTagCompound.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,4 +41,8 @@ public class WorldParser extends DimensionParser {
|
|||
}
|
||||
return super.getCompletions(env);
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass() {
|
||||
return WorldServer.class;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,10 @@ import java.util.Set;
|
|||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import game.command.ScriptArgs;
|
||||
import game.command.ScriptEnvironment;
|
||||
import game.command.ScriptException;
|
||||
import game.command.ScriptExecutable;
|
||||
import game.command.ScriptExecutor;
|
||||
import game.entity.Entity;
|
||||
import game.entity.types.EntityLiving;
|
||||
import game.init.EntityRegistry;
|
||||
|
@ -18,28 +18,23 @@ 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.addEnum("type", names);
|
||||
this.addEnum("type", String.class, names);
|
||||
this.setParamsOptional();
|
||||
this.addVector("position", true);
|
||||
this.addWorld("dim", true);
|
||||
this.addTag("tag");
|
||||
this.addFlag("noinit");
|
||||
|
||||
this.addFlag("noinit", 'n');
|
||||
this.addInt("count", 'c', 1, 1024, 1);
|
||||
this.addTag("postTag", 'p');
|
||||
}
|
||||
|
||||
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");
|
||||
NBTTagCompound tag = args.getTag("tag");
|
||||
NBTTagCompound postTag = args.getTag("postTag");
|
||||
public Object exec(ScriptEnvironment env, ScriptExecutor exec, String type, Vec3 pos, WorldServer world, NBTTagCompound tag, boolean noinit, int count, NBTTagCompound postTag) {
|
||||
for(int z = 0; z < count; z++) {
|
||||
Entity entity = EntityRegistry.createEntityByName(type, world);
|
||||
if(entity == null)
|
||||
|
@ -51,7 +46,7 @@ public class CommandSpawn extends ScriptExecutable {
|
|||
ent.merge(tag);
|
||||
entity.readFromNBT(ent);
|
||||
}
|
||||
if(init && (entity instanceof EntityLiving))
|
||||
if(!noinit && (entity instanceof EntityLiving))
|
||||
((EntityLiving)entity).onInitialSpawn(null);
|
||||
world.spawnEntityInWorld(entity);
|
||||
if(postTag != null) {
|
||||
|
@ -61,7 +56,7 @@ public class CommandSpawn extends ScriptExecutable {
|
|||
entity.readFromNBT(ent);
|
||||
}
|
||||
}
|
||||
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));
|
||||
exec.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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue