How to add cooldown to commands

Discussion in 'Plugin Development' started by jacklin213, Nov 15, 2012.

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


    Im trying to develop a plugin and i need to know how to put it on cool down after a player uses it, and the cool down is player specific
  2. Offline


    Have a boolean variable for the player with a schedule assigned to it.
    After a player uses it the boolean is set to false, and a timer begins and at the end of that it goes back to true.
    If a player tries to use it while it shows false, it cancels the command and sends them a message.
  3. Offline


    so ill have to do this to every player who uses the command?
  4. Offline


    It would be a method with an input of the player (in either string or player) which when the player types the command a scheduler is auto created from the method.
    Not sure if their are other ways to approach it but this is the way I would.
  5. Offline


    Have a list, not a boolean, a boolean is a one time use, you can't reuse it several times at once, a list you can.

    Add them to the list after they used the command, remove them after the timer is up.
  6. Offline


    Anyone mind doing an example
  7. Offline


    This is part of the plugin I'm making for my server
    In onEnable it runs voteReminder(120);
    Then the timer schedules, then after 120 seconds, it runs again, reminds the user again, etc.

    Main class:
    2. public void voteReminder(int seconds)
    3. {
    4. Timer timer = new Timer();
    5. timer.schedule(new VoteRemindTimer(), seconds * 1000);
    6. //add to a hashmap here
    7. }

    VoteRemindTimer class:
    2. package;
    4. import java.util.TimerTask;
    5. import me.iforgot290.bosscraft.BossCraft;
    6. import org.bukkit.configuration.file.FileConfiguration;
    8. public class VoteRemindTimer extends TimerTask
    9. {
    10. private BossCraft plugin;
    11. private VoteRemind method;
    13. public void run()
    14. {
    15. int seconds = this.plugin.getConfig().getInt("SecondsBetweenRemind");
    16. this.method.voteReminder(seconds);
    17. //take away from hashmap
    18. }
    19. }

    If you want to see if an user can do a command, just add them to a hash map in the voteReminder, then take them away in the VoteRemindTimer class

    Add an if statement in onCommand()
    if (hashmap.contains)

    And thats pretty much how.
  8. Offline


    use an HashMap of player names long values, and store the System.currentTimeMillis() there
    1. // Define the HashMap
    2. HashMap<String, Long> cooldown = new HashMap<String, Long>();
    3. // To check the time, assume Player p:
    4. // Check if the player has used the command before, and if he did, it was at least 10 seconds ago
    5. if(!cooldown.contains(p.getName()) || (System.currentTimeMillis() - cooldown.get(p.getName()) > 10000)
    6. {
    7. cooldown.put(p.getName(), System.currentTimeMillis());
    8. }
    9. else
    10. {
    11. p.sendMessage("Please wait another "+ Math.round((System.currentTimeMillis() - cooldown.get(p.getName()) )/1000) +" seconds");
    12. }
  9. Offline


    so do i stick this in a new method or just before i use the commnd
  10. Offline


    the if-else goes before you use the command
  11. Offline


    So like this?
    1. if (commandLabel.equalsIgnoreCase("cdr")){
    2. //your code here?
  12. Offline


    Well if you have only one command, yes. If you have multiple ones, you would have to modify the HashMap:
    1. HashMap<String, HashMap<String, Long>> cooldown = new HashMap<String, HashMap<String, Long>>();

    Where the first string is the command, and String in the nested HashMap is the player. (You could also switch this, so the first String is the player, and the HashMap contains the command name and the timestamp)
Thread Status:
Not open for further replies.

Share This Page