Solved Can we handle cancel() method of BukkitRunnable ?

Discussion in 'Plugin Development' started by Zen3515, Mar 27, 2014.

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

    Zen3515

    Code:java
    1. new BukkitRunnable(){
    2.  
    3. @Override
    4. public void run() {
    5. ......
    6. ......
    7. }
    8.  
    9. @Override
    10. public synchronized void cancel()throws IllegalStateException{
    11. can we do this ????? <-------------------------
    12. ......
    13. ......
    14. Bukkit.getScheduler().cancelTask(getTaskId());
    15. }


    When we create getLogger() method in main class(class that extends JavaPlugin) we can change all of old getLogger() method, so is there was any way to change cancel() method in BukkitRunnable() ?

    Update: http://jd.bukkit.org/dev/doxygen/d0/d84/BukkitRunnable_8java_source.html link to BukkitRunnable.java source
     
  2. Offline

    RawCode

    why you ask before testing?
    is someone die when you test things yourself?
     
  3. Offline

    Zen3515

    may be ?
    I said "is there was any way to change" because I 've test and not working....
     
  4. Offline

    RawCode

    post you code then.
     
  5. Offline

    Zen3515

    At "public synchronized void cancel()throws IllegalStateException{" I add "System.out.print"Code reached"".
    after this cancel I didn't receive "Code reached" message.
     
  6. Offline

    Rocoty

    Yes you can override it. I do not think it is final. However I would advise you to call super.cancel() somewhere within the overridden method body. Unless you have a good reason not to
     
    Zen3515 likes this.
  7. Offline

    AmShaegar

    Completely working:
    Code:
    [12:21:20 INFO]: 1
    [12:21:20 INFO]: 2
    [12:21:21 INFO]: 3
    [12:21:22 INFO]: 4
    [12:21:23 INFO]: 5
    >test
    [12:21:24 INFO]: Cancelled!
    Code:java
    1. private BukkitRunnable r;
    2.  
    3. @Override
    4. public void onEnable() {
    5. r = new BukkitRunnable() {
    6.  
    7. private int c = 0;
    8.  
    9. @Override
    10. public void run() {
    11. System.out.println(++c);
    12. }
    13.  
    14. @Override
    15. public void cancel() throws IllegalStateException {
    16. System.out.println("Cancelled!");
    17. super.cancel();
    18. }
    19. };
    20. r.runTaskTimer(this, 0, 20);
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    24. if(commandLabel.equalsIgnoreCase("test")) {
    25. r.cancel();
    26. }
    27. return true;
    28. }
     
    Zen3515 likes this.
  8. Offline

    Zen3515

    I have to say thank you for your two above reply that working but not working perfect,
    I have a lot of creating itself task that I store on List<Integer> taskIDList.
    If I use for(int id : taskIDList) and call getServer().getScheduler().cancelTask(id);, it doesn't use our cancel method.

    Do I have another way to cancel task in "taskIDList" list or I only have to change type of taskIDList to List<BukkitTask> or List<BukkitRunnable> ?
     
  9. Offline

    AmShaegar

    Nope, that way wont ever work. The CraftScheduler never calls the cancel() method. It uses an internal cancel0() method you cannot override. You need to save the BukkitRunnable if you want to use your overridden cancel() method.
     
    Zen3515 likes this.
  10. Offline

    Zen3515

    OK, thank you :)
     
Thread Status:
Not open for further replies.

Share This Page