Solved Config int problem

Discussion in 'Plugin Development' started by Mr. Sandwich, May 16, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Mr. Sandwich

    Code:
                      for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                            Integer xpCap = getConfig().getInt("PlayerData.XPLevelCap." + player.getName());
                                if (player.getLevel() > xpCap*2) {
                                    player.setLevel(xpCap*2);
                                }
                      }
    
    Hey so I am trying to add an int in the config so if a player's xp level is more than 2 times higher than the xp level cap thats in the config then it will set him back to 2 times higher than the xp cap(so he wont be able to go above it) but it doesnt work and the xpCap is 0 for some reason(even though in the config its 20)
    Please help
     
  2. Offline

    WinX64

    It all depends where you're putting this piece of code.
    Also, how did you confirm that the xp cap on the server is really set to 0?
    Are you sure that the path exists inside the config? If it doesn't, the returned value in your code will default to 0.
     
  3. Offline

    Mr. Sandwich

    I did player.sendMessage(xpCap + ""); and it told me its 0 and this is the full code:
    Code:
        public void onEnable() {
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                    public void run() {
                        for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                            Integer xpCap = getConfig().getInt("PlayerData.XPLevelCap." + player.getName());
                                if (player.getLevel() > xpCap*2) {
                                    player.setLevel(xpCap*2);
                                }
                        }
                    }
                }, 0, 10);
        }
    
    Also yes I am sure that this path exits in the config
    Code:
    PlayerData:
      XPLevelCap:
        _TheDeadpool_: '20'
    
     
  4. Offline

    WinX64

    What are you using to save the data back to the config?
    It is saved as a string and not an int.
     
  5. Offline

    Mr. Sandwich

    Code:
                                              getConfig().set("PlayerData.XPLevelCap." + player.getName(), args[2]);
                                              saveConfig();
    
    then how do I save it as an int?
     
  6. Offline

    WinX64

    I'm not sure if it will automatically try to convert it back to an int for you. Try to save the value as an int.

    Edit: Convert it to an int then save.
     
    Last edited: May 16, 2016
  7. Offline

    Mr. Sandwich

    Thanks it works now!
     
Thread Status:
Not open for further replies.

Share This Page