Solved Cancel a delayed task on movement

Discussion in 'Spigot Plugin Development' started by Dramatically, Apr 22, 2017.

Thread Status:
Not open for further replies.
  1. Basically, what I am trying to do is: when player type specific command (/spawn) they will be teleported to the location set, and if they move whilst/in those 5 seconds it will do nothing, to check for movement I use a timer runnable and various variables and an event;

    Code (Text):

    new BukkitRunnable() {

                        @Override
                        public void run() {
                            try {
                             
                                final Player player = (Player) sender;
                                if (dontMove.contains(player)) {
                                    player.teleport(loc(player));
                                    player.sendMessage("§e§l(!) §7You have been teleported to §e§nspawn!");
                                    dontMove.remove(player);
                                    done = true;
                                } else {
                                 
                                }
                            } catch (final Exception ex) {
                                sender.sendMessage("Something went wrong...");
                                try {
                                    dontMove.remove((Entity) sender);
                                } catch (Exception ex2) {
                                 
                                }
                            }

                        }
                    }.runTaskLater(this, 20 * 5L);
                 
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            final Player player = (Player) sender;
                            if (done == true) {
                                return;
                            }
                         
                            if (dontMove.contains(player)) {
                                return;
                            }
                         
                            if (!(dontMove.contains(player))) {
                                if (done == false) {
                                    player.sendMessage("§e§l(!) §7Teleportation cancelled, you moved");
                                    dontMove.remove(player);
                                    this.cancel();
                                } else {
                                 
                                }
                            } else {
                             
                            }
                         
                        }
                     
                    }.runTaskTimer(this, 0L, 20 * 5L);
     

    The Event (Movement)
    Code (Text):

    @EventHandler
        public void move(PlayerMoveEvent event) {
            if (event.getTo().getBlockX() != event.getFrom().getBlockX()) {
                if (dontMove.contains(event.getPlayer())) {
                    dontMove.remove(event.getPlayer());
                }
            }
        }
     
    currently it does nothing when you move, it just teleports you in 5seconds even if you move
     
  2. Make sure you are using a synchronized method to retrieve the value or making a synchronized List via
    Code (Text):
    List<Object> objList = Collections.synchronizedList(new ArrayList<Object>());
    Also I recommend storing the UUID and not the whole Player object

    Your event also only checks for movement in the X axis, you might want to change that
     
    #2 MartinStone, Apr 22, 2017
    Last edited: Apr 22, 2017
  3. Okay thanks guys!

    I have rewrote the code to:

    Lists, Maps:
    Code (Text):

    List<Player> teleporting = new ArrayList<Player>();
        Map<Player, BukkitTask> tasks = new HashMap<Player, BukkitTask>();
     
    Tasks:
    Code (Text):

    try {
                        tasks.put((Player) sender, new BukkitRunnable() {
                                @Override
                                public void run() {
                                    try {
                                       
                                        final Player player = (Player) sender;
                                        if (teleporting.contains(player)) {
                                            player.teleport(loc(player));
                                            player.sendMessage("§e§l(!) §7You have been teleported to §e§nspawn!");
                                            teleporting.remove(player);
                                        } else {
                                           
                                        }
                                    } catch (final Exception ex) {
                                        sender.sendMessage("Something went wrong...");
                                        try {
                                            teleporting.remove((Player) sender);
                                        } catch (Exception ex2) {
                                           
                                        }
                                    }
           
                                }
                            }.runTaskLater(this, 20 * 5L));
                    } catch (Exception ex) {
                        sender.sendMessage("Sorry, something went wrong...");
                    }
     
    Movement event:
    Code (Text):

    @EventHandler
        public void move(PlayerMoveEvent event) {
            if (event.getTo().getBlockX() != event.getFrom().getBlockX()) {
                if (teleporting.contains(event.getPlayer())) {
                    teleporting.remove(event.getPlayer());
                    tasks.remove(event.getPlayer());
                    event.getPlayer().sendMessage("§e§l(!) §7Teleportation cancelled, you moved");
                }
            }
        }
     
    And it does not work? I move and it says nothing
     
  4. FIXED: I missed out this line of code:
    Code (Text):

            Bukkit.getPluginManager().registerEvents(this, this);
     
     
Thread Status:
Not open for further replies.