Taken Kit : Cooldown help

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

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

    TCO_007

    Hello! I am new to coding and have never done a cooldown before. I have attempted it many times but always failed to get it to work. Basically what I am trying to do it the endermage kit from MCPVP. I want it so that there is a 5 second cooldown between teleporting players so they cant just spam it repeatedly. Any help here guys? Ive tried referring to videos and such and I havent gotten much help. Thanks!
    Taken Kit Event:
    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 a player to you!");
    12.  
    13. }
    14. }
    15. }
    16. }
     
  2. Offline

    nuclearmissile

    For a job such as this, you'll want to first keep an ArrayList of players so you can keep separate timers for each one. Then you use the Bukkit Scheduler, which is fairly straightforward but a pain in the ass to learn at first, to time out 100 ticks using a boolean variable to restrict whether the player can use the ability.
     
  3. Offline

    coasterman10

    If you want to track how many seconds are left, use a Map of UUIDs to Integers, then store their name in the map with an Integer of 5. Use a BukkitRunnable to decrement this every second and then remove their name after it reaches zero.

    If you don't need to tell the player how many seconds are left, just store their name in a Set, and use a BukkitRunnable to remove their name from the set after 5 seconds.

    In both cases, you check if it contains the player's name (in the map, as a key) to see if they are cooling down.
     
  4. Offline

    Zethariel

    Or, if you want to have just one scheduled task instead of a task for each player, you can set one task that will work the list each second, removing the players and decreasing their cooldown. I'd also choose player names instead of UUIDs (unless you want the CD's to persist through server resets)
     
  5. Offline

    TCO_007

    This is what I have now but it says there are two errors in it. It tries to change "add" to "addAll" and then when its "addAll" back to "add". It also says that scheduleSyncDelayedTask is "not applicable". Any help here?
    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. if (plugin.TakenCool.contains(p.getName())){
    8. p.sendMessage(ChatColor.RED + "You may not use the teleporter yet!");
    9.  
    10. }
    11. final Player pl = (Player) event.getPlayer();
    12. for (Entity tmp : nearby)
    13. if (tmp instanceof Player) {
    14. tmp.teleport(pl.getLocation());
    15. pl.teleport(pl.getLocation());
    16. plugin.TakenCool.add(pl.getName());
    17. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    18. public void run() {
    19. plugin.TakenCool.remove(pl.getName());
    20. }
    21. }, 5);
    22. pl.sendMessage(ChatColor.GOLD + "You have teleported a player to you!");
    23.  
    24. }
    25. }
    26. }
    27. }
     
  6. Offline

    TCO_007

    coasterman10 Does that look fine to you? Or anyone else?
     
Thread Status:
Not open for further replies.

Share This Page