Search in sources :

Example 1 with Selector

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

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

the class SelectorResolver method addScoreFilters.

private void addScoreFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    sel.getArguments();
}
Also used : Selector(org.spongepowered.api.text.selector.Selector)

Example 3 with Selector

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

the class SelectorResolver method addDimensionFilters.

private void addDimensionFilters(final Vector3d position, List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Vector3d boxDimensions = getPositionOrDefault(ORIGIN, ArgumentTypes.DIMENSION);
    Vector3d det1 = position;
    Vector3d det2 = position.add(boxDimensions);
    final Vector3d boxMin = det1.min(det2);
    final Vector3d boxMax = det1.max(det2);
    if (sel.has(ArgumentTypes.DIMENSION.x())) {
        filters.add(input -> {
            Vector3d pos = input.getLocation().getPosition();
            return pos.getX() >= boxMin.getX() && pos.getX() <= boxMax.getX();
        });
    }
    if (sel.has(ArgumentTypes.DIMENSION.y())) {
        filters.add(input -> {
            Vector3d pos = input.getLocation().getPosition();
            return pos.getY() >= boxMin.getY() && pos.getY() <= boxMax.getY();
        });
    }
    if (sel.has(ArgumentTypes.DIMENSION.z())) {
        filters.add(input -> {
            Vector3d pos = input.getLocation().getPosition();
            return pos.getZ() >= boxMin.getZ() && pos.getZ() <= boxMax.getZ();
        });
    }
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) Selector(org.spongepowered.api.text.selector.Selector)

Example 4 with Selector

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

the class SelectorResolver method addLevelFilters.

private void addLevelFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Integer> levelMin = sel.get(ArgumentTypes.LEVEL.minimum());
    Optional<Integer> levelMax = sel.get(ArgumentTypes.LEVEL.maximum());
    if (levelMin.isPresent()) {
        final int actualMin = levelMin.get();
        filters.add(input -> {
            Optional<ExperienceHolderData> xp = input.get(ExperienceHolderData.class);
            return xp.isPresent() && xp.get().level().get() >= actualMin;
        });
    }
    if (levelMax.isPresent()) {
        final int actualMax = levelMax.get();
        filters.add(input -> {
            Optional<ExperienceHolderData> xp = input.get(ExperienceHolderData.class);
            return xp.isPresent() && xp.get().level().get() <= actualMax;
        });
    }
}
Also used : ExperienceHolderData(org.spongepowered.api.data.manipulator.mutable.entity.ExperienceHolderData) Selector(org.spongepowered.api.text.selector.Selector)

Example 5 with Selector

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

the class SelectorResolver method addNameFilters.

private void addNameFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Argument.Invertible<String>> nameOpt = sel.getArgument(ArgumentTypes.NAME);
    if (nameOpt.isPresent()) {
        final String name = nameOpt.get().getValue();
        final boolean inverted = nameOpt.get().isInverted();
        filters.add(input -> {
            Optional<DisplayNameData> dispName = input.get(DisplayNameData.class);
            return inverted ^ (dispName.isPresent() && name.equals(dispName.get().displayName().get().toPlain()));
        });
    }
}
Also used : DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector)

Aggregations

Selector (org.spongepowered.api.text.selector.Selector)22 Invertible (org.spongepowered.api.text.selector.Argument.Invertible)8 Vector3d (com.flowpowered.math.vector.Vector3d)4 Entity (org.spongepowered.api.entity.Entity)4 Collection (java.util.Collection)2 Predicate (java.util.function.Predicate)2 DisplayNameData (org.spongepowered.api.data.manipulator.mutable.DisplayNameData)2 ExperienceHolderData (org.spongepowered.api.data.manipulator.mutable.entity.ExperienceHolderData)2 GameModeData (org.spongepowered.api.data.manipulator.mutable.entity.GameModeData)2 EntityType (org.spongepowered.api.entity.EntityType)2 Player (org.spongepowered.api.entity.living.player.Player)2 GameMode (org.spongepowered.api.entity.living.player.gamemode.GameMode)2 Team (org.spongepowered.api.scoreboard.Team)2 TeamMember (org.spongepowered.api.scoreboard.TeamMember)2 Argument (org.spongepowered.api.text.selector.Argument)2 SelectorType (org.spongepowered.api.text.selector.SelectorType)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 Score (org.spongepowered.api.scoreboard.Score)1 SelectorText (org.spongepowered.api.text.SelectorText)1