Chunk Locations

I’m working with chunk locations and the chunk grid, but I’m having some trouble with a few methods. Basically what I want to do is take a world location and:

  1. Find the chunk that it is in
  2. Find the chunk location in that chunk that corresponds to it

My current code is as follows:

public JLSCCompound getData(Location<World> location) {
    Chunk chunk = location.getExtent().getChunk(location.getBlockPosition()).get();
    Location<Chunk> chunkLocation = chunk.getLocation(location.getPosition());
    return this.getData(location.getExtent(), location, chunkLocation);
}

The other getData method being called accepts a world, a location in that world, and a chunk location.

Any help would be appreciated.

For number 1, there’s already a getChunk(Vector3d) in World.

For number 2, you can use location.sub(chunk.getBlockMin()). However, note that methods in Chunk use the absolute location - the location as seen by the world.

@JBYoshi
Thanks!
And for my purposes, I do need the location relative to the chunk, not the world.

Edit:
So this should work?

public JLSCCompound getData(Location<World> location) {
        Chunk chunk = location.getExtent().getChunk(location.getBlockPosition()).get();
        Location<Chunk> chunkLocation = chunk.getLocation(location.getPosition()).sub(chunk.getBlockMin().toDouble());
        return this.getData(location.getExtent(), location, chunkLocation);
    }

(also, the method appears to be getChunk(Vector3i), not Vector3d…

Take a look at ChunkLayout.

@DDoS
And that will translate coordiantes from world to 1625616? If so, it’s the perfect solution!