Counter On Move Cancel

Discussion in 'Plugin Development' started by FuZioN720, May 28, 2013.

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

    FuZioN720

    Hello, i am wondering how to make it so if a counter is going and the player moves it cancels the counter and event. For example i have something going on action right click air, this action make a delay so so after 3 secs the player gets teleported, but i want to make it so if the player moves in the 3 secs cancel the counter and the teleport and send the player a message of cancellation.

    Here is my action code:
    Code:java
    1.  
    2. if ((event.getAction() == Action.RIGHT_CLICK_AIR) &&
    3. (player.getItemInHand().getItemMeta().hasDisplayName()) &&
    4. (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD +"Hearthstone"))) {
    5. try {
    6. player.sendMessage(ChatColor.GOLD + "Teleporting in" + ChatColor.RED + " 3 secs " + ChatColor.GOLD + "Don't move.");
    7. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    8. public void run() {
    9. player.teleport(getHearthstone(player));
    10. player.sendMessage(ChatColor.GREEN + "You have been teleported to your Hearthstone's location.");
    11. }
    12. }, 60L);
    13. } catch (Exception e) {
    14. player.sendMessage(ChatColor.RED + "You might not have set your Hearthstone location.");
    15. }
    16. }
    17.  
     
  2. Offline

    Rocoty

    scheduleSyncDelayedTask returns an integer ID of the task. You can store that integer to a list or set, and then use it later when you call getServer().getScheduler().cancelTask(taskid); And you'd probably want to call that in an event handler for PlayerMoveEvent
     
  3. Offline

    FuZioN720

    Rocoty I'm a little confused i get what your saying but can you just make code so i can understand it better thanks BTW
     
  4. Offline

    Rocoty

    I can make some pseudo-code for you:

    Code:
    if condition to start teleport is true
        int taskId = scheduler.scheduleSyncDelayedTask(args);
        teleportTimers.put(player's name, taskId)
     
    PlayerMoveEvent handler declaration
        scheduler.cancelTask(teleportTimers.get(player's name))
        teleportTimers.remove(player's name)
    Might want to remove the entry when the run() method in the Runnable is called as well... teleportTimers is a Map by the way
     
  5. Offline

    BajanAmerican

    FuZioN720 I would personally make a boolean and check for if the player's location has changed. If so, the the boolean is true, and if the boolean is true, cancel the event.
     
  6. Offline

    FuZioN720

  7. Offline

    Rocoty

    FuZioN720 Weeeeeell, you'd still have to cancel the task somehow. If I get BajanAmerican correctly, it would do just the same as my suggestion, except it would just refrain from teleporting when the task gets run. You'd still have to create a Map for the player to put their boolean. Oh, and it would require the task to know which player it is and all that stuff...

    But I might be getting their suggestion completely wrong...
     
  8. Offline

    FuZioN720

Thread Status:
Not open for further replies.

Share This Page