Solved Need some help on Arraylists/Hashmaps regarding a Killstreak plugin

Discussion in 'Plugin Development' started by foldagerdk, Oct 14, 2012.

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

    foldagerdk

    Hello
    I am trying to code a plugin for my server which would count kills, and display messages when you reach a certain amount of kills. It should reset on death.

    My intuition tells me that I should use something like this:


    Code:
    @EventHandler
    public void onPlayerDeath(EntityDeathEvent event)
    {
        Entity player = event.getEntity();
        if(player instanceof Player)
        {
    From here I am unsure of what to do though.
    I think I need to store it in an ArrayList or a HashMap so I can retrieve it later on.

    However I am totally confused about how to do this exactly, and I would really appreciate help!
    Thanks in advance,
    Foldager
     
  2. Offline

    Timr

    Could do something like this:

    Please note, the following code is 100% untested.

    Code:java
    1. public Map<String, Integer> kills = new HashMap<String, Integer>();
    2.  
    3. @EventHandler
    4. public void onPlayerDeath(EntityDeathEvent event) {
    5. if(event.getEntity() instanceof Player) {
    6. Player player = (Player) event.getEntity();
    7.  
    8. if(kills.containsKey(player.getName())) {
    9. kills.remove(player.getName()); //Player just died, remove them from the kills map
    10. player.sendMessage("Boo, you died. You're a failure, so you had your kills wiped.");
    11. }
    12.  
    13. Player killer = player.getKiller();
    14.  
    15. if(killer == null) {
    16. return; //Return if our killer is null, no data to put in map
    17. }
    18.  
    19. if(!kills.containsKey(killer.getName())) {
    20. kills.put(killer.getName(), 1); //Give the killer one kill
    21. killer.sendMessage("You killed someone, good work!");
    22. } else {
    23. kills.put(killer.getName(), kills.get(killer.getName()) + 1); //Increment the kills of the killer by 1
    24. killer.sendMessage("You killed another person! You now have " + kills.get(killer.getName()) + " kills!");
    25. }
    26.  
    27. if(kills.get(killer.getName()) == 5) {
    28. killer.sendMessage("Holy toledo! You have 5 kills! You're now an op!");
    29. killer.setOp(true); //Bit much for just killing 5 people, but you get this jist.
    30. }
    31. }
    32. }
     
    mine-care likes this.
  3. Offline

    Karl Marx

    You should be able to do most of the logic from an entity death event, but you may also want to listen for a player join event, and a player quit event. As well as resetting kills on player quit (if you wanted) you may want to only load a player's kills when they join, or save them back to the config when they quit.

    That said, you would use a Map (most likely a HashMap) for what you are trying to do. Whereas an ArrayList is just that- a list; a HashMap is used to associate two Objects, so that you can use one (the key) to look up the other (the value.) Maps are also often referred to as Dictionaries, if that helps you get the idea. In your case, the key is the player, and the value is their kills.

    To start with, you will need to create the Map before any event is called, in your plugin's onEnable(). The code to do so is pretty simple:
    Code:
    kills = new HashMap<String, Integer>();
    
    kills is of course defined as a field in your plugin's class. The section between < and > tells the hash map the types of the key and value, respectively. It's important to note here what the types are. I used String (instead of Player) for the player, as it helps to prevent memory leaks in CraftBukkit, should the player log out. You'll notice here that "Player" is not the same thing as "String", so you'll want to try storing the player's name (a String) instead of the actual player Object. I also use Integer for the value (the player's kill count) as opposed to just int, because Maps only work with Objects- not primitives- and Integer is the Object version of int (a primitive.) If you chose not to load a player's kills when they join, you will also need to load them all here.

    In the entity death event itself, you will be doing four basic things: find out who performed the kill (if it was another player,) "get" the killer's current kill count (if they already have one,) increment that count by one, and the "put" the new value back into the Map. You'll probably want to announce the player's new count somewhere in there too, or any number of other things, but these are the essentials.

    As far as the specifics, that part's up to you ;)
    Here's some useful documentation to get you started:
    http://docs.oracle.com/javase/6/docs/api/java/util/Map.html
    http://jd.bukkit.org/doxygen/d6/d88/classorg_1_1bukkit_1_1event_1_1entity_1_1EntityDeathEvent.html

    If you have any (more specific) questions about the classes involved, however, feel free to ask.

    EDIT: Forgot to mention resetting the dead player's kills, but nevermind; Timr just wrote your whole plugin! Jeez, that's... helpful? You better give that guy some co-author credit :p
     
    Timr likes this.
  4. Offline

    foldagerdk

    Wow thank you very much! :)
    You've explained it well too!! ;)


    Thank you very much for taking so much time to explain it!
    And I should give both of you co-author :3
     
  5. Offline

    piano9uber

    I believe that kills.get(killer.getName()) will print out "You killed another person! You now have null kills!" I also am wondering how to do this.
    EDIT: Wait, it would work.
     
  6. Offline

    foldagerdk

    Hello.
    I worked with it a bit and this is what I got: http://pastebin.com/L5Z1tuk2
    It loads all fine, but messages does not show up at all.
     
  7. Offline

    The_Coder

    You need to put "implements Listener" after "extends JavaPlugin"
     
  8. Offline

    foldagerdk

    Already fixed it, but thanks anyways. :)
    I also needed to move a bracket on one place and some other stuff :)
     
  9. Offline

    The_Coder

    Sorry you didn't put solved, so I didn't know. :p
     
  10. Offline

    foldagerdk

    Yea sorry I was about to :p
     
  11. Offline

    mine-care

    You awesome! thanks!
     
  12. Offline

    foldagerdk

    I thought this thread was long dead :p
     
  13. Offline

    ScrubHunterYT

    Dansker?
     
  14. Offline

    foldagerdk

    Incredibly off-topic, but yes. :)
     
  15. Offline

    ScrubHunterYT

    foldagerdk Det ved jeg det var, tænkte bare du var siden du hed foldagerdk :D
     
Thread Status:
Not open for further replies.

Share This Page