Solved How to execute a method every x seconds

Discussion in 'Spigot Plugin Development' started by NegativeExpert9, Jan 14, 2019.

    • Agree Agree x 1
  1. I tried reading the api reference, if you don't mind, could you give me a basic example?
  2. md_5

    Administrator Developer

  3. Here's an example:
    Code (Java):
    public void onEnable() {
       new BukkitRunnable() {
           public void run() {
       }.runTaskTimer(this, 1000L, 1000L); //Delays in ticks

    private void Shout() {
       // Do smth
    That's how I do it. There's also another way but I forgot lul
  4. Why you delay the task? Also dont use BRs, they are bad.

    Code (Java):
    YourMain plugin = // get instance of your main
    plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, -> {
    // your code comes here
    }, 0, 20); // 0 is delay before the runnable acutally start running, 20 is the period of the runnable which will be ran. Everything is in ticks (20 ticks -> 1 sec)
    • Optimistic Optimistic x 2
  5. The this in your code refers to the instance of the anonymouse class that extends BukkitRunnable and not that of the JavaPlugin. You will get an error in your IDE because the class does not have that method.

    Not to mention the fact that naming conventions dictate that methods should start with a lower case letter.
  6. MiniDigger


    Why are Bukkit runnables bad? Lol
  7. There are several reasons why they're bad.
  8. MiniDigger


    ok, I am listening, you can start listing a few. I never heard of any before, so you can just list all you know.
    • Agree Agree x 1
  9. You do realize BRs are pretty much doing what you're doing right there, right?

Share This Page