Hey, I'm programming a plugin that does various things, but I've hit a stumbling block. I went to the wiki to find out more about time delays and found this piece of code that would help me out: myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() { public void run() { getServer().broadcastMessage("This message is broadcast by the main thread"); } }, 60L); It works great, but I need to be able to customize the delay. Since 60L goes to 3 seconds, I infer that 20 --> 1 second. But how can I have an integer that is editable in a config file replace this? Please help.
myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() { public void run() { getServer().broadcastMessage("This message is broadcast by the main thread"); } }, long.parseLong(config.getInt("delay in seconds")) * 20L);
hmm.. I feel dum for missing that. Anyways, couldn't I just use "main.int * 20L"? Without the Long.parseLong... Chiller AbeJ
Thanks AbeJ and Chiller . Furthermore, on the statement, we use the variable "myPlugin" two times.. I have this inside an onPlayerLogin event listener. I can just replace the first myPlugin with the a player variable, but what about the second? Should I just use Plugin "plgn = null;" ?
Chiller myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() { public void run() {
Chiller Thanks again. One more question.. What if I wanted something done by the plugin constantly, until a said amount of time passed, how would I do it?
No, don't do that. You should use a reference to your actual plugin object. There are many ways to do this. You can pass the plugin to whichever object this code is in, or you can put the plugin object in a static variable somewhere. You just need a reference to your plugin.
Chiller So like.. Code: int i =0; plr.getServer().getScheduler().scheduleSyncDelayedTask(new Contributor(), new Runnable() { public void run() { while (i < Contributor.time + 1) { //Content here i++; } } }, 0L, 20L); That would run it constantly, every second until the time is up? (Time measured in seconds.)
Please DO NOT listen to Chiller. They don't understand this. You don't create a new instance of your plugin class. That will mess things up, and generally not work. Just pass in "this" in the constructor. I'm a bit pressed for time, hopefully someone explains it better below me.
Will this work, then? int i =0;plr.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {public void run() {while (i < Contributor.time + 1) {//Content herei++;}}}, 0L, 20L); ? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Sorry :-(. I was really tired when I wrote that. Your method simply wouldn't work. That's why I said what I did.