Possible issue with player.setAttribute()

  • Hi @red51!


    Before version 0.9.2.2 I was using player.setAttribute() in an event method for PlayerConnectEvents. Something like this:


    Java
    @EventMethod(Threading.Async)
    public void onPlayerConnectEvent(PlayerConnectEvent evt)
    {
    Player player = evt.getPlayer();
    player.setAttribute("xyz", 123);
    }

    But this leads now to null pointer exception when calling player.getAttribute("xyz") later in code somewhere.
    I have to move this now to the PlayerSpawnEvent method to circumvent that. Example:

    Java
    @EventMethod(Threading.Async)
    public void onPlayerSpawnEvent(PlayerSpawnEvent evt)
    {
    Player player = evt.getPlayer();
    player.setAttribute("xyz", 123);
    }


    Can you confirm that something has changed there under the hood? :)

  • I tried it in singleplayer, but unfortunately I wasn't able to reproduce this issue =O I tried this:

    Java
    @EventMethod
    public void onSpawn(PlayerSpawnEvent evt){
    evt.getPlayer().setAttribute("level", 1337);
    }
    @EventMethod
    public void onCommand(PlayerCommandEvent evt){
    System.out.println("Attr: "+evt.getPlayer().getAttribute("level"));
    }

    When entering a command, the attribute was printed as intended ?(


    Can you maybe post your code here? :)

  • I made a plugin for reproducing this issue. :)


    The attribute "level" is set in onConnect().
    When using chat command /attr get, it prints null instead of 1337.
    But: after reloading with "rp" in console, all works fine, no uninitialised attribute!


    But when you set the attribute in onSpawn(), all works (almost?) fine.


    I append both the plugin jar and the source code to play with.
    Hopefully this can help you to track it down.


    Chat commands:
    /attr get prints the value of "level" (should be 1337 but null)
    /attr exception force an exception
    /attr print output value of "level" for each player currently online on server


    plus:
    /attr set setting "level" to 1337


    Cheers! (and please press the thumb that HSV wins today!!) :)


    P.S.: I just found out that it is random if getAttribute() returns null or the intended value.
    Eventually you have to restart the game completely. For me the second start did it.

Participate now!

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