code cleanup: use records where possible
This commit is contained in:
parent
f30141c8f3
commit
8e0bbd06c2
139 changed files with 957 additions and 1777 deletions
|
@ -1043,7 +1043,7 @@ public final class Server implements IThreadListener {
|
|||
}
|
||||
else {
|
||||
Position rpos = this.getRandomSpawnPosition(origin);
|
||||
nplayer.setLocationAndAngles(rpos.x, rpos.y, rpos.z, rpos.yaw, rpos.pitch);
|
||||
nplayer.setLocationAndAngles(rpos.x(), rpos.y(), rpos.z(), rpos.yaw(), rpos.pitch());
|
||||
// this.movePlayerToSpawn(nplayer);
|
||||
}
|
||||
world.loadChunk((int)nplayer.posX >> 4, (int)nplayer.posZ >> 4);
|
||||
|
|
|
@ -2,32 +2,5 @@ package server.command;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
public class Argument {
|
||||
private final Parameter parameter;
|
||||
private final int position;
|
||||
private final String[] inputs;
|
||||
private final Map<String, Object> values;
|
||||
|
||||
public Argument(Parameter parameter, int position, String[] inputs, Map<String, Object> values) {
|
||||
this.parameter = parameter;
|
||||
this.position = position;
|
||||
this.inputs = inputs;
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public Parameter getParameter() {
|
||||
return this.parameter;
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public String[] getInputs() {
|
||||
return this.inputs;
|
||||
}
|
||||
|
||||
public Map<String, Object> getValues() {
|
||||
return this.values;
|
||||
}
|
||||
public record Argument(Parameter parameter, int position, String[] inputs, Map<String, Object> values) {
|
||||
}
|
||||
|
|
|
@ -30,8 +30,8 @@ public class ArgumentSplitter {
|
|||
}
|
||||
|
||||
public static ArgumentSplitter parseArgs(CommandEnvironment env, String str, String[] argv, CachedExecutable cached) {
|
||||
Map<String, Parameter> parameters = cached.getParameters();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.getPositionals());
|
||||
Map<String, Parameter> parameters = cached.parameters();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.positionals());
|
||||
// String[] argv = ArgumentParser.splitString(str);
|
||||
Map<String, Argument> args = Maps.newHashMap();
|
||||
int ppos = 0;
|
||||
|
@ -46,7 +46,7 @@ public class ArgumentSplitter {
|
|||
}
|
||||
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.containsKey(param.getName())) {
|
||||
if(param != null && param.positional() && !args.containsKey(param.name())) {
|
||||
for(int n = 0; n < positionals.size(); n++) {
|
||||
if(param == positionals.get(n)) {
|
||||
positionals.remove(n);
|
||||
|
@ -64,71 +64,71 @@ public class ArgumentSplitter {
|
|||
}
|
||||
if(param == null)
|
||||
throw new RunException("Position %d: Argument '%s' ist unbekannt", z, arg);
|
||||
if(args.containsKey(param.getName()))
|
||||
throw new RunException("Position %d: Parameter '%s' mehrfach angegeben", z, param.getName());
|
||||
int nargs = param.getParsers().size();
|
||||
if(args.containsKey(param.name()))
|
||||
throw new RunException("Position %d: Parameter '%s' mehrfach angegeben", z, param.name());
|
||||
int nargs = param.parsers().size();
|
||||
// if(!pos)
|
||||
// z += 1;
|
||||
if(z + (pos ? 0 : 1) + nargs > argv.length)
|
||||
if(nargs == 1 && param.getName().equals(param.getParsers().get(0).getName()))
|
||||
throw new RunException("Position %d: Argument '%s' benötigt einen Parameter", z, param.getName());
|
||||
if(nargs == 1 && param.name().equals(param.parsers().get(0).getName()))
|
||||
throw new RunException("Position %d: Argument '%s' benötigt einen Parameter", z, param.name());
|
||||
else
|
||||
throw new RunException("Position %d: Argument '%s' benötigt %d Parameter (%s)", z, param.getName(), nargs,
|
||||
joinArgs(param.getParsers()));
|
||||
throw new RunException("Position %d: Argument '%s' benötigt %d Parameter (%s)", z, param.name(), nargs,
|
||||
joinArgs(param.parsers()));
|
||||
Map<String, Object> params = Maps.newHashMapWithExpectedSize(nargs);
|
||||
String[] inputs = new String[nargs + (pos ? 0 : 1)];
|
||||
int apos = 0;
|
||||
for(int n = pos ? 0 : 1; n < nargs + (pos ? 0 : 1); n++) {
|
||||
String par = inputs[n] = argv[z + n];
|
||||
ArgumentParser parser = param.getParsers().get(apos);
|
||||
ArgumentParser parser = param.parsers().get(apos);
|
||||
if(parse && (par.startsWith("--") || (par.startsWith("-") && par.length() == 2)))
|
||||
if(nargs == 1 && param.getName().equals(parser.getName()))
|
||||
throw new RunException("Position %d: Argument '%s': '%s' als Parameter verwendet", z + n, param.getName(), par);
|
||||
if(nargs == 1 && param.name().equals(parser.getName()))
|
||||
throw new RunException("Position %d: Argument '%s': '%s' als Parameter verwendet", z + n, param.name(), par);
|
||||
else
|
||||
throw new RunException("Position %d: Argument '%s': '%s' als Parameter '%s' (#%d) verwendet", z + n,
|
||||
param.getName(), par, parser.getName(), apos + 1);
|
||||
param.name(), par, parser.getName(), apos + 1);
|
||||
try {
|
||||
params.put(parser.getName(), parser.parse(env, par));
|
||||
}
|
||||
catch(Throwable e) {
|
||||
if(nargs == 1 && param.getName().equals(parser.getName()))
|
||||
if(nargs == 1 && param.name().equals(parser.getName()))
|
||||
throw new RunException(e, "Position %d: Argument '%s': Parameter konnte nicht interpretiert werden", z + n,
|
||||
param.getName());
|
||||
param.name());
|
||||
else
|
||||
throw new RunException(e, "Position %d: Argument '%s': Parameter '%s' (#%d) konnte nicht interpretiert werden", z + n,
|
||||
param.getName(), parser.getName(), apos + 1);
|
||||
param.name(), parser.getName(), apos + 1);
|
||||
}
|
||||
apos += 1;
|
||||
}
|
||||
if(!pos)
|
||||
inputs[0] = arg;
|
||||
args.put(param.getName(), new Argument(param, z, inputs, params));
|
||||
args.put(param.name(), new Argument(param, z, inputs, params));
|
||||
z += nargs - (pos ? 1 : 0);
|
||||
}
|
||||
for(Parameter param : parameters.values()) {
|
||||
if(!args.containsKey(param.getName())) {
|
||||
if(param.isRequired()) {
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
if(!args.containsKey(param.name())) {
|
||||
if(param.required()) {
|
||||
for(ArgumentParser parser : param.parsers()) {
|
||||
if(parser.getDefault(env) == null)
|
||||
throw new RunException("Argument '%s' muss angegeben werden", param.getName());
|
||||
throw new RunException("Argument '%s' muss angegeben werden", param.name());
|
||||
}
|
||||
}
|
||||
else if(param.getParsers().isEmpty()) {
|
||||
else if(param.parsers().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> params = Maps.newHashMapWithExpectedSize(param.getParsers().size());
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
Map<String, Object> params = Maps.newHashMapWithExpectedSize(param.parsers().size());
|
||||
for(ArgumentParser parser : param.parsers()) {
|
||||
params.put(parser.getName(), parser.getDefault(env));
|
||||
}
|
||||
args.put(param.getName(), new Argument(param, -1, null, params));
|
||||
args.put(param.name(), new Argument(param, -1, null, params));
|
||||
}
|
||||
}
|
||||
return new ArgumentSplitter(args, str, env);
|
||||
}
|
||||
|
||||
private static Iterable<String> getParam(CommandEnvironment env, String[] argv, CachedExecutable cached) {
|
||||
Map<String, Parameter> parameters = cached.getParameters();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.getPositionals());
|
||||
Map<String, Parameter> parameters = cached.parameters();
|
||||
List<Parameter> positionals = Lists.newArrayList(cached.positionals());
|
||||
Set<String> args = Sets.newHashSet();
|
||||
int ppos = 0;
|
||||
boolean parse = true;
|
||||
|
@ -139,9 +139,9 @@ public class ArgumentSplitter {
|
|||
if(z == argv.length - 1) {
|
||||
if(ppos < positionals.size()) {
|
||||
param = positionals.get(ppos);
|
||||
if(param.getParsers().isEmpty()) // np
|
||||
if(param.parsers().isEmpty()) // np
|
||||
return null;
|
||||
return param.getParsers().get(0).getCompletions(env);
|
||||
return param.parsers().get(0).getCompletions(env);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
|
@ -153,7 +153,7 @@ public class ArgumentSplitter {
|
|||
}
|
||||
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())) {
|
||||
if(param != null && param.positional() && !args.contains(param.name())) {
|
||||
for(int n = 0; n < positionals.size(); n++) {
|
||||
if(param == positionals.get(n)) {
|
||||
positionals.remove(n);
|
||||
|
@ -169,22 +169,22 @@ public class ArgumentSplitter {
|
|||
else {
|
||||
return null;
|
||||
}
|
||||
if(param == null || args.contains(param.getName()))
|
||||
if(param == null || args.contains(param.name()))
|
||||
return null;
|
||||
int nargs = param.getParsers().size();
|
||||
int nargs = param.parsers().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);
|
||||
return param.parsers().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());
|
||||
args.add(param.name());
|
||||
z += nargs - (pos ? 1 : 0);
|
||||
}
|
||||
return null;
|
||||
|
@ -195,7 +195,7 @@ public class ArgumentSplitter {
|
|||
if(comp == null /* || comp.length == 0 */ ) {
|
||||
Set<String> params = Sets.newTreeSet();
|
||||
boolean all = last.startsWith("--");
|
||||
for(String param : cached.getParameters().keySet()) {
|
||||
for(String param : cached.parameters().keySet()) {
|
||||
if(all || param.length() == 1)
|
||||
params.add(param.length() == 1 ? "-" + param : ("--" + param));
|
||||
}
|
||||
|
@ -204,43 +204,15 @@ public class ArgumentSplitter {
|
|||
return comp;
|
||||
}
|
||||
|
||||
// public ScriptArg getArg(String name) {
|
||||
// return this.arguments.get(name);
|
||||
// }
|
||||
|
||||
public boolean hasArg(String name) {
|
||||
public boolean has(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) {
|
||||
// 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;
|
||||
// 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) {
|
||||
public <T> T get(String name, String par) {
|
||||
Argument arg = this.arguments.get(name);
|
||||
if(arg == null)
|
||||
return null;
|
||||
Object value = arg.getValues().get(par);
|
||||
Object value = arg.values().get(par);
|
||||
return value == null ? null : (T)value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,21 +7,7 @@ import java.util.Map;
|
|||
import common.collect.Lists;
|
||||
import common.collect.Maps;
|
||||
|
||||
public class CachedExecutable {
|
||||
private final Executable executable;
|
||||
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, Method method) {
|
||||
this.executable = executable;
|
||||
this.parameters = parameters;
|
||||
this.positionals = positionals;
|
||||
this.name = executable.getName();
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public record CachedExecutable(Executable executable, Map<String, Parameter> parameters, List<Parameter> positionals, Method method) {
|
||||
public static CachedExecutable cacheExecutable(Executable executable) {
|
||||
Map<String, Parameter> parameters = Maps.newTreeMap();
|
||||
Map<String, Parameter> params = executable.getParameters();
|
||||
|
@ -29,12 +15,12 @@ public class CachedExecutable {
|
|||
Parameter[] positions = new Parameter[parameters.size()];
|
||||
int pos = -1;
|
||||
for(Parameter param : params.values()) {
|
||||
if(param.isPositional()) {
|
||||
positions[param.getPosition()] = param;
|
||||
pos = param.getPosition() > pos ? param.getPosition() : pos;
|
||||
if(param.positional()) {
|
||||
positions[param.position()] = param;
|
||||
pos = param.position() > pos ? param.position() : pos;
|
||||
}
|
||||
if(param.hasShortName())
|
||||
parameters.put("" + param.getShortName(), param);
|
||||
if(param.shorthand())
|
||||
parameters.put("" + param.shortName(), param);
|
||||
}
|
||||
List<Parameter> positionals = Lists.newArrayList();
|
||||
for(int z = 0; z <= pos; z++) {
|
||||
|
@ -44,17 +30,17 @@ public class CachedExecutable {
|
|||
}
|
||||
List<Class<?>> classes = Lists.newArrayList(CommandEnvironment.class, Executor.class);
|
||||
for(Parameter param : executable.getParamList()) {
|
||||
ArgCombiner<?> combiner = param.getCombiner();
|
||||
ArgCombiner<?> combiner = param.combiner();
|
||||
if(combiner != null) {
|
||||
classes.add(combiner.getTypeClass());
|
||||
continue;
|
||||
}
|
||||
if(param.getParsers().isEmpty()) {
|
||||
if(param.parsers().isEmpty()) {
|
||||
classes.add(boolean.class);
|
||||
continue;
|
||||
}
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
classes.add(parser.getTypeClass(param.isRequired()));
|
||||
for(ArgumentParser parser : param.parsers()) {
|
||||
classes.add(parser.getTypeClass(param.required()));
|
||||
}
|
||||
}
|
||||
Method method;
|
||||
|
@ -67,27 +53,11 @@ public class CachedExecutable {
|
|||
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;
|
||||
}
|
||||
|
||||
public List<Parameter> getPositionals() {
|
||||
return this.positionals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
public String name() {
|
||||
return this.executable.getName();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
return this.executable.getName();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import common.collect.Lists;
|
||||
import common.collect.Maps;
|
||||
|
@ -55,10 +56,10 @@ public class CommandEnvironment {
|
|||
|
||||
public void registerExecutable(Executable executable) {
|
||||
CachedExecutable cached = CachedExecutable.cacheExecutable(executable);
|
||||
if(this.executables.containsKey(cached.getName()))
|
||||
throw new IllegalStateException("Befehl " + cached.getName() + " ist bereits registriert");
|
||||
this.executables.put(cached.getName(), cached);
|
||||
this.registerReplacer(cached.getName(), new Function<String, String>() {
|
||||
if(this.executables.containsKey(cached.name()))
|
||||
throw new IllegalStateException("Befehl " + cached.name() + " ist bereits registriert");
|
||||
this.executables.put(cached.name(), cached);
|
||||
this.registerReplacer(cached.name(), new Function<String, String>() {
|
||||
public String apply(String str) {
|
||||
Object o;
|
||||
try {
|
||||
|
@ -77,14 +78,14 @@ public class CommandEnvironment {
|
|||
public void registerReplacer(String var, Function<String, String> function) {
|
||||
if(this.builtins.contains(var))
|
||||
throw new IllegalStateException("Variable " + var + " ist bereits registriert");
|
||||
this.replacers.add(new PatternReplacer(var, true, function));
|
||||
this.replacers.add(new PatternReplacer(Pattern.compile("\\$\\((" + Pattern.quote(var) + "[^\\)]*)\\)"), function));
|
||||
this.builtins.add(var);
|
||||
}
|
||||
|
||||
public void registerVariable(String var, Variable variable) {
|
||||
if(this.builtins.contains(var))
|
||||
throw new IllegalStateException("Variable " + var + " ist bereits registriert");
|
||||
this.replacers.add(new PatternReplacer(var, false, new Function<String, String>() {
|
||||
this.replacers.add(new PatternReplacer(Pattern.compile("\\$\\((" + Pattern.quote(var) + ")\\)"), new Function<String, String>() {
|
||||
public String apply(String ign) {
|
||||
return variable.get();
|
||||
}
|
||||
|
@ -107,7 +108,7 @@ public class CommandEnvironment {
|
|||
private String substitute(String str) {
|
||||
StringBuffer sb = new StringBuffer(str);
|
||||
for(PatternReplacer replacer : this.replacers) {
|
||||
replacer.replaceVar(sb);
|
||||
replacer.replace(sb);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
@ -137,12 +138,12 @@ public class CommandEnvironment {
|
|||
}
|
||||
ArgumentSplitter args = ArgumentSplitter.parseArgs(this, cmd, argv, cached);
|
||||
List<Object> params = Lists.newArrayList(this, this.currentExecutor);
|
||||
for(Parameter param : cached.getExecutable().getParamList()) {
|
||||
ArgCombiner combiner = param.getCombiner();
|
||||
for(Parameter param : cached.executable().getParamList()) {
|
||||
ArgCombiner combiner = param.combiner();
|
||||
if(combiner != null) {
|
||||
Object[] data = (Object[])Array.newInstance(combiner.getInputClass(), param.getParsers().size());
|
||||
Object[] data = (Object[])Array.newInstance(combiner.getInputClass(), param.parsers().size());
|
||||
for(int z = 0; z < data.length; z++) {
|
||||
data[z] = args.getUnchecked(param.getName(), param.getParsers().get(z).getName());
|
||||
data[z] = args.get(param.name(), param.parsers().get(z).getName());
|
||||
if(data[z] == null) {
|
||||
data = null;
|
||||
break;
|
||||
|
@ -151,16 +152,16 @@ public class CommandEnvironment {
|
|||
params.add(data == null ? null : combiner.combine(data));
|
||||
continue;
|
||||
}
|
||||
if(param.getParsers().isEmpty()) {
|
||||
params.add(args.hasArg(param.getName()));
|
||||
if(param.parsers().isEmpty()) {
|
||||
params.add(args.has(param.name()));
|
||||
continue;
|
||||
}
|
||||
for(ArgumentParser parser : param.getParsers()) {
|
||||
params.add(args.getUnchecked(param.getName(), parser.getName()));
|
||||
for(ArgumentParser parser : param.parsers()) {
|
||||
params.add(args.get(param.name(), parser.getName()));
|
||||
}
|
||||
}
|
||||
try {
|
||||
o = cached.getMethod().invoke(cached.getExecutable(), params.toArray(new Object[params.size()]));
|
||||
o = cached.method().invoke(cached.executable(), params.toArray(new Object[params.size()]));
|
||||
}
|
||||
catch(InvocationTargetException e) {
|
||||
if(e.getCause() instanceof RuntimeException)
|
||||
|
|
|
@ -34,7 +34,7 @@ public class DimensionParser extends CompletingParser {
|
|||
if(!this.useSender)
|
||||
return null;
|
||||
Position pos = env.getExecutor().getExecPos();
|
||||
return pos == null ? null : UniverseRegistry.getDimension(pos.dim);
|
||||
return pos == null ? null : UniverseRegistry.getDimension(pos.dim());
|
||||
// if(dim == null)
|
||||
// throw new ScriptException("Unbekannte Dimension '%s'");
|
||||
}
|
||||
|
|
|
@ -69,15 +69,15 @@ public class DoubleParser extends DefaultingParser {
|
|||
if(this.defType != null)
|
||||
switch(this.defType) {
|
||||
case X:
|
||||
return pos == null ? null : pos.x;
|
||||
return pos == null ? null : pos.x();
|
||||
case Y:
|
||||
return pos == null ? null : pos.y;
|
||||
return pos == null ? null : pos.y();
|
||||
case Z:
|
||||
return pos == null ? null : pos.z;
|
||||
return pos == null ? null : pos.z();
|
||||
case YAW:
|
||||
return pos == null ? null : (double)pos.yaw;
|
||||
return pos == null ? null : (double)pos.yaw();
|
||||
case PITCH:
|
||||
return pos == null ? null : (double)pos.pitch;
|
||||
return pos == null ? null : (double)pos.pitch();
|
||||
}
|
||||
return (Double)super.getDefault(env);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
public interface Executable {
|
||||
// Object exec(ScriptEnvironment env, ScriptArgs args);
|
||||
Map<String, Parameter> getParameters();
|
||||
List<Parameter> getParamList();
|
||||
String getName();
|
||||
|
|
|
@ -5,44 +5,11 @@ import common.util.BlockPos;
|
|||
import common.util.Position;
|
||||
import server.Server;
|
||||
|
||||
public class FixedExecutor implements Executor {
|
||||
private final Server server;
|
||||
private final String id;
|
||||
|
||||
private String name;
|
||||
private Position position;
|
||||
|
||||
public FixedExecutor(Server server, String id, String name, Position pos) {
|
||||
this.server = server;
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.position = pos;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setPosition(Position pos) {
|
||||
this.position = pos;
|
||||
}
|
||||
|
||||
public record FixedExecutor(Server server, String getExecId, String getExecName, Position getExecPos) implements Executor {
|
||||
public void logConsole(String msg) {
|
||||
this.server.logConsole(msg);
|
||||
}
|
||||
|
||||
public String getExecId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getExecName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public Position getExecPos() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public Entity getPointedEntity() {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -68,11 +68,11 @@ public class IntParser extends DefaultingParser {
|
|||
if(this.defType != null)
|
||||
switch(this.defType) {
|
||||
case X:
|
||||
return pos == null ? null : ExtMath.floord(pos.x);
|
||||
return pos == null ? null : ExtMath.floord(pos.x());
|
||||
case Y:
|
||||
return pos == null ? null : ExtMath.floord(pos.y);
|
||||
return pos == null ? null : ExtMath.floord(pos.y());
|
||||
case Z:
|
||||
return pos == null ? null : ExtMath.floord(pos.z);
|
||||
return pos == null ? null : ExtMath.floord(pos.z());
|
||||
}
|
||||
return (Integer)super.getDefault(env);
|
||||
}
|
||||
|
|
|
@ -2,52 +2,12 @@ package server.command;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
public class Parameter {
|
||||
private final String name;
|
||||
private final char shortName;
|
||||
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, ArgCombiner<?> combiner) {
|
||||
this.name = name;
|
||||
this.shortName = shortName;
|
||||
this.position = position;
|
||||
this.required = required;
|
||||
this.parsers = parsers;
|
||||
this.combiner = combiner;
|
||||
}
|
||||
|
||||
public boolean isPositional() {
|
||||
public record Parameter(String name, char shortName, int position, boolean required, List<ArgumentParser> parsers, ArgCombiner<?> combiner) {
|
||||
public boolean positional() {
|
||||
return this.position >= 0;
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public List<ArgumentParser> getParsers() {
|
||||
return this.parsers;
|
||||
}
|
||||
|
||||
public ArgCombiner<?> getCombiner() {
|
||||
return this.combiner;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return this.required;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public boolean hasShortName() {
|
||||
public boolean shorthand() {
|
||||
return this.shortName != 0;
|
||||
}
|
||||
|
||||
public char getShortName() {
|
||||
return this.shortName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,20 +4,8 @@ import java.util.function.Function;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PatternReplacer {
|
||||
private final String variable;
|
||||
private final boolean matchAll;
|
||||
private final Pattern pattern;
|
||||
private final Function<String, String> function;
|
||||
|
||||
public PatternReplacer(String variable, boolean matchAll, Function<String, String> function) {
|
||||
this.variable = variable;
|
||||
this.matchAll = matchAll;
|
||||
this.pattern = Pattern.compile("\\$\\((" + Pattern.quote(variable) + (matchAll ? "[^\\)]*" : "") + ")\\)");
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public void replaceVar(StringBuffer sb) {
|
||||
public record PatternReplacer(Pattern pattern, Function<String, String> function) {
|
||||
public void replace(StringBuffer sb) {
|
||||
String str = sb.toString();
|
||||
sb.delete(0, sb.length());
|
||||
Matcher matcher = this.pattern.matcher(str);
|
||||
|
@ -30,20 +18,4 @@ public class PatternReplacer {
|
|||
}
|
||||
matcher.appendTail(sb);
|
||||
}
|
||||
|
||||
public String getVariable() {
|
||||
return this.variable;
|
||||
}
|
||||
|
||||
public boolean matchesAll() {
|
||||
return this.matchAll;
|
||||
}
|
||||
|
||||
public Pattern getPattern() {
|
||||
return this.pattern;
|
||||
}
|
||||
|
||||
public Function<String, String> getFunction() {
|
||||
return this.function;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,27 +29,27 @@ public class CommandHelp extends Command {
|
|||
return;
|
||||
}
|
||||
List<String> list = Lists.newArrayList();
|
||||
for(Entry<String, Parameter> entry : command.getParameters().entrySet()) {
|
||||
for(Entry<String, Parameter> entry : command.parameters().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>() {
|
||||
if(entry.getKey().length() == 1 && !param.positional() && param.shorthand()) {
|
||||
list.add("-" + param.shortName() + (param.parsers().isEmpty() ? " (" + param.name() + ")" : (param.parsers().size() == 1 &&
|
||||
param.parsers().get(0).getName().equals(param.name()) ? " <" + param.name() + ">" :
|
||||
" (" + param.name() + ")" + Util.buildLines(" ", new Function<ArgumentParser, String>() {
|
||||
public String apply(ArgumentParser parser) {
|
||||
return "<" + parser.getName() + ">";
|
||||
}
|
||||
}, param.getParsers()))));
|
||||
}, param.parsers()))));
|
||||
}
|
||||
}
|
||||
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>() {
|
||||
for(Parameter param : command.positionals()) {
|
||||
list.add((param.required() ? "<" : "[") + (param.parsers().size() == 1 &&
|
||||
param.parsers().get(0).getName().equals(param.name()) ? param.name() :
|
||||
"(" + param.name() + ") " + Util.buildLines(" ", new Function<ArgumentParser, String>() {
|
||||
public String apply(ArgumentParser parser) {
|
||||
return "<" + parser.getName() + ">";
|
||||
}
|
||||
}, param.getParsers())) + (param.isRequired() ? ">" : "]"));
|
||||
}, param.parsers())) + (param.required() ? ">" : "]"));
|
||||
}
|
||||
exec.logConsole("%s %s", command.getName(), Util.buildLines(" ", list));
|
||||
exec.logConsole("%s %s", command.name(), Util.buildLines(" ", list));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ public class CommandOfflinetp extends Command {
|
|||
throw new RunException("Spieler '%s' konnte nicht gefunden werden", user);
|
||||
for(Entity entity : entities) {
|
||||
entity.teleport(pos);
|
||||
exec.logConsole("%s nach %d, %d, %d in %s teleportiert", entity.getCommandName(), (int)pos.x, (int)pos.y, (int)pos.z, UniverseRegistry.getDimension(pos.dim).getFormattedName(false));
|
||||
exec.logConsole("%s nach %d, %d, %d in %s teleportiert", entity.getCommandName(), (int)pos.x(), (int)pos.y(), (int)pos.z(), UniverseRegistry.getDimension(pos.dim()).getFormattedName(false));
|
||||
}
|
||||
return entities.size();
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public class CommandTime extends Command {
|
|||
|
||||
public Object exec(CommandEnvironment env, Executor exec, String timeStr, boolean absolute) {
|
||||
Position pos = exec.getExecPos();
|
||||
WorldServer world = pos == null ? env.getServer().getSpace() : env.getServer().getWorld(pos.dim);
|
||||
WorldServer world = pos == null ? env.getServer().getSpace() : env.getServer().getWorld(pos.dim());
|
||||
long time = absolute ? 0L : world.getDayTime();
|
||||
long fwd = parseDayTime(world.dimension, timeStr);
|
||||
if(fwd >= 0L) {
|
||||
|
|
|
@ -22,7 +22,7 @@ public class CommandTp extends Command {
|
|||
Position pos = target.getPos();
|
||||
for(Entity entity : entities) {
|
||||
entity.teleport(pos);
|
||||
exec.logConsole("%s nach %d, %d, %d in %s teleportiert", entity.getCommandName(), (int)pos.x, (int)pos.y, (int)pos.z, UniverseRegistry.getDimension(pos.dim).getFormattedName(false));
|
||||
exec.logConsole("%s nach %d, %d, %d in %s teleportiert", entity.getCommandName(), (int)pos.x(), (int)pos.y(), (int)pos.z(), UniverseRegistry.getDimension(pos.dim()).getFormattedName(false));
|
||||
}
|
||||
return entities.size();
|
||||
}
|
||||
|
|
|
@ -42,12 +42,12 @@ public class CommandWarp extends Command {
|
|||
pos = env.getServer().getWarps().get(warp);
|
||||
if(pos == null)
|
||||
throw new RunException("Warp '%s' existiert nicht", warp);
|
||||
else if(env.getServer().getWorld(pos.dim) == null)
|
||||
throw new RunException("Warp '%s' hat kein Level (%s)", warp, pos.dim);
|
||||
else if(env.getServer().getWorld(pos.dim()) == null)
|
||||
throw new RunException("Warp '%s' hat kein Level (%s)", warp, pos.dim());
|
||||
// }
|
||||
for(Entity entity : entities) {
|
||||
entity.teleport(pos);
|
||||
exec.logConsole("%s nach %d, %d, %d in %s teleportiert", entity.getCommandName(), (int)pos.x, (int)pos.y, (int)pos.z, UniverseRegistry.getDimension(pos.dim).getFormattedName(false));
|
||||
exec.logConsole("%s nach %d, %d, %d in %s teleportiert", entity.getCommandName(), (int)pos.x(), (int)pos.y(), (int)pos.z(), UniverseRegistry.getDimension(pos.dim()).getFormattedName(false));
|
||||
}
|
||||
return entities.size();
|
||||
}
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
package server.world;
|
||||
|
||||
import common.block.Block;
|
||||
import common.util.BlockPos;
|
||||
|
||||
public class BlockEventData
|
||||
{
|
||||
private BlockPos position;
|
||||
private Block blockType;
|
||||
|
||||
/** Different for each blockID */
|
||||
private int eventID;
|
||||
private int eventParameter;
|
||||
|
||||
public BlockEventData(BlockPos pos, Block blockType, int eventId, int p_i45756_4_)
|
||||
{
|
||||
this.position = pos;
|
||||
this.eventID = eventId;
|
||||
this.eventParameter = p_i45756_4_;
|
||||
this.blockType = blockType;
|
||||
}
|
||||
|
||||
public BlockPos getPosition()
|
||||
{
|
||||
return this.position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Event ID (different for each BlockID)
|
||||
*/
|
||||
public int getEventID()
|
||||
{
|
||||
return this.eventID;
|
||||
}
|
||||
|
||||
public int getEventParameter()
|
||||
{
|
||||
return this.eventParameter;
|
||||
}
|
||||
|
||||
public Block getBlock()
|
||||
{
|
||||
return this.blockType;
|
||||
}
|
||||
|
||||
public boolean equals(Object p_equals_1_)
|
||||
{
|
||||
if (!(p_equals_1_ instanceof BlockEventData))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
BlockEventData blockeventdata = (BlockEventData)p_equals_1_;
|
||||
return this.position.equals(blockeventdata.position) && this.eventID == blockeventdata.eventID && this.eventParameter == blockeventdata.eventParameter && this.blockType == blockeventdata.blockType;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "TE(" + this.position + ")," + this.eventID + "," + this.eventParameter + "," + this.blockType;
|
||||
}
|
||||
}
|
13
server/src/main/java/server/world/TickEvent.java
Executable file
13
server/src/main/java/server/world/TickEvent.java
Executable file
|
@ -0,0 +1,13 @@
|
|||
package server.world;
|
||||
|
||||
import common.block.Block;
|
||||
import common.util.BlockPos;
|
||||
|
||||
public record TickEvent(BlockPos position, Block block, int id, int parameter) {
|
||||
public boolean equals(Object other) {
|
||||
if(!(other instanceof TickEvent))
|
||||
return false;
|
||||
TickEvent event = (TickEvent)other;
|
||||
return this.position.equals(event.position) && this.id == event.id && this.parameter == event.parameter && this.block == event.block;
|
||||
}
|
||||
}
|
|
@ -257,7 +257,7 @@ public final class WorldServer extends AWorldServer {
|
|||
FeatureOres[] gens = new FeatureOres[this.dimension.getOres().size()];
|
||||
for(int z = 0; z < gens.length; z++) {
|
||||
Ore gen = this.dimension.getOres().get(z);
|
||||
gens[z] = new FeatureOres(gen.state, gen.count, gen.more, gen.size, gen.min, gen.max, gen.dist);
|
||||
gens[z] = new FeatureOres(gen.state(), gen.count(), gen.more(), gen.size(), gen.min(), gen.max(), gen.dist());
|
||||
}
|
||||
return gens;
|
||||
}
|
||||
|
@ -268,7 +268,7 @@ public final class WorldServer extends AWorldServer {
|
|||
FeatureLakes[] gens = new FeatureLakes[this.dimension.getLakes().size()];
|
||||
for(int z = 0; z < gens.length; z++) {
|
||||
Lake gen = this.dimension.getLakes().get(z);
|
||||
gens[z] = new FeatureLakes(gen.state, gen.filler, gen.top, gen.chance, gen.minHeight, gen.maxHeight, gen.ratiod);
|
||||
gens[z] = new FeatureLakes(gen.state(), gen.filler(), gen.top(), gen.chance(), gen.minHeight(), gen.maxHeight(), gen.ratiod());
|
||||
}
|
||||
return gens;
|
||||
}
|
||||
|
@ -279,7 +279,7 @@ public final class WorldServer extends AWorldServer {
|
|||
FeatureLiquids[] gens = new FeatureLiquids[this.dimension.getLiquids().size()];
|
||||
for(int z = 0; z < gens.length; z++) {
|
||||
Liquid gen = this.dimension.getLiquids().get(z);
|
||||
gens[z] = new FeatureLiquids(gen.state, gen.chance, gen.minHeight, gen.maxHeight, gen.lower);
|
||||
gens[z] = new FeatureLiquids(gen.state(), gen.chance(), gen.minHeight(), gen.maxHeight(), gen.lower());
|
||||
}
|
||||
return gens;
|
||||
}
|
||||
|
@ -949,18 +949,18 @@ public final class WorldServer extends AWorldServer {
|
|||
public static void saveWarps(Map<String, Position> warps) {
|
||||
Map<Integer, List<TagObject>> map = Maps.newHashMap();
|
||||
for(Entry<String, Position> pos : warps.entrySet()) {
|
||||
Dimension dim = UniverseRegistry.getDimension(pos.getValue().dim);
|
||||
Dimension dim = UniverseRegistry.getDimension(pos.getValue().dim());
|
||||
if(dim != null) {
|
||||
List<TagObject> list = map.get(pos.getValue().dim);
|
||||
List<TagObject> list = map.get(pos.getValue().dim());
|
||||
if(list == null)
|
||||
map.put(pos.getValue().dim, list = Lists.newArrayList());
|
||||
map.put(pos.getValue().dim(), list = Lists.newArrayList());
|
||||
TagObject warp = new TagObject();
|
||||
warp.setString("Name", pos.getKey());
|
||||
warp.setDouble("X", pos.getValue().x);
|
||||
warp.setDouble("Y", pos.getValue().y);
|
||||
warp.setDouble("Z", pos.getValue().z);
|
||||
warp.setFloat("Yaw", pos.getValue().yaw);
|
||||
warp.setFloat("Pitch", pos.getValue().pitch);
|
||||
warp.setDouble("X", pos.getValue().x());
|
||||
warp.setDouble("Y", pos.getValue().y());
|
||||
warp.setDouble("Z", pos.getValue().z());
|
||||
warp.setFloat("Yaw", pos.getValue().yaw());
|
||||
warp.setFloat("Pitch", pos.getValue().pitch());
|
||||
list.add(warp);
|
||||
}
|
||||
}
|
||||
|
@ -1158,9 +1158,9 @@ public final class WorldServer extends AWorldServer {
|
|||
}
|
||||
|
||||
public void addBlockEvent(BlockPos pos, Block blockIn, int eventID, int eventParam) {
|
||||
BlockEventData blockeventdata = new BlockEventData(pos, blockIn, eventID, eventParam);
|
||||
TickEvent blockeventdata = new TickEvent(pos, blockIn, eventID, eventParam);
|
||||
|
||||
for(BlockEventData blockeventdata1 : this.queue[this.blockEvtIdx]) {
|
||||
for(TickEvent blockeventdata1 : this.queue[this.blockEvtIdx]) {
|
||||
if(blockeventdata1.equals(blockeventdata)) {
|
||||
return;
|
||||
}
|
||||
|
@ -1174,12 +1174,12 @@ public final class WorldServer extends AWorldServer {
|
|||
int i = this.blockEvtIdx;
|
||||
this.blockEvtIdx ^= 1;
|
||||
|
||||
for(BlockEventData blockeventdata : this.queue[i]) {
|
||||
for(TickEvent blockeventdata : this.queue[i]) {
|
||||
if(this.fireBlockEvent(blockeventdata)) {
|
||||
this.server.sendNear((double)blockeventdata.getPosition().getX(), (double)blockeventdata.getPosition().getY(),
|
||||
(double)blockeventdata.getPosition().getZ(), 64.0D, this.dimension.getDimensionId(),
|
||||
new SPacketBlockAction(blockeventdata.getPosition(), blockeventdata.getBlock(), blockeventdata.getEventID(),
|
||||
blockeventdata.getEventParameter()));
|
||||
this.server.sendNear((double)blockeventdata.position().getX(), (double)blockeventdata.position().getY(),
|
||||
(double)blockeventdata.position().getZ(), 64.0D, this.dimension.getDimensionId(),
|
||||
new SPacketBlockAction(blockeventdata.position(), blockeventdata.block(), blockeventdata.id(),
|
||||
blockeventdata.parameter()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1187,10 +1187,10 @@ public final class WorldServer extends AWorldServer {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean fireBlockEvent(BlockEventData event) {
|
||||
State iblockstate = this.getState(event.getPosition());
|
||||
return iblockstate.getBlock() == event.getBlock()
|
||||
? iblockstate.getBlock().onBlockEventReceived(this, event.getPosition(), iblockstate, event.getEventID(), event.getEventParameter())
|
||||
private boolean fireBlockEvent(TickEvent event) {
|
||||
State iblockstate = this.getState(event.position());
|
||||
return iblockstate.getBlock() == event.block()
|
||||
? iblockstate.getBlock().onBlockEventReceived(this, event.position(), iblockstate, event.id(), event.parameter())
|
||||
: false;
|
||||
}
|
||||
|
||||
|
@ -1661,7 +1661,7 @@ public final class WorldServer extends AWorldServer {
|
|||
this.loaderList.clear();
|
||||
for(Iterator<Entry<String, Position>> iter = this.server.getWarps().entrySet().iterator(); iter.hasNext();) {
|
||||
Entry<String, Position> pos = iter.next();
|
||||
if(pos.getValue().dim == this.dimension.getDimensionId())
|
||||
if(pos.getValue().dim() == this.dimension.getDimensionId())
|
||||
iter.remove();
|
||||
}
|
||||
Region.finishWrite();
|
||||
|
@ -2742,7 +2742,7 @@ public final class WorldServer extends AWorldServer {
|
|||
this.getChunk(pos).setModified(true);
|
||||
}
|
||||
|
||||
private static class EventList extends ArrayList<BlockEventData> {
|
||||
private static class EventList extends ArrayList<TickEvent> {
|
||||
private EventList() {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue