【问题标题】:How do I run a task once after 2 seconds?如何在 2 秒后运行一次任务?
【发布时间】:2015-02-18 13:51:00
【问题描述】:

在我的插件中,我有一个/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
}

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

【问题讨论】:

  • 您不需要e.getEntity().getPlayer(),因为e.getEntity() 已经是Player 类型。

标签: java scheduled-tasks bukkit


【解决方案1】:

您应该使用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);
    }
}

【讨论】:

  • @MattyFenny 我用那个变量来表示一个通用的 Location 对象,因为我不知道你是如何定义 spawn 的。
  • 我想应该是double x = //coords of x; double y = //coords of y; double z = //coords of z; Location spawn = new Location(player.getWorld(), x, y, z);
  • @MattyFenny 你想得到玩家所在世界的重生吗?
  • @MattyFenny 查看编辑。你说的不起作用是什么意思。
  • 我测试过,当玩家杀死某人时,除了我设置发送给他们的消息之外什么都没有发生。
猜你喜欢
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多