Search in sources :

Example 1 with Invertible

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

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

the class SelectorResolver method addTeamFilters.

private void addTeamFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Invertible<String>> teamOpt = sel.getArgument(ArgumentTypes.TEAM);
    if (teamOpt.isPresent()) {
        Invertible<String> teamArg = teamOpt.get();
        final boolean inverted = teamArg.isInverted();
        final Collection<Team> teams = Sponge.getGame().getServer().getServerScoreboard().get().getTeams();
        filters.add(new Predicate<Entity>() {

            @Override
            public boolean test(Entity input) {
                if (input instanceof TeamMember) {
                    return inverted ^ collectMembers(teams).contains(((TeamMember) input).getTeamRepresentation());
                }
                return false;
            }

            private Collection<Text> collectMembers(Collection<Team> teams) {
                ImmutableSet.Builder<Text> users = ImmutableSet.builder();
                for (Team t : teams) {
                    users.addAll(t.getMembers());
                }
                return users.build();
            }
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) TeamMember(org.spongepowered.api.scoreboard.TeamMember) Collection(java.util.Collection) Team(org.spongepowered.api.scoreboard.Team) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector)

Example 3 with Invertible

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

the class SelectorResolver method addTeamFilters.

private void addTeamFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Invertible<String>> teamOpt = sel.getArgument(ArgumentTypes.TEAM);
    if (teamOpt.isPresent()) {
        Invertible<String> teamArg = teamOpt.get();
        final boolean inverted = teamArg.isInverted();
        final Collection<Team> teams = Sponge.getGame().getServer().getServerScoreboard().get().getTeams();
        filters.add(new Predicate<Entity>() {

            @Override
            public boolean test(Entity input) {
                if (input instanceof TeamMember) {
                    return inverted ^ collectMembers(teams).contains(((TeamMember) input).getTeamRepresentation());
                }
                return false;
            }

            private Collection<Text> collectMembers(Collection<Team> teams) {
                ImmutableSet.Builder<Text> users = ImmutableSet.builder();
                for (Team t : teams) {
                    users.addAll(t.getMembers());
                }
                return users.build();
            }
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) TeamMember(org.spongepowered.api.scoreboard.TeamMember) Collection(java.util.Collection) Team(org.spongepowered.api.scoreboard.Team) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector)

Aggregations

Entity (org.spongepowered.api.entity.Entity)3 Invertible (org.spongepowered.api.text.selector.Argument.Invertible)3 Selector (org.spongepowered.api.text.selector.Selector)3 Collection (java.util.Collection)2 Team (org.spongepowered.api.scoreboard.Team)2 TeamMember (org.spongepowered.api.scoreboard.TeamMember)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Predicate (java.util.function.Predicate)1 Player (org.spongepowered.api.entity.living.player.Player)1 SelectorType (org.spongepowered.api.text.selector.SelectorType)1