How to add a kill timer?

Discussion in 'Plugin Development' started by iFamasssxD, Jun 23, 2013.

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

    iFamasssxD

    I was wondering how I would make is so when a player kills another player 3 times it starts a timer and I can send them the message "You need to wait another (time) minutes!".

    I've used hashmaps and that only allows me to set it after the first kill and the repeating server task isnt what I need because I need it to be for each individual player. I also did cooldowns but there was no way for me to log how much time was left. I only need the basics of it and the rest im sure I can figure out!

    Thanks in advanced!
     
  2. Offline

    G4meM0ment

    I don't understand completly what you're trying to do. But you could save every players kill value in a hasmap with an int value and check how it this value is.
    And a time could be checked by a while loop or via damagelistener (if you want to prevent damage, while the timer is active) and use System.currentTimeMillis.
    I always prefer file saving for that, it allows you to continue timers after restart.
     
  3. I think I understood what you want to do. I have a kind of solution, not tried, telle me :

    First you get to create an object, called playerKills :
    Code:java
    1. package yourPlugin.yourPackage;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.entity.Player;
    6.  
    7. public class PlayerKills {
    8. Player killer;
    9. HashMap<Player, Integer> playersKilled = new HashMap<Player, Integer>();
    10.  
    11. public Player getKiller() {
    12. return killer;
    13. }
    14.  
    15. public void setKiller(Player killer) {
    16. this.killer = killer;
    17. }
    18.  
    19. public HashMap<Player, Integer> getPlayersKilled() {
    20. return playersKilled;
    21. }
    22.  
    23. }

    Here we have an object containing the killer, and all players he killed, with the number of kills.

    Then, in your main class you create a HashMap containing all players and the new object :
    Code:java
    1. HashMap<Player, PlayerKills> kills = new HashMap<Player, PlayerKills>();

    First you have to register all players connecting by typing :
    Code:java
    1. PlayerKills pk = new PlayerKills();
    2. pk.setPlayer(player);
    3. kills.put(player, pk);

    Then, when a player kill another one you add this kill by doing :
    Code:java
    1. if(kills.get(player).getPlayersKilled().contains(playerKilled)){
    2. kills.get(player).getPlayersKilled().put(playerKilled, kills.get(player).getPlayersKilled().get(playerKilled) + 1);
    3. } else {
    4. kills.get(player).getPlayersKilled().put(playerKilled, 1);
    5. }

    Finally, to check if the player killed the playerKilled 3 times do on the EntityDeathEvent, once checked if the entity is a player and if it has been killed by a player :
    Code:java
    1. if(kills.get(player).getPlayersKilled().contains(playerKilled)){
    2. if(kills.get(player).getPlayersKilled().get(playerKilled) == 3)){
    3. //yourstuff
    4. }
    5. }

    To do the timer, you have to run a task, and in this task, set a boolean to false to set the player unkillable by the other, and to true when the task is finished.

    I hope I've been understandable, if not, don't worry to ask me.

    Have a nice day,

    Paulo.
     
  4. Offline

    Chinwe

    Paulo_Desticraft Nice, though it is better to save the player's name to the map instead of the whole Player object -

    Code:java
    1. HashMap<String, PlayerKills> kills = new HashMap<String, PlayerKills>();
    2. // Then use this:
    3. kills.put(player.getName(), pk);
    4.  
    5. // And to get the player from the map again:
    6. Player p = Bukkit.getPlayer(name);
    7.  
     
  5. chinwe Why not, I just wanted him to still be able to get player informations in the object ;).
     
  6. Offline

    iFamasssxD

    Paulo_Desticraft i appreciate your response and i will most likely use that method for checking the kills. But the main problem for me was the timer and how to make it different for each individual player. The way i had it was there was a public int and when the players were killed it disabled them and started the timer countdown. However this timer was the same for all the players. So if the one player had 3 minutes left then everyone only had 3 minutes left because it was going off the same timer.

    chinwe Thanks to you too :)
     
  7. Do you know how to use BukkitTasks ? If yes, just create a HashMap <Player, BukkitTask> ;)
     
  8. Offline

    iFamasssxD

    I havent gotten that far it seems :( I looked it up but I dont see much on how to use it.
     
  9. Don't actually got the time to explain you this night, I'll do tomorrow. You should have a look on Google ;)
     
  10. Offline

    iFamasssxD

  11. Have a look at this : ;)

     
  12. Offline

    iFamasssxD

    @Paulo_Destricraft I've seen this video and the only problem with this is that I cant say You have ___ minutes left with this method. Or atleast I couldnt figure out how.
     
  13. Offline

    iFamasssxD

  14. Offline

    iFamasssxD

    bumpz...
     
Thread Status:
Not open for further replies.

Share This Page