Can't make delay in plugin

Discussion in 'Plugin Development' started by fastpiter8769, May 13, 2021.

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

    fastpiter8769

    Hello everyone i want to do plugin, but i need to know how to make a delay. I tried with Thread.sleep(); but when i do this the server have lag. Thread.sleep(TimeUnit.SECONDS.toMillis(5)); when i try to do this it is working, but server has lag for 5 seconds and i can't type any command before sleep ends. So how i can do this without lag?
     
  2. Online

    timtower Administrator Administrator Moderator

    Strahan likes this.
  3. Offline

    Strahan

    Never ever sleep a thread in Minecraft unless it's a thread you specifically made separate from the main thread. As you see, it blocks everything. If I wanted to have a five second delay between running a command and having it send me a message for example, I'd do:
    Code:
    onCommand {
      playerObject.sendMessage("Fuse lit.  Detonating in five seconds!");
      lightFuse(playerObject);
    }
    
    private void lightFuse(Player p) {
      new BukkitRunnable() {
        @Override
        public void run() {
          if (!p.isOnline()) return;
    
          p.sendMessage("Boom!");
        }
      }.runTaskLater(pluginInstance, 100L);
    }
    the 100L in runTaskLater means 100 tick delay. 20 ticks = 1 second, ideally.
     
Thread Status:
Not open for further replies.

Share This Page