Hashmap update errors

Discussion in 'Plugin Development' started by plasticono, Aug 8, 2016.

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

    plasticono

    So basically I want to make a ranking system based on how many kills you have. Ive tried using switch() methods and is just hasn't updated. Here's my code
    Code:
    @EventHandler
      public void onDeath3456(PlayerDeathEvent e){
          Player p = e.getEntity().getKiller();
          if(plugin.getConfig().getInt("stats." + p.getName() + ".kills") < 5){
          plugin.rank.put(p.getName(), Rank.BASIC);
        if(plugin.getConfig().getInt("stats." + p.getName() + ".kills") >= 5){
              plugin.rank.put(p.getName(), Rank.ROOKIE);
         }else if(plugin.getConfig().getInt("stats." + p.getName() + ".kills") >= 16){
          plugin.rank.put(p.getName(), Rank.AMATUER);
          plugin.getConfig().set("stats." + p.getName() + ".rank", plugin.rank.get(p.getName()));
         }
          }
      }
    Please help! Thanks
     
  2. Offline

    Zombie_Striker

    Please don't tell me you have over three thousand other events. Method names should not have any numbers.
    A) Killer is not always a player. do instanceof checks before creating this instance
    B) Killer can be null. Make sure it is not null before you use it.
    You forgot the "}else". Currently, it is testing if the number is both greater than AND less than 5. I don't think you will ever receive a Schrodinger Integer.
    A) The second if statement (the one with the else) will never be true. Since that is only called if the first value is false, the number would both have to be greater than 16 and less than 5, which does not many and sense.
    B) Encapsulation. You are only setting the config when it is over 16 (which will never be the case). You will need to make sure it sets the config no matter which rank the player is.
    C) You are forgetting to save the config. Save the config by adding "saveConfig()" once you set the value.
     
  3. Offline

    plasticono

    @Zombie_Striker Thanks! and no I just have like 5 ondeath so i just wrote some numbers :D
     
  4. Offline

    Zombie_Striker

    @plasticono
    You should only have one of each event for your entire plugin. You should merge all your events.
     
  5. Offline

    plasticono

    @Zombie_Striker This still isn't working is there a better way to do this?
     
  6. Offline

    Zombie_Striker

  7. Offline

    plasticono

    @Zombie_Striker
    Code:
     @EventHandler
      public void onDeathStats(PlayerDeathEvent e){
          if(e.getEntity().getKiller() instanceof Player){
              int kills = plugin.getConfig().getInt("stats." + e.getEntity().getKiller().getName() + ".kills");
             
          Player p = e.getEntity().getKiller();
          if(kills < 4){
          plugin.level.put(p.getName(), Rank.BASIC);
          plugin.getConfig().set("stats."  + p.getName()  + ".level", Rank.BASIC.toString());
          }else if(plugin.getConfig().getInt("stats." + p.getName() + ".kills") >4 && kills < 10){
              plugin.getConfig().set("stats."  + p.getName()  + ".level", Rank.ROOKIE.toString());
              plugin.level.put(p.getName(), Rank.ROOKIE);
          }else if(plugin.getConfig().getInt("stats." + p.getName() + ".kills") >9 && kills < 50){
          plugin.level.put(p.getName(), Rank.AMATUER);
          plugin.getConfig().set("stats."  + p.getName()  + ".level", Rank.AMATUER.toString());
          }else if(kills >49 && kills < 150){
              plugin.level.put(p.getName(), Rank.SEMI_PRO);
             plugin.getConfig().set("stats."  + p.getName()  + ".level", Rank.SEMI_PRO.toString());
    //      plugin.getConfig().set("stats." + p.getName() + ".rank", plugin.rank.get(p.getName()));
          plugin.saveConfig();
         }
          plugin.saveConfig();
        
          }
      }
    Bump!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 13, 2016
Thread Status:
Not open for further replies.

Share This Page