Always running code

Discussion in 'Plugin Development' started by jumbledProgram, Aug 24, 2020.

Thread Status:
Not open for further replies.
  1. Is there any way i can have code that runs every tick? or every 5 ticks? or 6? ect.
     
  2. Offline

    KarimAKL

    @jumbledProgram Yeah, using a BukkitRunnable. You can create one by creating an anonymous class and override the run() method.
     
  3. wait... so would this code from the wiki run all the time, constantly saying "Welcome to bukkit!"?
    Code:
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    
    public final class ExamplePlugin extends JavaPlugin {
    
    @Override
    public void onEnable() {
    new ExampleListener(this);
    }
    }
    
    class ExampleListener implements Listener {
    
    private final ExamplePlugin plugin;
    
    public ExampleListener(ExamplePlugin plugin) {
    this.plugin = plugin;
    this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
    
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
    // Create the task anonymously and schedule to run it once, after 20 ticks
    new BukkitRunnable() {
    
    @Override
    public void run() {
    // What you want to schedule goes here
    plugin.getServer().broadcastMessage(
    "Welcome to Bukkit! Remember to read the documentation!");
    }
    
    }.runTaskLater(this.plugin, 20);
    }
    
    }
    EDIT: nvm, im really confused
     
  4. Offline

    KarimAKL

    @jumbledProgram No, that'd broadcast a message 1 second after a player has joined. You want to use runTaskTimer instead of runTaskLater. (as mentioned below)

    Edit: I just noticed how confusing my answer was so, i edited it.
     
    Last edited by a moderator: Aug 24, 2020
  5. Offline

    Strahan

    No, not constantly. It's using runTaskLater, not runTaskTimer.

    Before you setup a task that runs all the time, ask yourself if you really need to. It's best to avoid stuff like that unless you really need it.
     
Thread Status:
Not open for further replies.

Share This Page