Get the chunk the player is in

Hi there. I’m making a plugin and I need to know how to find out the X, Y, and Z of the chunk the player is standing in. Has this even been implemented into the API?

public boolean onCommand(CommandSource source, String arguments, List<String> parents) throws CommandException {
    if(!(source instanceof Player)) {
        return false;
    }
    Player player = (Player) sender;
    int chunkX, chunkY, chunkZ;
    chunkX = //Get the X of the chunk that the player is standing in.
    chunkY = //and so on
    chunkZ = //...
    return true;
}

Heave you read the code?

2 Likes

I took a brief look at the code FerusGrim pointed to, and I believe this information could be found out by dividing the player’s getBlockX() and getBlockZ() functions by 16 and then flooring the result. I am not 100% sure, but that is how I would do it.

See

There is some discussion on possibly changing the extent hierarchy to be more useful

1 Like

Would this “BiomePosition” be something like chunks?

int chunkX = location.getBiomePosition().getX();
int chunkY = location.getBiomePosition().getY();

getBiomePosition() is a Vector2i and don’t use Z

No, biome position is the same as block position except there is no y dimension, as biomes are on a 2D x,z plane.

And please note your reply is 7 months after this thread was created, create a new thread or talk on IRC in future.

Right. This is the way I’m doing it

Intatable<Chunk> chunks = Game.getServer().getLoadedChunks();
for(Chunk chunk : chunks){
for(Entity entity : chunk.getEntities()){
if(entity.equals(player)){
return chunk;
} 
} 
} 

I’m currently on my phone. When I get on my real computer I’ll neaten up the code

@MoseMister That is a really bad way to grab a chunk an entity is in. You should be passing the player’s block position to one of the following World methods

so something like

int chunkX = player.getLocation().getBlockPosition().getX() >> 4;
int chunkZ = player.getLocation().getBlockPosition().getZ() >> 4;
Optional<Chunk> chunk = world.getChunk(chunkX, 0, chunkZ);
1 Like