tcr/java/src/game/command/StringParser.java

46 lines
1.8 KiB
Java
Raw Normal View History

2025-03-11 00:23:54 +01:00
package game.command;
2025-03-19 21:54:09 +01:00
import game.util.CharValidator;
2025-03-11 00:23:54 +01:00
public class StringParser extends DefaultingParser {
private final boolean allowEmpty;
private final Integer minLength;
private final Integer maxLength;
2025-03-19 21:54:09 +01:00
private final CharValidator validator;
2025-03-11 00:23:54 +01:00
2025-03-19 21:54:09 +01:00
public StringParser(String name, String def, boolean allowEmpty, Integer minLength, Integer maxLength, CharValidator validator,
2025-03-11 00:23:54 +01:00
Object ... completions) {
super(name, def, completions);
this.allowEmpty = allowEmpty;
this.minLength = minLength;
this.maxLength = maxLength;
this.validator = validator;
}
public String parse(ScriptEnvironment env, String input) {
if(!this.allowEmpty && input.isEmpty())
throw new ScriptException("Die Zeichenkette darf nicht leer sein");
if(this.minLength != null && input.length() < this.minLength)
if(this.maxLength != null)
throw new ScriptException("Die Zeichenkette muss zwischen %d .. %d Zeichen lang sein, habe %d ('%s')",
this.minLength, this.maxLength, input.length(), input);
else
throw new ScriptException("Die Zeichenkette muss mindestens %d Zeichen lang sein, habe %d ('%s')",
this.minLength, input.length(), input);
if(this.maxLength != null && input.length() > this.maxLength)
if(this.minLength != null)
throw new ScriptException("Die Zeichenkette muss zwischen %d .. %d Zeichen lang sein, habe %d ('%s')",
this.minLength, this.maxLength, input.length(), input);
else
throw new ScriptException("Die Zeichenkette darf höchstens %d Zeichen lang sein, habe %d ('%s')",
this.maxLength, input.length(), input);
2025-03-19 21:54:09 +01:00
if(this.validator != null && !this.validator.valid(input))
throw new ScriptException("Die Zeichenkette '%s' enthält ungültige Zeichen", input);
2025-03-11 00:23:54 +01:00
return input;
}
2025-03-19 21:54:09 +01:00
public Class<?> getTypeClass() {
return String.class;
}
2025-03-11 00:23:54 +01:00
}