34 lines
1.1 KiB
Java
34 lines
1.1 KiB
Java
![]() |
package game.command;
|
||
|
|
||
|
public class DoubleParser extends DefaultingParser {
|
||
|
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.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;
|
||
|
}
|
||
|
}
|