PaperMC/patches/api/0099-Additional-world.getNearbyEntities-API-s.patch

261 lines
13 KiB
Diff
Raw Normal View History

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
index 609e4908ed21f69b9e813500e702bbe784bff00c..a5be4e073d3406e876e54824aca5449f3262ee75 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;
@@ -625,6 +628,238 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
2021-06-11 14:02:28 +02:00
@NotNull
public Collection<Entity> getEntitiesByClasses(@NotNull Class<?>... classes);
+ // Paper start - additional getNearbyEntities API
2021-06-11 14:02:28 +02: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.
+ */
+ 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
+ }
+
+ /**
+ * 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.
+ */
+ 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
+ }
+
+ /**
+ * 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.
+ */
+ 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
+ }
+
+ /**
+ * 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
+ */
+ 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
+ }
+
+ /**
+ * 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
+ */
+ 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
+ }
+
+ /**
+ * 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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;
+ }
+ 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;
+ }
+ // Paper end - additional getNearbyEntities API
2021-06-11 14:02:28 +02:00
+
/**
* Get a list of all players in this World
*