我如何在 2 秒后 运行 一次任务?

How do I run a task once after 2 seconds?

在我的插件中,我有一个 /duel 命令。除了一件事,我已经做了我需要做的一切。当一名玩家死亡时,我需要杀手在 2 秒后被传送到重生。这是我的 onPlayerDeath() 的样子。

@EventHandler
public void onPlayerDeath(PlayerDeathEvent e) {
    Player p = e.getEntity().getPlayer();
    Player k = e.getEntity().getKiller();
    p.sendMessage(ChatColor.DARK_RED  +"[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Killed by " + ChatColor.BLUE + k.getName());
    k.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]"  + ChatColor.GREEN + " You killed " + ChatColor.BLUE + p.getName());
    p.getInventory().clear();
    k.getInventory().clear();
    p.getEquipment().clear();
    k.getEquipment().clear();
    k.setFlying(true);
    Bukkit.broadcastMessage(ChatColor.DARK_RED + "["+  ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.DARK_AQUA + k.getName() + ChatColor.AQUA + " just beat " + ChatColor.DARK_AQUA + p.getName() + ChatColor.AQUA + " in a duel!");
    // Teleport the killer to spawn after 2 seconds
}

以前用过,但是忘记怎么用了。

你应该使用Scheduler provided by Bukkit。 另见 World.getSpawnLocation()

Winner winner = new Winner(k);
/* 
 * The first argument is the unique instance of the main class
 * The second argument is the delay in ticks (1 second = 20 ticks)
 */
winner.runTaskLater(Plugin, 40L);

public class Winner extends BukkitRunnable {
    private Player player;
    public Winner(Player player) {
        this.player = player;
    }
    @Override
    public void run() {
        Location spawn = player.getWorld().getSpawnLocation();
        player.teleport(spawn);
    }
}