Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B+C] Add Location.getNearbyEntities. Adds BUKKIT-3868 #1068

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/bukkit/Location.java
@@ -1,9 +1,12 @@
package org.bukkit;

import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.util.NumberConversions;
import org.bukkit.util.Vector;

import java.util.Collection;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is in the wrong spot


/**
* Represents a 3-dimensional position in a world
*/
Expand Down Expand Up @@ -82,6 +85,19 @@ public Block getBlock() {
return world.getBlockAt(this);
}

/**
* Returns a collection of entities within a bounding box centered
* around this location
*
* @param x 1/2 the size of the box along x axis
* @param y 1/2 the size of the box along y axis
* @param z 1/2 the size of the box along z axis
* @return List<Entity> List of entities nearby
*/
public Collection<Entity> getNearbyEntities(double x, double y, double z) {
return world.getEntities(this, x, y, z);
}

/**
* Sets the x-coordinate of this location
*
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/bukkit/World.java
Expand Up @@ -429,6 +429,46 @@ public interface World extends PluginMessageRecipient, Metadatable {
*/
public Collection<Entity> getEntitiesByClasses(Class<?>... classes);

/**
* Returns a collection of entities within a bounding box centered around the
* specified location
*
* @param center the center of the bounding box
* @param x 1/2 the size of the box along x axis
* @param y 1/2 the size of the box along y axis
* @param z 1/2 the size of the box along z axis
* @return List<Entity> List of entities nearby
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This @return is incorrect for two reasons:

  1. The return type is defined
  2. The return type is wrong

*/
public Collection<Entity> getEntities(Location center, double x, double y, double z);

/**
* Get a collection of all entities in this World matching the given
* class/interface within a specific area
*
* @param center the center of the bounding box
* @param x 1/2 the size of the box along x axis
* @param y 1/2 the size of the box along y axis
* @param z 1/2 the size of the box along z axis
* @param cls The class representing the type of entity to match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cls is a badly named variable. Something like type or clazz would be better.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clazz is used in other places in the API, so I might suggest that has a slight argument in its favor

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually took this as a suggestion, straight from the existing code:

public Collection getEntitiesByClass(Class cls);

I'll make it clazz, though. I agree that "cls" is bad, but there was a precedent, so I went with it.

* @return A List of all Entities currently residing in this world that
* match the given class/interface within the given area
*/
public <T extends Entity> Collection<T> getEntitiesByClass(Location center, double x, double y, double z, Class<T> cls);

/**
* Get a collection of all entities in this World matching any of the
* given classes/interfaces within a specific area
*
* @param center the center of the bounding box
* @param x 1/2 the size of the box along x axis
* @param y 1/2 the size of the box along y axis
* @param z 1/2 the size of the box along z axis
* @param classes The classes representing the types of entity to match
* @return A List of all Entities currently residing in this world that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This @return is incorrect: the mentioned return type is incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha- again, this is all copy+paste .. I mean that's a poor excuse, but I tried to make these look like the existing getEntities methods.

That said, I'm changing all of mine to something like "A Collection of entities nearby", and leaving the old ones alone, e.g.:

     * @return A List of all Entities currently residing in this world that
     *     match the given class/interface
     */
    public <T extends Entity> Collection<T> getEntitiesByClass(Class<T> cls);

* match one or more of the given classes/interfaces in the area
*/
public Collection<Entity> getEntitiesByClasses(Location center, double x, double y, double z, Class<?>... classes);

/**
* Get a list of all players in this World
*
Expand Down