initial commit
This commit is contained in:
parent
3c9ee26b06
commit
22186c33b9
1458 changed files with 282792 additions and 0 deletions
44
java/src/game/command/EnumParser.java
Normal file
44
java/src/game/command/EnumParser.java
Normal file
|
@ -0,0 +1,44 @@
|
|||
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];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue