1.15.2 How do I create a simple cooldown for my EventHandler?

Discussion in 'Spigot Plugin Development' started by kuai090, Mar 7, 2020.

  1. I'm trying to make a round so the countdown will begin in 5 minutes. I tried TimeUnit.MINUTES.sleep(5), but my server crashes after that. The scheduleSyncDelayedTask() seems way too difficult to understand. Is there a simple example for a cooldown? This is my code.

    upload_2020-3-7_5-14-34.png
     
  2. You can use the "already provided" bukkitrunnable to run something after a certain amount of time
    Code (Text):
    new BukkitRunnable() {

                @Override
                public void run() {
                    //CodeHere
                   
                }
               
            }.runTaskLater(pl"<main instance", int"delayInTicks");
     
    • Agree Agree x 1
  3. What you have done is really no good practice. Use (as provided) the BukkitRunnable. What you are doing right now is to freeze the whole thread which is being interpreted by the server as a crash
     
  4. Yeah, I realized it after using it several times with thread.
     
  5. You don't even need to schedule a task.
    If you're just making a cooldown, make a variable to represent the last time the event happened.
    When the event happens, check if the time since the last event is greater than the cooldown. If it isn't, stop. Otherwise, update the variable to the current time and continue.

    Edit: You'll have to use System#currentTimeMillis and convert from milliseconds to minutes.