The story
In my plugin i haves need to calculate block position in chunk to use latter with chunk.getLODSurfaceLevel(bx,bz);
For some values with negative values, i get error:
java.lang.IndexOutOfBoundsException: Invalid x or z index (x: 32, z: 32)!
at net.risingworld.api.objects.world.Chunk.getLODSurfaceLevel(Chunk.java:144)
This is legit error because block position can have value between 0 and 31 and function called with 32!
But block position are calculated with methods from ChunkUtils.getBlockPositionX / Z / Y !
So i suspect here is something wrong.
Research
One of values who game me error are (X,Z): -32,-32
I go in game, activate debug view (F3) and noted some coordinates for reference:
goto -31.9999 95 -31.9999
- in game (via F3): Chunk -1 1 -1, Block 0 31 0
goto -32 95 -32
- in game (via F3): Chunk -2 1 -2, Block 31 31 31
goto -32.0001 95 -32.001
- in game (via F3): Chunk -2 1 -2, Block 31 31 31
After i go back to coding i wrote and run a method in plugin to get same data as reference (log.debug is a custom method who write data to a file):
log.debug("Global postion (X,Z) ={}, {} ==> Chunk Postion (X,Z) ={}, {} | Block Position (X,Z) ={}, {}",
ChunkUtils.getChunkPositionX(x),ChunkUtils.getChunkPositionZ(z),
ChunkUtils.getBlockPositionX(x, ChunkUtils.getChunkPositionX(x)),ChunkUtils.getBlockPositionZ(z, ChunkUtils.getChunkPositionZ(z))
log.debug("Global postion (X,Z) ={}, {} ==> Chunk Postion (X,Z) ={}, {} | Block Position (X,Z) ={}, {}",
ChunkUtils.getChunkPositionX(x),ChunkUtils.getChunkPositionZ(z),
ChunkUtils.getBlockPositionX(x, ChunkUtils.getChunkPositionX(x)),ChunkUtils.getBlockPositionZ(z, ChunkUtils.getChunkPositionZ(z))
log.debug("Global position (X,Z) ={}, {} ==> Chunk Position (X,Z) ={}, {} | Block Position (X,Z) ={}, {}",
ChunkUtils.getChunkPositionX(x),ChunkUtils.getChunkPositionZ(z),
ChunkUtils.getBlockPositionX(x, ChunkUtils.getChunkPositionX(x)),ChunkUtils.getBlockPositionZ(z, ChunkUtils.getChunkPositionZ(z))
Display More
And result!
Global position (X,Z) =-31.999, -31.999 ==> Chunk Position (X,Z) =-1, -1 | Block Position (X,Z) =0, 0
Global position (X,Z) =-32.000, -32.000 ==> Chunk Position (X,Z) =-2, -2 | Block Position (X,Z) =32, 32
Global poistion (X,Z) =-32.001, -32.001 ==> Chunk Position (X,Z) =-2, -2 | Block Position (X,Z) =31, 31
So for -32,-32 getBlockPositionX/Z give wrong result 32!
Further research
So culprit is getBlockPositionX/Z/Y
I opened class in my Eclipse and i see the following code :
public static int getBlockPositionX(float x, int chunkPositionX) {
return (int) x - chunkPositionX * CHUNK_SIZE_X;
if (x - (float) ((int) x) == 0.0F) {
return (int) x - chunkPositionX * CHUNK_SIZE_X - 1;
Display More
after i look at this code i don't understand why is condition if coordinate are lower than zero:
if (x - (float) ((int) x) == 0.0F) {
Because that for every negative coordinate who is integer number ( like -1.000, -2.000, etc.) block position is calculated wrong because added 0.5f and for this reason i get value 32 at boundary of chunk.
So i think a easy fix is to not add 0.5f for negative int values.
But maybe i miss something..
Thx red51