From 42a081572a8efbe94ed5f53b9f4f28dd92c9a775 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 3b027f67c..fbe7e76c0 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; @@ -635,6 +638,256 @@ public interface World extends PluginMessageRecipient, Metadatable { @NotNull public Collection getEntitiesByClasses(@NotNull Class... classes); + // Paper start + /** + * Gets nearby players 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. + */ + @NotNull + public default Collection getNearbyLivingEntities(@NotNull Location loc, double radius) { + return getNearbyEntitiesByType(org.bukkit.entity.LivingEntity.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 entities near location. This will always be a non-null collection. + */ + @NotNull + public default Collection getNearbyLivingEntities(@NotNull Location loc, double xzRadius, double yRadius) { + return getNearbyEntitiesByType(org.bukkit.entity.LivingEntity.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 entities near location. This will always be a non-null collection. + */ + @NotNull + public default Collection getNearbyLivingEntities(@NotNull Location loc, double xRadius, double yRadius, double zRadius) { + return getNearbyEntitiesByType(org.bukkit.entity.LivingEntity.class, loc, xRadius, yRadius, zRadius); + } + + /** + * Gets nearby players 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 + */ + @NotNull + public default Collection getNearbyLivingEntities(@NotNull Location loc, double radius, @Nullable Predicate predicate) { + return getNearbyEntitiesByType(org.bukkit.entity.LivingEntity.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 living entities near location. This will always be a non-null collection + */ + @NotNull + public default Collection getNearbyLivingEntities(@NotNull Location loc, double xzRadius, double yRadius, @Nullable Predicate predicate) { + return getNearbyEntitiesByType(org.bukkit.entity.LivingEntity.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 living entities near location. This will always be a non-null collection. + */ + @NotNull + public default Collection getNearbyLivingEntities(@NotNull Location loc, double xRadius, double yRadius, double zRadius, @Nullable Predicate predicate) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyPlayers(@NotNull Location loc, double radius) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyPlayers(@NotNull Location loc, double xzRadius, double yRadius) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyPlayers(@NotNull Location loc, double xRadius, double yRadius, double zRadius) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyPlayers(@NotNull Location loc, double radius, @Nullable Predicate predicate) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyPlayers(@NotNull Location loc, double xzRadius, double yRadius, @Nullable Predicate predicate) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyPlayers(@NotNull Location loc, double xRadius, double yRadius, double zRadius, @Nullable Predicate predicate) { + return getNearbyEntitiesByType(org.bukkit.entity.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. + */ + @NotNull + public default Collection getNearbyEntitiesByType(@Nullable Class clazz, @NotNull Location loc, double radius) { + return 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. + */ + @NotNull + public default Collection getNearbyEntitiesByType(@Nullable Class clazz, @NotNull Location loc, double xzRadius, double yRadius) { + return 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. + */ + @NotNull + public default Collection getNearbyEntitiesByType(@Nullable Class clazz, @NotNull Location loc, double xRadius, double yRadius, double zRadius) { + return 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. + */ + @NotNull + public default Collection getNearbyEntitiesByType(@Nullable Class clazz, @NotNull Location loc, double radius, @Nullable Predicate predicate) { + return 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. + */ + @NotNull + public default Collection getNearbyEntitiesByType(@Nullable Class clazz, @NotNull Location loc, double xzRadius, double yRadius, @Nullable Predicate predicate) { + return 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. + */ + @NotNull + public default Collection getNearbyEntitiesByType(@Nullable Class clazz, @NotNull Location loc, double xRadius, double yRadius, double zRadius, @Nullable Predicate predicate) { + if (clazz == null) { + clazz = Entity.class; + } + List nearby = new ArrayList<>(); + for (Entity bukkitEntity : 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 + /** * Get a list of all players in this World * diff --git a/src/main/java/org/bukkit/event/player/AsyncPlayerPreLoginEvent.java b/src/main/java/org/bukkit/event/player/AsyncPlayerPreLoginEvent.java index 6c09ea6c6..76c4a1a6e 100644 --- a/src/main/java/org/bukkit/event/player/AsyncPlayerPreLoginEvent.java +++ b/src/main/java/org/bukkit/event/player/AsyncPlayerPreLoginEvent.java @@ -42,8 +42,7 @@ public class AsyncPlayerPreLoginEvent extends Event { return profile; } - /** - * Changes the PlayerProfile the player will login as + /* * Changes the PlayerProfile the player will login as * @param profile The profile to use */ public void setPlayerProfile(@NotNull PlayerProfile profile) { -- 2.26.2