Cancel DelayedTask?

Discussion in 'Plugin Development' started by OverDodo, Feb 25, 2017.

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

    OverDodo

    Hey! I just coded a /spawn Command, which teleports the player after 3 Seconds. Could you help me cancelling it, if the player moves? Here is my code;


    PHP:
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                       
                        public 
    void run() {
                           
                        
    pl.sendMessage("§cYou were teleported to the Spawn!");
                        
    pl.teleport(Spawn);
                        }
                        }, 
    60L);
     
  2. Offline

    mine-care

    @OverDodo You should store the task in a map alongside player's uuid so that in the PlayerMoveEvent handler you can retreive the task (if any) and cancel it ;)
    Also make sure you follow naming conventions!
     
  3. Offline

    OverDodo

    Hey,

    I know, I shouldn't ask for that, but I never did smth with HashMaps before... Could you give me a code pretty please? :/
     
  4. Offline

    mine-care

    Absolutely not :p
    It is very easy for me to give you the code and send you on your way but experience shows that sooner or later you will either be back with another (similar) problem or the code is just going to be coppied over and pasted without bothering to understand the Datastructures and techniques used...
    That is just one of the reasons why we collectively avoid spoonfeeding.

    Take a look into Maps they are really simple and fun! They are essentially tables with two columns where the first column has unique keys and then the other column stores the value. So if you put the value 501 with key "room", and you get the value by key "room" you will be given back 501.
    Any Class type can be used as a key and or value. ;)
     
    Banjer_HD likes this.
  5. I recommend watching a youtube tutorial about HashMaps ;)
    It will take more time then if I just type it for you... But you will need it later on! :p

    EDIT: You can also use a HashMap with a UUID and a Boolean and set the boolean to false at the start. And if he moves(and is in the list), set it to true. Check for the boolean when you return the player to spawn and dont forget to remove the player from the HashMap ;)
     
  6. Offline

    RevvyDevvyDoo

    The BukkitRunnable object contains a 'cancel()' method. That's what you need to cancel the task.
     
Thread Status:
Not open for further replies.

Share This Page