How can I get the nearby entities of a location?

Discussion in 'Plugin Development' started by MrSnare, Jul 7, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    MrSnare

    I know how to get the nearby entities of a player but I dont want to do that
     
  2. Offline

    macguy8

    Ask lazer. Jks, here's a legit answer down below

    You can either spawn an entity, call .getNearbyEntities on it, and then remove it (I recommend using a falling block as this entity, as it's a bit harder to see than, say, a creeper, but it's still not the best way), or you can (if you just want players) loop through all the online players and get their distance (double distance = player.getLocation().distance(centerLocation)), and then check if the distance is less than or equal to what you need. If you want to apply this to mobs also, just loop all entities in the world (World.getEntities()) and run the same check.
     
  3. Offline

    MrSnare

    Haha funny! I think Ill check online players distance. I was going to get the chunk of the location and check the entities in the chunk to see if their distance is less than x but I would miss some nearby players if the location is on the edge of a chunk.
     
  4. Offline

    Minnymin3

    I've got a method for this:
    Code:java
    1. public Entity[] getNearbyEntities(Location l, int radius) {
    2. int chunkRadius = radius < 16 ? 1 : (radius - (radius % 16)) / 16;
    3. HashSet<Entity> radiusEntities = new HashSet<Entity>();
    4. for (int chX = 0 - chunkRadius; chX <= chunkRadius; chX++) {
    5. for (int chZ = 0 - chunkRadius; chZ <= chunkRadius; chZ++) {
    6. int x = (int) l.getX(), y = (int) l.getY(), z = (int) l.getZ();
    7. for (Entity e : new Location(l.getWorld(), x + (chX * 16), y, z
    8. + (chZ * 16)).getChunk().getEntities()) {
    9. if (e.getLocation().distance(l) <= radius
    10. && e.getLocation().getBlock() != l.getBlock()) {
    11. radiusEntities.add(e);
    12. }
    13. }
    14. }
    15. }
    16. return radiusEntities.toArray(new Entity[radiusEntities.size()]);
    17. }
     
    TheTrixsta and MrSnare like this.
  5. Offline

    macguy8

    Obviously (I realized I forgot this in my original post), square the first location and then use .distanceSquared, so it's a bit less intensive on the CPU.
     
  6. Offline

    blablubbabc

    Will this force chunks to be loaded? If I would use this near the border of loaded chunks, can this lead to chunks getting loaded but not unloaded anymore or other related problems?
     
  7. Offline

    Minnymin3

    I don't think so.
     
    blablubbabc likes this.
  8. Offline

    robbertie

    Sorry I am digging up a old thread but how did you fix it? MrSnare
     
  9. Offline

    NathanWolf

    I think you can use the method minnymin3 provided, just copy and paste it.
     
  10. Offline

    MrSnare

    Hey i did this
    Code:java
    1. public class LocationTask implements Runnable {
    2.  
    3. public double distance = 3.0;
    4.  
    5. @Override
    6. public void run() {
    7. HashMap<Location, String> blocks = tracker.getBlocks();
    8. for(Player player : Bukkit.getOnlinePlayers()){
    9. for (Map.Entry<Location, String> entry : blocks.entrySet()) {
    10. if(entry.getValue().equals("Correct String")){
    11. if(player.getLocation().distance(entry.getKey()) < distance){
    12. //you hit the jackpot baby!
    13. }
    14. }
    15. }
    16. }
    17. }
    18.  
    19. }
     
  11. Offline

    robbertie

Thread Status:
Not open for further replies.

Share This Page