Class EntitySelector<T extends org.bukkit.entity.Entity,E extends EntitySelector<T,E>>

java.lang.Object
wbs.utils.util.entities.selector.EntitySelector<T,E>
Type Parameters:
T - The subclass of entity to select
E - The class of an implementing type, for ease of use in end point methods.
Direct Known Subclasses:
LineOfSightSelector, RadiusSelector

public abstract class EntitySelector<T extends org.bukkit.entity.Entity,E extends EntitySelector<T,E>> extends Object
A helper class for selecting an entity or collection of entities in a variety of controllable ways.
Designed to replace the many selection methods in the now deprecated WbsEntities.
  • Field Details

    • clazz

      protected final Class<T extends org.bukkit.entity.Entity> clazz
      The subclass to be selected
    • range

      protected double range
      The range the selector will search in.
  • Constructor Details

    • EntitySelector

      public EntitySelector(Class<T> clazz)
      Parameters:
      clazz - The subclass to be selected
  • Method Details

    • getSelection

      @NotNull protected abstract @NotNull List<T> getSelection(org.bukkit.Location loc)
      Perform a selection and return all found.
      Parameters:
      loc - The location to perform the selection from
      Returns:
      All selections
    • getThis

      protected abstract E getThis()
      Returns:
      This object as the subclass
    • select

      @NotNull public @NotNull List<T> select(org.bukkit.Location loc)
      Selects all entities of the given type, limited to maxSelections.
      Parameters:
      loc - The location to perform the selection from
      Returns:
      An ordered selection, limited to maxSelections entities.
    • select

      @NotNull public final @NotNull List<T> select(org.bukkit.entity.Entity entity)
      Selects all entities of the given type, limited to maxSelections.
      Parameters:
      entity - The entity whose location will be used to perform the selection.
      Returns:
      An ordered selection, limited to maxSelections entities.
    • selectExcluding

      @NotNull public final @NotNull List<T> selectExcluding(T entity)
      Selects all entities of the given type, limited to maxSelections, excluding the given entity
      Parameters:
      entity - The entity whose location will be used to perform the selection.
      Returns:
      The first found entity, or null if none were found.
    • selectExcluding

      @NotNull public final @NotNull List<T> selectExcluding(org.bukkit.Location loc, T entity)
      Selects all entities of the given type, limited to maxSelections, excluding the given entity
      Parameters:
      loc - The location to perform the selection from
      entity - The entity to exclude from this operation only
      Returns:
      The first found entity, or null if none were found.
    • selectFirst

      @Nullable public final T selectFirst(org.bukkit.Location loc)
      Select only the first found selectable entity
      Parameters:
      loc - The location to perform the selection from
      Returns:
      The first found entity, or null if none were found.
    • selectFirst

      @Nullable public final T selectFirst(org.bukkit.entity.Entity entity)
      Select only the first found selectable entity. To ignore the provided entity, use selectFirstExcluding(Entity) instead.
      Parameters:
      entity - The entity whose location will be used to perform the selection.
      Returns:
      The first found entity, or null if none were found.
    • selectFirstExcluding

      @Nullable public final T selectFirstExcluding(T entity)
      Select only the first found selectable entity, excluding the provided one.
      Parameters:
      entity - The entity whose location will be used to perform the selection.
      Returns:
      The first found entity, or null if none were found.
    • selectFirstExcluding

      @Nullable public final T selectFirstExcluding(org.bukkit.Location loc, T entity)
      Select only the first found selectable entity, excluding the provided one.
      Parameters:
      loc - The location to perform the selection from
      entity - The entity to exclude from this operation only
      Returns:
      The first found entity, or null if none were found.
    • selectRandom

      @Nullable public final T selectRandom(org.bukkit.Location loc)
      Select a single random selectable entity
      Parameters:
      loc - The location to perform the selection from
      Returns:
      A random found entity, or null if none were found.
    • selectRandom

      @Nullable public final T selectRandom(org.bukkit.entity.Entity entity)
      Select a single random selectable entity To ignore the provided entity, use selectRandomExcluding(Entity) instead.
      Parameters:
      entity - The entity whose location will be used to perform the selection.
      Returns:
      A random found entity, or null if none were found.
    • selectRandomExcluding

      @Nullable public final T selectRandomExcluding(T entity)
      Select only the first found selectable entity, excluding the provided one.
      Parameters:
      entity - The entity whose location will be used to perform the selection.
      Returns:
      A random found entity, or null if none were found.
    • selectRandomExcluding

      @Nullable public final T selectRandomExcluding(org.bukkit.Location loc, T entity)
      Select only the first found selectable entity, excluding the provided one.
      Parameters:
      loc - The location to perform the selection from
      entity - The entity to exclude from this operation only
      Returns:
      A random found entity, or null if none were found.
    • filter

      protected final <U extends org.bukkit.entity.Entity> Set<T> filter(Collection<U> collection)
      A utility method for use in implementations, that filters the given collection against EntitySelector to ensure all instances are of type T (or removed otherwise)
      Type Parameters:
      U - Some extension of Entity
      Parameters:
      collection - The collection of entities to filter
      Returns:
      The filtered collection as a Set of T.
    • getRawPredicate

      @NotNull protected @NotNull Predicate<org.bukkit.entity.Entity> getRawPredicate()
      Returns:
      A new predicate against Entity that returns true if:
    • isValid

      protected boolean isValid(T entity)
      Parameters:
      entity - The entity to test.
      Returns:
      true if the entity is not excluded, and the custom predicate passes.
    • getRange

      public final double getRange()
      Returns:
      Gets the current range of selection
    • setRange

      public final E setRange(double range)
      Parameters:
      range - The range of selection
      Returns:
      The same object.
    • getMaxSelections

      public final int getMaxSelections()
      Returns:
      The maximum number of entities to find in a given select(Location).
    • setMaxSelections

      public final E setMaxSelections(int maxSelections)
      Parameters:
      maxSelections - The maximum number of entities to find in a given select(Location).
      Returns:
      The same object.
    • getPredicate

      @NotNull public final @NotNull Predicate<T> getPredicate()
      The current entity predicate.
      Returns:
      The same object.
    • setPredicate

      public final E setPredicate(Predicate<T> predicate)
      Parameters:
      predicate - The new predicate
      Returns:
      The same object.
    • setPredicateRaw

      public final E setPredicateRaw(Predicate<org.bukkit.entity.Entity> predicate)
      Parameters:
      predicate - The new predicate, which will also require a test for being an instance of T.
      Returns:
      The same object.
    • getExclude

      @NotNull public final @NotNull Set<T> getExclude()
      Returns:
      Gets the set of currently excluded entities
    • exclude

      public final E exclude(T toAdd)
      Parameters:
      toAdd - The entity to exclude
      Returns:
      The same object.
    • exclude

      public final E exclude(Collection<T> toAdd)
      Parameters:
      toAdd - The entities to exclude
      Returns:
      The same object.
    • unexclude

      public final boolean unexclude(T toRemove)
      Parameters:
      toRemove - The entity to remove from the exclusion set, to allow it to be selected
      Returns:
      The same object.
    • unexclude

      public final boolean unexclude(Collection<T> toRemove)
      Parameters:
      toRemove - The entities to remove from the exclusion set, to allow it to be selected
      Returns:
      The same object.