Cancel several repeating tasks

Discussion in 'Plugin Development' started by natinusala, Jan 2, 2013.

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

    natinusala

    Hello,

    I've got a problem with my repeating tasks. Each time a block is placed, this code is called :

    Code:
    taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {         
      @Override
      public void run()
      { 
        Bukkit.getServer().broadcastMessage("Dummy" + block.getX() + " " + block.getY() + " " + block.getZ()); 
      },
    0L, 200L);
    And when a block is destroyed :

    Code:
    Bukkit.getServer().getScheduler().cancelTask(taskID);
    
    When I place a block, everything works well, and the tasks stops well too when I break this block. Unfortunately, if I place two blocks and break the first one, the second one is destroyed instead of the first one.
    I think it's a task ID problem, how could I solve it ?
    Thanks !
     
  2. Offline

    Deathmarine

    Add the (taskid) to a list and cancel against the list.
     
  3. Offline

    raGan.

    You will have to remember taskID for each task you run and then cancel the ones you don't need anymore. Or you can have one master task and set of stored blocks saved somewhere.
     
  4. Offline

    natinusala

    Yes, a table with the block coordinates and the task id ?

    How can I remember each task ID ?
     
  5. Offline

    raGan.

    scheduleSyncRepeatingTask() returns it, that is why you do
    int taskID = taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(..)
     
  6. Offline

    natinusala

    Okay, so I do
    int task;
    taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(..)
    run{
    task = taskID;
    }

    But task will be overwritted if run is called each 10 seconds ?
     
  7. Offline

    fireblast709

    use a BukkitRunnable
    Code:java
    1. new BukkitRunnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. if(shouldCancel)
    7. {
    8. this.cancel();
    9. }
    10. }
    11. }.runTaskTimer(<plugin instance>, <initial delay>, <interval delay>);
     
  8. Offline

    natinusala

    Thanks, but
    taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {

    Gives me : The method scheduleSyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (Plugin, BukkitTask).

    If I try
    taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable(), 1, 1 {

    It gives me : Cannot instantiate the type BukkitRunnable.
     
  9. Offline

    fireblast709

    ^ use it like that
     
  10. Offline

    natinusala

    Yes, but what's before ? Nothing ?
     
  11. Offline

    fireblast709

    it would replace the Bukkit.getScheduler().scheduleSyncRepeatingTask(stuff)
     
  12. Offline

    natinusala

    Huuu okay, no problem. I'll try this.

    edit: Thanks it works, but how can I cancel it from another method, like onBlockBreak ?
     
  13. Offline

    Gungsu

    try use:

    public int task;

    in task:
    task = this.getTaskId();

    in onBlockBreak(){
    Bukkit.getScheduler().cancelTask(task);
    }

    but I try use: this.cancel(); and return a error in my server:
    and the cancelTask(task) return error too.

    Code:
    00:49:30 [WARNING] [tesourocast] Task #17 for tesourocast v1.0 generated an exce
    ption
    java.lang.IllegalStateException: Not scheduled yet
            at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:128
    )
            at org.bukkit.scheduler.BukkitRunnable.cancel(BukkitRunnable.java:17)
            at me.amauri.tesouro.castelo$1.run(castelo.java:62)
            at org.bukkit.craftbukkit.v1_4_6.scheduler.CraftTask.run(CraftTask.java:
    53)
            at org.bukkit.craftbukkit.v1_4_6.scheduler.CraftScheduler.mainThreadHear
    tbeat(CraftScheduler.java:345)
            at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:53
    0)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:22
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:49
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
     
  14. Offline

    Gungsu

    I resolved thus:
    Code:
    public int task;
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    ...codes...
    task = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable(){
                    public void run() {
                                            if (condicion for cancel) {
                                                  Bukkit.getScheduler().cancelTask(task);
                                    }
            }, 0L, 1200L);
    }
     
Thread Status:
Not open for further replies.

Share This Page