Attribute help? very new.

  • So to start off, i'll let you know for starters that i'm very new to java, as well as a few programming concepts.


    so I'm working on a project, and trying to use player.setAttribute, and player.getAttribute. from what i can tell these can save a value to a hashmap, and pull said value from said hashmap.


    i'm having trouble with .getAttribute.


    sample code for what i'm doing


    so and so odd thing is done, and saves something to a players Attribute.


    if(blah == 1){ player.setAttribute(testattribute, 1);
    }


    this part seems to "work"


    however i'm having problems pulling the Attribute with getAttribute


    Object test = player.getAttribute(testattribute);


    i get the error "testattribute cannot be resolved to a variable"


    i'm 95% sure i'm doing something wrong haha. if anyone could help point me in the right direction i'd love it! :):):)

  • First of all the variable testattribute needs to exist. So, somewhere before using it, it should be defined.


    Second, the prototype of the method in the API is: getAttribute(java.lang.String key); so the testattribute variable HAS to be a String; in summary, somewhere you have to put a statement like:

    Code
    String testattribute = "SomeFancyAttributeName";

    Third: when you set the attribute, you set it to an int; when you retrieve it, you need to cast it back to an int:

    Code
    int test = (int) player.getAttribute(testattribute);

    or maybe to Integer (I am quoting by memory, so I cannot test it):

    Code
    int test = (Integer) player.getAttribute(testattribute);

    Try these changes and tell us how is going.

  • still not 100% sure if my usage is correct, but i did realise i wasn't using " " around the variable. once i did the errors went away. so maybe fixed haha.


    also thank you Miwarre!, that helps.

  • As Player.getAttribute() and Player.setAttribute() expect a String, providing them with a String fixes the compiler complains. This alone will not fix the run issue of firstly setting the attribute to an int and then retrieving it as a generic Object.


    The compiler does not complains about the statement


    Code
    Object test = player.getAttribute(testattribute);

    as it is syntactically correct, but you will be able to do almost nothing with the test Object you get back. Depending on what you are going to use it for, you will need to cast to it to something else, most likely (unless you are trying something very fancy!) to the same type you initially set it to, i.e. an int.

  • understood. its been somewhat of a learning experience here working with hashmaps. a good learning experience though!


    I'm very excited for the api to release. I'm working on making a "bag" that you can open via a command, it will be tied to an existing world chest (not sure on some specifics of this yet), or it will be stored via an sql database. whichever ends up being the laziest way haha. so a player types said command, and an inventory pops up as if you've opened a chest. (even if i can't quite do it with the api as-is, its a good learning platform).

Participate now!

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