tcr/java/src/game/command/LongParser.java

38 lines
1.2 KiB
Java
Raw Normal View History

2025-03-11 00:23:54 +01:00
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;
}
2025-03-19 21:54:09 +01:00
public Class<?> getTypeClass() {
return this.hasDefault() ? long.class : Long.class;
}
2025-03-11 00:23:54 +01:00
}