Hello everybody! If a player logs in, my plugin should send a message, every 5 seconds for 10 times or so... I tried to do a SyncRepeatingTask... Only problem is that it doesn't send the message or that the task doesn't start. It's the first time I'm using the scheduler... Here's my code: Main class: Code: public class MyPlugin extends JavaPlugin { public MyPluginPlayerListener playerListener = new MyPluginPlayerListener(this); public void onEnable() { this.getServer().getPluginManager().registerEvents(playerListener, this); } public void onDisable() { ... } } PlayerListener: Code: public class MyPluginPlayerListener implements Listener { public MyPlugin plugin; public MyPluginPlayerListener(MyPlugin myplugin) { plugin = myplugin; } public void onPlayerLogin(PlayerLoginEvent event) { MyPluginTestTask testTask = new MyPluginTestTask(event.getPlayer(), "Testmessage!"); plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, testTask, 100L, 1000L); } } And the Task class: Code: public MyPluginTestTask implements Runnable { private Player player = null; private String message = ""; public MyPluginTestTask(Player player, String message) { this.player = player; this.message = message; } public void run() { player.sendMessage(message); } } So the mainclass creates a playerlistener. The playerlistener has a PlayerLoginEvent. If a player logs in, the event should start the testTask as a SyncRepeatingTask for 10 times and every 10 seconds. What's wrong? Thank you for reading and answering sds
I think you need to have the Override text above your run() method. Like so: Code: @Override public void run() { player.sendMessage(message); }
Your code creates a new repeating task executing firstly after 5 seconds, then every 50 seconds and continuing forever. The code below will create an (infinite) task running every 5 seconds: Code: plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, testTask, 100L, 100L); To solve the infinite repetition add a variable "static int taskID" to MyPluginTestTask, and set it like this: Code: MyPluginTestTask.taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, testTask, 100L, 100L); also add a variable "int count = 0" to MyPluginTestTask which is increased on every execution of run(), and if count reaches 10, stop the task using Bukkit.getScheduler().cancelTask(taskID);
Ok now it is like this: Code: public class MyPluginPlayerListener implements Listener { public MyPlugin plugin; public MyPluginPlayerListener(MyPlugin myplugin) { plugin = myplugin; } public void onPlayerLogin(PlayerLoginEvent event) { MyPluginTestTask testTask = new MyPluginTestTask(event.getPlayer(), "Teeest!", 5); MyPluginTestTask.taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, testTask, 100L, 100L); } } Code: public class MyPluginTestTask implements Runnable { static int taskID; private Player player = null; private String message = ""; private int count = 0; private int max = 0; public MyPluginTestTask(Player player, String message, int max) { this.player = player; this.message = message; this.max = max; } public void run() { player.sendMessage(message); count += 1; if (count >= max) { Bukkit.getScheduler().cancelTask(taskID); } } }