How to create a "Timer"?

Discussion in 'Plugin Development' started by Ancetras, Apr 7, 2011.

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

    Ancetras

    I'd like to create a Timer which every 5 minutes sends a message to all players online.

    How can i do this?
     
  2. Offline

    Survivalist

    Make a thread class, and use Thread.sleep to wait for 5 minutes etcetc.
     
  3. Offline

    nickguletskii

  4. Offline

    Carnes

    Code:
    public class YourPlugin extends JavaPlugin {
    messageTimer messageTimer;
    public void onEnable() {
        messageTimer = new messageTimer(this);
        getServer().getScheduler().scheduleAsyncRepeatingTask(this, messageTimer, 10L, 6000L);
    
    ...
    
    and your class like:
    Code:
    public class messageTimer implements Runnable
    {
        public YourPlugin parent;
    
        public messageTimer(YourPlugin _parent)
        {
            parent=_parent;
        }
    
        public void run()
        {
               //code for what happens every 6000ticks or 5min
    ...
    
     
    Svenyboy, mxE333xm and ne0nx3r0 like this.
  5. Offline

    Sammy

    Use @Carnes way man !!
    Never use Timers or threads they aren't synchronized with bukkits thread and will cause you serious troubles !
     
  6. Offline

    Ancetras

    Thank you!
    And if I want to put him into another existing plugin? >.<
     
  7. Offline

    Sammy

    Can you explain it a little better ?
     
  8. Offline

    Carnes

    I'd say start with making it a standalone plugin first. Something that just says "Hello World!" every 5 min. Then it'll become more obvious to you how to move this functionality into an existing plugin. It'll mostly be cut & paste : )
     
  9. Offline

    Ancetras

    Ok, i try!

    x3

    I'll report you here if there are some problems!
     
  10. Offline

    nickguletskii

    I don't care. I hate the scheduler.
     
    ne0nx3r0 likes this.
  11. Offline

    Sammy

    Why ?
    Even if you don't like them, you must know that using timers and threads is only safe if you synchronize them, and you didn't mention it to him, at least do that much :)
     
  12. Offline

    Ancetras

    Uhm!
    This code works!
    But if I want to change the time maybe from 5 minutes to 10 minutes? o_o

    Thank you! I'm newbie >.<
     
  13. Offline

    mxE333xm

    Then change Paramter number four of the scheduleAsyncRepeatingTask method.
     
  14. Offline

    Sammy

    6000L = 60*5*20L so just do 60*10*20L ^^
     
  15. Offline

    Ancetras

    Ooooh *_*

    Thank you everyone!

    I'll report you here for some problems!
     
Thread Status:
Not open for further replies.

Share This Page