Bukkit runnable help

Discussion in 'Spigot Plugin Development' started by Ean244, Jun 7, 2017.

  1. I am trying to reset an itemstacks lore that shows a countdown time every second in a runnable. i tried using inv#setItem() but the lore does not change. anyone know why?o_O
    • Friendly Friendly x 1
  2. Try updating the lore instead of setting the item. Also, show us your code
  3. Would it be possible to see your code so that we can help you?


    Serait-il possible de voir ton code pour qu'on puisse t'aider ?
  4. Code (Text):
    Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable() {
                ItemStack pot_stack = new ItemBuilder(new ItemStack(Material.CAULDRON)).setName(SlotMachine.formatChatColor(.pot + " &7in the pot").toItemStack();
                ItemStack clock = new ItemBuilder(new ItemStack(Material.WATCH)).setName(formatChatColor("W")).setLore(formatChatColor("&7Time left uaw is &2" + format)).toItemStack();
                public void run() {
                    format = getformatTime();
                        inv.setItem(47, pot_stack);
                        inv.setItem(51, clock);
                    }, 0, 20);
  5. nvm fixed it. Thanks anyway:)
  6. How did i fix?
  7. Update the inventory

    Inviato dal mio SM-G930F utilizzando Tapatalk
  8. #8 Ean244, Jun 7, 2017
    Last edited: Jun 7, 2017
  9. You have to save the task ID to a variable and call the Bukkit.getScheulder().cancelTask( int id ); method
  10. int task;

    task = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable() {

    • Like Like x 1
  11. Thought of it but,
    It is stored as BukkitTask, and the method takes an int; What should i do?
  12. Then use "scheduleSyncRepeatingTask" method.
    • Like Like x 1
  13. thanks for your help
  14. BukkitRunnables have their own cancel method. No need to revert to the old bukkit scheduler. ;)
  15. but the local bla bla bla havent beeen initialised

    @BillyGalbreath any suggestions?
  16. I don't understand. If you haven't initialized the BukkitRunnable and started it, then why are you worried about cancelling it?
  17. ill send u my code
    Code (Text):
    int taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    if(something) {
    Bukkit.getServer().getScheduler().cancelTask(taskID); //The local variable taskID may not have been initialized
  18. That's not a BukkitRunnable. You are using the old scheduler system which doesn't include all the fancy new tools. Using the old scheduler is harder to work with.
    • Winner Winner x 1
  19. If you look at the code you are creating an anonymous inner class by opening and closing the new Runnable() in brackets. Hence local is inside that "anonymous class", so the only way to cancel it is if you initialize the outer class and have the task ID be static or pass the ID through the runnable, and if you choose to pass a new argument into the runnable you would have to modify the existing Runnable class so instead you can make a custom class that implements Runnable.
  20. Code (Text):
    new BukkitRunnable() {
    if(something) {
    Bukkit.getServer().getScheduler().cancelTask(this.getTaskId());// can i do this?
    ok i changed my code
    now its working...
    Thanks!:p @BillyGalbreath
    But can you check if this is valid