Search code examples
javatimeminecraftwait

Wait a time before do action without stop server


I'm working on a bedwars plugin for minecraft server, everythings work but at the starting i want a 5sec timer before starting the game and show a title on the game, i worked on this code :

        TimeUnit.SECONDS.sleep(1);
        sendtitle("4");
        TimeUnit.SECONDS.sleep(1);
        sendtitle("3");
        TimeUnit.SECONDS.sleep(1);
        sendtitle("2");
        TimeUnit.SECONDS.sleep(1);
        sendtitle("1");
        TimeUnit.SECONDS.sleep(1);
        sendtitle("SusyBaka");

But i completly pausing the server, i tried to use multi thread but the title don't apear, can someone help me ?


Solution

  • You should NOT use Thread.sleep() or something like that on spigot-server because such as you said, it stop the FULL server (and it can make player timed out)

    To make a timer, use this :

    public int count = 5;
    public BukkitTask task;
    
    public void method() {
       task = Bukkit.getServer().getScheduler().runTaskTimer(MyPlugin.getInstance(), () -> {
          if(count == 0) {
             task.cancel();
             sendtitle("SusyBaka");
          } else {
             count--;
             sendtitle("Start in " + count + " seconds");
          }
       }, 20, 20); // 20 ticks = 1 second
    }