Cancel an Scheduler Task

Discussion in 'Plugin Development' started by splint, May 30, 2014.

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

    splint

    Hi, I was a beguiner in the java dev', and I have a little problem now. I made a plugin, with spout, to listen music in game. I've made a loop fonction, and that work, but I didn't know how to stop that loop !

    My code:
    Code:java
    1. if (command.getName().toLowerCase().equals("playloop")) {
    2. if (player.hasPermission("spouties.play")) {
    3. Urlcheck urlckeck = new Urlcheck();
    4. urlckeck.urlcheck(sender, command, commandLabel, args);
    5. if (urlckeck.urlcheck == 1) {
    6.  
    7. sender.sendMessage(ChatColor.RED + "play");
    8. final String t = args[1];
    9. int i = Integer.parseInt(t);
    10. i = i*20;
    11.  
    12. final int loop = this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    13. public void run() {
    14. SoundManager music = SpoutManager.getSoundManager();
    15. music.playCustomMusic(Plugin, (SpoutPlayer)player, args[0], false);
    16. //If w = 1, Cancel this task ("loop")
    17. }
    18. }
    19. , 0L, i);
    20. }
    21. return true;
    22. }

    Thank you !
     
  2. Offline

    Adriani6

    Code:java
    1. Bukkit.getScheduler().cancelTask(loop);
     
  3. Offline

    splint

    I've already see that, but when I add if (w = 0) { Bukkit.getScheduler().cancelTask(loop); }, it said "loop" was not initialized.

    Code:java
    1. if (command.getName().toLowerCase().equals("playloop")) {
    2. if (player.hasPermission("spouties.play")) {
    3. Urlcheck urlckeck = new Urlcheck();
    4. urlckeck.urlcheck(sender, command, commandLabel, args);
    5. if (urlckeck.urlcheck == 1) {
    6.  
    7. sender.sendMessage(ChatColor.RED + "play");
    8. final String t = args[1];
    9. int i = Integer.parseInt(t);
    10. i = i*20;
    11.  
    12. final int loop = this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    13. public void run() {
    14. SoundManager music = SpoutManager.getSoundManager();
    15. music.playCustomMusic(Plugin, (SpoutPlayer)player, args[0], false);
    16. if (w == 0){
    17. Bukkit.getScheduler().cancelTask(loop);
    18. }
    19. }
    20. }
    21. , 0L, i);
    22. }
    23. return true;
    24. }
    25.  
    26.  
    27. else {
    28. sender.sendMessage(ChatColor.RED + "You don't have the permission spouties.play!");
    29. }
    30. }

    "The local variable loop may not have been initialized"
     
  4. Offline

    Europia79

    splint

    You don't have to cancel the task from inside the run() method.

    I'm assuming you're getting 'w' from user input ? If so, just check where you get the user input, then cancel the task there instead.
     
  5. Offline

    splint

    But when I try that, eclipse say he doesn't know the variable loop and tell me to create it.

    So, I used a brutal way to stop that loop, with
    Code:java
    1. Bukkit.getScheduler().cancelTasks(this);

    because I can't find how to stop just the loop task, but that work !

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  6. Offline

    splint

    This time, I can't use that hard method, I need to cancel just one task...

    This is my code. I need to cancel "loop1"

    Code:java
    1. if (pr.getId().startsWith("music") == true) {
    2. String i = pr.getId();
    3. if (i != null){
    4. enter = 1;
    5. url = plugin.getConfig().getString("url." + i);
    6. String timestring = plugin.getConfig().getString("time." + i);
    7. time = Integer.parseInt(timestring);
    8. time = time*20;
    9.  
    10. if (b==0){
    11. b = 1;
    12. @SuppressWarnings("unused")
    13. int loop1 = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    14. public void run() {
    15. SoundManager music = SpoutManager.getSoundManager();
    16. music.playCustomMusic(plugin, (SpoutPlayer)p, url, false);
    17. }
    18. }
    19. , 0L, time);
    20. }
    21. }
    22. }
    23. else {
    24. enter = 0;
    25. b = 0;}
    26. plugin.getServer().getScheduler().cancelTask(loop1); // It say it didn't know the variable loop1. That because it's final, but how to fix it ?
    27. }
    28. }
    29. }


    Anyone ?
     
  7. Offline

    bloodless2010

    This is what I did:
    Declare your BukkitTask (not int),
    Code:
    private BukkitTask loop1;
    Now setup your scheduler like so..
    Code:
    loop1 = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    public void run() {
    SoundManager music = SpoutManager.getSoundManager();
    music.playCustomMusic(plugin, (SpoutPlayer)p, url, false);
    }
    }
    , 0L, time);
    and then you can use this method to cancel it:
    Code:
        public void stopTimer() {
            if (loop1 != null) {
                loop1.cancel();
            }
        }
    Hope I helped! :)
     
  8. Offline

    splint

    Thank bloodless2010, but now, on line 65:
    Code:java
    1. loop1 = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {

    I got "loop1 cannot be resolved to a type"

    If you want to see the full page code: http://pastebin.com/AJKKGgP7
     
  9. Offline

    fireblast709

  10. Offline

    splint

    I need some help :(

    Mhhh, I found a fix. I just remove the
    Code:java
    1. @SuppressWarnings("unused")

    and for some strange reason, that work now. But at this point, I got a new error...
    That simple, when you enter in the specific area, nothing happend, like if the
    Code:java
    1. plugin.getServer().getScheduler().cancelTask(loop1);

    seem to completly "kill" the task and cannot be relaunched

    EDIT: I said nothing, all works !
    Thank you guy's !
     
Thread Status:
Not open for further replies.

Share This Page