tcr/java/src/game/command/DoubleParser.java

91 lines
2.7 KiB
Java
Raw Normal View History

2025-03-11 00:23:54 +01:00
package game.command;
2025-03-25 23:10:40 +01:00
import game.world.BlockPos;
2025-03-18 01:29:36 +01:00
import game.world.Position;
2025-03-11 00:23:54 +01:00
public class DoubleParser extends DefaultingParser {
2025-03-18 01:29:36 +01:00
public static enum DefType {
X, Y, Z, YAW, PITCH;
}
private final DefType defType;
2025-03-11 00:23:54 +01:00
private final Double min;
private final Double max;
2025-03-25 23:10:40 +01:00
private final boolean center;
2025-03-11 00:23:54 +01:00
2025-03-25 23:10:40 +01:00
public DoubleParser(String name, Double def, Double min, Double max, boolean center, Object ... completions) {
2025-03-11 00:23:54 +01:00
super(name, def, completions);
2025-03-18 01:29:36 +01:00
this.defType = null;
this.min = min;
this.max = max;
2025-03-25 23:10:40 +01:00
this.center = center;
2025-03-18 01:29:36 +01:00
}
2025-03-25 23:10:40 +01:00
public DoubleParser(String name, DefType type, Double min, Double max, boolean center) {
super(name, null);
2025-03-18 01:29:36 +01:00
this.defType = type;
2025-03-11 00:23:54 +01:00
this.min = min;
this.max = max;
2025-03-25 23:10:40 +01:00
this.center = center;
2025-03-11 00:23:54 +01:00
}
2025-03-25 23:10:40 +01:00
public Double parse(CommandEnvironment env, String input) {
Double pre = this.defType != null && input.startsWith("~") ? this.getDefault(env) : null;
input = pre != null ? input.substring(1) : input;
2025-03-11 00:23:54 +01:00
double value;
try {
value = Double.parseDouble(input);
}
catch(NumberFormatException e) {
2025-03-25 23:10:40 +01:00
throw new RunException("Ungültige Gleitkommazahl '%s'", input);
2025-03-11 00:23:54 +01:00
}
2025-03-25 23:10:40 +01:00
if(this.center && pre == null && input.indexOf('.') < 0)
value += 0.5;
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 %f .. %f liegen, habe %f", 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 %f betragen, habe %f", 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 %f .. %f liegen, habe %f", 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 %f betragen, habe %f", this.max, value);
2025-03-11 00:23:54 +01:00
return value;
}
2025-03-18 01:29:36 +01:00
2025-03-25 23:10:40 +01:00
public Double getDefault(CommandEnvironment env) {
2025-03-18 01:29:36 +01:00
Position pos = this.defType == null ? null : env.getExecutor().getExecPos();
switch(this.defType) {
case X:
return pos == null ? null : pos.x;
case Y:
return pos == null ? null : pos.y;
case Z:
return pos == null ? null : pos.z;
case YAW:
return pos == null ? null : (double)pos.yaw;
case PITCH:
return pos == null ? null : (double)pos.pitch;
}
return (Double)super.getDefault(env);
}
2025-03-19 21:54:09 +01:00
public Class<?> getTypeClass() {
return this.hasDefault() ? double.class : Double.class;
}
2025-03-25 23:10:40 +01:00
public String[] getCompletions(CommandEnvironment env) {
BlockPos pos = this.defType == null ? null : env.getExecutor().getPointedPosition();
switch(this.defType) {
case X:
return pos == null ? null : new String[] {"" + pos.getX()};
case Y:
return pos == null ? null : new String[] {"" + pos.getY()};
case Z:
return pos == null ? null : new String[] {"" + pos.getZ()};
}
return super.getCompletions(env);
}
2025-03-11 00:23:54 +01:00
}