NoticeAnnouncer.java v1.0
定时广播自定义服务器公告,可随时通过命令编辑和查看当前公告内容,公告持久保存,自动定时推送。
命令列表
- servernotice服务器公告编辑与查看:/servernotice edit <内容> 或 /servernotice view
package cc.scriptirc.notice;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
/**
* @pluginName NoticeAnnouncer
* @author ScriptIrc Engine
* @version 1.0
* @description 定时广播自定义服务器公告,可随时通过命令编辑和查看当前公告内容,公告持久保存,自动定时推送。
* [command]servernotice|服务器公告编辑与查看:/servernotice edit <内容> 或 /servernotice view[/command]
* [Permission]scriptirc.notice.edit|允许编辑服务器公告内容[/Permission]
*/
public class NoticeAnnouncer extends JavaPlugin {
// 公告内容
private String notice = "欢迎来到服务器!";
// 定时任务ID
private int taskId = -1;
// 配置文件对象
private File noticeFile;
private FileConfiguration noticeConfig;
// 定时公告间隔,单位tick(20tick=1秒),这里设置为5分钟(6000tick)
private final long ANNOUNCE_PERIOD = 6000L;
@Override
public void onEnable() {
// 初始化公告配置文件
loadNoticeFile();
// 启动定时广播任务
startAnnounceTask();
getLogger().info("NoticeAnnouncer已启用,当前公告:" + notice);
}
@Override
public void onDisable() {
// 关闭插件时,保存公告内容,并取消任务
saveNoticeFile();
cancelAnnounceTask();
getLogger().info("NoticeAnnouncer已关闭");
}
/**
* 处理/servernotice edit <内容> 及 /servernotice view 命令
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// 命令无参数或参数view,显示公告
if (args.length == 0 || (args.length == 1 && "view".equalsIgnoreCase(args[0]))) {
sender.sendMessage(ChatColor.GREEN + "[公告] " + ChatColor.YELLOW + this.notice);
return true;
}
if ("edit".equalsIgnoreCase(args[0])) {
// 权限校验
if (!sender.hasPermission("scriptirc.notice.edit")) {
sender.sendMessage(ChatColor.RED + "你没有权限编辑公告!");
return true;
}
// 编辑内容不能为空
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "用法: /servernotice edit <新的公告内容>");
return true;
}
// 拼接新公告内容
StringBuilder sb = new StringBuilder();
for (int i = 1; i < args.length; i++) {
sb.append(args[i]);
if (i != args.length - 1) sb.append(" ");
}
String newNotice = sb.toString().trim();
// 更新内存内容和保存
this.notice = newNotice;
saveNoticeFile();
sender.sendMessage(ChatColor.GREEN + "公告内容已更新为:" + ChatColor.YELLOW + this.notice);
return true;
}
// 命令无效
sender.sendMessage(ChatColor.RED + "用法: /servernotice edit <新的公告内容> 或 /servernotice view");
return true;
}
/**
* 加载或初始化YML公告配置文件
*/
private void loadNoticeFile() {
// 指定配置文件路径
noticeFile = new File(getDataFolder(), "notice.yml");
// 若无插件目录,自动创建
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}
// 若无公告文件,则创建并写入默认内容
if (!noticeFile.exists()) {
noticeConfig = new YamlConfiguration();
noticeConfig.set("notice", this.notice);
try {
noticeConfig.save(noticeFile);
} catch (IOException e) {
getLogger().warning("公告文件保存失败:" + e.getMessage());
}
}
// 读取公告内容
noticeConfig = YamlConfiguration.loadConfiguration(noticeFile);
this.notice = noticeConfig.getString("notice", this.notice);
}
/**
* 保存公告内容到配置文件
*/
private void saveNoticeFile() {
if (noticeConfig != null && noticeFile != null) {
noticeConfig.set("notice", this.notice);
try {
noticeConfig.save(noticeFile);
} catch (IOException e) {
getLogger().warning("公告文件保存失败:" + e.getMessage());
}
}
}
/**
* 启动定时广播任务
*/
private void startAnnounceTask() {
this.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
@Override
public void run() {
// 广播公告内容
Bukkit.broadcastMessage(ChatColor.AQUA + "[公告] " + ChatColor.YELLOW + notice);
}
}, ANNOUNCE_PERIOD, ANNOUNCE_PERIOD); // 首次延迟+周期
}
/**
* 取消广播任务
*/
private void cancelAnnounceTask() {
if (this.taskId != -1) {
Bukkit.getScheduler().cancelTask(this.taskId);
this.taskId = -1;
}
}
}