Check if entity is in chunk

Discussion in 'Spigot Plugin Development' started by Leolyyn, Apr 24, 2017.

  1. Hi,
    I have the following problem. I want to check if a entity is in the chunk that contains the world spawn (where you appear when you join the server) or if it is in a chunk surrounding the spawn chunk (just the chunk next to the spawn chunk). I allready figured out how to check if it is in the spawn chunk but i have no idea how to check if it is in a chunk next to it. Anyone can help me out with this?

    Here the code i allready have:

    Code (Text):
    if(entity.getLocation().getChunk() == entity.getWorld().getSpawnLocation().getChunk()){
                                //do stuff
                            }
    I'm thankfull for any help :D.
     
  2. WAS

    WAS

    Maybe something like this will help you. I had the same question last year and was fed this function. It's served me good.

    A radius of 1 would be a 1 chunk radius check.
    Code (Java):
      public Set<Entity> getEntitiesInChunks(Location location, int radius) {
         Block b = location.getBlock();
         Set<Entity> entities = new HashSet<Entity>();
         for ( int x = -16 * radius; x <= 16 * radius; x += 16 ) {
           for ( int z = -16 * radius; z <= 16 * radius; z += 16 ) {
             for ( Entity e : b.getRelative(x, 0, z).getChunk().getEntities() ) {
               entities.add(e);
             }
           }
         }
         return entities;
       }