Solved Stopping scheduleSyncRepeatingTask

Discussion in 'Plugin Development' started by Omniscimus, Jan 31, 2014.

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

    Omniscimus

    Hey!
    So I'm trying to make a little plugin that spawns firework rockets at certain locations when somebody issues a command. It shouldn't be just one rocket per location; I want the spawning to go on until somebody issues another command. For this purpose, I use a 'scheduleSyncRepeatingTask'.
    Now, the first command, the spawning and the repeating of the whole, works just fine. The problem is the second command. I've been trying to stop the Bukkit Task with Bukkit.getScheduler().cancelTask(id); but they just keep spawning.
    I've also tried to use an 'if' loop with the condition that the code inside the Runnable will only be run if a boolean is true. That way, I thought that if I set the boolean to 'false' if the second command is issued, the code wouldn't be executed anymore. However, this didn't work either.

    I've double-checked my plugin.yml; that one is OK. Eclipse is giving me no errors at all.
    I hope that somebody can help me! ;)





    This is my code:
    Code:java
    1. //A package
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. //And some imports for the Fireworks
    8.  
    9. public final class VuurwerkShow extends JavaPlugin {
    10. public boolean onCommand(final CommandSender sender, Command cmd, final String commandLabel, String[] args) {
    11. if (commandLabel.equalsIgnoreCase("startfireworks")) {
    12.  
    13. //Here are some ArrayLists to be used by the code in the Runnable (working fine)
    14.  
    15. int scheduler = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    16. public void run() {
    17. //Some code that spawns random Firework Rockets at specified locations (working fine)
    18. }
    19. }, 0, 20);
    20.  
    21. if(commandLabel.equalsIgnoreCase("stopfireworks")) {
    22. Bukkit.getScheduler().cancelTask(scheduler); //This part isn't working.
    23. }
    24. }
    25. return true;
    26. }
    27. }
     
  2. Offline

    werter318

  3. Offline

    TheDummy

    The method "scheduleSyncRepeatingTask" returns a BukkitTask. Simply store that BukkitTask to a variable and then use the "cancelTask" method using it as a parameter when you want it to end.

    Code:java
    1. // Store this variable somewhere
    2. BukkitTask taskToCancel = null;
    3.  
    4. // Schedule the task somewhere
    5. taskToCancel = getScheduler().scheduleSyncRepeatingTask( plugin , runnable , delay , period );
    6.  
    7. // Cancel it using the stored variable
    8. getScheduler().cancelTask( taskToCancel );
     
  4. Offline

    Omniscimus

    TheDummy Thanks for your help, but it seems that the method returns an int. And when I turn it into an int, it doesn't work either: the second command gives me no response..

    werter318 Thanks for your help! This is what I made:
    Code:java
    1. class Stopper extends BukkitRunnable {
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. if(commandLabel.equalsIgnoreCase("stopvuurwerk")) {
    4. this.cancel();
    5. }
    6. return true;
    7. }
    8. }

    I'm so sorry, but I'm sort of a dummy at making multiple classes. It gives me the error: 'The type Stopper must implement the inherited abstract method Runnable.run()' How should I implement it?

    (And I like your signature xD)
     
  5. Offline

    TheDummy

    Omniscimus I'm sorry about that. I always mix up the two types. One will return an int and the other returns a BukkitTask. Some principle applies though. Store the taskID integer and then use the integer to cancel it.

    The "must implement Runnable.run()" means you need to make a method in the format:

    Code:java
    1. @Override
    2. public void run()
    3. {
    4. // Put code for what the task should do here
    5. }
     
  6. Offline

    Omniscimus

    Many thanks! I fixed it by putting the cancelTask method in an if-loop with a boolean as the condition inside the Runnable. The command switches the boolean.
     
Thread Status:
Not open for further replies.

Share This Page