Skip to content

Commit

Permalink
修复命令注册, 设置sqlite运行模式
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Aug 14, 2023
1 parent abf9db7 commit b167114
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/main/java/aplini/ipacwhitelist/IpacWhitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -61,7 +63,7 @@ public void onServerLoad(ServerLoadEvent event) {
throw new RuntimeException(e);
}

getLogger().info("[IpacWhitelist] 启动等待结束");
getLogger().info("启动等待结束");
// 允许加入
allowJoin = true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// 添加一个账户
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)){
Expand All @@ -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);
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/aplini/ipacwhitelist/util/SQL_io.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ whitelist:
# 参观账户
# 参观账户允许不在白名单中的玩家加入服务器, 可以限制其功能
visit:
# 如果关闭, 则不会注册相关的事件, 修改后需要重启服务器
# 如果开启, 不在白名单中的玩家将会以参观账户的形式加入服务器
# 如果开启, 不在白名单和移出白名单的玩家将会以参观账户的形式加入服务器. 白名单过期和封禁中的账户依然不可加入服务器
# 关闭可重载配置, 但开启它需要重启服务器
enable: false
# 最多允许同时加入多少参观账户
max-visit-player: 10
Expand Down Expand Up @@ -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服务器正在启动'
Expand Down

0 comments on commit b167114

Please sign in to comment.