Search in sources :

Example 16 with Selector

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

the class SelectorResolver method addRadiusFilters.

private void addRadiusFilters(final Vector3d position, List<Predicate<Entity>> filters) {
    final Selector sel = this.selector;
    Optional<Integer> radiusMin = sel.get(ArgumentTypes.RADIUS.minimum());
    Optional<Integer> radiusMax = sel.get(ArgumentTypes.RADIUS.maximum());
    if (radiusMin.isPresent()) {
        int radMin = radiusMin.get();
        final int radMinSquared = radMin * radMin;
        filters.add(input -> input.getLocation().getPosition().distanceSquared(position) >= radMinSquared);
    }
    if (radiusMax.isPresent()) {
        int radMax = radiusMax.get();
        final int radMaxSquared = radMax * radMax;
        filters.add(input -> input.getLocation().getPosition().distanceSquared(position) <= radMaxSquared);
    }
}
Also used : Selector(org.spongepowered.api.text.selector.Selector)

Example 17 with Selector

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

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)

Example 18 with Selector

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

the class SelectorResolver method addTypeFilters.

private void addTypeFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Argument.Invertible<EntityType>> typeOpt = sel.getArgument(ArgumentTypes.ENTITY_TYPE);
    if (typeOpt.isPresent()) {
        Argument.Invertible<EntityType> typeArg = typeOpt.get();
        final boolean inverted = typeArg.isInverted();
        final EntityType type = typeArg.getValue();
        filters.add(input -> inverted ^ input.getType() == type);
    }
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) Argument(org.spongepowered.api.text.selector.Argument) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector)

Example 19 with Selector

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

the class Text method of.

/**
 * Builds a {@link Text} from a given array of objects.
 *
 * <p>For instance, you can use this like
 * <code>Text.of(TextColors.DARK_AQUA, "Hi", TextColors.AQUA, "Bye")</code>
 * </p>
 *
 * <p>This will create the correct {@link Text} instance if the input object
 * is the input for one of the {@link Text} types or convert the object to a
 * string otherwise.</p>
 *
 * <p>For instances of type {@link TextRepresentable} (e.g. {@link Text},
 * {@link Builder}, ...) the formatting of appended text has priority over
 * the current formatting in the method, e.g. the following results in a
 * green, then yellow and at the end green again {@link Text}:</p>
 *
 * <code>Text.of(TextColors.GREEN, "Hello ", Text.of(TextColors.YELLOW,
 * "Spongie"), '!');</code>
 *
 * @param objects The object array
 * @return The built text object
 */
public static Text of(Object... objects) {
    // Shortcut for lonely TextRepresentables
    if (objects.length == 1 && objects[0] instanceof TextRepresentable) {
        return ((TextRepresentable) objects[0]).toText();
    }
    final Text.Builder builder = builder();
    TextFormat format = TextFormat.NONE;
    HoverAction<?> hoverAction = null;
    ClickAction<?> clickAction = null;
    ShiftClickAction<?> shiftClickAction = null;
    boolean changedFormat = false;
    for (Object obj : objects) {
        // Text formatting + actions
        if (obj instanceof TextFormat) {
            changedFormat = true;
            format = (TextFormat) obj;
        } else if (obj instanceof TextColor) {
            changedFormat = true;
            format = format.color((TextColor) obj);
        } else if (obj instanceof TextStyle) {
            changedFormat = true;
            format = format.style(obj.equals(TextStyles.RESET) ? TextStyles.NONE : format.getStyle().and((TextStyle) obj));
        } else if (obj instanceof TextAction) {
            changedFormat = true;
            if (obj instanceof HoverAction) {
                hoverAction = (HoverAction<?>) obj;
            } else if (obj instanceof ClickAction) {
                clickAction = (ClickAction<?>) obj;
            } else if (obj instanceof ShiftClickAction) {
                shiftClickAction = (ShiftClickAction<?>) obj;
            } else {
            // Unsupported TextAction
            }
        } else if (obj instanceof TextRepresentable) {
            // Special content
            changedFormat = false;
            Text.Builder childBuilder = ((TextRepresentable) obj).toText().toBuilder();
            // Merge format (existing format has priority)
            childBuilder.format(format.merge(childBuilder.format));
            // Overwrite text actions if *NOT* present
            if (childBuilder.clickAction == null) {
                childBuilder.clickAction = clickAction;
            }
            if (childBuilder.hoverAction == null) {
                childBuilder.hoverAction = hoverAction;
            }
            if (childBuilder.shiftClickAction == null) {
                childBuilder.shiftClickAction = shiftClickAction;
            }
            builder.append(childBuilder.build());
        } else {
            // Simple content
            changedFormat = false;
            Text.Builder childBuilder;
            if (obj instanceof String) {
                childBuilder = builder((String) obj);
            } else if (obj instanceof Translation) {
                childBuilder = builder((Translation) obj);
            } else if (obj instanceof Translatable) {
                childBuilder = builder(((Translatable) obj).getTranslation());
            } else if (obj instanceof Selector) {
                childBuilder = builder((Selector) obj);
            } else if (obj instanceof Score) {
                childBuilder = builder((Score) obj);
            } else {
                childBuilder = builder(String.valueOf(obj));
            }
            if (hoverAction != null) {
                childBuilder.onHover(hoverAction);
            }
            if (clickAction != null) {
                childBuilder.onClick(clickAction);
            }
            if (shiftClickAction != null) {
                childBuilder.onShiftClick(shiftClickAction);
            }
            builder.append(childBuilder.format(format).build());
        }
    }
    if (changedFormat) {
        // Did the formatting change without being applied to something?
        // Then just append an empty text with that formatting
        final Text.Builder childBuilder = builder();
        if (hoverAction != null) {
            childBuilder.onHover(hoverAction);
        }
        if (clickAction != null) {
            childBuilder.onClick(clickAction);
        }
        if (shiftClickAction != null) {
            childBuilder.onShiftClick(shiftClickAction);
        }
        builder.append(childBuilder.format(format).build());
    }
    if (builder.children.size() == 1) {
        // Single content, reduce Text depth
        return builder.children.get(0);
    }
    return builder.build();
}
Also used : Translation(org.spongepowered.api.text.translation.Translation) Translatable(org.spongepowered.api.text.translation.Translatable) TextAction(org.spongepowered.api.text.action.TextAction) Score(org.spongepowered.api.scoreboard.Score) HoverAction(org.spongepowered.api.text.action.HoverAction) TextStyle(org.spongepowered.api.text.format.TextStyle) ClickAction(org.spongepowered.api.text.action.ClickAction) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) TextFormat(org.spongepowered.api.text.format.TextFormat) TextColor(org.spongepowered.api.text.format.TextColor) Selector(org.spongepowered.api.text.selector.Selector)

Example 20 with Selector

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

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