Search in sources :

Example 11 with Selector

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

the class JsonTextSelectorSerializer method deserialize.

@Override
public SelectorText deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    final JsonObject obj = json.getAsJsonObject();
    final Selector selector = Selector.parse(obj.get(SELECTOR).getAsString());
    final SelectorText.Builder builder = SelectorText.builder(selector);
    deserialize(obj, builder, context);
    return builder.build();
}
Also used : SelectorText(org.spongepowered.api.text.SelectorText) JsonObject(com.google.gson.JsonObject) Selector(org.spongepowered.api.text.selector.Selector)

Example 12 with Selector

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

Example 13 with Selector

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

the class SelectorResolver method addGamemodeFilters.

private void addGamemodeFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<GameMode> gamemode = sel.get(ArgumentTypes.GAME_MODE);
    // If the gamemode is NOT_SET, that means accept any
    if (gamemode.isPresent() && gamemode.get() != GameModes.NOT_SET) {
        final GameMode actualMode = gamemode.get();
        filters.add(input -> {
            Optional<GameModeData> mode = input.get(GameModeData.class);
            return mode.isPresent() && mode.get() == actualMode;
        });
    }
}
Also used : GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) GameModeData(org.spongepowered.api.data.manipulator.mutable.entity.GameModeData) Selector(org.spongepowered.api.text.selector.Selector)

Example 14 with Selector

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

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 15 with Selector

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

the class SelectorResolver method addRotationFilters.

private void addRotationFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    // If the Z's are uncommented, don't forget to implement them
    // Optional<Double> rotMinZ = sel.get(ArgumentTypes.ROTATION.minimum().z());
    // Optional<Double> rotMaxZ = sel.get(ArgumentTypes.ROTATION.maximum().z());
    Optional<Double> rotMinX = sel.get(ArgumentTypes.ROTATION.minimum().x());
    if (rotMinX.isPresent()) {
        final double rmx = rotMinX.get();
        filters.add(input -> input.getRotation().getX() >= rmx);
    }
    Optional<Double> rotMinY = sel.get(ArgumentTypes.ROTATION.minimum().y());
    if (rotMinY.isPresent()) {
        final double rmy = rotMinY.get();
        filters.add(input -> input.getRotation().getY() >= rmy);
    }
    Optional<Double> rotMaxX = sel.get(ArgumentTypes.ROTATION.maximum().x());
    if (rotMaxX.isPresent()) {
        final double rx = rotMaxX.get();
        filters.add(input -> input.getRotation().getX() <= rx);
    }
    Optional<Double> rotMaxY = sel.get(ArgumentTypes.ROTATION.maximum().y());
    if (rotMaxY.isPresent()) {
        final double ry = rotMaxY.get();
        filters.add(input -> input.getRotation().getY() <= ry);
    }
}
Also used : 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