Self-cancelling SyncRepeatingTask

Discussion in 'Plugin Development' started by foldagerdk, Jan 29, 2014.

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

    foldagerdk

    Hey guys!

    I need assistance creating a SyncRepeatingTask which cancels itself if a condition is true.

    This is what I currently have:
    Code:java
    1. int TaskID = getPlugin().getServer().getScheduler().scheduleSyncRepeatingTask(getPlugin(), new Runnable() {
    2. public void run() {
    3. if (someBoolean()){
    4. //stuff
    5. } else {
    6. //nothing
    7. getPlugin().getServer().getScheduler().cancelTask(TaskID);
    8. }
    9. }
    10. }, 1, 1);


    What I intend to do is run //stuff every 1 tick, with a 1 tick delay on the first - but only as long as someBoolean is true. If the someBoolean value is changed to false, //nothing is done and the task with TaskID id is cancelled.

    This does not happen however, and my IDEA tells me the following:
    Initializer "TaskID" is redundant.
    Variable "TaskID" is accessed from inner class and needs to be declared final.

    Declaring TaskID as final does not change anything. I would really prefer seeing snippets as well as explanations. Thanks in advance! :)

    EDIT: Linking me to the docs will not help me, as I have tried to read them through multiple times without any luck.
     
  2. Offline

    werter318

    TaskId will be assigned to an id that gets returned after it's done, so I recommend to make a seperate class for it, and let it extend BukkitRunnable, that way you can easily call this.cancel() (Might be a different method).
     
  3. Offline

    foldagerdk


    werter318 Thank you!
    I got it working! I have another issue though.. How can I transfer a from my first class to the class that extends BukkitRunnable? Like say I have an Arrow Object and I want the BukkitRunnable to do something with each arrow individually..?
     
  4. Offline

    werter318

    foldagerdk Make a constructor with an arrow object as parameter, and then store it in the class :)
     
  5. Offline

    foldagerdk

    werter318
    Can you give me an example? ;)
     
  6. Offline

    Desle

    foldagerdk
    I always use a new method with the ScheduleSyncDelayedTask, and it would just repeat the method over and over again, unless not told to
     
  7. Offline

    foldagerdk

    That is so close to what I had! Thank you! :)
     
    KraZ__ likes this.
  8. Offline

    werter318

    KraZ__ Why would you make an abstract class that basically just swaps the run method with a loop method, and the cancel thing doesn't make sense either... I'm sorry xD

    EDIT: If you only call cancel, it will keep on running forever, which is something you DON'T want.

    KraZ__ Except it isn't because the only thing you added is a cancel method, but if you call that method, the Runnable isn't going to stop instead it's still going to check if it's cancelled everytime it runs, which is ineffecient.

    But because this is off-topic, I won't reply on this subject any more. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 11, 2022
Thread Status:
Not open for further replies.

Share This Page