'Minecraft Bukkit Plugin, runTask(Plugin plugin)
i try to make a minecraft plugin with bukkit and i will try to change the time every 500ms but when i start that in the main thread, the server crashes. So i tried the runTask(); function but i do not know where i can find the parameter Plugin, because it says it needs runTask(Plugin plugin);. Here is my code:
dayNight file:
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.plugin.java.JavaPlugin;
public class dayNight implements CommandExecutor
{
    public void sleep(int time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }
    public static void changeTimeEvent(CommandSender sender, int time)
    {
        sender.getServer().getWorld("world").setTime(time);
    }
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
        Plugin plugin;
        new dayNightTask().runTask(plugin);
and the dayNightTask file:
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;
public class dayNightTask extends BukkitRunnable
{
    public void sleep(int time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }
    @Override
    public void run()
    {
        World world = Bukkit.getWorld("world");
        for (int i = 0; i < 500000; i++)
        {
            world.setTime(0);
            sleep(500);
            world.setTime(16000);
            sleep(500);
        }
    }
}
I do not know how i can find the plugin parameter for the runTask(); function, please help :) best regards
Solution 1:[1]
Spigot include own timer task. You should NEVER use Thread.sleep() on spigot, and less in main thread. Here you are just totally blocking the main thread.
You should use Bukkit.getScheduler().
- Use - Runnableinstead of- BukkitRunnable
- Use method showed in documentation. In your case, - runTaskLatershould answer
- Finally, this is what you will have in your - dayNightTaskclass:
public class dayNightTask implements Runnable {
    @Override
    public void run() {
        World world = Bukkit.getWorld("world");
        for (int i = 0; i < 500000; i++) {
            world.setTime(0);
            Bukkit.getScheduler().runTaskLater(MyPlugin.getInstance(), () ->  world.setTime(16000), 10);
        }
    }
}
10 is in tick. 20 ticks = 1 second, if you want each 0.5 second, you want each 10 ticks.
Now, in your command, you should use :
Bukkit.getScheduler().runTaskTimer(plugin, new dayNightTask(), delay);
PS: The plugin instance should the object of the class that extends JavaPlugin. Such as said in documentation (Eclipse, Intellij Idea ...) you should have a class that extendds JavaPlugin.
You should save the instance of this, you can do this in 2 ways:
- Pass object into constructor, for example: new dayNight(this)then in the constructor, do :
private JavaPlugin plugin;
public dayNight(JavaPlugin pl) {
   this.plugin = pl;
}
- Create a static class :
// declare object and method
private static JavaPlugin instance;
public static JavaPlugin getInstance() {
   return instance;
}
@Override
public void onEnable() { // this class should already exist
   instance = this; // set instance as current object
}
Solution 2:[2]
You can try using Executors in java. They're asynchron and easy to use.
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | |
| Solution 2 | Levi Heßmann | 
