commands, ...

This commit is contained in:
Sen 2025-03-25 23:10:40 +01:00
parent 66421e806e
commit 75f91dbf4c
44 changed files with 1035 additions and 631 deletions

View file

@ -1,5 +1,6 @@
package game.command;
import game.world.BlockPos;
import game.world.Position;
public class DoubleParser extends DefaultingParser {
@ -10,43 +11,50 @@ public class DoubleParser extends DefaultingParser {
private final DefType defType;
private final Double min;
private final Double max;
private final boolean center;
public DoubleParser(String name, Double def, Double min, Double max, Object ... completions) {
public DoubleParser(String name, Double def, Double min, Double max, boolean center, Object ... completions) {
super(name, def, completions);
this.defType = null;
this.min = min;
this.max = max;
this.center = center;
}
public DoubleParser(String name, DefType type, Double min, Double max, Object ... completions) {
super(name, null, completions);
public DoubleParser(String name, DefType type, Double min, Double max, boolean center) {
super(name, null);
this.defType = type;
this.min = min;
this.max = max;
this.center = center;
}
public Double parse(ScriptEnvironment env, String input) {
public Double parse(CommandEnvironment env, String input) {
Double pre = this.defType != null && input.startsWith("~") ? this.getDefault(env) : null;
input = pre != null ? input.substring(1) : input;
double value;
try {
value = Double.parseDouble(input);
}
catch(NumberFormatException e) {
throw new ScriptException("Ungültige Gleitkommazahl '%s'", input);
throw new RunException("Ungültige Gleitkommazahl '%s'", input);
}
if(this.center && pre == null && input.indexOf('.') < 0)
value += 0.5;
if(this.min != null && value < this.min)
if(this.max != null)
throw new ScriptException("Die Zahl muss im Bereich %f .. %f liegen, habe %f", this.min, this.max, value);
throw new RunException("Die Zahl muss im Bereich %f .. %f liegen, habe %f", this.min, this.max, value);
else
throw new ScriptException("Die Zahl muss mindestens %f betragen, habe %f", this.min, value);
throw new RunException("Die Zahl muss mindestens %f betragen, habe %f", this.min, value);
if(this.max != null && value > this.max)
if(this.min != null)
throw new ScriptException("Die Zahl muss im Bereich %f .. %f liegen, habe %f", this.min, this.max, value);
throw new RunException("Die Zahl muss im Bereich %f .. %f liegen, habe %f", this.min, this.max, value);
else
throw new ScriptException("Die Zahl darf höchstens %f betragen, habe %f", this.max, value);
throw new RunException("Die Zahl darf höchstens %f betragen, habe %f", this.max, value);
return value;
}
public Double getDefault(ScriptEnvironment env) {
public Double getDefault(CommandEnvironment env) {
Position pos = this.defType == null ? null : env.getExecutor().getExecPos();
switch(this.defType) {
case X:
@ -66,4 +74,17 @@ public class DoubleParser extends DefaultingParser {
public Class<?> getTypeClass() {
return this.hasDefault() ? double.class : Double.class;
}
public String[] getCompletions(CommandEnvironment env) {
BlockPos pos = this.defType == null ? null : env.getExecutor().getPointedPosition();
switch(this.defType) {
case X:
return pos == null ? null : new String[] {"" + pos.getX()};
case Y:
return pos == null ? null : new String[] {"" + pos.getY()};
case Z:
return pos == null ? null : new String[] {"" + pos.getZ()};
}
return super.getCompletions(env);
}
}