gpt4 book ai didi

java - bukkit - 等待?

转载 作者:行者123 更新时间:2023-11-29 04:37:39 26 4
gpt4 key购买 nike

最近我开始开发一个 Bukkit 插件,我需要有关 hibernate/等待代码的帮助。我是 java 的新手,所以如果你能解释一下,我将不胜感激。这是我的代码:

@EventHandler
public void onTnt(PlayerInteractEntityEvent e) {
Player clicker = e.getPlayer();
Player rightclick = (Player) e.getRightClicked();
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.DARK_PURPLE + "AN INSTAKILLER HAS YOU AS A TARGET! SNEAK NOW!");
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.DARK_RED + "AN INSTAKILLER HAS YOU AS A TARGET! SNEAK NOW!");
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.GOLD + "AN INSTAKILLER HAS YOU AS A TARGET! SNEAK NOW!");
/* Then wait 3 sec. I've tried Thread.Sleep, wait() and sleep(), no sucess*/

if (!(rightclick.isSneaking())) {
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.GOLD + "Bye bye.");
rightclick.setHealth(0);

}

}

最佳答案

虽然通常你会使用 Thread.sleep 做这样的事情,但这会卡住整个服务器,并且在那段时间内不会处理事件(所以即使玩家停止偷偷摸摸他们这边,他们仍然会从服务器看到的地方偷偷摸摸)。

您需要做的是使用 BukkitRunnable(或者常规的 RunnablegetServer().getScheduler().scheduleSyncDelayedTask) :

@EventHandler
public void onTnt(PlayerInteractEntityEvent e) {
Player clicker = e.getPlayer();
// Needs to be final to reference it later
final Player rightclick = (Player) e.getRightClicked();
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.DARK_PURPLE + "AN INSTAKILLER HAS YOU AS A TARGET! SNEAK NOW!");
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.DARK_RED + "AN INSTAKILLER HAS YOU AS A TARGET! SNEAK NOW!");
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.GOLD + "AN INSTAKILLER HAS YOU AS A TARGET! SNEAK NOW!");


BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
if (!(rightclick.isSneaking())) {
rightclick.sendMessage(ChatColor.GRAY + "~" + ChatColor.GOLD + "Bye bye.");
rightclick.setHealth(0);
}
}
};
// Run the task on this plugin in 3 seconds (60 ticks)
task.runTaskLater(this, 20 * 3);
}

关于java - bukkit - 等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40590132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com