Will this scheduler stop?

Discussion in 'Spigot Plugin Development' started by Mobile_Strike, Apr 16, 2017.

  1. So I have started working with schedules but I am unsure of how they stop exactly.
    I have this code which is supposed to repeat a task every second while a player is sprinting and stop the task when they stop sprinting. The program doesn't recognize cancel() as a method so I wanted to know if it was necessary. Also please tell me if there is a better way to do this. Thanks in advance

    Code (Text):
    @EventHandler
        public void onSprint(PlayerToggleSprintEvent event)
        {
            Player player = event.getPlayer();
            Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable()
            {
                @Override
                public void run()
                {
                    if(player.isSprinting())
                    {
                        // do something
                    }
                    else
                        cancel();
                }
            }, 0L, 20L);
        }
     
  2. Try
    Code (Text):
    return;
    - Instead of cancel(); as that method won't be recognized.
     
    • Funny Funny x 2
  3. here is how i did timers.

    In Main clas OnEnable
    Code (Text):
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new RadiationTimer(this), 0L, 20L);
    In Target Class
    Code (Text):
    public class RadiationTimer implements Runnable {

        Main plugin;

        public RadiationTimer(Main plugin) {
            this.plugin = plugin;

        }

        public void run() {
            for (Player p : plugin.getServer().getOnlinePlayers()) {
                if (p.hasPotionEffect(PotionEffectType.POISON))
                    continue; // if they already have it don't re-apply it.
                Biome biome = p.getLocation().getBlock().getBiome();
                if (biome == Biome.DESERT || biome == Biome.SWAMPLAND) {
                     p.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 1200, 10));
                }
            }

        }

    }
     
  4. I tried this, now when I run it I get an error saying my cast to plugin does not work, How would I reference the plugin in the first parameter passed to scheduleSyncRepeatingTask
     
  5. Maybe like Main.run(p);?
     
  6. Won't this always run the program, I want a timer for every player that starts sprinting and ends that timer when they stop. I've seen code like this but I don't know how to apply it to my case. Basically, I need it inside the EventHandler
     
  7. When you schedule a task, like a new runnable or a scheduled task it returns an integer taskid.

    If you record the task id for that player you can then call getServer().getScheduler().cancelTask(taskid) to stop it


    Sent from my iPhone using Tapatalk
     
  8. When using a self cancelling task, use BukkitRunnable instead of Runnable. You will then have access to a .cancel() method.
     
  9. As he said, just use BukkitRunnable class.
     
    • Agree Agree x 2