Tiiill
Tiiill

Reputation: 11

Java (Bukkit) - Wait before executing next line

I am currently having problems with a plugin. I want to give a player effects for 5 seconds and after the 5 seconds, it should be day. My problem is, that the day command is executing instantly, so it is day, before the effects are over. Can you help me, that the command for setting the time to 1000 waits 5 seconds before executing? Help would be appreciated, Till

p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 100, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100, true));
                    p.sendMessage("§aGute Nacht!"); //german, dont worry :D
//here should be the delay of 5 seconds
                    Bukkit.getWorld("world").setTime(1000);

Upvotes: 1

Views: 761

Answers (2)

jacob
jacob

Reputation: 4956

You should look into scheduleSyncDelayedTask I haven't tested this, but you should be aiming for something like this.

myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
  public void run() {
      Bukkit.getWorld("world").setTime(1000);
  }
}, 100);

scheduleSyncDelayedTask

Schedules a once off task to occur as soon as possible. This task will be executed by the main server thread.

Parameters:

plugin - Plugin that owns the task

task - Task to be executed

Returns: Task id number (-1 if scheduling failed)

Upvotes: 2

Drayke
Drayke

Reputation: 397

I would also recommend using the BukkitRunnable. This class is provided as an easy way to handle scheduling tasks.

new BukkitRunnable(
  public void run() {
    Bukkit.getWorld("world").setTime(1000);
  }
).runTaskLater​(yourPluginInstance, delayInTicks);

Remember that 20 sever ticks is equal to 1 server second. So delayInTicks = 5s * 20 = 100

Upvotes: 3

Related Questions