Solved Player Only Ints

Discussion in 'Plugin Development' started by BrickBoy55, Dec 16, 2014.

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

    BrickBoy55

    Hi, I'm trying to add a feature to my plugin where it keeps track of your kill-streak. My only problem is I can't figure out how to make a player only int. Any help is greatly appreciated! :)
     
  2. Offline

    NovaGamingBrian

    Save stuff in a configuration file

    onPlayerDeath
    add a kill to the player its killstreak
    save the kill into a config/mysql database
    profit ?
     
  3. @BrickBoy55 There's many ways to do that:
    1) Config - using Bukkit's ConfigurationAPI to create a custom config to save the data - saves on reload/restart
    2) HashMap - storing the player's UUID and kill streak - doesn't save on reload/restart
    3) Scoreboard system

    Don't save to a mysql database, unless you plan on storing a LOT of data or require querying the database (finding top players/comparing kill streaks between other players), otherwise it would be inefficient compared to a config file.

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

    BrickBoy55

    I would perfer to do a HashMap but I'm not sure how it would work. Can you please explain it?

    Would this work?
    Code:
    public HashMap<UUID, Player> killstreak = new HashMap<UUID, Player>();
    
    But how would I add kill streaks?

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

    Ivan

    Code:java
    1.  
    2. HashMap<UUID, Integer> playerKills = new HashMap<UUID, Integer>();
    3.  
    4. //to set kills:
    5. playerKills.put(uuid,kills);
    6.  
    7. //to get kills:
    8. playerKills.get(uuid);
     
  6. Offline

    Skionz

    @BrickBoy55 That wouldn't work. Change the generics to either Player or UUID and Integer.
     
  7. Offline

    mythbusterma

    @BrickBoy55

    Why are you associating UUIDs to Players? That doesn't make much sense unless you want to find Players by their UUIDs. Do you understand what a Map is? Go look it up.
     
  8. Offline

    BrickBoy55

    I think I might have it now:
    I define it with:
    Code:
    public HashMap<Player, Integer> killstreak = new HashMap<Player, Integer>();
    
    Then to add to the killstreak:
    Code:
    killstreak.put(player, killstreak.get(killstreak) + 1);
    
    Am I correct or not?
     
  9. Offline

    mythbusterma

    @BrickBoy55

    You assume Player is in the Map with that code, you're going to get an NPE at some point. Make sure you handle that.
     
  10. Offline

    uksspy

    If you were to use that method use UUIDs as the keys as opposed to Players.
     
  11. Offline

    BrickBoy55

    @mythbusterma
    NPE?

    Would it still give me an 'NPE' if I make it a UUID?
     
  12. Offline

    Ivan

    No it wont. You shouldn't be storing Player objects in collections, unless if it's a weak link collection.
     
  13. Offline

    mythbusterma

    @BrickBoy55

    .....you understand you need a rudimentary understanding of Java before you try to create a plugin, right?
     
  14. Offline

    BrickBoy55

    Yes but I never used a HashMap before AND I don't know what a NPE is.
     
  15. Offline

    Ivan

    NullPointerException, which you would've known if you learnt the basics of Java :p
     
  16. Offline

    mythbusterma

    Yes, it would still NPE, and I don't see why you shouldn't store the Player in a normal HashMap, as long as you clean up after yourself.


    @BrickBoy55

    NullPointerException. HashMap is a simple associative Map, and you should be able to find any information you need about it on the JavaDocs.
     
  17. Offline

    Ivan

    Well yeah but in that case you should be monitoring the PlayerDisconnect, PlayerDeath and PlayerRespawn events :p. Also it has been stated countless times that it causes memory leaks (unless you clean up like you said), and it is included in the Allmighty Mistake List, although you have a point.
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page