Repeating tasks

Discussion in 'Plugin Development' started by DarkPizzaX, Mar 13, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    DarkPizzaX

    I haven't made a plugin in a while and I'm trying to do something but I need to know how to run a task once every second until the server ends. How do I do this? I tried looking it up but I didn't find anything :'(.
     
  2. Offline

    stuntguy3000

  3. Offline

    DarkPizzaX

    That isn't helpful. Could you show me an example of that in action? I can't find where it allows it to constantly repeat.
     
  4. Offline

    Duning

    lol, my father has the same image profile like you.
     
  5. Better first check both links out before responding...There are examples on the wiki page.
     
  6. Try this:

    In your Runnable Class:
    Code:JAVA
    1. public class YourClass extends BukkitRunnable {
    2. //Extends BukkitRunnable to create a run method
    3.  
    4. private final JavaPlugin plugin;
    5. //Declares your plugin variable
    6.  
    7. public YourClass(JavaPlugin plugin) {
    8. this.plugin = plugin;
    9. }
    10. //This is called from your main class and sets your plugin variable
    11.  
    12. public void run() {
    13. //In here put what you want to run every second
    14. }
    15. }


    In your main class:
    NOTE: This is only an example, the runnable BukkitTask can be set and called anywhere

    Code:JAVA
    1. public class YourMainClass extends JavaPlugin {
    2.  
    3. BukkitTask TaskName = new YourClass(this).runTaskTimer(this, 20, 20);
    4. //This initiates a task that executes the code in your run method in YourClass.
    5. //20 is equal to one second as for each second on a server, 20 ticks of the game
    6. //pass and the time is measured in ticks.
    7.  
    8. }
     
    Wizardo367 likes this.
  7. Offline

    Adzwoolly

    Thank you lots, this worked PERFECTLY!
     
  8. Offline

    DarkPizzaX

    Thank!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
  9. Offline

    the_merciless

    What if i want to have 2 seperate repeating tasks, can i put them both in the same class. If so, how? I cant work it out.
     
  10. Offline

    Zach_1919

    That's really helpful, but how does the Main class know where to get the run method? And don't you have to put new Runnable or new BukkitRunnable, or else it won't know which one to do?

    EDIT: okay, I understand that, but when I set the task variable in my main class, it gives me a warning saying I never use it. When it runs over the variable will it actually start the task or will it only start it once I use the variable?
     
Thread Status:
Not open for further replies.

Share This Page