如何在服务器范围内设置计时器

How to set a timer server-wide

该图块没有正确描述它,所以我将尝试在这里描述它:

我有一个 bukkit 插件,它是一个小游戏。

它必须有一些代码 运行 10 分钟然后 运行 另一个代码直到游戏结束

我目前有这个: 定时器 timer = new Timer(); timer.schedule(新的 TimerTask() {

            public void run() {

                // code for 10 minutes

            }

        }, 600000);

//之后的代码

然而,这只影响单个玩家,不影响那个世界。 因此,如果一个玩家加入,他将等待 10 分钟,然后 运行 代码的另一部分,依此类推,当目的是 10 分钟时,即使没有玩家也开始自己计算。 谢谢

你的问题很可能是因为你的代码都是由事件触发的? 这意味着它只会影响触发该事件的玩家。

相反,您需要一个通用插件,该插件不会触发事件(登录除外),而是使用计时器,然后获取所有玩家的列表,然后 运行 在 [=17 上输入您的代码=] 他们。然后在 10 分钟后它将退出到您的其他代码,并且 运行 在剩下的时间里。

编辑:粗略示例:

import org.bukkit.plugin.java.JavaPlugin;

    public final class {$PluginName} extends JavaPlugin {
        @Override
        public void onEnable() { //This should proberbly be done onCommand rather than onEnable
            Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    long time = System.currentTimeMillis();
                    while (some condition....){
                        //load list of players
                        //now iterate through player list and do your code
                        //check if 10min has passed:
                        if ((System.currentTimeMillis() - time) > 600000){
                            //now break the loop and run your other code for the rest of the minigame
                            break;
                        }
                    }
                    //code for the rest of the minigame
                    while (true){
                        //load list of players
                        //now iterate through player list and do your code for the rest of the time
                    }
                }
           }); 
           thread.start();               
        }
    }