2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
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
2024-04-06 21:53:39 +02:00
index a55e92dc09202437abac9cb5b7aacf3c275f5b9c..e2cd4750ec51da5e6a93f31a9b644516f64f7972 100644
2021-06-11 14:02:28 +02:00
--- 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;
2024-04-06 21:53:39 +02:00
@@ -636,6 +639,238 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
2021-06-11 14:02:28 +02:00
@NotNull
public Collection<Entity> getEntitiesByClasses(@NotNull Class<?>... classes);
2023-11-26 00:03:02 +01:00
+ // Paper start - additional getNearbyEntities API
2021-06-11 14:02:28 +02:00
+ /**
2021-12-28 00:45:36 +01:00
+ * Gets nearby LivingEntities within the specified radius (bounding box)
2021-06-11 14:02:28 +02:00
+ * @param loc Center location
+ * @param radius Radius
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<LivingEntity> getNearbyLivingEntities(final @NotNull Location loc, final double radius) {
+ return this.getNearbyEntitiesByType(LivingEntity.class, loc, radius, radius, radius);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2021-12-28 00:45:36 +01:00
+ * Gets nearby LivingEntities within the specified radius (bounding box)
2021-06-11 14:02:28 +02:00
+ * @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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<LivingEntity> getNearbyLivingEntities(final @NotNull Location loc, final double xzRadius, final double yRadius) {
+ return this.getNearbyEntitiesByType(LivingEntity.class, loc, xzRadius, yRadius, xzRadius);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2021-12-28 00:45:36 +01:00
+ * Gets nearby LivingEntities within the specified radius (bounding box)
2021-06-11 14:02:28 +02:00
+ * @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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<LivingEntity> getNearbyLivingEntities(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius) {
+ return this.getNearbyEntitiesByType(LivingEntity.class, loc, xRadius, yRadius, zRadius);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2021-12-28 00:45:36 +01:00
+ * Gets nearby LivingEntities within the specified radius (bounding box)
2021-06-11 14:02:28 +02:00
+ * @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
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<LivingEntity> getNearbyLivingEntities(final @NotNull Location loc, final double radius, final @Nullable Predicate<? super LivingEntity> predicate) {
+ return this.getNearbyEntitiesByType(LivingEntity.class, loc, radius, radius, radius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2021-12-28 00:45:36 +01:00
+ * Gets nearby LivingEntities within the specified radius (bounding box)
2021-06-11 14:02:28 +02:00
+ * @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
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<LivingEntity> getNearbyLivingEntities(final @NotNull Location loc, final double xzRadius, final double yRadius, final @Nullable Predicate<? super LivingEntity> predicate) {
+ return this.getNearbyEntitiesByType(LivingEntity.class, loc, xzRadius, yRadius, xzRadius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2021-12-28 00:45:36 +01:00
+ * Gets nearby LivingEntities within the specified radius (bounding box)
2021-06-11 14:02:28 +02:00
+ * @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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<LivingEntity> getNearbyLivingEntities(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate<? super LivingEntity> predicate) {
+ return this.getNearbyEntitiesByType(LivingEntity.class, loc, xRadius, yRadius, zRadius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<Player> getNearbyPlayers(final @NotNull Location loc, final double radius) {
+ return this.getNearbyEntitiesByType(Player.class, loc, radius, radius, radius);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<Player> getNearbyPlayers(final @NotNull Location loc, final double xzRadius, final double yRadius) {
+ return this.getNearbyEntitiesByType(Player.class, loc, xzRadius, yRadius, xzRadius);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<Player> getNearbyPlayers(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius) {
+ return this.getNearbyEntitiesByType(Player.class, loc, xRadius, yRadius, zRadius);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<Player> getNearbyPlayers(final @NotNull Location loc, final double radius, final @Nullable Predicate<? super Player> predicate) {
+ return this.getNearbyEntitiesByType(Player.class, loc, radius, radius, radius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<Player> getNearbyPlayers(final @NotNull Location loc, final double xzRadius, final double yRadius, final @Nullable Predicate<? super Player> predicate) {
+ return this.getNearbyEntitiesByType(Player.class, loc, xzRadius, yRadius, xzRadius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull Collection<Player> getNearbyPlayers(final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate<? super Player> predicate) {
+ return this.getNearbyEntitiesByType(Player.class, loc, xRadius, yRadius, zRadius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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 <T> the entity type
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull <T extends Entity> Collection<T> getNearbyEntitiesByType(final @Nullable Class<? extends T> clazz, final @NotNull Location loc, final double radius) {
+ return this.getNearbyEntitiesByType(clazz, loc, radius, radius, radius, null);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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 <T> the entity type
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull <T extends Entity> Collection<T> getNearbyEntitiesByType(final @Nullable Class<? extends T> clazz, final @NotNull Location loc, final double xzRadius, final double yRadius) {
+ return this.getNearbyEntitiesByType(clazz, loc, xzRadius, yRadius, xzRadius, null);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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 <T> the entity type
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull <T extends Entity> Collection<T> getNearbyEntitiesByType(final @Nullable Class<? extends T> clazz, final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius) {
+ return this.getNearbyEntitiesByType(clazz, loc, xRadius, yRadius, zRadius, null);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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 <T> the entity type
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull <T extends Entity> Collection<T> getNearbyEntitiesByType(final @Nullable Class<? extends T> clazz, final @NotNull Location loc, final double radius, final @Nullable Predicate<? super T> predicate) {
+ return this.getNearbyEntitiesByType(clazz, loc, radius, radius, radius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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 <T> the entity type
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default @NotNull <T extends Entity> Collection<T> getNearbyEntitiesByType(final @Nullable Class<? extends T> clazz, final @NotNull Location loc, final double xzRadius, final double yRadius, final @Nullable Predicate<? super T> predicate) {
+ return this.getNearbyEntitiesByType(clazz, loc, xzRadius, yRadius, xzRadius, predicate);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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 <T> the entity type
+ * @return the collection of entities near location. This will always be a non-null collection.
+ */
2023-11-26 00:03:02 +01:00
+ default <T extends Entity> @NotNull Collection<T> getNearbyEntitiesByType(@Nullable Class<? extends Entity> clazz, final @NotNull Location loc, final double xRadius, final double yRadius, final double zRadius, final @Nullable Predicate<? super T> predicate) {
+ if (clazz == null) {
2021-06-11 14:02:28 +02:00
+ clazz = Entity.class;
+ }
2023-11-26 00:03:02 +01:00
+ final List<T> nearby = new ArrayList<>();
+ for (final Entity bukkitEntity : this.getNearbyEntities(loc, xRadius, yRadius, zRadius)) {
2021-06-11 14:02:28 +02:00
+ //noinspection unchecked
+ if (clazz.isAssignableFrom(bukkitEntity.getClass()) && (predicate == null || predicate.test((T) bukkitEntity))) {
+ //noinspection unchecked
+ nearby.add((T) bukkitEntity);
+ }
+ }
+ return nearby;
+ }
2023-11-26 00:03:02 +01:00
+ // Paper end - additional getNearbyEntities API
2021-06-11 14:02:28 +02:00
+
/**
* Get a list of all players in this World
*