33 lines
1.1 KiB
Java
33 lines
1.1 KiB
Java
package game.command;
|
|
|
|
public class LongParser extends DefaultingParser {
|
|
private final Long min;
|
|
private final Long max;
|
|
|
|
public LongParser(String name, Long def, Long min, Long max, Object ... completions) {
|
|
super(name, def, completions);
|
|
this.min = min;
|
|
this.max = max;
|
|
}
|
|
|
|
public Long parse(ScriptEnvironment env, String input) {
|
|
long value;
|
|
try {
|
|
value = Long.parseLong(input);
|
|
}
|
|
catch(NumberFormatException e) {
|
|
throw new ScriptException("Ungültige Ganzzahl '%s'", input);
|
|
}
|
|
if(this.min != null && value < this.min)
|
|
if(this.max != null)
|
|
throw new ScriptException("Die Zahl muss im Bereich %d .. %d liegen, habe %d", this.min, this.max, value);
|
|
else
|
|
throw new ScriptException("Die Zahl muss mindestens %d betragen, habe %d", this.min, value);
|
|
if(this.max != null && value > this.max)
|
|
if(this.min != null)
|
|
throw new ScriptException("Die Zahl muss im Bereich %d .. %d liegen, habe %d", this.min, this.max, value);
|
|
else
|
|
throw new ScriptException("Die Zahl darf höchstens %d betragen, habe %d", this.max, value);
|
|
return value;
|
|
}
|
|
}
|