cannot cancel bukkittask

Discussion in 'Plugin Development' started by atesin, Apr 9, 2014.

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

    atesin

    hi ... i am making a custom portals plugin, i am using BukkitRunnable

    i had try to follow instructions at scheduling wiki, but is not that specific (only tells about self canceling a task) and what i had tried does not work for me, i am storing tasks in a map

    i had created 2 events that works fine (enter, leave portal) and one task (teleport), entering the portal delays the teleport task 4 sec, and leaving should cancel but it does not
    Code:java
    1. public final class MyListener implements Listener{
    2.  
    3. private static Map<String, BukkitTask> warps = new HashMap<String, BukkitTask>();
    4.  
    5. // constructor, register listener, etc
    6.  
    7. @EventHandler
    8. public void onPortalEnter(PortalEnterEvent event){
    9.  
    10. console.log("enter portal");
    11. warps.put(event.player.getName(), new WarpTask(event.player, event.location).runTaskLater(plugin, 80));
    12. }
    13.  
    14. @EventHandler
    15. public void onPortalLeave(PortalLeaveEvent event){
    16.  
    17. console.log("leave portal");
    18. warps.get(event.player.getName()).cancel();
    19. warps.remove(event.player.getName());
    20. }
    21. }
    Code:java
    1. public class WarpTask extends BukkitRunnable{
    2. private Player player;
    3. private Location dest;
    4.  
    5. // constructor, etc
    6.  
    7. @Override
    8. public void run(){
    9.  
    10. // do the teleport
    11. console.log("teleported");
    12. }
    13. }


    if you enter a portal and wait 4sec and console works, it displays
    > enter portal
    > teleported

    if you enter and leave at once it should display
    > enter portal
    > leave portal

    but instead shows
    > enter portal
    > leave portal
    > teleported

    i had tried many combinations, what i am doing wrong?
     
  2. Offline

    IvanTheForth

    atesin First things first, I recommend that you do not use a hashmap for that. In your runnable class, you can have a string variable for the player in the constructor, and a method that returns that. Then, you could have the WarpTask initialized in the constructor of your "MyListener" class. That way, you could reference it in both the onPortalLeave() and onPortalEnter() methods. This may not solve your problem, but it will definitely clean up your code.
     
  3. Offline

    atesin

    i cant see the problem of storing tasks in a hashmap since all i am storing are references .. i cannot think another way to link and retrieve a player with his task .. i am trying to follow advices that tell me not to use scheduler

    i tried to create and store WarpTasks also, then get it and call runTaskLater() to try cancelling, i tried to ovverrride some methods also with no result

    i think the problem is once i create BukkitTask (by calling runTaskLater(), or WarpTasks) i can not access it anymore, then i cant cancel not call any method or property

    help me please

    i resolved this way, probably is not the better way but is the only i found

    store taskId's, and in run() method check if task id is present before run code

    Code:java
    1. public final class MyListener implements Listener{
    2.  
    3. public static Map<String, Integer> warps = new HashMap<String, Integer>();
    4.  
    5. // constructor, register listener, etc
    6.  
    7. @EventHandler
    8. public void onPortalEnter(PortalEnterEvent event){
    9.  
    10. console.log("enter portal");
    11. WarpTask warp = new WarpTask(this, event.player, event.location);
    12. // must schedule task before call getTaskId()
    13. warp.runTaskLater(plugin, 80);
    14. warps.put(event.player.getName(), warp.getTaskId());
    15.  
    16. }
    17.  
    18. @EventHandler
    19. public void onPortalLeave(PortalLeaveEvent event){
    20.  
    21. console.log("leave portal");
    22. warps.remove(event.player.getName());
    23. }
    24. }
    Code:java
    1. public class WarpTask extends BukkitRunnable{
    2. private Player player;
    3. private Location dest;
    4. private MyListener parent;
    5.  
    6. public WarpTask(MyListener parent, Player player, Location loc) {
    7. this.parent = parent;
    8. this.player = player;
    9. this.dest = loc;
    10. }
    11.  
    12. @Override
    13. public void run(){
    14.  
    15. if (!(parent.warps.containsValue(this.getTaskId()))){
    16.  
    17. // canceled
    18. console.log("canceled");
    19. return;
    20. }
    21.  
    22. // do teleport (player, location)
    23. console.log("teleported");
    24. }
    25. }


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

Share This Page