How to add a cooldown

Discussion in 'Plugin Development' started by JjPwN1, Aug 6, 2012.

Thread Status:
Not open for further replies.
  1. How would you go about adding a cooldown to using a command? Say, I was coding a plugin to when a user types "/bbyjump" they get the jump potion effect for 10 seconds. How would I add a cool-down so they can't use the command until another 20 seconds (or w/e I specify)?

    The code would kinda go like this:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("bbyjump")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));
            return false;
    Unless you must do an else statement.
  2. You have to keep track of cooldowns separately for each player, otherwise one player's cooldown would prevent everyone from using the command

    So the ideal way is to use a HashMap<String,Long> where the key (string) is the player's name and the value (long) is the time the command was last executed.

    When you go to execute the command, you look in the hashmap for the player's name to get the time the player last executed the command, subtract the current time from it to get how long it's been since it was executed, and check to see if that time is less than the cooldown amount, if so the command is still "on cooldown"
  3. ^ This, although I think the cooldowns would get messed up if a server admin changes the world's time (Not 100% sure) It all depends what "time" you store as the Long.
  4. new Date().getTime()



    both return the "real world time" according to the PC clock of the server, not the game time

    Cooldowns can still be messed up because of daylight savings time though :p In the fall when the clock moves back, if you used your ability at exactly the wrong time you might have to wait an hour before you can use it again because it's 4 oclock no wait it's 3 oclock again, DAMMIT

    lol :p
  5. System.currentTimeMillis() returns the number of milliseconds since midnight, Jan 1, 1970 UTC. So it's not affected by daylight savings changes - it always increases.
  6. Oh yeah good catch.
Thread Status:
Not open for further replies.

Share This Page