tcr/java/src/game/command/LongParser.java
2025-03-25 23:10:40 +01:00

37 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(CommandEnvironment env, String input) {
long value;
try {
value = Long.parseLong(input);
}
catch(NumberFormatException e) {
throw new RunException("Ungültige Ganzzahl '%s'", input);
}
if(this.min != null && value < this.min)
if(this.max != null)
throw new RunException("Die Zahl muss im Bereich %d .. %d liegen, habe %d", this.min, this.max, value);
else
throw new RunException("Die Zahl muss mindestens %d betragen, habe %d", this.min, value);
if(this.max != null && value > this.max)
if(this.min != null)
throw new RunException("Die Zahl muss im Bereich %d .. %d liegen, habe %d", this.min, this.max, value);
else
throw new RunException("Die Zahl darf höchstens %d betragen, habe %d", this.max, value);
return value;
}
public Class<?> getTypeClass() {
return this.hasDefault() ? long.class : Long.class;
}
}