How to make a cooldown?

Discussion in 'Plugin Development' started by AdityaTD, Feb 26, 2015.

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


    Hey I want to add a cooldown to a command so how to do it? Also I wanna make a command which spawns some moves and killthem after 5 seconds!
  2. Online


    HashMap + System.currentTimeMillis();
  3. Offline


    Can please do a example??
  4. Offline


    'spawns some moves'
    What do you mean why this

    Use a scheduler to kill the player after a few seconds ?
  5. Offline


    @Ruptur @CraftCreeper6 I have never made a countdown before can you make one and also describing what's happening where?
  6. Offline



    First thing i would do is

        public boolean onCommdand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                if (cmd.getName().equalsIgnoreCase("killme")) {
                    sender.sendMessage("You cant kill yourself - you are the console");
                return true
            final Player player = (Player) sender;
            final int timeToRun = 5 // this will be in seconds
            // command was kill then do
            if (cmd.getName().equalsIgnoreCase("killme")) {
                p.sendMessage("You will be killed in " + timeToRun);
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    public void run() {
                        player.sendMessage("After " + timeToRun + " seconds you have been killed");
                }, 20 * timeToRun);
                // takes plugin - runnable and time to wait in server ticks
                // 20 ticks a second
            return true;
  7. Offline


    @Rupturin I wanna make a cooldown and not a delay!
  8. Offline


    Create a Map with the player as the key and long as value. When the player does the action, put the player and the system's time in the Map. When the player does the action again, compare the System's time with the long value assosiated with the player.
  9. Offline


    It would help if you are a bit more specific in what you say
    'killthem after 5 seconds!'

    To cooldown - a google search is always avaliable

    Cooldown can be done in many ways, the most basic way:
    Create a arraylist and store the players in it
    Schedule a delayed task to remove them from the arraylist.
    Whilst they are in the arraylist then they wont be able to do what you are
    trying to prevent them from doing.

    Something like this ..
    ArrayList<Player> playersOnCooldown = new ArrayList<Player>();
    Player p = event.getPlayer();
    do something ...
    Last edited: Feb 26, 2015
  10. Offline


    That's a timer, not a cooldown.
    • create an scheduleAsyncRepeatingTask
    • call a static integer to 5
    • set the scheduler to 20L and integer--; every run
    • send a message every 20L (equals 1 sec)
    • and if the integer equals 0 cancle the task hope that will help :)
  11. Offline


    @sn1cko Why would you use async?
  12. Offline


    @sn1cko Why on earth would you make is async? Why on earth would you have a static field? Why on earth are you describing a timer and not a cooldown?
  13. Offline


    One thing i like to use is ExpiringMap is very simple to use and works exactly like a HashMap. you can also do stuff when certain keys expire which is awesome.
  14. Offline



    Seems like a whole lot of over kill to me. Just check the time when you want to do something.
  15. @sn1cko @Ruptur why using a seperate thread to create a cooldown? using much memory for nothing. i guess your threads would repeat until the cooldown is over and do something while this. this is inefficient. in my opinion also over kill as @mythbusterma mentioned on the ExpiringMap. simply use:
    private Map<UUID, Long> calls=new HashMap<UUID, Long>(); // list with current cooldowns
    private long commandCooldown=2000; // cooldown in millis
    public void onCommand(/*param*/){
    UUID commandCallerID = /*get the player who called the command and take the uuid*/;
    if(isCommandWithCooldown()){// tests if it is the command you want to add the cooldown to
      long remainingCooldown = 0;
        remainingCooldown = calls.get(commandCallerID)+this.commandCooldown-System.currentTimeMillis();
      if(remainingCooldown <=0){
       calls.put(commandCallerID, System.currentTimeMillis());
       /*execute your commandstuff*/
       /*handle: cooldown not over. remainingCooldown = time to wait until caller is able to call again*/
    This way there is no seperate Thread running and cooldown stuff is only called when the method is called. so no resource wasting. you might also should remove the uuid of a player who left

    btw: i didn't write this in an editor, so there are maybe some methods with the wrong name, but i think you all can understand what i am doing, right?

    EDIT: you maybe should put the commandstuff and the cooldown together in a own method and call it instead of calling the commandstuff after you checked the cooldown in the onCommand(..) method.
  16. Offline



    Cooldown :
    The minimum length of time that the player needs to wait after using an ability before they can use it again. Similar to the reload time and firing rate of weapons. For example, a machine gun has very fast firing rate, so it has a very low cooldown between shots. Comparatively, a shotgun has a long reload/cooldown time between each shots.

    Timer :
    an automatic mechanism for activating a device at a preset time.


    He wanted a cooldown - i gave him a basic example of how he can cooldown.
    Before criticizing everyone it would help if you read the thread.
    Thank you.
  17. Offline


    @Ruptur So you would know that using a scheduler for a "cooldown" is the less efficient of the two ways to do then!
    Your use of a scheduler made your example a Timer rather than a Cooldown because it will remove the player from the arraylist after X amount of time.
    You shouldn't need to schedule anything. You should just compare the time at which they performed the last action and the current action.
  18. Offline


    And a cooldown is stopping a player from accessing a feature for a certain time.

    Thank you for the tip, i understand using a scheduler is inefficent but i wasn't on my computer at the time and i didn't fully remember the correct names and syntax.

    And @AdityaTD and his incomprehensible post mentioned and i quote
    'killthem after 5 seconds!'
    So a scheduler would be most appropriate don't you think?
    Last edited: Feb 27, 2015
    tomudding likes this.
  19. Offline


    Yes, a scheduler would be needed for this since you can't really depend on user interaction for the wait.
    tomudding likes this.
Thread Status:
Not open for further replies.

Share This Page