Help with countdown timer

Discussion in 'Plugin Development' started by Josh774, Dec 17, 2018.

Thread Status:
Not open for further replies.
  1. I have a countdown timer it works and all. Pretty much when a player types a command they will have to wait 5 seconds before teleporting, however if they move it gets cancelled. This all works apart from when I try it with 2 player at the same time it just messes up and cancels it for both players. I have tried some things but I would appreciate some help.

    Note: im pretty new to this.

    Code:
                  
                    countdown = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                        int numberCount = 3;
                        int locX = y.getLocation().getBlockX();
                        int locY = y.getLocation().getBlockY();
                        public void run() {
                                if(numberCount != -1) {
                                    if(numberCount != 0) {
                                        if(locX == y.getLocation().getBlockX() && locY == y.getLocation().getBlockY()) {
                                            p.sendMessage("" + numberCount);
                                            numberCount--;
                                        }else {
                                            plugin.getServer().getScheduler().cancelTask(countdown);
    
                                        }
    
                                    }else {
                                         //code
    
    
                                }
               }else {
                                    plugin.getServer().getScheduler().cancelTask(countdown);
                                }
    
     
  2. How are you taking the player's instance? Are you saving it as a variable somewhere? If you are not doing this, things might go wrong.

    What I did to create the same effect, is creating a Runnable like this:
    Code:
    public class CountdownSchedulerTP implements Runnable {
    
        private int id;
        private final Player target;
    
        private int counter;
        private int counterMax;
    
        private int posX;
        private int posY;
        private int posZ;
    
        public CountdownSchedulerTP(Player target, int counter) {
            this.target = target;
    
            this.posX = target.getLocation().getBlock().getRelative(BlockFace.DOWN).getX();
            this.posY = target.getLocation().getBlock().getRelative(BlockFace.DOWN).getY();
            this.posZ = target.getLocation().getBlock().getRelative(BlockFace.DOWN).getZ();
    
            if (counter < 1) {
                throw new IllegalArgumentException("Counter must be greater than 1.");
            } else {
                this.counter = counter;
                this.counterMax = counter;
            }
        }
    
    
        @Override
        public void run() {
            Block down = target.getLocation().getBlock().getRelative(BlockFace.DOWN);
      
            if (down.getX() != posX || down.getY() != posY
                        || down.getZ() != posZ) {
                this.cancel(); // Cancel teleport due to moving
                return;
            } else if (target.getNoDamageTicks() > 0) {
                this.cancel(); // Cancel teleport due to taking damage from any source (player, mob, tnt etc.)
                return;
            }
      
            if(counter == counterMax) {
                target.sendMessage("Teleporting in " + counter + "...");
                counter--;
                return;
            } else if (counter > 0 && counter < counterMax) {
                counter--;
                return;
            } else {
                // Execute teleport
                this.cancel();
            }
        }
    
        public int getId() {
            return id;
        }
    
        public void setTaskId(int id) {
            this.id = id;
        }
    
        private void cancel() {
            Bukkit.getScheduler().cancelTask(id);
        }
    
    }
    
    and then call it like this:
    Code:
    CountdownSchedulerTP cs = new CountdownSchedulerTP(player, 5);
    cs.setTaskId(Bukkit.getScheduler().scheduleSyncRepeatingTask({plugin instance}, cs, 0, 20));
    {plugin instance} should be replaced with an instance of your Main class. The '0' and '20' are for ticks before the Runnable starts (in this case it will start instantly), the 20 is the amount of ticks for the interval (in this case, 20 ticks is the interval, which is equal to 1 second in real life).
     
    Last edited: Dec 17, 2018
Thread Status:
Not open for further replies.

Share This Page