Search in sources :

Example 1 with SelectorType

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

the class SelectorResolver method makeFilter.

private Predicate<Entity> makeFilter() {
    // for easier reading
    final Selector sel = this.selector;
    Vector3d position = getPositionOrDefault(this.position, ArgumentTypes.POSITION);
    List<Predicate<Entity>> filters = Lists.newArrayList();
    addTypeFilters(filters);
    addDimensionFilters(position, filters);
    addRadiusFilters(position, filters);
    addLevelFilters(filters);
    addGamemodeFilters(filters);
    addNameFilters(filters);
    addRotationFilters(filters);
    addTeamFilters(filters);
    addScoreFilters(filters);
    SelectorType selectorType = sel.getType();
    Optional<Invertible<EntityType>> type = sel.getArgument(ArgumentTypes.ENTITY_TYPE);
    // isn't an ALL_ENTITIES selector or it is a RANDOM selector for only players
    boolean isPlayerOnlySelector = selectorType == SelectorTypes.ALL_PLAYERS || selectorType == SelectorTypes.NEAREST_PLAYER || (selectorType == SelectorTypes.RANDOM && type.isPresent() && !type.get().isInverted() && type.get().getValue() != EntityTypes.PLAYER);
    if (isPlayerOnlySelector) {
        // insert at the start so it applies first
        filters.add(0, requireTypePredicate(Entity.class, Player.class));
    }
    return Functional.predicateAnd(filters);
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) Vector3d(com.flowpowered.math.vector.Vector3d) SelectorType(org.spongepowered.api.text.selector.SelectorType) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector) Predicate(java.util.function.Predicate)

Example 2 with SelectorType

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

the class SelectorResolver method resolve.

public Set<Entity> resolve() {
    SelectorType selectorType = this.selector.getType();
    int defaultCount = 1;
    if (INFINITE_TYPES.contains(selectorType)) {
        defaultCount = 0;
    }
    int maxToSelect = this.selector.get(ArgumentTypes.COUNT).orElse(defaultCount);
    Set<? extends Extent> extents = getExtentSet();
    int count = 0;
    ImmutableSet.Builder<Entity> entities = ImmutableSet.builder();
    for (Extent extent : extents) {
        Collection<Entity> allEntities = extent.getEntities();
        if (selectorType == SelectorTypes.RANDOM) {
            List<Entity> entityList = new ArrayList<>(allEntities);
            Collections.shuffle(entityList);
            allEntities = entityList;
        }
        for (Entity e : allEntities) {
            if (!this.selectorFilter.test(e)) {
                continue;
            }
            entities.add(e);
            count++;
            if (maxToSelect != 0 && count > maxToSelect) {
                break;
            }
        }
    }
    return entities.build();
}
Also used : Entity(org.spongepowered.api.entity.Entity) ImmutableSet(com.google.common.collect.ImmutableSet) Extent(org.spongepowered.api.world.extent.Extent) SelectorType(org.spongepowered.api.text.selector.SelectorType) ArrayList(java.util.ArrayList)

Example 3 with SelectorType

use of org.spongepowered.api.text.selector.SelectorType in project LanternServer by LanternPowered.

the class SelectorResolver method resolve.

public Set<Entity> resolve() {
    SelectorType selectorType = this.selector.getType();
    int defaultCount = 1;
    if (INFINITE_TYPES.contains(selectorType)) {
        defaultCount = 0;
    }
    int maxToSelect = this.selector.get(ArgumentTypes.COUNT).orElse(defaultCount);
    Set<? extends Extent> extents = getExtentSet();
    int count = 0;
    ImmutableSet.Builder<Entity> entities = ImmutableSet.builder();
    for (Extent extent : extents) {
        Collection<Entity> allEntities = extent.getEntities();
        if (selectorType == SelectorTypes.RANDOM) {
            List<Entity> entityList = new ArrayList<Entity>(allEntities);
            Collections.shuffle(entityList);
            allEntities = entityList;
        }
        for (Entity e : allEntities) {
            if (!this.selectorFilter.test(e)) {
                continue;
            }
            entities.add(e);
            count++;
            if (maxToSelect != 0 && count > maxToSelect) {
                break;
            }
        }
    }
    return entities.build();
}
Also used : Entity(org.spongepowered.api.entity.Entity) ImmutableSet(com.google.common.collect.ImmutableSet) Extent(org.spongepowered.api.world.extent.Extent) SelectorType(org.spongepowered.api.text.selector.SelectorType) ArrayList(java.util.ArrayList)

Example 4 with SelectorType

use of org.spongepowered.api.text.selector.SelectorType 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)

Example 5 with SelectorType

use of org.spongepowered.api.text.selector.SelectorType in project LanternServer by LanternPowered.

the class SelectorResolver method makeFilter.

private Predicate<Entity> makeFilter() {
    // for easier reading
    final Selector sel = this.selector;
    final Vector3d position = getPositionOrDefault(this.position, ArgumentTypes.POSITION);
    final List<Predicate<Entity>> filters = new ArrayList<>();
    addTypeFilters(filters);
    addDimensionFilters(position, filters);
    addRadiusFilters(position, filters);
    addLevelFilters(filters);
    addGamemodeFilters(filters);
    addNameFilters(filters);
    addRotationFilters(filters);
    addTeamFilters(filters);
    addScoreFilters(filters);
    SelectorType selectorType = sel.getType();
    final Optional<Argument.Invertible<EntityType>> type = sel.getArgument(ArgumentTypes.ENTITY_TYPE);
    // isn't an ALL_ENTITIES selector or it is a RANDOM selector for only players
    final boolean isPlayerOnlySelector = selectorType == SelectorTypes.ALL_PLAYERS || selectorType == SelectorTypes.NEAREST_PLAYER || (selectorType == SelectorTypes.RANDOM && type.isPresent() && !type.get().isInverted() && type.get().getValue() != EntityTypes.PLAYER);
    if (isPlayerOnlySelector) {
        // insert at the start so it applies first
        filters.add(0, requireTypePredicate(Entity.class, Player.class));
    }
    return Functional.predicateAnd(filters);
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) Vector3d(com.flowpowered.math.vector.Vector3d) SelectorType(org.spongepowered.api.text.selector.SelectorType) ArrayList(java.util.ArrayList) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector) Predicate(java.util.function.Predicate)

Aggregations

SelectorType (org.spongepowered.api.text.selector.SelectorType)5 Entity (org.spongepowered.api.entity.Entity)4 ArrayList (java.util.ArrayList)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Predicate (java.util.function.Predicate)2 Player (org.spongepowered.api.entity.living.player.Player)2 Invertible (org.spongepowered.api.text.selector.Argument.Invertible)2 Selector (org.spongepowered.api.text.selector.Selector)2 Extent (org.spongepowered.api.world.extent.Extent)2 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