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;
|
|
|
|
}
|
|
|
|
|
2025-03-25 23:10:40 +01:00
|
|
|
public Long parse(CommandEnvironment env, String input) {
|
2025-03-11 00:23:54 +01:00
|
|
|
long value;
|
|
|
|
try {
|
|
|
|
value = Long.parseLong(input);
|
|
|
|
}
|
|
|
|
catch(NumberFormatException e) {
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Ungültige Ganzzahl '%s'", input);
|
2025-03-11 00:23:54 +01:00
|
|
|
}
|
|
|
|
if(this.min != null && value < this.min)
|
|
|
|
if(this.max != null)
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Die Zahl muss im Bereich %d .. %d liegen, habe %d", this.min, this.max, value);
|
2025-03-11 00:23:54 +01:00
|
|
|
else
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Die Zahl muss mindestens %d betragen, habe %d", this.min, value);
|
2025-03-11 00:23:54 +01:00
|
|
|
if(this.max != null && value > this.max)
|
|
|
|
if(this.min != null)
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Die Zahl muss im Bereich %d .. %d liegen, habe %d", this.min, this.max, value);
|
2025-03-11 00:23:54 +01:00
|
|
|
else
|
2025-03-25 23:10:40 +01:00
|
|
|
throw new RunException("Die Zahl darf höchstens %d betragen, habe %d", this.max, value);
|
2025-03-11 00:23:54 +01:00
|
|
|
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
|
|
|
}
|