Adding to the cooldown

Discussion in 'Plugin Development' started by TCO_007, Apr 12, 2014.

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

    TCO_007

    I had someone help me write this code for a cooldown type of thing for my kit. It basically is supposed to keep the Taken (endermage kit renamed) from teleporting for 5 seconds after they do it. So once they teleport someone, they have to wait 5 seconds to do it again. I have no idea what to do to actually add the player to it. Any help?
    Code:java
    1. @ EventHandler
    2. public void onTakenClick(PlayerInteractEvent event) {
    3. Player p = (Player) event.getPlayer();
    4. if (plugin.Taken.contains(p.getName())){
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getType() == Material.IRON_INGOT) {
    6. List<Entity> nearby = p.getNearbyEntities(3, 20000, 3);
    7. for (Entity tmp : nearby)
    8. if (tmp instanceof Player) {
    9. tmp.teleport(p.getLocation());
    10. p.teleport(p.getLocation());
    11. p.sendMessage(ChatColor.GOLD + "You have teleported " + ((Player) tmp).getName() + " to you!");
    12.  
    13.  
    14.  
    15. }
    16. }
    17. }
    18.  
    19. }
    20.  
    21. HashMap<String, Integer> map = new HashMap<String, Integer>();
    22.  
    23. public void addCooldown(String name, int time){
    24. map.put(name, time);
    25. }
    26.  
    27. public boolean hasCooldown(String name){
    28. return map.containsKey(name);
    29. }
    30.  
    31. public void removeOneSecond(String name){
    32. map.put(name, map.get(name) - 1);
    33. }
    34.  
    35. public int getSeconds(String name){
    36. if(map.containsKey(name)){
    37. return map.get(name);
    38. }else{
    39. return 0;
    40. }
    41. }
    42. public void startTask(){
    43. new BukkitRunnable(){
    44. public void run(){
    45. for(String entry : map.keySet()){
    46. removeOneSecond(entry);
    47. int amount = getSeconds(entry);
    48. if(amount <= 0){
    49. map.remove(entry);
    50. }
    51. }
    52. }
    53. }.runTaskTimer(Main.getInstance(), 0, 20); //Runs every second, also. Im not 100% sure but this might cause a concurrent modification exception
    54. }
    55. }
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  


    I also tried using this method of cooldown:
    Code:java
    1. ArrayList<String> TakenCooldown = new ArrayList<String>();
    2. @ EventHandler
    3. public void onTakenClick(PlayerInteractEvent event) {
    4. final Player p = (Player) event.getPlayer();
    5. if (plugin.Taken.contains(p.getName())){
    6. if (event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getType() == Material.IRON_INGOT) {
    7. List<Entity> nearby = p.getNearbyEntities(3, 20000, 3);
    8. for (Entity tmp : nearby)
    9. if (tmp instanceof Player) {
    10. tmp.teleport(p.getLocation());
    11. p.teleport(p.getLocation());
    12. TakenCooldown.add(p.getName());
    13. p.sendMessage(ChatColor.GOLD + "You have teleported " + ((Player) tmp).getName() + " to you!");
    14. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    15. public void run() {
    16. TakenCooldown.remove(p.getName());
    17. }
    18. }, 0L, 10L);
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26. }
    27. }
    28. }
    29.  
    30. }
    31.  

    but it crosses out the "scheduleAsyncRepeatingTask" and says The method scheduleAsyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (Events, new Runnable(){}, long, long)

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

    Fhbgsdhkfbl

    If you have any other questions, tag me, or message me on skype "whosmagma'

    You'll Need this:
    List<String> cd = new ArrayList<String>();

    try this: put it after it teleports a player to you

    Code:
    cd.add(e.getPlayer().getName());
                      e.getPlayer().sendMessage(ChatColor.RED + "You are now on cooldown for 10 seconds");
                      this.getServer().getScheduler().runTaskLater(this, new Runnable(){
                          
                          @Override
                          public void run() {
                              cd.remove(e.getPlayer().getName());
                              e.getPlayer().sendMessage(ChatColor.GOLD + "You are no longer on cooldown");
                          }
                      }, 200);
                  }
                }
            }
        }
        }
     
  3. Offline

    TCO_007

    This is what It looks like now but on the section where it says ".getServer()"
    it is underlined in red and says "The method getServer() is undefined for the type Events" Fhbgsdhkfbl
     
Thread Status:
Not open for further replies.

Share This Page