Cancel repeating task from inside itself

Discussion in 'Bukkit Help' started by Slamaram, Nov 19, 2017.

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

    Slamaram

    I am trying to make a scoreboard update once it is created but when the player leaves the game, the console spits out errors because it is trying to apply a scoreboard to a null player.
    Code:
    final int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    {
        public void run()
        {
            if (player != null)
            {
                player.setScoreboard(sb);
            } else
            {
                Bukkit.getScheduler().cancelTask(task);
            }
        }
    }, 0, 10);
    This is my code, I have tried looking at other threads but none have the answer.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Slamaram Use a BukkitRunnable. Then you can use a cancel method from within.
     
  3. Offline

    Slamaram

    Thanks for the help, I got it working by doing
    Code:
    new BukkitRunnable()
    {
        public void run()
        {
            if (player != null)
            {
                player.setScoreboard(sb);
            } else
            {
                this.cancel();
            }
        }
    }.runTaskTimer(this, 0, 10);
    Since
    Code:
    final int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable()
    {
        public void run()
        {
            if (player != null)
            {
                player.setScoreboard(sb);
            } else
            {
                Bukkit.getScheduler().cancelTask(task);
            }
        }
    }, 0, 10);
    Was deprecated.
     
Thread Status:
Not open for further replies.

Share This Page