Solved Cancelling ASync Task

Discussion in 'Plugin Development' started by YoFuzzy3, Dec 5, 2012.

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

    YoFuzzy3

    So I've been trying to get this to work for quite a while. I've searched the Bukkit Forums multiple times on how to cancel these tasks but I can't get it to work.

    Here's my code:
    Code:java
    1.  
    2. private static int goldBoots;
    3.  
    4. @EventHandler(priority = EventPriority.MONITOR)
    5. public void goldBoots(InventoryCloseEvent event){
    6. final Player player = (Player) event.getPlayer();
    7. final ItemStack boots = player.getInventory().getBoots();
    8. PotionEffectType jumpEffectType = PotionEffectType.JUMP;
    9. final PotionEffect jumpEffect = new PotionEffect(jumpEffectType, 100, 1);
    10. goldBoots = getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    11. public void run(){
    12. if(boots != null){
    13. if(boots.getType() == Material.GOLD_BOOTS){
    14. getServer().broadcastMessage("Start");
    15. player.addPotionEffect(jumpEffect, true);
    16. }else{
    17. getServer().broadcastMessage("Stop");
    18. Bukkit.getScheduler().cancelTask(goldBoots);
    19. }
    20. }else{
    21. getServer().broadcastMessage("Stop");
    22. Bukkit.getServer().getScheduler().cancelTask(goldBoots);
    23. }
    24. }
    25. }, 0L, 60L);
    26. }
    27.  


    I've put in debug messages so I know what's going on. Once I put on gold boots the loop runs and continuously sends the "Start" message. When I take off the gold boots it successfully triggers the "Stop" message but at the same time the "Start" message just keeps running. If anyone can help me solve this I'd greatly appreciate it.

    Thanks.
     
  2. Offline

    Wolvereness Bukkit Team Member

    Use a BukkitRunnable. Also, you shouldn't be using "Async."
     
  3. Offline

    YoFuzzy3

    So I changed Runnable to BukkitRunnable and Async to Sync, same thing still happens.

    I only used an Async task because that's what was in the example on the wiki. I'm a visual learner, so I often don't take in everything I read, so from the example pieces of code on the wiki I thought that you use Sync for a scheduled task and Async for a repeating task. Now that I read the wiki properly I see my mistake, and I feel a bit stupid for making it. But did the Async task really have to be deprecated? I feel like I caused it, and that others will hate me for it.
     
  4. Offline

    Tirelessly

    What are you talking about? He means dont use Async because you're trying to use methods which are not thread safe.
     
  5. Offline

    YoFuzzy3

  6. Offline

    feildmaster

    What the hell wiki page did you look at, so I can remove it. (also, it was deprecated because it's not needed and causes confusion)
     
  7. Offline

    YoFuzzy3

    http://wiki.bukkit.org/Scheduler_Programming
    The wiki is fine, I just didn't read properly.
     
  8. Offline

    Tirelessly

  9. Offline

    YoFuzzy3

    Anyway can we go back on topic? Why isn't my SyncRepeatingTask stopping once I take off the gold boots?

    Code:java
    1.  
    2. private static int goldBoots;
    3.  
    4. @EventHandler(priority = EventPriority.MONITOR)
    5. public void goldBoots(InventoryCloseEvent event){
    6. final Player player = (Player) event.getPlayer();
    7. final ItemStack boots = player.getInventory().getBoots();
    8. PotionEffectType jumpEffectType = PotionEffectType.JUMP;
    9. final PotionEffect jumpEffect = new PotionEffect(jumpEffectType, 100, 1);
    10. goldBoots = getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable(){
    11. public void run(){
    12. if(boots != null){
    13. if(boots.getType() == Material.GOLD_BOOTS){
    14. getServer().broadcastMessage("Start");
    15. player.addPotionEffect(jumpEffect, true);
    16. }else{
    17. getServer().broadcastMessage("Stop");
    18. Bukkit.getScheduler().cancelTask(goldBoots);
    19. }
    20. }else{
    21. getServer().broadcastMessage("Stop");
    22. Bukkit.getServer().getScheduler().cancelTask(goldBoots);
    23. }
    24. }
    25. }, 0L, 60L);
    26. }
    27.  
     
  10. Offline

    Tirelessly

    Because boots will never be null. You're not updating the reference to the player's boots.
     
  11. Offline

    YoFuzzy3

    Oh I see, it works now. Thank you.
     
  12. Offline

    Wolvereness Bukkit Team Member

    On another note, the practice should be simply making a BukkitRunnable, and calling one of its methods to schedule it. If you use the designated methods, it lets you call this.cancel() from inside of itself.
     
  13. Offline

    fireblast709

    Since when is BukkitRunnable implemented?
     
Thread Status:
Not open for further replies.

Share This Page