Search in sources :

Example 1 with Argument

use of org.spongepowered.api.text.selector.Argument in project SpongeCommon by SpongePowered.

the class SpongeSelectorFactory method parseRawSelector.

@Override
public Selector parseRawSelector(String selector) {
    checkArgument(selector.startsWith("@"), "Invalid selector %s", selector);
    // If multi-character types are possible, this handles it
    int argListIndex = selector.indexOf('[');
    if (argListIndex < 0) {
        argListIndex = selector.length();
    } else {
        int end = selector.indexOf(']');
        checkArgument(end > argListIndex && selector.charAt(end - 1) != ',', "Invalid selector %s", selector);
    }
    String typeStr = selector.substring(1, argListIndex);
    checkArgument(idToType.containsKey(typeStr), "No type known as '%s'", typeStr);
    SelectorType type = idToType.get(typeStr);
    try {
        Map<String, String> rawMap;
        if (argListIndex == selector.length()) {
            rawMap = ImmutableMap.of();
        } else {
            rawMap = EntitySelector.getArgumentMap(selector.substring(argListIndex + 1, selector.length() - 1));
        }
        Map<ArgumentType<?>, Argument<?>> arguments = parseArguments(rawMap);
        return new SpongeSelector(type, ImmutableMap.copyOf(arguments));
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid selector " + selector, e);
    }
}
Also used : Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Argument(org.spongepowered.api.text.selector.Argument) SelectorType(org.spongepowered.api.text.selector.SelectorType) ArgumentType(org.spongepowered.api.text.selector.ArgumentType) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Argument (org.spongepowered.api.text.selector.Argument)1 ArgumentType (org.spongepowered.api.text.selector.ArgumentType)1 SelectorType (org.spongepowered.api.text.selector.SelectorType)1