How to add a cooldown

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

  1. Online

    JjPwN1

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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:
    Code:
        @Override
        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));
                //cooldown
        }
            return false;
        }
    }
    Unless you must do an else statement.

    This post has been edited 1 time. It was last edited by JjPwN1 Aug 7, 2012.
  2. Offline

    pzxc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    ^ 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. Offline

    pzxc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    new Date().getTime()

    or

    System.currentTimeMillis()

    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. Offline

    desht

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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.

    This post has been edited 1 time. It was last edited by desht Aug 7, 2012.
  6. Offline

    pzxc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Oh yeah good catch.

Share This Page