Questions on Chunk getX() and getZ()

Discussion in 'Spigot Plugin Development' started by TheDigitalDev, May 10, 2017.

  1. Hey! I'm sorta new to spigot, so I have a question on the getX() and getZ() functions of Chunk. It seems to only repeat the getChunkAt/Location coordinates that you provide. I feel like i'm missing something here... thanks!.
  2. md_5

    Administrator Developer

    What do you expect it to do?
    Sometimes you might have just the chunk and want to know where it came from.
  3. Oh that makes sense then, I was hoping for a corner coordinate or something, I guess i'll have to figure out how to do that. Thanks for the response!
  4. md_5

    Administrator Developer

    Chunks are 16 blocks wide, so just multiply x / z by 16
  5. Though if you want to get the corner block (from a location), it'd be faster to modulo:

    Code (Java):
    int xCorner = loc.getX() - (loc.getX() % 16)
  6. Oh I just made a method with a for loop that added x until it made a number divisible by 16, I guess you're method is way more effecient .-. thanks
  7. If you have chunk coords and you want block coords just do

    Code (Text):
    int blockX = chunkX << 4;
    int blockZ = chunkZ << 4;
    This will effectively give you the northwest block's world coordinate (minus the y/height coordinate).

    Just reverse the bitshift signs for the opposite (block coords to chunk coords).

    Code (Text):
    int chunkX = blockX >> 4;
    int chunkZ = blockZ >> 4;
    Bitshifting like this is arguably faster than multiplying/dividing by 16. Especially in a heavy/repeating setting.
    • Winner Winner x 1
  8. You know for literally writing this an hour ago:
    Code (Java):
    long val = Long.reverse((((long) 0x48454c4c) << 8 | 0x4f) << 8 | 0b1010) >>> 0x40 - (8 * 6);
    while (val > 0) { System.out.print((char) (Long.reverse(val & 0xFF) >>> 0x38)); val >>>= 8; }
    You'd think I'd have suggested that too. Billy's idea is better here.