package game.command; 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; public DoubleParser(String name, Double def, Double min, Double max, Object ... completions) { super(name, def, completions); 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; 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; } 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); } }