Repeating task isn't stopping?

Discussion in 'Plugin Development' started by Staartvin, Jul 30, 2012.

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

    Staartvin

    Hello everyone. I've been busy trying to stop my repeating task.

    For some reason the task doesn't stop even when I say so.

    This is how I start it:
    Code:
      LookItsATask lookitsatask = new LookItsATask(plugin, playername, ++pid);
                LookItsATask task = new LookItsATask(plugin, playername, ++pid);
                task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, lookitsatask ,0L, 20L));
    This is how I want it to stop:
    Code:
        private int pid;
        @SuppressWarnings("unused")
        private int count = 0;
        Players players = new Players(this);
     
        public LookItsATask(AntiAddict plugin, String name, int pid)
        {
            this.plugin = plugin;
            this.name = name;
            this.pid = pid;
        }
       
       
     
        public void setPid(int pid)
        {
        this.pid = pid;
        }
     
        public Player getPlayer() // Just in case you want to use the class out of the BukkitScheduler, too, and need to get the name...
        {
            Player player = plugin.getServer().getPlayerExact(name);
            if (player == null) {
            return null;
            } else if (player.isOnline()){
                return plugin.getServer().getPlayer(name);
            } else {
                return null;
            }
       
        }
     
        public void run()
        {
           
           
            Player player = getPlayer();
            System.out.print(player);
            if (player == null) {
                    Bukkit.getServer().getScheduler().cancelTask(pid);
                return;
            }
    Does anyone has a solution?
     
  2. Offline

    rjVapes

    If the cancel isn't working properly you could just use a non-repeating task and store a delay instead of the pid, and re-call the task if player != null.
     
  3. Offline

    r0306

    Staartvin
    If you're scheduling this for multiple players and this is inside a shared class, it's going to overwrite the id.
     
Thread Status:
Not open for further replies.

Share This Page