【发布时间】:2021-05-19 17:11:31
【问题描述】:
我尝试制作一个可以检测服务器上是否没有人的 spigot 插件。我需要这个计时器,如果服务器上没有人,它应该停止计时器并将计时器的时间保存在文本文件中。有什么办法可以用插件做到这一点。 谢谢你的每一个帮助,亚伦。
【问题讨论】:
我尝试制作一个可以检测服务器上是否没有人的 spigot 插件。我需要这个计时器,如果服务器上没有人,它应该停止计时器并将计时器的时间保存在文本文件中。有什么办法可以用插件做到这一点。 谢谢你的每一个帮助,亚伦。
【问题讨论】:
如何创建文本文件:
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 构造。
【讨论】:
查看Bukkit#getOnlinePlayers(); 并检查它是否为空。您可以检查玩家何时与服务器断开连接,也可以执行重复的计划任务。
要将计时器保存在 yml 文件中,请使用 YamlConfiguration。更多详情请看这里:https://www.spigotmc.org/wiki/config-files/#creating-the-file
如果您需要更多帮助(可能还有更快的答案),您可以在 discord 上加我:Pierre#7757。如果我们找到任何好的解决方案,我会编辑此评论。
【讨论】: