Trying to use delayed tasks in a repeating task

Discussion in 'Plugin Development' started by SharpBit, Feb 18, 2020.

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

    SharpBit

    I'm trying to initiate a 10 second countdown every 5 minutes without lagging the server.

    Here is my code:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if (args[0].equalsIgnoreCase("start")) {
                @SuppressWarnings("unchecked")
                Collection<Player> players = (Collection<Player>) Bukkit.getOnlinePlayers();
    
                if (players.size() != 2) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.RED + "There must be 2 players for death swap!"));
                    return true;
                }
             
                int sec = (int) 20L;
                plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, DeathSwapCommand::scheduleSwap, sec * 270, sec * 300);
    
                Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.GREEN + "Swapping in 5 minutes."));
            } else {
                sender.getServer().getScheduler().cancelTasks(plugin);
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.GREEN + "Stopped swapping."));
            }
         
            return true;
        }
     
        public static void scheduleSwap() {
    
            @SuppressWarnings("unchecked")
            List<Player> players = (List<Player>) Bukkit.getOnlinePlayers();
         
            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.RED + "Swapping in 30 seconds."));
         
            int sec = (int) 20L;
    
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { public void run() {} }, sec * 20);
         
         
            for (int i = 10; i > 1; i--) {
                Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.RED + "Swapping in " + i + " seconds!"));
    
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { public void run() {} }, sec);
            }
         
            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.RED + "Swapping in 1 second!"));
         
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { public void run() {} }, sec);
         
            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', ChatColor.RED + "Swap now."));
            Location player0 = players.get(0).getLocation();
            Location player1 = players.get(1).getLocation();
    
            players.get(0).teleport(player1);
            players.get(1).teleport(player0);
        }
    Instead of properly sending a message with 30 seconds left and then a 10 second count down, it sends all 11 messages at the same time, at the same time of teleportation of players. How do I fix this?

    Notice the times of these messages are all the same: https://i.imgur.com/woMtwzd.png


    I don't know how to format code on this website so the indents are messed up but hopefully it's still readable
     
    Last edited by a moderator: Feb 18, 2020
  2. Offline

    Kars

    This line does nothing. You call it 3 times. Notice the empty run method.
    PHP:
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { public void run() {} }, sec 20);
    Your code gives me the impression you don't know how a scheduler works.
    scheduleSyncRepeatingTask takes delay and period (final 2 parameters) as ticks, so you schedule the entirety of scheduleSwap to run every 1,4 seconds or so, after a 15 second delay.
    You see all the messages at once because you send them all at once in this loop:
    PHP:
    for (int i 101i--) {
                
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&'ChatColor.RED "Swapping in " " seconds!"));

                
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { public void run() {} }, sec);
            }
    The scheduled task there does nothing.
     
  3. Offline

    SharpBit

    The problem is that variables inside a Runnable must be final so I can't use "i" to count down to 1 second. Also how is it running the whole thing in 1.4 seconds? Please explain


    Edit: I now understand that only the code in the runnable is delayed and after that line, everything is immediately run, not delayed.

    I fixed the final issue using this: https://bukkit.org/threads/create-a-simple-countdown.372228/

    Solved!
     
    Last edited: Feb 19, 2020
  4. Offline

    Kars

    I said it runs it every 1.4 seconds, not that it ran in 1.4 seconds, and this is readily apparent.
     
  5. Offline

    Sw_aG

    1. make a BukkitRunnable that will run every 5 minutes
    2. make the runnable run a method that will run every 20 ticks
     
Thread Status:
Not open for further replies.

Share This Page