2025-03-11 00:23:54 +01:00
|
|
|
package game.command;
|
|
|
|
|
2025-03-18 01:29:36 +01:00
|
|
|
import game.world.Position;
|
|
|
|
|
2025-03-11 00:23:54 +01:00
|
|
|
public class DoubleParser extends DefaultingParser {
|
2025-03-18 01:29:36 +01:00
|
|
|
public static enum DefType {
|
|
|
|
X, Y, Z, YAW, PITCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final DefType defType;
|
2025-03-11 00:23:54 +01:00
|
|
|
private final Double min;
|
|
|
|
private final Double max;
|
|
|
|
|
|
|
|
public DoubleParser(String name, Double def, Double min, Double max, Object ... completions) {
|
|
|
|
super(name, def, completions);
|
2025-03-18 01:29:36 +01:00
|
|
|
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;
|
2025-03-11 00:23:54 +01:00
|
|
|
this.min = min;
|
|
|
|
this.max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Double parse(ScriptEnvironment env, String input) {
|
|
|
|
double value;
|
|
|
|
try {
|
|
|
|
value = Double.parseDouble(input);
|
|
|
|
}
|
|
|
|
catch(NumberFormatException e) {
|
|
|
|
throw new ScriptException("Ungültige Gleitkommazahl '%s'", input);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
else
|
|
|
|
throw new ScriptException("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);
|
|
|
|
else
|
|
|
|
throw new ScriptException("Die Zahl darf höchstens %f betragen, habe %f", this.max, value);
|
|
|
|
return value;
|
|
|
|
}
|
2025-03-18 01:29:36 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2025-03-11 00:23:54 +01:00
|
|
|
}
|