[Util] Creating Dynamic Cooldowns

Discussion in 'Resources' started by Compressions, Jul 21, 2013.

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

    Compressions

    Hello! I have created a utility that novice programmers can use to create dynamic cooldowns with ease. I have written up a class for you, therefore not requiring any external JAR's.

    Firstly, create a class called Cooldown and paste the following code in:
    The class is available for download on GitHub.
    NOTE: Follow the instructions or it won't work properly!
    Code:
    package us.compressions.example;
     
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
     
    public class Cooldown {
     
        // change Main to your plugin's main class
        public Main p;
     
        public Cooldown(Main i) {
            p = i;
        }
     
        int task;
     
        public void setCooldownLength(Player player, int time,
                HashMap<String, Integer> hashmap) {
            hashmap.put(player.getName(), time);
        }
     
        public int getTimeLeft(Player player, HashMap<String, Integer> hashmap) {
            int time = hashmap.get(player.getName());
            return time;
        }
     
        public void startCooldown(final Player player,
                final HashMap<String, Integer> hashmap) {
            task = Bukkit.getServer().getScheduler()
                    .scheduleSyncRepeatingTask(p, new BukkitRunnable() {
                        public void run() {
                            int time = hashmap.get(player.getName());
                            if (time != 0) {
                                hashmap.put(player.getName(), time - 1);
                            } else {
                                hashmap.remove(player.getName());
                                Bukkit.getServer().getScheduler().cancelTask(task);
                            }
                        }
                    }, 0L, 20L);
        }
     
    }
    Here is an example of how you can implement this utility:
    NOTE: getTimeLeft, setCooldownLength, and createCooldown are methods from the Cooldown class!
    Code:
        // this is for storing players' names and their cooldown time
        HashMap<String, Integer> map = new HashMap<String, Integer>();
     
        // example method for using a cooldown
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if (player.getItemInHand().getType().equals(Material.STONE_AXE)) {
                    if (map.containsKey(player.getName())) {
                        player.sendMessage(ChatColor.RED
                                + "You are still on cooldown for another "
                                + getTimeLeft(player, map) + " seconds!");
                    } else {
                        player.getWorld().strikeLightning(
                                e.getClickedBlock().getLocation());
                        setCooldownLength(player, 10, map);
                        startCooldown(player, map);
                    }
                }
            }
        }
    Here is the in-game output:

    [​IMG]
    I hope this utility helped you in creating dynamic cooldowns for your plugin.
    If this helped you, please show some support by giving this post a like!
    Thanks and happy coding! :)

    EDIT: Sorry about how messy and inefficient the code is. I posted this and realized it wouldn't work, so I rushed out a working version. I will clean this up and make more efficient methods soon! :)
     
    Pitazzo, Zen3515, KingFaris11 and 3 others like this.
  2. Offline

    Jumla

    Since I assume the getTimeLeft function will be called way less often then once a second, seems like it'd make more sense to just make a time variable called whatever you decide, then compare a players time to that variable... If that makes any sense. Looping through a hashmap each second may not be the most efficient way to go.
     
    zachoooo likes this.
  3. Offline

    TigerHix

    Thanks :) I will implement this to my plugin.
     
  4. Offline

    zachoooo

    It's not very efficient to use a scheduler to change the values for a cooldown. It would be much better to just store the time and compare it. Just imagine if you used this method and had a couple hundred players with like 100+ different abilities and commands to keep track of. That would be over 10,000 operations each second for a simple cooldown.
     
  5. Offline

    Loogeh

    zachoooo You'd still need a scheduler to compare them. But yeah that's what I did in my tutorial.
     
  6. Offline

    zachoooo

    No you dont
     
  7. Offline

    Loogeh

    zachoooo How? You'd need to compare them every 1 or 2 ticks to see one needs to be removed?
    I'd like to know how to do it without a scheduler :D Unless you mean when someone interacts it checks for their cooldown or something.
     
  8. Offline

    zachoooo

    Yeah, you just have it do the check when it needs to be checked. It would only use a tiny amount of ram
     
  9. Offline

    Loogeh

    zachoooo Okay, I do it with a scheduler so I can notify the player when a cooldown is finished :)
     
Thread Status:
Not open for further replies.

Share This Page