Need a simple example of Scheduler Programming

Discussion in 'Plugin Development' started by Thumbz, Aug 15, 2012.

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


    Basically, I want to know how to make a plugin that counts seconds from 1 to 3

    Something like:

    if (commandLabel.equalsIgnoreCase("ThreeSecondTimer"))
    //Something involving Scheduler Programming

    I'll be posting this information directly to the developer wiki. Thank you.

    P.S. I checked but the tutorial code there is heavily lacking in comments, to the point I'm not sure it covers what I'm asking about
  2. Offline


    public int n;
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
        public void run() {
            if (n >=  4){
                n = 0;
    }, 20, 20);
    Theres probably even a way to make it simpler LOL
    Endorphinex and Thumbz like this.
  3. Offline


                    .scheduleAsyncDelayedTask(this, new Runnable() {
                        public void run() {
                            Delayed Task
                    }, (Delay in MS)L);
  4. Offline


    So int n is accessible even after sending the Runnable to the Scheduler? I was thinking that scheduler accepts Runnables and then runs them independently of the function that Schedules them. Is that not how it works?

    Also, what are the 20's in } 20, 20); for?
  5. Offline


    sorry, you should actually change public int n; to public final int n; - Runnables require a final. And the 20s are the duration in ticks between repeats. A tick is 1/20th of a second.
  6. Offline


    But then you wont be able to change / update n
    You just have to put it outside of the whole method
  7. Offline


    yeah... public int n; lol
    im working on a plugin right now thats making me frustrated.... so im not really putting much thought into it.
  8. Offline


    Whats the plugin do?!
  9. Offline


    This is just an educated guess, but this might work:
    1. public final int[] array = {1, 2, 3};
    3. Bukkit.getScheduler.scheduleSyncRepeatingTask(this, new Runnable(){
    4. public void run(){
    5. for(int n : array){
    6. if(n == 3){
    7. //do stuff(You counted to 3!)
    8. }
    9. break;
    10. }
    11. }
    12. }, 0, 20);

    EDIT: Thinking about it some more, this may just put it in an endless loop...
  10. Offline


    Makes a cursor follow all player positions on any map. Basically making the xbox360 maps (if you've played it before). I am even adding colors for players with permission. And it deals with a lot of performance, math, and tedious stuff.
  11. Offline


    This is client side right?
  12. Offline


    Nope, server side. Bukkit has an API for changing maps
  13. Offline


    Wait a cursor as in what? I thought you were saying like a minimap.
  14. Offline


    The white arrow on a map when you look at it is a cursor. I am adding colored cursors in red blue and green for all players in the game, to everyones maps. I am also going to be making a command for creating a map with a much larger scale, which means you can see more of the world... etc
  15. Offline


    What you're looking for, if you haven't found it already, is something like this:
    2. Bukkit.getScheduler().scheduleSyncRepeatingTask(this [as in your plugin instance], new Runnable()
    3. {
    4. public void run()
    5. {
    6. updatePlayerPositionsOnMap();
    7. }
    8. }, 60, 60);

    Long story short, here's the rundown:
    • You schedule it with an instance of your plugin, and an instance of a runnable. Usually, it's an anonymous inner class--but sometimes it helps to create your own Runnable class so you can pass variables into the scheduler (avoiding a lot of final issues)
    • At the end, you have 2 variables. First variable is going to be the delay in ticks, the second is going to be how often your task repeats. There are 20 ticks in a single second, roughly, so if you want a 3 second delay with a 3 second repeating interval, you'd give 60 as both of the variables.
    Other than that, everything is fairly straight forward.
  16. Offline


    This looks like it should work to me. I'll have to test it later. It's not really necessary in the plugin I'm making, but it should be a nice touch. I do feel like bumping this one more time, though, to see if anyone can say for sure whether or not this is how a runnable works.
  17. Offline


    Just on a little side note, i just realized it should be getScheduler(), not getScheduler. Not too big, since Eclipse would easily catch this, but just thought I would point that out.
  18. Offline


    1. Bukkit.getScheduler.scheduleSyncRepeatingTask(this, new Runnable(){
    2. public static int n;
    3. public void run(){
    4. if(n==0){
    5. n = 1;
    6. }
    7. System.out.println(n);
    8. n++;
    9. }
    10. },20L,20L);
    13. //20L means 1 sec in ticks

    EDIT: tested and works
  19. Offline


    4thegame3 Public static fields are almost always wrong. This is certainly the case when using anonymous classes.
Thread Status:
Not open for further replies.

Share This Page