Solved Cancel a delayed task when player moves.

Discussion in 'Plugin Development' started by xItsJ0hny, Dec 18, 2018.

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

    xItsJ0hny

    Hello,

    So I want to cancel a delayed task when the player moves. So far I add the player who runs the command into an arraylist and remove him from it after task runs, but then I'm confused on what code I should add into the player move event. First, I check if the player is in the arraylist and remove him in the end but how can I cancel the specific task because I have couple more of them.

    PS: If you need the code, I can provide the parts needed.

    Regards,
    iJ0hny.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @xItsJ0hny Make a map<uuid, BukkitTask>, then you can cancel it.
     
  3. Offline

    xItsJ0hny

    @timtower So I should delete the arraylist? Also, how do I make clear to the hashmap which task I refer to? Because I got 3 of them as a whole (1 inside the command and outside on 2 different events).
     
  4. Offline

    timtower Administrator Administrator Moderator

    ArrayList will be replaced by the hashmap.
    The task that you want to cancel should be the value in the hashmap.
     
  5. Offline

    xItsJ0hny

    @timtower With hashmaps though I can't do {hashmap_name}.add(player); like in arraylist
     
  6. Offline

    timtower Administrator Administrator Moderator

    @xItsJ0hny No, you do HashMap#put(key, value)
     
  7. Offline

    xItsJ0hny

    @timtower Ok, so instead of key I need the UUID of the player and the value I replace it with the runnable or?
     
  8. Offline

    timtower Administrator Administrator Moderator

    UUID is correct, and runnables create a BukkitTask I believe when you start then, put that as value in the map.
    Those tasks have a cancel method.
     
  9. Offline

    xItsJ0hny

    @timtower I already have the task that I want to cancel ( in screenshot ).
     

    Attached Files:

  10. Offline

    timtower Administrator Administrator Moderator

    @xItsJ0hny scheduleSynchDelayedTask has a return value that you are not using.
    That would be the BukkitTask.
     
  11. Offline

    xItsJ0hny

    @timtower Ah, maybe an example would help. Is that return value supposed to be inside the player move event or?
     
  12. Offline

    timtower Administrator Administrator Moderator

    Can't copy the code from an image.
    And this:
    BukkitTask task = scheduleSynchDelayedTask<rest of the code>

    Then put that task in the map.
     
  13. Offline

    xItsJ0hny

    @timtower There's the code. Still though, where you say <rest of the code> you mean just the (this, new Runnable()); ?

    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
             public void run() {
               p.teleport(loc);
               p.sendMessage(ChatColor.translateAlternateColorCodes('&',SS.this.getConfig().getString("Spawn-Message")));
               if (SS.this.getConfig().getBoolean("Spawn-Effect")) {
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
               }
             }
           }, 20 * getConfig().getInt("Cooldown"));
     
  14. Offline

    timtower Administrator Administrator Moderator

    @xItsJ0hny And everything that was behind it.
    Code:
    BukkitTask task = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
             public void run() {
               p.teleport(loc);
               p.sendMessage(ChatColor.translateAlternateColorCodes('&',SS.this.getConfig().getString("Spawn-Message")));
               if (SS.this.getConfig().getBoolean("Spawn-Effect")) {
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
               }
             }
           }, 20 * getConfig().getInt("Cooldown"));
    map.put(p.getUniqueID(), task);
     
  15. Offline

    xItsJ0hny

    @timtower It's all highlighted red for some reason. Is there a specific place to put it or?
     
  16. Offline

    timtower Administrator Administrator Moderator

    Hover your mouse over it, find out why it is highlighted.
     
  17. Offline

    xItsJ0hny

    @timtower Type mismatch: cannot convert from int to BukkitTask and it gives me a quick fix of replacing BukkitTask with int.
     
  18. Offline

    timtower Administrator Administrator Moderator

    Don't you get deprecation warnings on that scheduleSyncDelayedTask than?
     
  19. Offline

    xItsJ0hny

    @timtower Nope. No warnings on that task mate.
     
  20. Offline

    timtower Administrator Administrator Moderator

    I use my runnables in a different way.
    I use something like this:
    Code:
    BukkitTask task = new BukkitRunnable()
    {
             public void run() {
               p.teleport(loc);
               p.sendMessage(ChatColor.translateAlternateColorCodes('&',SS.this.getConfig().getString("Spawn-Message")));
               if (SS.this.getConfig().getBoolean("Spawn-Effect")) {
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
                 p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 0);
               }
             }
           }.runTaskLater(plugin, 20*getConfig().getInt("Cooldown"));
    map.put(p.getUniqueID(), task);
    Forgive the formatting, written without IDE.
     
  21. Offline

    xItsJ0hny

    @timtower I see. So, should I rewrite the task your way or?
     
  22. Offline

    timtower Administrator Administrator Moderator

    You are the programmer, it is your code. I am just giving options.
     
  23. Offline

    xItsJ0hny

    @timtower I understand. Though, I still can't quite find a way to make this work. Maybe a screenshot of my whole code would help you?
     
  24. Offline

    timtower Administrator Administrator Moderator

    Screenshots of code don't work really well.
     
  25. Offline

    xItsJ0hny

    @timtower Maybe my code in pastebin then?
     
  26. Offline

    timtower Administrator Administrator Moderator

    Or on here using code blocks.
     
  27. Offline

    xItsJ0hny

    @timtower I actually got it working, but now I want it to be a true/false before all this happens. So I placed an if checking a specific boolean in config file. Though, when it's true it works fine. But when it's set to false it doesn't work properly. Any ideas?
     
  28. Offline

    timtower Administrator Administrator Moderator

    Can't say anything about it.
    I have no idea where you are checking things.
     
  29. Offline

    xItsJ0hny

  30. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page