45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
![]() |
package game.command;
|
||
|
|
||
|
import java.util.Map;
|
||
|
|
||
|
import game.collect.Maps;
|
||
|
|
||
|
public class EnumParser extends DefaultingParser {
|
||
|
private final Map<String, Object> lookup = Maps.newHashMap();
|
||
|
private final Object[] selections;
|
||
|
|
||
|
private static <T> String joinArgs(T[] iter) {
|
||
|
StringBuilder sb = new StringBuilder("'");
|
||
|
for(T obj : iter) {
|
||
|
if(sb.length() > 1)
|
||
|
sb.append("', '");
|
||
|
sb.append(String.valueOf(obj));
|
||
|
}
|
||
|
return sb.append("'").toString();
|
||
|
}
|
||
|
|
||
|
public EnumParser(String name, Object def, Object ... selections) {
|
||
|
super(name, def, selections);
|
||
|
this.selections = selections;
|
||
|
for(Object o : selections) {
|
||
|
this.lookup.put(o.toString().toLowerCase(), o);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Object parse(ScriptEnvironment env, String input) {
|
||
|
Object value = this.lookup.get(input.toLowerCase());
|
||
|
if(value != null)
|
||
|
return value;
|
||
|
int id = -1;
|
||
|
try {
|
||
|
id = Integer.parseInt(input);
|
||
|
}
|
||
|
catch(NumberFormatException e) {
|
||
|
}
|
||
|
if(id < 0 || id >= this.selections.length)
|
||
|
throw new ScriptException("Ungültige Auswahl '%s', muss eine von diesen Optionen sein: %s", input,
|
||
|
joinArgs(this.selections));
|
||
|
return this.selections[id];
|
||
|
}
|
||
|
}
|