Cooldowns

Discussion in 'Plugin Development' started by david123718, Aug 17, 2014.

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

    david123718

    How do I make a cooldown? And if the player tries to do the command it says how long more until you can do the command. I am making a kitpvp plugin with eclipse.
     
  2. Offline

    Dragonphase

    david123718

    When the player issues the command you want to add a cooldown for, store the command, the current time (System.currentTimeMillis()) and the length of the cooldown:

    Code:java
    1. Map<String, CommandCooldown> cooldowns;


    CommandCooldown is an arbitrary class name where you can retrieve the time you issued the command (getIssueTime()) and the length of the cooldown (getCooldown()). An example usage for adding a new cooldown to the map:

    Code:java
    1. CommandCooldown cooldown = new CommandCooldown();
    2. cooldown.setIssueTime(System.currentTimeMillis());
    3. cooldown.setCooldown(3600000); //1 hour = 3600000 milliseconds.
    4.  
    5. cooldowns.put(commandName, cooldown);


    When you want to check to see if the cooldown has passed, you would do something along the lines of this:

    Code:java
    1. boolean cooldownPassed = System.currentTimeMillis() - cooldowns.get(commandName).getIssueTime() >= cooldowns.get(commandName).getCooldown();


    If you want to retrieve the remaining length of the cooldown, you would do:

    Code:java
    1. long remainingTime = cooldowns.get(commandName).getCooldown() - (System.currentTimeMillis() - cooldowns.get(commandName).getIssueTime());
     
  3. Offline

    david123718

    But how do I tell the player How long you have left to the cooldown
     
  4. Offline

    Dragonphase

  5. Offline

    david123718

    Sorry Im confused can you give me a example please?

    Bump

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

    Garris0n

    Bump once per 24 hours.

    somePlayerInstance.sendMessage("something")?
     
  7. Offline

    Dragonphase

    david123718

    I won't get any alerts if you don't Tahg me or reply to me, be sure to do that when replying.
     
Thread Status:
Not open for further replies.

Share This Page