runnable

Discussion in 'Spigot Plugin Development' started by TheFuzzyHead5, May 28, 2017.

  1. making a repeating task cancel itself.

    you cannot modify variables outside of the runnable as they must be final, so how can I check if the runnable is on it's 6th (or greater) run, for example?

    I know how to cancel it, but can't tell how many times it has run. eg code:

    Code (Text):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {

    public void run() {

    }

    }, 0L, 200L);
     
  2. in the run just do cancel();
    and a workaround is to use maps/lists instead of variables
     
    • Like Like x 1
  3. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Try using BukkitRunnable:

    Code (Text):
    BukkitRunnable r = new BukkitRunnable(){
       
        @Override
         public void run(){
             this.cancel(); //Cancels the timer
         }

    }

    r.runTaskTimer(plugin, delay, interval);
    (Something along the lines of that)
     
    • Like Like x 1
  4. that is what he is using
     
    • Like Like x 1
  5. Declare a counter variable inside the Runnable body, but outside the run method. You can change this field from within the Runnable as you please.
     
    • Like Like x 1
  6. Use this answer, but remember that you can't access that counter variable outside of your BukkitRunnable. If you need to access the counter outside of the runnable, use @Z3tr0nix's workaround.
     
    • Like Like x 2
  7. Code (Text):
    new BukkitRunnable(){
       int counter = 0;
        @Override
         public void run(){
    if(counter >= 6){
             this.cancel(); //Cancels the timer
    }
    counter++;
         }

    }

    r.runTaskTimer(plugin, delay, interval);

    it's kinda easy
     
    • Agree Agree x 1
  8. I'm sure French is easy when you know it
     
    • Like Like x 1
  9. Yeah, take as long as you need to pick up on this stuff. There's no rush. Here's a wiki page that'll teach you more on BukkitRunnables and scheduling tasks: http://bukkit.gamepedia.com/Scheduler_Programming
     
  10. I'm completely fine with scheduling, but I've never dealt with BukkitRunnables - from what I understand they are runnables taking up an entire class?
     
  11. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    They're an extension of Runnable.
     
    • Agree Agree x 1