Solved Dynamic int

Discussion in 'Spigot Plugin Development' started by Stellrow, Aug 12, 2018.

  1. So after doing some stuff i ended up at the change lore
    So in the lore i set to show the lvl,xp but the xpfornextlevel is just a bit hard for my brain to handle

    Im trying to set the int variable of nlvl(nextlvl) by checking each level
    Code (Text):
    if(plugin.getLVL(uuid) == 2) {
                    int nlvl = 500;
                }else
                if(plugin.getLVL(uuid) == 1) {
                    int nlvl = 500;
                }else {
                    int nlvl = 300;
                }
    Something like this but the variable is not found by the string
    Code (Text):
    String lp9 = ChatColor.GRAY + "Experienta " + ChatColor.GREEN + plugin.getXP(uuid) + ChatColor.GRAY + "/" + ChatColor.RED + nlvl;
    How can i make the variable dynamic?
     
  2. There are no dynamic variables in Java unless you use reflection to imitate it. Otherwise use a Map to map the player uuid to a level.
     
  3. Initialize the variable in a higher scope, then change the variable.
    Code (Java):
    int nlvl;

    if (plugin.getLVL(uuid) == 2) {
        nlvl = 500;
    } else {
        if (plugin.getLVL(uuid) == 1) {
            nlvl = 500;
        } else {
            nlvl = 500;
        }
    }

    // Set String here
    I would suggest a basic Java course is in place though, this is very basic Java...
     
  4. md_5

    Administrator Developer

    You need to learn java....

    int nlvl;

    Needs to be declared outside the if statements.

    variables are not visible past the closing } in the block they were declared.
     
    • Agree Agree x 2
  5. I created methods that set the nextLVL value of every player, so at the int i just say get the value of NextLVL so this should solve the problem