我正在尝试制作一个锦标赛类型的插件,其中有一个 start-game
命令,可以在从 10 倒数后开始游戏。但是,我找不到等待 1 秒的方法,因为倒计时中的一个间隔。我尝试过 Object.wait(long)
和 BukkitScheduler.setRepeatingDelayedTask()
但它们都不适合我 (object.wait()
) > 在我的编辑器中返回 java.lang.IllegalMonitorStateException: current thread is not Owner
和 BukkitScheduler.setRepeatingDelayedTask()
错误)。有人知道解决方法吗?
最佳答案
// See how many seconds have been counted
private int count;
private BukkitTask task;
public void doStuff() {
// Reset count
count = 0;
task = new BukkitRunnable() {
@Override
public void run() {
// If count is less than or equal to ten, increment count, otherwise set count to -1.
count = count <= 10 ? count + 1 : -1;
if (count >= 10 || count == -1) {
// Cancel task if count is over 10s
task.cancel();
return;
}
Bukkit.getLogger().log(Level.INFO, "Seconds passed: " + count + "!";
}
}.runTaskTimer(YourPlugin.getPlugin(), 0, 20);
}
关于java - 如何为 Bukkit 插件从 10 开始倒计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69608306/