Class EntityQuery<T>

Type Parameters:
T - The type of value selected by this query.

public final class EntityQuery<T>

A reusable, fluent query for selecting entities without game-specific collection boilerplate.

Filters are cumulative. Calling sorted(Comparator) or nearestTo(Point2D) replaces any previously selected ordering. Terminal operations evaluate the current contents of the source collection; list() returns an unmodifiable snapshot.

See Also:
  • Constructor Details

    • EntityQuery

      public EntityQuery(Collection<T> source)
      Creates a query over a collection.
      Parameters:
      source - The live source collection, or null for an empty query.
  • Method Details

    • matching

      public EntityQuery<T> matching(Predicate<T> filter)
      Adds a predicate to the filters already configured on this query.
      Parameters:
      filter - The predicate to add.
      Returns:
      This query.
      Throws:
      NullPointerException - if filter is null.
    • tagged

      public EntityQuery<T> tagged(String tag)
      Keeps entities that have the specified tag.
      Parameters:
      tag - The tag to match.
      Returns:
      This query.
    • named

      public EntityQuery<T> named(String name)
      Keeps entities whose name equals the specified name.
      Parameters:
      name - The name to match; may be null.
      Returns:
      This query.
    • alive

      public EntityQuery<T> alive()
      Excludes dead combat entities while retaining values that are not combat entities.
      Returns:
      This query.
    • dead

      public EntityQuery<T> dead()
      Keeps only dead combat entities.
      Returns:
      This query.
    • team

      public EntityQuery<T> team(int team)
      Keeps only combat entities assigned to a team.
      Parameters:
      team - The team identifier to match.
      Returns:
      This query.
    • enemyOf

      public EntityQuery<T> enemyOf(ICombatEntity entity)
      Keeps combat entities whose team differs from the reference entity's team.
      Parameters:
      entity - The entity whose team is considered friendly.
      Returns:
      This query.
      Throws:
      NullPointerException - if entity is null.
    • within

      public EntityQuery<T> within(Point2D point, double distance)
      Keeps entities whose center is at most the specified distance from a point.
      Parameters:
      point - The center of the search area in map coordinates.
      distance - The inclusive maximum distance in map units.
      Returns:
      This query.
      Throws:
      NullPointerException - if point is null.
      IllegalArgumentException - if distance is negative.
    • nearestTo

      public EntityQuery<T> nearestTo(Point2D point)

      Orders entities by increasing distance from a point.

      Values that are not entities sort after all entities.

      Parameters:
      point - The reference point in map coordinates.
      Returns:
      This query.
      Throws:
      NullPointerException - if point is null.
    • sorted

      public EntityQuery<T> sorted(Comparator<T> order)
      Replaces the current ordering with a comparator.
      Parameters:
      order - The comparator to use.
      Returns:
      This query.
      Throws:
      NullPointerException - if order is null.
    • list

      public List<T> list()
      Evaluates this query and returns its matching values.
      Returns:
      An unmodifiable result list in the configured order.
    • first

      public Optional<T> first()
      Finds the first matching value in configured or source order.
      Returns:
      The first match, or an empty optional when nothing matches.
    • count

      public long count()
      Counts matching values without applying the configured ordering.
      Returns:
      The number of matching values.
    • isEmpty

      public boolean isEmpty()
      Tests whether this query has no matching values.
      Returns:
      true when no value matches.
    • in

      public static <T> EntityQuery<T> in(Environment environment, Class<? extends T> type)
      Creates a query over all entities of a type in an environment.
      Type Parameters:
      T - The requested entity type.
      Parameters:
      environment - The environment to query.
      type - The requested entity type.
      Returns:
      A query over the environment's matching entities.
      Throws:
      NullPointerException - if environment or type is null.