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

40 lines
1.6 KiB
Java
Raw Normal View History

2025-03-11 00:23:54 +01:00
package game.command;
import java.util.function.Predicate;
public class StringParser extends DefaultingParser {
private final boolean allowEmpty;
private final Integer minLength;
private final Integer maxLength;
private final Predicate<Character> validator;
public StringParser(String name, String def, boolean allowEmpty, Integer minLength, Integer maxLength, Predicate<Character> validator,
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);
return input;
}
}