How to use scheduler programming?

Discussion in 'Plugin Development' started by Squirrelcoding, Jul 25, 2021.

  1. Offline

    Squirrelcoding

    The title is very straightforward, I am having a bit of trouble with using the scheduler programming (https://bukkit.fandom.com/wiki/Scheduler_Programming). So, I just need an answer/explanation/example for the following:
    1. When to use?
    2. How to use?
    3. Where do I put the file?

    By #3 I mean where do I put the following code in the plugin's codebase:
    Code:
    //taken from tutorial
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class ExampleTask extends BukkitRunnable {
    
        private final JavaPlugin plugin;
    
        public ExampleTask(JavaPlugin plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public void run() {
            // What you want to schedule goes here
            plugin.getServer().broadcastMessage("Welcome to Bukkit! Remember to read the documentation!");
        }
    
    }
    Thank you!
     
  2. Offline

    KarimAKL

    Whenever you want to delay a task by some ticks.

    You can create an anonymous BukkitRunnable.
    Code:Java
    1. // Immediate code here
    2. new BukkitRunnable() {
    3. @Override
    4. public void run() {
    5. // Delayed code here
    6. }
    7. }.runTaskLater(pluginInstance, amountOfTicks);
    8. // Immediate code here
     
  3. Offline

    handsomedonkey

    Bukkit scheduler is just Bukkit's way of running tasks with real-time delays(just like events are its way to run code when an event occurs in game). Say for example you want to wait 2 seconds before sending a message. Using some ordinary methods might cause the whole server to pause. This is why Bukkit has created this scheduler thing. There are a few ways you can use scheduler.

    The longer way:

    The way you gave as an example is by making a BukkitRunnable object. You can go in your main class and do something like ...

    Code:
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
     
     
     
        @Override
        public void onEnable(){
            //on Enable
        }
     
        @Override
        public void onDisable() {
            //on Disable
        }
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if(label.equalsIgnoreCase("runTask")) {
                MyTask task = new MyTask();
                //Wait 0 ticks before executing for the first time. Wait 60 ticks between each consecutive execution
                task.runTaskTimer(this, 0, 60);
            }
         
            return false;
        }
     
    }
    
    
    This will run the task MyTask every 3 seconds after a player runs /runtask. Now you can create this MyTask class by creating a new file called MyTask.java and putting something like this in it.

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class MyTask extends BukkitRunnable {
     
        @Override
        public void run(){
            Bukkit.broadcastMessage("Hello, everyone. This message will spam a lot. You can use /reload to stop me");
        }
    }
    
    Whatever is in the
    Code:
     public void run() 
    function will be the task that will run every 3 seconds.


    The easier way:

    The way Karim showed you is probably easier though. Instead of making a completly seperate file and class you can just put this single line of code.

    Code:
    new BukkitRunnable() {
    @Override
    public void run() {
      // Delayed code here
    }
    }.runTaskLater(pluginInstance, amountOfTicks);
    
    
    All this is doing is creating an anonymous(unnamed) new BukkitRunnable object in one line and immediatly running the .runTaskLater function on it without storing it.

    You should replace 'pluginInstance' with an instance of your main class. So if you are running this in your main class just write
    Code:
     this 
    . Otherwise you should store it as a variable and send it to whatever class you are going to call runTaskLater in so that you can access it there.

    EDIT: I forgot to add this but 20 ticks is one second in real time, but sometimes if a server gets laggy it can be more ticks for the players.
     
    KarimAKL likes this.

Share This Page