BUG ChunkUtils.getBlockPositionX/Z/Y give wrong result for negative values

The next update will be available on Wednesday, December 18, in the early evening (GMT+1).

This update will not yet replace the Java version, instead it is the actual content update. We'll provide more information about the transition together with the update.
  • 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:

    Code
    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):





    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 :



    after i look at this code i don't understand why is condition if coordinate are lower than zero:


    Java
    if (x - (float) ((int) x) == 0.0F) {
    x += 0.5F;
    }


    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

  • Thanks for letting me know! Unfortunately the block position calculation in the API is indeed wrong... apparently the calculation was already wrong back then in the old Plugin API for the Java version (and we just copied the Utils class from the old API) :thinking: The weird thing is, the native game implementation (and even the implementation the Java version was using) is correct, so it looks like this error was solely introduced in the Plugin API for the Java version back then (and ported to the new API accordingly).


    Anyway, we will fix this with the next update :)


  • Well this explains the problem I was getting with my trails plugin back in the java version lol I knew something was up with that then and reported to you red51

    Yahgiggle Steam Signature, real name Deon Hamilton :thumbsup: Server @ ip 139.99.136.76:4255 Name (The Federation of territory) Unity :thumbsup:

    If at first your code does not work call it version 0.1 with the odd bug :thinking:

    My Own Website




    31245-logo-png

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!