diff --git a/src/main/java/aplini/ipacwhitelist/IpacWhitelist.java b/src/main/java/aplini/ipacwhitelist/IpacWhitelist.java index 690fe8e..3df7818 100644 --- a/src/main/java/aplini/ipacwhitelist/IpacWhitelist.java +++ b/src/main/java/aplini/ipacwhitelist/IpacWhitelist.java @@ -4,11 +4,13 @@ import aplini.ipacwhitelist.Listener.onPlayerJoin; import aplini.ipacwhitelist.util.SQL_io; import aplini.ipacwhitelist.Listener.onVisitPlayerJoin; +import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.server.ServerLoadEvent; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -35,7 +37,7 @@ public void onEnable() { // 注册监听器 getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(new onPlayerJoin(this), this); - getServer().getPluginManager().registerEvents(new CommandHandler(this), this); + Objects.requireNonNull(Bukkit.getPluginCommand("wl")).setExecutor(new CommandHandler(this)); if(plugin.getConfig().getBoolean("visit.enable", false)){ getServer().getPluginManager().registerEvents(new onVisitPlayerJoin(this), this); @@ -61,7 +63,7 @@ public void onServerLoad(ServerLoadEvent event) { throw new RuntimeException(e); } - getLogger().info("[IpacWhitelist] 启动等待结束"); + getLogger().info("启动等待结束"); // 允许加入 allowJoin = true; }); diff --git a/src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java b/src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java index bf4ae69..1c55af7 100644 --- a/src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java +++ b/src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java @@ -35,11 +35,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command // 重新连接数据库 case "reconnect_database" -> { allowJoin = false; - SQL_io.closeConnection(); - SQL_io.connection(); + SQL_io.reconnect(); SQL_io.initialize(); - allowJoin = true; sender.sendMessage(plugin.getConfig().getString("message.command.reconnect-database", "")); + allowJoin = true; } // 添加一个账户 diff --git a/src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java b/src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java index 6087c27..78800ec 100644 --- a/src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java +++ b/src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java @@ -55,7 +55,7 @@ public void onPlayerLogin(PlayerLoginEvent event) { case WHITE -> // 白名单 event.setResult(PlayerLoginEvent.Result.ALLOWED); - // 不存在or已移出白名单, 参观账户 + // 不存在/已移出白名单, 参观账户 case NOT, VISIT -> { // 不在白名单中 // 是否启用参观账户 if(plugin.getConfig().getBoolean("visit.enable", false)){ @@ -73,8 +73,8 @@ public void onPlayerLogin(PlayerLoginEvent event) { } case WHITE_EXPIRED -> { // 白名单已过期 - getLogger().info("[IpacWhitelist] %s 白名单已过期或不在白名单中".formatted(event.getPlayer().getName())); - event.setKickMessage(plugin.getConfig().getString("message.join.not", "").replace("%player%", event.getPlayer().getName())); + getLogger().info("[IpacWhitelist] %s 白名单已过期".formatted(event.getPlayer().getName())); + event.setKickMessage(plugin.getConfig().getString("message.join.expired", "").replace("%player%", event.getPlayer().getName())); event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST); } diff --git a/src/main/java/aplini/ipacwhitelist/util/SQL_io.java b/src/main/java/aplini/ipacwhitelist/util/SQL_io.java index c3ab03b..9b57576 100644 --- a/src/main/java/aplini/ipacwhitelist/util/SQL_io.java +++ b/src/main/java/aplini/ipacwhitelist/util/SQL_io.java @@ -43,6 +43,12 @@ public static synchronized void closeConnection() { throw new RuntimeException(e); } } + // 重连 + public static synchronized void reconnect() { + closeConnection(); + connection(); + } + // 初始化数据库 public static synchronized void initialize() { try { @@ -54,6 +60,18 @@ public static synchronized void initialize() { // SQLITE if(db.equalsIgnoreCase("sqlite")){ + + // 使用 WAL 模式. 自动复用碎片空间 + connection.prepareStatement(""" + PRAGMA journal_mode = WAL; + PRAGMA auto_vacuum = 2; + """ + ).execute(); + + // 重新连接数据库 + reconnect(); + + // 加载数据表 connection.prepareStatement(""" CREATE TABLE IF NOT EXISTS "%s" ( "ID" INTEGER NOT NULL, diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 292d467..3d959f3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -34,8 +34,8 @@ whitelist: # 参观账户 # 参观账户允许不在白名单中的玩家加入服务器, 可以限制其功能 visit: - # 如果关闭, 则不会注册相关的事件, 修改后需要重启服务器 - # 如果开启, 不在白名单中的玩家将会以参观账户的形式加入服务器 + # 如果开启, 不在白名单和移出白名单的玩家将会以参观账户的形式加入服务器. 白名单过期和封禁中的账户依然不可加入服务器 + # 关闭可重载配置, 但开启它需要重启服务器 enable: false # 最多允许同时加入多少参观账户 max-visit-player: 10 @@ -100,6 +100,7 @@ message: # 玩家加入 join: not: '§6IpacEL §f> §b您不在白名单中或已失效, 请联系管理员恢复§7: §a%player%' + expired: '§6IpacEL §f> §b太久没有上线? 请联系管理员恢复白名单§7: §a%player%' black: '§6IpacEL §f> §b您被列入黑名单: §a%player%' err: '§6IpacEL §f> §a发生内部错误, 请稍后重试或联系管理员解决' starting: '§6IpacEL §f> §b服务器正在启动'