Solved Waiting 5 seconds before moving onto next line

Discussion in 'Spigot Plugin Development' started by TechyGaming, Feb 7, 2020.

  1. Hi,
    I am trying to make a custom UHC plugin for my server to fit its exact needs however, I cannot work out how to add a "waiting" times to it.

    Example:
    Broadcasts: "Starting in 10 secs..."
    waits 5 seconds...
    Broadcasts: "Starting in 5 secs..."

    I have searched the forums and other various websites for over 2 hours trying to find something that could help me but I can only find things that either don't work or are unrelated.

    Here's my code:
    Code (Java):
    package me.techygaming.uhc.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.techygaming.uhc.Main;
    import me.techygaming.uhc.ui.UHCAdmin;
    import me.techygaming.uhc.utils.Utils;

    public class UHCAdminCommand implements CommandExecutor {
       
        private Main plugin;
       
        public UHCAdminCommand(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("uhca").setExecutor(this);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)){
                sender.sendMessage("This command cannot be used through console!");
                return true;
            }
           
            Player plr = (Player) sender;
           
            if (plr.hasPermission("techcraft.uhc.admin")){
                if (args.length == 0) {
                    plr.openInventory(UHCAdmin.GUI(plr));
                    plr.sendMessage(Utils.chat(plugin.getConfig().getString("prefix") + plugin.getConfig().getString("openingGUI")));
                }
                if (args.length >= 1) {
                    // HOST ARGUMENT
                    if(args[0].equalsIgnoreCase("host")) {
                        Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("prefix") + plugin.getConfig().getString("hostBroadcast").replace("%host%", plr.getName())));
                    }
                    // START ARGUMENT
                    if(args[0].equalsIgnoreCase("start")) {
                        Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("prefix") + plugin.getConfig().getString("start10Broadcast")));
                        // WAIT HERE FOR 5 SECONDS
                        // THEN RUN:
                        Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("prefix") + plugin.getConfig().getString("start5Broadcast")));
                    }
                }
                return true;
            } else {
                plr.sendMessage(Utils.chat(plugin.getConfig().getString("prefix") + plugin.getConfig().getString("noPermission")));
            }
            return false;
        }
    }
     
    I have marked the part where I would like it to wait with notes. (Line 45/46)
     
  2. I hope I understood the question properly.

    https://www.spigotmc.org/threads/creating-a-countdown.266702/

    Just make a BukkitRunnable and have it start at 10 seconds. For every 20 ticks, have it decrease your seconds variable be subtracted by 1. Check when 5 seconds hit, and then give the player a message.
     
    #2 C10sp, Feb 7, 2020
    Last edited: Feb 7, 2020
  3. Code (Text):
    Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> {
                    // your code
                }, 200);
    This will schedule your message after 200 ticks, which is 5s.
     
  4. 20 ticks in a second, so actually 100 ticks is equivalent to 5 seconds.
     
  5. Code (Java):
    new BukkitRunnable()
    {
        @Override
        public static void run()
        {
            Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("prefix") + plugin.getConfig().getString("start5Broadcast")));
        }
    }.runTaskLater(plugin, 100);
    I would just make a runnable at each second (20 ticks) that broadcast the remaining waiting time using countdown % 5 == 0 or countdown <=5
    Bukkit.broadcastMessage(countdown + " seconds left!");
     
    • Agree Agree x 2
  6. Strahan

    Benefactor

    Just do Thread.sleep(5000); :D

    (please don't, I'm joking hehe)
     
    • Winner Winner x 2
  7. Done, thanks!
     
  8. Oh, and while we‘re at it:
    Code (Java):
    long past = System.currentTimeInMilis();
    while(true) {
        If (System.currentTimeInMilis() - past >= 5000) break;
    }