Player.hasAttribute() issue?

  • I am having issues when using Player.hasAttribute() early in the plug-in execution. For instance, this simplified example plug-in calls that method in several player event handlers:



    This generates the following exception in the onPlayerConnect() when a player logs into the server:


    Code
    java.lang.NullPointerException
    at pluginapi.objects.PlayerAPI.hasAttribute(SourceFile:194)
    at com.vistamaresoft.test.Test.onPlayerConnect(Test.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at pluginapi.PluginEventHandler.triggerEvent(SourceFile:169)
    at z.l.execute(SourceFile:93)
    at v.b.run(SourceFile:24)


    This exception is apparently caught somewhere in the call chain, as it does not crash the server.


    In this sample plug-in, the same Player.hasAttribute() method called in other event handlers does not raise any exception.


    HOWEVER, in other plug-ins with more complex (and presumably lengthy) player initialisation (on connect or on spawn), I get an exception in the PlayerChangePositionEvent handler, which is apparently called before the PlayerSpawnEvent handler. This exception DOES crash the server.


    Uses of that method in or after the PlayerSpawnEvent handler has been called seem to work correctly.


    In all these cases, if I replace the offending call to Player.hasAttribute() with if (player.getAttribute(key_testAttr) == null) (semantically equivalent) no exception is raised.


    This may be a work-around, but it seems like something strange is happening.

  • Oh... that's indeed an issue. The internal attributes map only gets created once a plugin sets an attribute, unfortunately the hasAttribute() method doesn't take this into account, so calling this method before any plugin has created at least a single attribute for this plugin raises an exception X/


    Another workaround (instead of checking getAttribute() for null) would be to set a dummy attribute when the player connects or spawns.


    I'm sorry for this issue, it will be fixed with the next update!

Participate now!

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