Stats not adding up? (HashMaps)

Discussion in 'Plugin Development' started by Monkey_Swag, Jul 22, 2014.

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

    Monkey_Swag

    So basically, I am creating a plugin where I use the PlayerDeathEvent to add kills and points to the killer and remove points from the p#getEntity();

    None of these are registering for some reason, and I do not know what to do. I do not get any stacktraces either. Please note I do not work much with HashMaps. Any help is appreciated thanks.
    PlayerDeath class:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e){
    3. Player p = (Player) e.getEntity();
    4. if(p.getKiller() instanceof Player){
    5. Player killer = p.getKiller();
    6.  
    7. killer.sendMessage("§aAdded +5 points to your account!");
    8. p.sendMessage("§c-3 Points for dying!");
    9.  
    10. Main.deaths.put(p.getUniqueId(), Main.deaths.get(p.getUniqueId()) + 1);
    11. Main.points.put(p.getUniqueId(), Main.points.get(p.getUniqueId()) - 3);
    12.  
    13. Main.kills.put(killer.getUniqueId(), Main.kills.get(p.getUniqueId()) + 1);
    14. Main.points.put(killer.getUniqueId(), Main.points.get(p.getUniqueId()) + 5);
    15. }
    16. }
     
  2. Offline

    xmarinusx

    Monkey_Swag
    Do you put the players on 0 kills/deaths/points when they join or something like that? The code looks fine to me.
     
  3. Offline

    Monkey_Swag

    xmarinusx I've got a command to check all the stats and this shows up http://prntscr.com/45apxy
    I also have a class that makes it so when you first join it sets your stats to 0.
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e){
    3. Player p = e.getPlayer();
    4. if(!p.hasPlayedBefore()){
    5. Main.points.put(p.getUniqueId(), 0);
    6. Main.kills.put(p.getUniqueId(), 0);
    7. Main.deaths.put(p.getUniqueId(), 0);
    8. }
    9. }


    anyone help? <3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    ImDeJay

    hashmaps do not persist through server restarts/reloads.

    So when you put something nito a hashmap, its only there until the server reloads/restarts.

    So in your code above, your only putting the info into the hashmap if they havnt played before.
    but if your using an alt account for testing, then that account has played before. so "if(p.hasplayedbefore)" will always be true. so your code above is what is broken and why its returning null because its not setting their data.

    What you need to do is when the plugin is disabled, you need to save the data you have in the hashmap to a file or when the player leaves the server. then onEnable you need to reload that data into your hashmaps.
    Idk if what i said made sense, i was talking and typing at the same time so it might be just rambling.

    and instead of doing "if(!p.hasplayedbefore)" you should do a null check.

    when player joins, check if his/her information is present within your player files, if its not, then they havnt played before. this is when you set their stats to 0.
    If it is present you need to get that info from the file and put it into your hashmap.
     
Thread Status:
Not open for further replies.

Share This Page