Solved How does the chunk .getX and .getZ work?

Discussion in 'Spigot Plugin Development' started by CodingCyClone, Jun 27, 2021.

  1. Code (Text):
    Chunk startingChunk = p.getWorld().getChunkAt(p.getLocation());

    World world = startingChunk.getWorld();
    Chunk chunk = world.getChunkAt(xCombined, zCombined);
    Here is what I dont get when I call startingChunk.getX and .getZ it returns the chunk coordinates but when I call chunk.getX and .getZ it returns the block coordinates. My question is how can I get chunk.getX to return the x coordinate of the chunk?
  2. getChunkAt(location).getX();
  3. Wait how would that work because thats what I already have. I am providing the location in the form of x and z coordinates and then I call chunk.getX(). So how can I get the actual coordinates of the chunk because right now chunk.getX just returns xCombined which is useless since I need the coordinates of the chunk...
  4. Then try location.getChunk().getX();
    • Agree Agree x 1
  5. It is just the block position with the x and z component shifted by 4.
  6. There is no location… please look at what I have because saying that gets me no closer to a solution
  7. So I should just use bitwise and shift them instead? I don’t really see why calling chunk.getX() is broken but if I have to do it manually I could. Is it a right or left shift?
  8. (Other post of me just deleted as it was wrong)

    So you claim that Chunk.getX() and Chunk.getZ() are not returning chunk coordinates, but block coordinates.

    Please provide some code which can show this problem where you output all relevant information to the console alongside the console output.

    I highly doubt that there is an error in that code, it is more likely some problem in your code.
  9. This was my initial question but I did not get any answers on it and since I figured out the issue I made a more condensed version.
  10. Are you passing Block or Chunk coordinates to world.getChunkAt()? The method takes Chunk coordinates. If you want to get the chunk from the Block coordinates you need to call world.getChunkAt(x >> 4, z >> 4)
    • Like Like x 1
  11. Ok thank you but is there a reason one works and the other does not because both pass in real world coordinates. Also do I even need all that chunk stuff can’t I just create two long variables with the combined value shifted by the bitwise operator?
  12. The other two getChunkAt Methods do this bit shift conversion:

    Code (Java):

    // From org.bukkit.craftbukkit.v1_12_R1.CraftWorld (didn't have a 1.8 spigot jar, but that does not matter, because this is implementation of Bukkit API which has existed for many years)

    public Chunk getChunkAt(int x, int z) {
        return, z).bukkitChunk;

    public Chunk getChunkAt(Block block) {
        return this.getChunkAt(block.getX() >> 4, block.getZ() >> 4);

    public Chunk getChunkAt(Location location) {
        return this.getChunkAt(location.getBlockX() >> 4, location.getBlockZ() >> 4);

    Depends on your usecase what exactly you need, also what exactly do you mean with "two long variables" and "combined value"?
  13. Code (Text):
    long xCombined = (baseX + x) >> 4;
    long zCombined = (baseZ + z) >> 4;
    long chunkID = xCombined & 0xffffffffL | ( zCombined & 0xffffffffL) << 32;
  14. For that usecase alone, this will work. The ChunkCoordIntPair class has this built-in:

    Code (Java):

    // net.minecraft.server.v1_8_R3.ChunkCoordIntPair

    public static long a(int var0, int var1) {
        return (long)var0 & 0xffffffffL | ((long)var1 & 0xffffffffL) << 32;
  15. Yes but like I said initially it did not work which is why I apparently have to shift them manually. Anyways I tested the code I have and all 50 chunks were correctly added!
  16. Minor nitpick - one of the masks is unnecessary, this will work the same:
    Code (Java):
    long chunkId = ((long) zCombined << 32) | (xCombined & 0xFFFFFFFFL);