Spigot 插件,服务器上没有人

Spigot Plugin, nobody on Server

我尝试制作一个 spigot 插件,它可以检测服务器上是否有人。我需要这个作为计时器,如果服务器上没有人,它应该停止计时器并将计时器的时间保存在文本文件中。有什么办法可以用插件来做到这一点。 感谢您的帮助,Aaron。

查看Bukkit#getOnlinePlayers();并检查它是否为空。您可以检查玩家何时与服务器断开连接,也可以执行重复的计划任务。

要将计时器保存在 yml 文件中,请使用 YamlConfiguration。有关详细信息,请参阅此处:https://www.spigotmc.org/wiki/config-files/#creating-the-file

如果您需要更多帮助(可能需要更快的答复),您可以在 discord 上加我:Pierre#7757。如果我们找到任何好的解决方案,我会编辑此评论。

如何创建文本文件:

        try {
            File file = new File(Yourplugin.getPlugin(YourPlugin.class).getDataFolder().getPath(), "filename.txt");
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException exception){
            System.out.println(exception.toString());
        }

如何读取文本文件:

        File file = new File(YourPlugin.getPlugin(Yourplugin.class).getDataFolder().getPath(), "filename.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = br.readline(); //can be null, if nothing is in file

如何写入文件:

            File file = new File(YourPlugin.getPlugin(Yourplugin.class).getDataFolder().getPath(), "filename.txt");
            BufferedWriter wr = new BufferedWriter(new FileWriter(file));
            br.write("yourtime");

您应该像第一个示例一样在每个示例中进行 try-catch 构造。