Skip to content

Instantly share code, notes, and snippets.

@aikar
Created April 4, 2019 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/c66c9572b58338a8c17ef89fed9e49be to your computer and use it in GitHub Desktop.
Save aikar/c66c9572b58338a8c17ef89fed9e49be to your computer and use it in GitHub Desktop.
commit 0b8e5141862ffa710d1b8448b2d28924d442b9a2
Author: Aikar <aikar@aikar.co>
Date: Wed Apr 3 23:37:33 2019 -0400
Add World#getNearbyEntities Helpers and Java 8
The server requires Java 8 so there's no reason we shouldn't go ahead
and unlock the power of Java 8 on the API.
This PR is an example of how Java 8 enables API-Only API additions
that do not even require a Server side addition.
Future PR's now benefit in that overloaded method signatures will
be able to apply defaults at the API level and only need to implement
a single method on the server.
This promotes a cleaner code contribution for future PR's.
Considering that 1.13 API can not be used in a non Java 8 environment,
there is no risk to breaking plugins that run on older servers.
diff --git a/pom.xml b/pom.xml
index 09c297b0..f9caef4f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,8 +14,6 @@
<properties>
<skipTests>true</skipTests>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@@ -86,7 +84,7 @@
<!-- annotations -->
<dependency>
<groupId>org.jetbrains</groupId>
- <artifactId>annotations-java5</artifactId>
+ <artifactId>annotations</artifactId>
<version>17.0.0</version>
<scope>provided</scope>
</dependency>
@@ -118,6 +116,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
+ <source>1.8</source>
+ <target>1.8</target>
<!-- we use the Eclipse compiler as it doesn't need a JDK -->
<compilerId>eclipse</compilerId>
</configuration>
@@ -168,30 +168,6 @@
<properties>
<skipTests>false</skipTests>
</properties>
- <build>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>animal-sniffer-maven-plugin</artifactId>
- <version>1.17</version>
- <executions>
- <execution>
- <phase>process-classes</phase>
- <goals>
- <goal>check</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <signature>
- <groupId>org.codehaus.mojo.signature</groupId>
- <artifactId>java17</artifactId>
- <version>1.0</version>
- </signature>
- </configuration>
- </plugin>
- </plugins>
- </build>
</profile>
</profiles>
</project>
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 0c84737a..c26b17fb 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -1,7 +1,11 @@
package org.bukkit;
import java.io.File;
+
+import com.google.common.base.Preconditions;
import org.bukkit.generator.ChunkGenerator;
+
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -374,7 +378,7 @@ public interface World extends PluginMessageRecipient, Metadatable {
* @param speed Speed of the arrow. A recommend speed is 0.6
* @param spread Spread of the arrow. A recommend spread is 12
* @param clazz the Entity class for the arrow
- * {@link org.bukkit.entity.SpectralArrow},{@link org.bukkit.entity.Arrow},{@link org.bukkit.entity.TippedArrow}
+ * {@link SpectralArrow},{@link Arrow},{@link TippedArrow}
* @return Arrow entity spawned as a result of this method
*/
@NotNull
@@ -480,6 +484,252 @@ public interface World extends PluginMessageRecipient, Metadatable {
@NotNull
public Collection<Entity> getEntitiesByClasses(@NotNull Class<?>... classes);
+ /**
+ * 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<LivingEntity> getNearbyLivingEntities(@NotNull Location loc, double radius) {
+ return getNearbyEntitiesByType(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<LivingEntity> getNearbyLivingEntities(@NotNull Location loc, double xzRadius, double yRadius) {
+ return getNearbyEntitiesByType(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<LivingEntity> getNearbyLivingEntities(@NotNull Location loc, double xRadius, double yRadius, double zRadius) {
+ return getNearbyEntitiesByType(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<LivingEntity> getNearbyLivingEntities(@NotNull Location loc, double radius, @Nullable Predicate<LivingEntity> predicate) {
+ return getNearbyEntitiesByType(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<LivingEntity> getNearbyLivingEntities(@NotNull Location loc, double xzRadius, double yRadius, @Nullable Predicate<LivingEntity> predicate) {
+ return getNearbyEntitiesByType(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<LivingEntity> getNearbyLivingEntities(@NotNull Location loc, double xRadius, double yRadius, double zRadius, @Nullable Predicate<LivingEntity> predicate) {
+ return 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.
+ */
+ @NotNull
+ public default Collection<Player> getNearbyPlayers(@NotNull Location loc, double radius) {
+ return 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.
+ */
+ @NotNull
+ public default Collection<Player> getNearbyPlayers(@NotNull Location loc, double xzRadius, double yRadius) {
+ return 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.
+ */
+ @NotNull
+ public default Collection<Player> getNearbyPlayers(@NotNull Location loc, double xRadius, double yRadius, double zRadius) {
+ return 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.
+ */
+ @NotNull
+ public default Collection<Player> getNearbyPlayers(@NotNull Location loc, double radius, @Nullable Predicate<Player> predicate) {
+ return 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.
+ */
+ @NotNull
+ public default Collection<Player> getNearbyPlayers(@NotNull Location loc, double xzRadius, double yRadius, @Nullable Predicate<Player> predicate) {
+ return 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.
+ */
+ @NotNull
+ public default Collection<Player> getNearbyPlayers(@NotNull Location loc, double xRadius, double yRadius, double zRadius, @Nullable Predicate<Player> predicate) {
+ return getNearbyEntitiesByType(Player.class, loc, xRadius, yRadius, zRadius, predicate);
+ }
+
+ /**
+ * Gets all nearby entities of the specified type, within the specified radius (bounding box)
+ * @param type 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.
+ */
+ @NotNull
+ public default <T extends Entity> Collection<T> getNearbyEntitiesByType(@NotNull Class<? extends T> type, @NotNull Location loc, double radius) {
+ return getNearbyEntitiesByType(type, 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 type 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.
+ */
+ @NotNull
+ public default <T extends Entity> Collection<T> getNearbyEntitiesByType(@NotNull Class<? extends T> type, @NotNull Location loc, double xzRadius, double yRadius) {
+ return getNearbyEntitiesByType(type, loc, xzRadius, yRadius, xzRadius, null);
+ }
+
+ /**
+ * Gets all nearby entities of the specified type, within the specified radius (bounding box)
+ * @param type 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.
+ */
+ @NotNull
+ public default <T extends Entity> Collection<T> getNearbyEntitiesByType(@NotNull Class<? extends T> type, @NotNull Location loc, double xRadius, double yRadius, double zRadius) {
+ return getNearbyEntitiesByType(type, loc, xRadius, yRadius, zRadius, null);
+ }
+
+ /**
+ * Gets all nearby entities of the specified type, within the specified radius (bounding box)
+ * @param type 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.
+ */
+ @NotNull
+ public default <T extends Entity> Collection<T> getNearbyEntitiesByType(@NotNull Class<? extends T> type, @NotNull Location loc, double radius, @Nullable Predicate<T> predicate) {
+ return getNearbyEntitiesByType(type, 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 type 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.
+ */
+ @NotNull
+ public default <T extends Entity> Collection<T> getNearbyEntitiesByType(@NotNull Class<? extends T> type, @NotNull Location loc, double xzRadius, double yRadius, @Nullable Predicate<T> predicate) {
+ return getNearbyEntitiesByType(type, loc, xzRadius, yRadius, xzRadius, predicate);
+ }
+
+ /**
+ * Gets all nearby entities of the specified type, within the specified radius (bounding box)
+ * @param type 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.
+ */
+ @NotNull
+ public default <T extends Entity> Collection<T> getNearbyEntitiesByType(@NotNull Class<? extends T> type, @NotNull Location loc, double xRadius, double yRadius, double zRadius, @Nullable Predicate<T> predicate) {
+ Preconditions.checkNotNull(type, "Type can not be null");
+ List<T> nearby = new ArrayList<>();
+ for (Entity bukkitEntity : getNearbyEntities(loc, xRadius, yRadius, zRadius)) {
+ //noinspection unchecked
+ if (type.isAssignableFrom(bukkitEntity.getClass()) && (predicate == null || predicate.test((T) bukkitEntity))) {
+ //noinspection unchecked
+ nearby.add((T) bukkitEntity);
+ }
+ }
+ return nearby;
+ }
+
/**
* Get a list of all players in this World
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment