[Solved] How to create a repeating task that cancels it's self after certain time or iterations

Discussion in 'Spigot Plugin Development' started by moo3oo3oo3, Dec 7, 2014.

  1. I want to loop though somehting for a certain ammount of time or iterations. I tried:
    Code (Text):
    int stop = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                        public void run() {
                            if (something) {
                                Bukkit.getScheduler().cancelTask(stop);
                            }
                        }
                    }, 0L, 300L);
    but the Bukkit.getScheduler().cancelTask(stop) gives an error saying "The local veriable stop may not have been initialized"

    EDIT (Solution): Use:
    Code (Text):
    new BukkitRunnable()
                        {
                            public void run()
                            {
                                //Use cancel(); if you want to close this repeating task.
                            }
                        }.run //auto complete this statement
     
    #1 moo3oo3oo3, Dec 7, 2014
    Last edited: Dec 8, 2014
    • Informative Informative x 1
  2. Create a BukkitRunnable class.
     
    • Agree Agree x 2
  3. so put this inside a class?
     
  4. Make a delayed task to cancel the task?

    Code (Text):

    public void Timer() {
    int stop = 0 ;
    stop = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                        public void run() {
                        CODE HERE
                        StopTimer();
                        }
                    }, 0L, 300L);
    }

    private void StopTimer() {
      Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                                Bukkit.getScheduler().cancelTask(stop);
                        }
                    }, 600L);
    }

    UNTESTED CODE!


    That Would allow 2 runs edit delay as Needed

    P.S Not sure if thats best way to do it
     
  5. Code (Java):
    new BukkitRunnable()
    {
        @Override
        public void run()
        {
            if(needsCancel)
            {
                this.cancel();
            }
        }
    }.runTaskWhatever(...);
     
    • Informative Informative x 8
  6. But I want the repeating taskt o cancel it's self when a certain thing happens
     
  7. make the needscancel a boolean and make another thing to make it true or false
     
  8. This post is really old, but i don't want to bother making a new one with the same sorta topic, so i might just aswell respond to this with my problem. I seem to occur this problem whenever i try to cancel the task vanishspam
    Code (Text):
    int vanishspam = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                                public void run() {
                                    p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 999999999, 1));
                                }
                            }, 10, getConfig().getInt("VanishSpam.delay"));
                           
                            int vanishspam1 = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                                public void run() {
                                    p.removePotionEffect(PotionEffectType.INVISIBILITY);
                                }
                            }, 10, getConfig().getInt("VanishSpam.delay"));
                           
                        }
                    } else if(vanished.contains(p.getName())) {
                       
                        Bukkit.getScheduler().cancelTask(vanishspam);
                        Bukkit.getScheduler().cancelTask(vanishspam1);
                       
                    }
    The problem occurs down here by else if:

    Code (Text):
    else if(vanished.contains(p.getName())) {
                       
                        Bukkit.getScheduler().cancelTask(vanishspam);
                        Bukkit.getScheduler().cancelTask(vanishspam1);
                       
    The problem is that it cannot read the vanishspam here: It says that it needs to create a field. like it's not reading the int above it. And the int says that it's not being used. Any thoughts?