我正在开发一个 Minecraft Bukkit 插件,我知道如何处理事件和一切,但我不确定该怎么做。我还没有真正编写代码,所以这是我想做的一个基本示例:
public void playerDead() {
runCommand(commandHere)
//Wait 2 minutes.
runCommand(otherCommandHere
}
我只需要等待两分钟的部分。其他一切都包括在内。
EDIT2:如果其他人在运行过程中死亡,我似乎需要将延迟重置为开始。有什么建议吗?
最佳答案
因为我看到你想在玩家死后执行你的操作。那么你肯定不想用 Thread.sleep(x);
你能做的就是为死去的玩家创造一个冷却时间。
public Map<String, Long> cooldown = new HashMap<String, Long>();
Long time = cooldown.get(player.getName());
if(time - System.currentTimeMillis() > 10*1000)
cooldown.put(player.getName(), System.currentTimeMillis());
else
int remains = (int)Math.floor(10 - System.currentTimeMillis());
代码引用here .
或者您可以创建任务以像这样运行:
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
{
public void playerDied()
{
// Your code here.
}
}, <delay in ticks>);
获取对您的插件的引用并将其作为参数 plugin
传递。或者,如果您很懒,只需将它写在插件中并传递给 this
。
关于java - 等待两分钟调用一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25413725/