Solved Stopping Runnables

Discussion in 'Spigot Plugin Development' started by MakingTheMagic, May 11, 2016.

Thread Status:
Not open for further replies.
  1. Hello there!

    I have this code, that runs every 10 seconds.
    Code (Text):
     final BukkitTask id = new BukkitRunnable()
                             {
                             public void run()
                             {
                                  int color = r.nextInt(4);
                                  if(color == 1)
                                  {
                                      player.getInventory().setItem(3, green);
                                  }
                                  if(color == 2)
                                  {
                                      player.getInventory().setItem(3, yellow);
                                  }
                                  if(color == 3)
                                  {
                                      player.getInventory().setItem(3, red);
                                  }
                                  if(color == 0)
                                  {
                                      player.getInventory().setItem(3, white);
                                  }
                             }                                    
                }.runTaskTimer(JavaPlugin.getPlugin(MainClass.class), 0, 2 * 20);
    But I want it to stop after a certain amount of time so I have a runnable that runs later:

    Code (Text):
         new BukkitRunnable()
                 {
                 public void run()
                 {
                     
                    id.cancel();
               
                   
                       ItemStack ride = new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte)8);
                        ItemMeta ridem = ride.getItemMeta();
                       
                        ridem.setDisplayName("Ā§cSaved!");
                        ride.setItemMeta(ridem);
                       
                   

                        player.getInventory().setItem(3, ride);
                 }                                    
    }.runTaskLater(JavaPlugin.getPlugin(MainClass.class), 1360);
                                   
    But it doesn't stop. It just sets the item to the inventory and removes it continuing the 10 second runnable.

    Any ideas?

    Thank you,
    any advice is appreciated!

    :D
     
  2. Try to restore the Bukkit.getScheduler().runTaskTimer(...).getTaskId(); to an integer object, then make a runTaskLater and do Bukkit.getScheduler().cancelTask(id);
    I haven't tested this out, but hope it works for you.
    Code (Text):
            int id = Bukkit.getScheduler().runTaskTimer(Main.instance, new Runnable() {
                public void run() {
                    //TODO: stuff.
                }
            }, 20, 20).getTaskId();
            Bukkit.getScheduler().runTaskLater(Main.instance, new Runnable() {
                public void run() {
                    //TODO: cancel id.
                    Bukkit.getScheduler().cancelTask(id);
                }
            }, 100);
     
    • Agree Agree x 1
  3. Thank you, I will try this now!
     
  4. Using BukkitRunnable is better than Runnable, Bukkit didn't add them because why not... You can just cancel the task with cancel(). That's by far cleaner than messing with IDs.
     
    • Agree Agree x 1
  5. Thank you! :D
     
Thread Status:
Not open for further replies.