Logout Command (Repeat the Task issue)

Discussion in 'Plugin Development' started by hostadam, Mar 7, 2017.

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

    hostadam

    The command works perfectly, it kicks the player out of the server, and it counts down time remaining. But it does not repeat the countdown when you run the command again, it doesn't do anything at all.

    Code:
    public class Logout implements Listener, CommandExecutor {
      
        public int log = 30;
        public static ArrayList <String> logout = new ArrayList();
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String [] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("logout")) {
                if(!logout.contains(p.getName())) {
                    logout.add(p.getName());
                    p.sendMessage(ChatColor.YELLOW + "Safely logging out in " + ChatColor.GOLD + "30 seconds!");
                }
                Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(OuthostHCF.getPlugin(), new Runnable() {
                    public void run() {
                        if(log != 0) {
                            log -= 1;
                            p.sendMessage(ChatColor.GREEN + "Safely logging out in " + log);
                          
                            if(log == 0L) {
                                p.kickPlayer(ChatColor.GREEN + "You have been safely logged out.");
                                logout.remove(p.getName());
                                Bukkit.getScheduler().cancelTask(log);
                                return;
                        }
                    }
                }}, 0L, 20L);
                }
            return false;
    
    }
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @hostadam Don't use async when you use Bukkit calls.
    And that is because the first one is still running.
     
  3. Offline

    hostadam

    So how would this be fixed?
     
  4. Offline

    timtower Administrator Administrator Moderator

    Multiple ways of doing it.
    I prefer a single runnable that handles all players and only gets started in the onEnable.
    Running the command again would mean that it would reset the timer.
    Or you keep track of the runnables in a HashMap and cancel the old one to start a new one.
     
  5. Offline

    hostadam

    Thank to you!
     
Thread Status:
Not open for further replies.

Share This Page