Timing

Discussion in 'Plugin Development' started by webmessia, Feb 21, 2011.

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

    webmessia

    Hey all. I have a newbie question. Is there any way of running code in my plugin every few seconds? I feel there must be a cleaner way than running a do while loop and using Thread.sleep in the onEnable() method call.

    The reason I want to do this is that I wanted to create a plugin that checks at a given time interval, how far players have moved from a given point. And if they have moved too far away from the point teleport them backwards. Basically I wanted to write an anti-expansion plugin as it's the only type of greifing that the AMAZING plugin dev's here havn't seemed to have solved. All help is appreciated :).
     
  2. Offline

    Edward Hand

    First I should say that using Thread.sleep within plugins is almost always very bad, unless you are doing it within a separate thread you have created, as it will make the whole server hang.

    It sounds to me like the best way to do what you want is using events (specifically PLAYER_MOVE). If you don't know what I'm talking about, there are plenty of beginners plugin guides lying around the forums and elsewhere that will help you.
     
  3. Offline

    darknesschaos

    I don't know much about the scheduler, but I feel that this will help out on doing things however many ticks.
     
  4. Offline

    Raphfrk

    Carrying out repeating tasks is one of the things that the scheduler is for.

    This code can be used to repeat the same task every so often.

    Code:
    int taskId = getServer().getScheduler().scheduleSyncRepeatingTask(yourPlugin, new Runnable() {
        public void run() {
            <code to repeat>
        }, delay, period);
    
    If you are doing something complex, it might be worth creating a separate class that implements Runnable.

    delay is delay until the first run and period is the delay between runs. Also, they count in ticks so 20 ticks = 1 second.

    You can cancel the task with

    Code:
    getServer().getScheduler().cancelTask(taskId);
    
    --- merged: Feb 21, 2011 11:56 PM ---
    There is info on the scheduler on the wiki.
     
  5. Offline

    webmessia

    Thanks guys. To Edward: Yeah I thought using Thread.sleep was going to be a bad idea. I might give PLAYER_MOVE events a try, but my worry is that that event is being called far more often than my code needs to run.
    To Raphfrk: Thanks, I knew Bukkit had some sort of Scheduler in it, but I had no idea how to use it.
     
  6. Offline

    Raphfrk

    Btw, using the scheduler is a better way than using PLAYER_MOVE. Player move gets called very often. OTOH, if everyone stopped moving (or if the server was empty), then it is never triggered.

    The point of the scheduler is to provide consistency.
     
  7. Offline

    Sammy

    Im using thread pools for my plugin and it looks very stable so far.
    Later today I'll post my code here, I don't have it now =)
    --- merged: Feb 23, 2011 7:17 PM ---
    Here is it, hope it helps ! :)

    Code:
    ScheduledExecutorService SySchedule = Executors.newSingleThreadScheduledExecutor();
    Code:
     //Creates a pool of Schedule Threads with TimeHandlers
            final ScheduledFuture<?> TimeLeftHandler = SySchedule.scheduleAtFixedRate(new Runnable() {
    
                // Updates Timeleft Every 10seconds ( less updates less lag?!?!)
                public void run() {
    
    
                    }
                }
            }, 10, 10, TimeUnit.SECONDS);
            SySchedule.schedule(new Runnable() {
    
                // Final run method, triggered when the duration ends
                public void run() {
    
    
                    TimeLeftHandler.cancel(true);
                }
            }, duration , TimeUnit.MINUTES);
    
        }
    JavaDocs
     
Thread Status:
Not open for further replies.

Share This Page