getConfig().getInt() doesnt work?

Discussion in 'Plugin Development' started by Ganga, Jun 11, 2014.

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

    Ganga

    Hello Guys!
    I make a plugin for my own server and yesterday I started to try out the Config-System with getConfig()....
    My problem is, that I can write in the Config.yml but I cant read the integers when I use them later.
    For example I wanted to set the Integer -1 on player death, but it doesnt work.
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event) {
    3. if(event.getEntity() instanceof Player){
    4. Player p = event.getEntity().getPlayer();
    5. this.getConfig().set("Config.players.playerlifes."+p.getName(), this.getConfig().getInt("Config.players.playerlifes."+p.getName()) - 1); // DOESNT WORK!
    6. saveConfig();
    7.  
    8. }
    9. }
    10.  


    Set integers works:

    Code:java
    1. if(command.getName().equalsIgnoreCase("something")){ //command for ops
    2. if(p != null){
    3. if(p.isOp()){
    4. getConfig().set("Config.players.playerlifes."+p.getName(), 3); // 3 lifes
    5. getConfig().set("Config.players.playerteam."+p.getName(), 0); //set the team
    6. p.setDisplayName("Frozenplayer");
    7. p.sendMessage("Finished");
    8. }
    9. }
    10. }
    11.  
    12.  
    13. }


    What am I doing wrong? :'(
    Thank you for a reply.
     
  2. Try getting the value you first, doing the subtraction, then setting the value.
     
  3. Offline

    Ganga

    Didnt work, but thanks ;)
    Maybe its something with the getEntity? :confused:
     
  4. Offline

    Ganga

    I guess I can help myself now:

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event) {
    3. if(event.getEntity() instanceof Player){
    4. reloadConfig();
    5. Player p = event.getEntity().getPlayer();
    6. int playerlife = getConfig().getInt("Config.players.playerlifes." + p.getName());
    7. playerlife --;
    8. this.getConfig().set("Config.players.playerlifes."+p.getName(), playerlife );
    9. saveConfig();
    10. }
    11. }
    12.  
     
  5. Offline

    1Rogue

    I wouldn't use configs for this, you should have your own class that manages this.
     
  6. Offline

    Ganga

    @1Rogue
    But when I restart the server or something like this. I wanted to have my playerlifes saved. :)
     
  7. Offline

    Skionz

    Ganga likes this.
  8. Offline

    87pen

    Code:
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            if(event.getEntity() instanceof Player){
                Player p = (Player)event.getEntity();
                int PlayerLives = getConfig().getInt(p.getName() + ".Lives");
                getConfig().set(p.getName() + ".Lives", PlayerLives - 1);
                saveConfig();
            }
        }
    
    This should work.
     
    Ganga likes this.
  9. Offline

    Ganga

    @87pen
    dude thats pretty much the same as I got :D
     
    87pen likes this.
  10. Offline

    1Rogue

    So just have your manager save the info to a config file when you shut down, and load it when you start back up! It'll be faster, too.
     
    Ganga likes this.
  11. Offline

    87pen

    Mine works tho :/ or at least should.
     
    Ganga likes this.
Thread Status:
Not open for further replies.

Share This Page