90 lines
2.7 KiB
Java
90 lines
2.7 KiB
Java
package game.command;
|
|
|
|
import game.world.BlockPos;
|
|
import game.world.Position;
|
|
|
|
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 max;
|
|
private final boolean center;
|
|
|
|
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, boolean center) {
|
|
super(name, null);
|
|
this.defType = type;
|
|
this.min = min;
|
|
this.max = max;
|
|
this.center = center;
|
|
}
|
|
|
|
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 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 RunException("Die Zahl muss im Bereich %f .. %f liegen, habe %f", this.min, this.max, value);
|
|
else
|
|
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 RunException("Die Zahl muss im Bereich %f .. %f liegen, habe %f", this.min, this.max, value);
|
|
else
|
|
throw new RunException("Die Zahl darf höchstens %f betragen, habe %f", this.max, value);
|
|
return value;
|
|
}
|
|
|
|
public Double getDefault(CommandEnvironment 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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|