From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Mon, 30 Apr 2018 17:55:28 -0400 Subject: [PATCH] Additional world.getNearbyEntities API's Provides more methods to get nearby entities, and filter by types and predicates diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java index 369e92f2b5e4b8fc7f5bf8a97058e827c91e4c59..79e5b429fe5660695e9fa603f8bc1fc1352b8950 100644 --- a/src/main/java/org/bukkit/World.java +++ b/src/main/java/org/bukkit/World.java @@ -1,6 +1,9 @@ package org.bukkit; import java.io.File; +import org.bukkit.generator.ChunkGenerator; + +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -663,6 +666,238 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient @NotNull public Collection getEntitiesByClasses(@NotNull Class... classes); + // Paper start - additional getNearbyEntities API + /** + * Gets nearby LivingEntities within the specified radius (bounding box) + * @param loc Center location + * @param radius Radius + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyLivingEntities(final @NotNull Location loc, final double radius) { + return this.getNearbyEntitiesByType(LivingEntity.class, loc, radius, radius, radius); + } + + /** + * Gets nearby LivingEntities within the specified radius (bounding box) + * @param loc Center location + * @param xzRadius X/Z Radius + * @param yRadius Y Radius + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyLivingEntities(final @NotNull Location loc, final double xzRadius, final double yRadius) { + return this.getNearbyEntitiesByType(LivingEntity.class, loc, xzRadius, yRadius, xzRadius); + } + + /** + * Gets nearby LivingEntities within the specified radius (bounding box) + * @param loc Center location + * @param xRadius X Radius + * @param yRadius Y Radius + * @param zRadius Z radius + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyLivingEntities(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius) { + return this.getNearbyEntitiesByType(LivingEntity.class, loc, xRadius, yRadius, zRadius); + } + + /** + * Gets nearby LivingEntities within the specified radius (bounding box) + * @param loc Center location + * @param radius X Radius + * @param predicate a predicate used to filter results + * @return the collection of living entities near location. This will always be a non-null collection + */ + default @NotNull Collection getNearbyLivingEntities(final @NotNull Location loc, final double radius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(LivingEntity.class, loc, radius, radius, radius, predicate); + } + + /** + * Gets nearby LivingEntities within the specified radius (bounding box) + * @param loc Center location + * @param xzRadius X/Z Radius + * @param yRadius Y Radius + * @param predicate a predicate used to filter results + * @return the collection of living entities near location. This will always be a non-null collection + */ + default @NotNull Collection getNearbyLivingEntities(final @NotNull Location loc, final double xzRadius, final double yRadius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(LivingEntity.class, loc, xzRadius, yRadius, xzRadius, predicate); + } + + /** + * Gets nearby LivingEntities within the specified radius (bounding box) + * @param loc Center location + * @param xRadius X Radius + * @param yRadius Y Radius + * @param zRadius Z radius + * @param predicate a predicate used to filter results + * @return the collection of living entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyLivingEntities(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(LivingEntity.class, loc, xRadius, yRadius, zRadius, predicate); + } + + /** + * Gets nearby players within the specified radius (bounding box) + * @param loc Center location + * @param radius X/Y/Z Radius + * @return the collection of living entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyPlayers(final @NotNull Location loc, final double radius) { + return this.getNearbyEntitiesByType(Player.class, loc, radius, radius, radius); + } + + /** + * Gets nearby players within the specified radius (bounding box) + * @param loc Center location + * @param xzRadius X/Z Radius + * @param yRadius Y Radius + * @return the collection of living entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyPlayers(final @NotNull Location loc, final double xzRadius, final double yRadius) { + return this.getNearbyEntitiesByType(Player.class, loc, xzRadius, yRadius, xzRadius); + } + + /** + * Gets nearby players within the specified radius (bounding box) + * @param loc Center location + * @param xRadius X Radius + * @param yRadius Y Radius + * @param zRadius Z Radius + * @return the collection of players near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyPlayers(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius) { + return this.getNearbyEntitiesByType(Player.class, loc, xRadius, yRadius, zRadius); + } + + /** + * Gets nearby players within the specified radius (bounding box) + * @param loc Center location + * @param radius X/Y/Z Radius + * @param predicate a predicate used to filter results + * @return the collection of players near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyPlayers(final @NotNull Location loc, final double radius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(Player.class, loc, radius, radius, radius, predicate); + } + + /** + * Gets nearby players within the specified radius (bounding box) + * @param loc Center location + * @param xzRadius X/Z Radius + * @param yRadius Y Radius + * @param predicate a predicate used to filter results + * @return the collection of players near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyPlayers(final @NotNull Location loc, final double xzRadius, final double yRadius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(Player.class, loc, xzRadius, yRadius, xzRadius, predicate); + } + + /** + * Gets nearby players within the specified radius (bounding box) + * @param loc Center location + * @param xRadius X Radius + * @param yRadius Y Radius + * @param zRadius Z Radius + * @param predicate a predicate used to filter results + * @return the collection of players near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyPlayers(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(Player.class, loc, xRadius, yRadius, zRadius, predicate); + } + + /** + * Gets all nearby entities of the specified type, within the specified radius (bounding box) + * @param clazz Type to filter by + * @param loc Center location + * @param radius X/Y/Z radius to search within + * @param the entity type + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyEntitiesByType(final @Nullable Class clazz, final @NotNull Location loc, final double radius) { + return this.getNearbyEntitiesByType(clazz, loc, radius, radius, radius, null); + } + + /** + * Gets all nearby entities of the specified type, within the specified radius, with x and x radius matching (bounding box) + * @param clazz Type to filter by + * @param loc Center location + * @param xzRadius X/Z radius to search within + * @param yRadius Y radius to search within + * @param the entity type + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyEntitiesByType(final @Nullable Class clazz, final @NotNull Location loc, final double xzRadius, final double yRadius) { + return this.getNearbyEntitiesByType(clazz, loc, xzRadius, yRadius, xzRadius, null); + } + + /** + * Gets all nearby entities of the specified type, within the specified radius (bounding box) + * @param clazz Type to filter by + * @param loc Center location + * @param xRadius X Radius + * @param yRadius Y Radius + * @param zRadius Z Radius + * @param the entity type + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyEntitiesByType(final @Nullable Class clazz, final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius) { + return this.getNearbyEntitiesByType(clazz, loc, xRadius, yRadius, zRadius, null); + } + + /** + * Gets all nearby entities of the specified type, within the specified radius (bounding box) + * @param clazz Type to filter by + * @param loc Center location + * @param radius X/Y/Z radius to search within + * @param predicate a predicate used to filter results + * @param the entity type + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyEntitiesByType(final @Nullable Class clazz, final @NotNull Location loc, final double radius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(clazz, loc, radius, radius, radius, predicate); + } + + /** + * Gets all nearby entities of the specified type, within the specified radius, with x and x radius matching (bounding box) + * @param clazz Type to filter by + * @param loc Center location + * @param xzRadius X/Z radius to search within + * @param yRadius Y radius to search within + * @param predicate a predicate used to filter results + * @param the entity type + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyEntitiesByType(final @Nullable Class clazz, final @NotNull Location loc, final double xzRadius, final double yRadius, final @Nullable Predicate predicate) { + return this.getNearbyEntitiesByType(clazz, loc, xzRadius, yRadius, xzRadius, predicate); + } + + /** + * Gets all nearby entities of the specified type, within the specified radius (bounding box) + * @param clazz Type to filter by + * @param loc Center location + * @param xRadius X Radius + * @param yRadius Y Radius + * @param zRadius Z Radius + * @param predicate a predicate used to filter results + * @param the entity type + * @return the collection of entities near location. This will always be a non-null collection. + */ + default @NotNull Collection getNearbyEntitiesByType(@Nullable Class clazz, final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate predicate) { + if (clazz == null) { + clazz = Entity.class; + } + final List nearby = new ArrayList<>(); + for (final Entity bukkitEntity : this.getNearbyEntities(loc, xRadius, yRadius, zRadius)) { + //noinspection unchecked + if (clazz.isAssignableFrom(bukkitEntity.getClass()) && (predicate == null || predicate.test((T) bukkitEntity))) { + //noinspection unchecked + nearby.add((T) bukkitEntity); + } + } + return nearby; + } + // Paper end - additional getNearbyEntities API + /** * Get a list of all players in this World *