Cancel ScheduledRepeatingTask

Discussion in 'Plugin Development' started by CapitanChewbacca, Jun 30, 2014.

Thread Status:
Not open for further replies.
  1. I have the following code:
    Code:java
    1. if (command.getName().equalsIgnoreCase("fw")) {
    2. int amount = 1;
    3. final Player pFW = (Player) sender;
    4. if (args.length == 1) {
    5. try {
    6. amount = Integer.valueOf(args[0]);
    7. }
    8. catch (Exception exc) {
    9. p.sendMessage(ChatColor.RED+"That's not a number");
    10. exc.getStackTrace();
    11. return true;
    12. }
    13. if (amount < 1) return Util.sendMessage(p, ChatColor.RED+"The amount must at least be 1");
    14. }
    15. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    16.  
    17. Random r = new Random();
    18.  
    19. @Override
    20. public void run() {
    21. Firework firework = (Firework) pFW.getWorld().spawnEntity(pFW.getLocation(), EntityType.FIREWORK);
    22. FireworkMeta fwm = firework.getFireworkMeta();
    23. fwm.setPower(r.nextInt(2) + 1);
    24.  
    25. int rt = r.nextInt(5) + 1;
    26. FireworkEffect.Type type = FireworkEffect.Type.BALL;
    27. if (rt == 1) type = FireworkEffect.Type.BALL;
    28. if (rt == 2) type = FireworkEffect.Type.BALL_LARGE;
    29. if (rt == 3) type = FireworkEffect.Type.BURST;
    30. if (rt == 4) type = FireworkEffect.Type.CREEPER;
    31. if (rt == 5) type = FireworkEffect.Type.STAR;
    32.  
    33. List<Color> colors = new ArrayList<Color>();
    34. int rc = r.nextInt(3) + 1;
    35. for (int i = 0; i < rc; i++) {
    36. colors.add(Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
    37. }
    38. List<Color> colorsFade = new ArrayList<Color>();
    39. int rcf = r.nextInt(2) + 1;
    40. for (int i = 0; i < rcf; i++) {
    41. colorsFade.add(Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
    42. }
    43.  
    44. FireworkEffect fwe;
    45. if (r.nextInt(2) == 0) {
    46. fwe = FireworkEffect.builder().with(type).withColor(colors).flicker(r.nextBoolean()).trail(r.nextBoolean()).build();
    47. } else {
    48. fwe = FireworkEffect.builder().with(type).withColor(colors).withFade(colorsFade).flicker(r.nextBoolean()).trail(r.nextBoolean()).build();
    49. }
    50. fwm.addEffect(fwe);
    51. firework.setFireworkMeta(fwm);
    52. }
    53. }, 0L, 5L);
    54. p.sendMessage(ChatColor.BLUE+"Launched"+((amount == 1) ? " a firework" : " "+ amount +" fireworks"));
    55. return true;
    56. }

    Now, this task is happening every 5 ticks. The problem is that I would like to stop it after an specific amount of times.
    Quick review of the code:
    - It checks if the player typed an amount.
    - It starts a task that will spawn random fireworks every 5 ticks (1/4 second).
    - After an specific amount of fireworks launched the task will stop (Thats what I can't do).
     
  2. Offline

    The Fancy Whale

    this.cancel();
    Or you can get the task name or task id and cancel it that way. But if you put the cancel inside of the repeating you would want to do this.cancel();
     
  3. Offline

    3Shawnster

    Define an int as it.
    ex:
    Code:java
    1. PluginManager pm;
    2. int i;
    3.  
    4. public void onEnable()
    5. {
    6. i = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    7. {
    8.  
    9. public void run()
    10. {
    11. //do stuff
    12. }
    13.  
    14. }, 20l, 20l);
    15. }
    16.  
    17. public void onDisable()
    18. {
    19.  
    20. getServer().getScheduler().cancelTask(i);
    21.  
    22. }
     
  4. There are a few methods but none of the is cancel. http://prntscr.com/3y71xf
     
  5. Offline

    The Fancy Whale

    Code:java
    1. public static BukkitRunnable tasks; //not in a method

    Code:java
    1. tasks = new BukkitRunnable() {
    2.  
    3. public void run(){
    4.  
    5.  
    6. this.cancel(); //if you want to cancel within the runnable
    7.  
    8.  
    9. }
    10. };
    11. tasks.runTaskTimer(plugin, 0L, 20L);
    12. tasks.cancel(); //if you want to cancel elsewhere

    Like that?
     
  6. Ohh, I was using the java Runnable, not BukkitRunnable. Thanks ^^
     
    The Fancy Whale likes this.
  7. Offline

    The Fancy Whale

    No problem glad I could help :D
     
    CapitanChewbacca likes this.
  8. The Fancy Whale Sorry I'm bothering you again but now I have an error when I cancel the task. This is my actual code:
    Code:java
    1. if (command.getName().equalsIgnoreCase("fw")) {
    2. int amount1 = 1;
    3. final Player pFW = (Player) sender;
    4. if (args.length == 1) {
    5. try {
    6. amount1 = Integer.valueOf(args[0]);
    7. }
    8. catch (Exception exc) {
    9. p.sendMessage(ChatColor.RED+"That's not a number");
    10. exc.getStackTrace();
    11. return true;
    12. }
    13. if (amount1 < 1) return Util.sendMessage(p, ChatColor.RED+"The amount must at least be 1");
    14. }
    15. final int amount = amount1;
    16. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
    17.  
    18. Random r = new Random();
    19. int i = 0;
    20.  
    21. @Override
    22. public void run() {
    23. i++;
    24. if (i > amount) this.cancel();
    25. Firework firework = (Firework) pFW.getWorld().spawnEntity(pFW.getLocation(), EntityType.FIREWORK);
    26. FireworkMeta fwm = firework.getFireworkMeta();
    27. fwm.setPower(r.nextInt(2) + 1);
    28.  
    29. int rt = r.nextInt(5) + 1;
    30. FireworkEffect.Type type = FireworkEffect.Type.BALL;
    31. if (rt == 1) type = FireworkEffect.Type.BALL;
    32. if (rt == 2) type = FireworkEffect.Type.BALL_LARGE;
    33. if (rt == 3) type = FireworkEffect.Type.BURST;
    34. if (rt == 4) type = FireworkEffect.Type.CREEPER;
    35. if (rt == 5) type = FireworkEffect.Type.STAR;
    36.  
    37. List<Color> colors = new ArrayList<Color>();
    38. int rc = r.nextInt(3) + 1;
    39. for (int i = 0; i < rc; i++) {
    40. colors.add(Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
    41. }
    42. List<Color> colorsFade = new ArrayList<Color>();
    43. int rcf = r.nextInt(2) + 1;
    44. for (int i = 0; i < rcf; i++) {
    45. colorsFade.add(Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
    46. }
    47.  
    48. FireworkEffect fwe;
    49. if (r.nextInt(2) == 0) {
    50. fwe = FireworkEffect.builder().with(type).withColor(colors).flicker(r.nextBoolean()).trail(r.nextBoolean()).build();
    51. } else {
    52. fwe = FireworkEffect.builder().with(type).withColor(colors).withFade(colorsFade).flicker(r.nextBoolean()).trail(r.nextBoolean()).build();
    53. }
    54. fwm.addEffect(fwe);
    55. firework.setFireworkMeta(fwm);
    56. }
    57. }, 0L, 5L);
    58. p.sendMessage(ChatColor.BLUE+"Launched"+((amount1 == 1) ? " a firework" : " "+ amount1 +" fireworks"));
    59. return true;
    60. }

    And this is the error:
    Code:
    [17:15:18] [Server thread/WARN]: [TestPlugin] Task #32 for TestPlugin v1.0 generated an exception
    java.lang.IllegalStateException: Not scheduled yet
        at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.scheduler.BukkitRunnable.cancel(BukkitRunnable.java:18) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at com.capitanchewbacca.test.Test$3.run(Test.java:582) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Why is this happening?
     
Thread Status:
Not open for further replies.

Share This Page