Solved Cancel a scheduler

Discussion in 'Plugin Development' started by MooshViolet, Aug 2, 2016.

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

    MooshViolet

    Hello everyone. I have encountered an issue. I am making a teleport plugin where it checks if players are nearby. If they are, it makes the player trying to teleport wait 10 seconds. However, If the player moves, I want it to cancel the teleport.
    Here is my code so far. What this does is it makes the player wait 10 seconds, but if they move, it does not cancel the teleport.
    Code:
                    for(Entity e : player.getNearbyEntities(20, 20, 20)){
                        if(e instanceof Player){
                            player.sendMessage(ChatColor.GRAY + "Someone is nearby, please wait 10 seconds to be warped.");
                            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                              public void run()
                              {
               
                                player.teleport(loc);
                                player.sendMessage(ChatColor.GRAY + "Teleporting...");
                                }
                               
                             
                            }, 200L);
                       return true;
                        }
                    }
     
  2. Offline

    ArsenArsen

  3. Offline

    MarinD99

  4. Offline

    MooshViolet

    I am having trouble incorporating this into my plugin. Any help?

    @Zombie_Striker
     
    Last edited: Aug 4, 2016
  5. Offline

    Oxyorum

    @MooshViolet
    You want to create your own class which extends BukkitRunnable, then, in that class' run() method, put the logic you wan to run (the code you already have). Notice that BukkitRunnable class has a cancel() method which you can then use to cancel the task.

    You'll want to do as indicated above (use BukkitRunnable#scheduleTaskTimer) using an instance of your class. You will want to create an instance for each player, and then store it somewhere so you can access it later (use a Map<UUID, BukkitRunnable> or similar). That way, you can use a PlayerMoveEvent listener to check if they moved before the 10 seconds were up. If they did, cancel the task for that player.
     
  6. Offline

    x_Jake_s

    Code:
                    if(e instanceof Player){
                              if ((EventClass).contains (e.getPlayer()) {
                     player.sendMessage(ChatColor.GRAY + "Someone is nearby, please wait 10 seconds to be warped.");
                            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                              public void run()
                              {
                                player.teleport(loc);
                                player.sendMessage(ChatColor.GRAY + "Teleporting...");
                                }
                            }, 200L);
                       return true;
                        }
                     }
                  }

    Create an arraylist of players, then create a onPlayerMoveEvent and whenever there are players near them place them into the list and when there aren't players near them take them out, then you can simply add the above script. It may be a messy solution but it does the trick I think.
     
  7. Use a BukkitRunnable:

    Code:
    new BukkitRunnable() {
    
    // run method
    // you can do cancel(); whenever you want
    
    }.runTaskLater(plugin, delay)
     
  8. Offline

    MooshViolet

    Thank you.
    Solved
     
    x_Jake_s likes this.
Thread Status:
Not open for further replies.

Share This Page