Skip to content

Commit

Permalink
错误修复和完善功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Sep 9, 2023
1 parent 91d3db0 commit cf083c4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 103 deletions.
3 changes: 0 additions & 3 deletions src/main/java/aplini/ipacwhitelist/IpacWhitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import aplini.ipacwhitelist.hook.hookAuthMe;
import aplini.ipacwhitelist.util.SQL;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent;
Expand All @@ -17,8 +16,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static org.bukkit.Bukkit.getServer;

public class IpacWhitelist extends JavaPlugin implements Listener {
private static IpacWhitelist plugin;
public static boolean allowJoin = false;
Expand Down
59 changes: 27 additions & 32 deletions src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import static aplini.ipacwhitelist.Func.CleanPlayerData.deletePlayerDataAll;
import static aplini.ipacwhitelist.IpacWhitelist.allowJoin;
import static aplini.ipacwhitelist.Func.EventFunc.startVisitConvertFunc;
import static aplini.ipacwhitelist.Listener.onVisitPlayerJoin.cleanVisitList;
import static aplini.ipacwhitelist.IpacWhitelist.allowJoin;
import static aplini.ipacwhitelist.util.SQL.whileDataForList;
import static aplini.ipacwhitelist.util.Util.getTime;
import static aplini.ipacwhitelist.util.Util.ifIsUUID32toUUID36;
Expand Down Expand Up @@ -345,20 +343,19 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
int maxLine = Integer.parseInt(inp2);

AtomicInteger i = new AtomicInteger();
CompletableFuture.runAsync(() -> {
whileDataForList(type, maxLine, (pd) -> {
i.getAndIncrement();
sender.sendMessage(plugin.getConfig().getString("message.command.list", "")
.replace("%num%", i.toString())
.replace("%ID%", String.valueOf(pd.ID))
.replace("%Type%", pd.Type.getName())
.replace("%Ban%", pd.Ban.getName())
.replace("%UUID%", pd.UUID)
.replace("%Name%", pd.Name)
.replace("%Time%", String.valueOf(pd.Time)));
}, () -> {
});
});
CompletableFuture.runAsync(() ->
whileDataForList(type, maxLine, (pd) -> {
i.getAndIncrement();
sender.sendMessage(plugin.getConfig().getString("message.command.list", "")
.replace("%num%", i.toString())
.replace("%ID%", String.valueOf(pd.ID))
.replace("%Type%", pd.Type.getName())
.replace("%Ban%", pd.Ban.getName())
.replace("%UUID%", pd.UUID)
.replace("%Name%", pd.Name)
.replace("%Time%", String.valueOf(pd.Time)));
}, () -> {
}));

return true;
}
Expand Down Expand Up @@ -398,9 +395,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// 遍历所有数据
AtomicInteger i = new AtomicInteger();
CompletableFuture.runAsync(() -> {
whileDataForList(sql, (pd) -> {
if(Objects.equals(pd.UUID, "")){
CompletableFuture.runAsync(() ->
whileDataForList(sql, (pd) -> {
if(Objects.equals(pd.UUID, "")){
return;
}
// 锁定这个参观账户
Expand All @@ -417,24 +414,22 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
// 清理结束, 等待指定时间
try {
TimeUnit.MILLISECONDS.sleep(plugin.getConfig().getInt("dev.deletePlayerDataAll.intervalTime", 100));
} catch (InterruptedException ignored) {
}
} catch (InterruptedException ignored) {}

// 取消锁定
onVisitPlayerJoin.cleanVisitList.remove(pd.UUID);

}, () -> {
// 运行结束 //
}, () -> {
// 运行结束 //

// 恢复参观账户
if (!plugin.getConfig().getBoolean("dev.deletePlayerDataAll.deletingLockPlayer", true)) {
onVisitPlayerJoin.disabledVisit = false;
}
// 恢复参观账户
if (!plugin.getConfig().getBoolean("dev.deletePlayerDataAll.deletingLockPlayer", true)) {
onVisitPlayerJoin.disabledVisit = false;
}

sender.sendMessage(plugin.getConfig().getString("message.command.clean-visit-ok", "")
.replace("%num%", i.toString()));
});
});
sender.sendMessage(plugin.getConfig().getString("message.command.clean-visit-ok", "")
.replace("%num%", i.toString()));
}));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static aplini.ipacwhitelist.IpacWhitelist.allowJoin;
import static aplini.ipacwhitelist.Func.EventFunc.startVisitConvertFunc;
import static aplini.ipacwhitelist.IpacWhitelist.allowJoin;
import static org.bukkit.Bukkit.getLogger;

public class onPlayerJoin implements Listener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import java.util.List;
import java.util.UUID;

import static aplini.ipacwhitelist.Func.EventFunc.startAsyncEventFunc;
import static aplini.ipacwhitelist.hook.hookAuthMe.autoLogin;
import static aplini.ipacwhitelist.hook.hookAuthMe.autoRegister;
import static aplini.ipacwhitelist.Func.EventFunc.startAsyncEventFunc;
import static aplini.ipacwhitelist.util.Type.VISIT;
import static org.bukkit.Bukkit.getLogger;

Expand Down
106 changes: 42 additions & 64 deletions src/main/java/aplini/ipacwhitelist/util/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.io.File;
import java.sql.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static aplini.ipacwhitelist.IpacWhitelist.getPlugin;
import static aplini.ipacwhitelist.util.Type.*;
Expand Down Expand Up @@ -56,63 +54,39 @@ public static synchronized void reconnect() {
// 初始化数据库
public static synchronized void initialize() {
try {

// String db = getPlugin().getConfig().getString("sql.db", "sqlite");
String db = "sqlite";

// SQLITE
if(db.equalsIgnoreCase("sqlite")){

// 使用 WAL 模式. 自动复用碎片空间
connection.prepareStatement("""
PRAGMA journal_mode = WAL;
PRAGMA auto_vacuum = 2;
"""
).execute();

// 重新连接数据库
reconnect();

// 是否启用大小写不敏感
String Name_COLLATE_NOCASE =
getPlugin().getConfig().getBoolean("sql.Name_COLLATE_NOCASE", true)
? "COLLATE NOCASE" : "";

// 加载数据表
connection.prepareStatement("""
CREATE TABLE IF NOT EXISTS "player" (
"ID" INTEGER NOT NULL,
"Type" INTEGER NOT NULL,
"Ban" INTEGER NOT NULL,
"UUID" TEXT NOT NULL,
"Name" TEXT NOT NULL %s,
"Time" INTEGER NOT NULL,
PRIMARY KEY("ID" AUTOINCREMENT)
);
CREATE INDEX IF NOT EXISTS IDX_Type ON "player" (Type);
CREATE INDEX IF NOT EXISTS IDX_Ban ON "player" (Ban );
CREATE INDEX IF NOT EXISTS IDX_UUID ON "player" (UUID);
CREATE INDEX IF NOT EXISTS IDX_Name ON "player" (Name);
CREATE INDEX IF NOT EXISTS IDX_Time ON "player" (Time);
""".formatted(Name_COLLATE_NOCASE)
).execute();
}else{
// connection.prepareStatement("""
// CREATE TABLE IF NOT EXISTS `%s` (
// `ID` bigint(7) NOT NULL,
// `Type` bigint(2) NOT NULL,
// `UUID` char(36) NOT NULL,
// `Name` varchar(16) NOT NULL,
// `Time` bigint(11) NOT NULL,
//
// INDEX `IDX_UUID` (`UUID`) USING BTREE,
// INDEX `IDX_Name` (`Name`) USING BTREE,
// INDEX `IDX_Type` (`Type`) USING BTREE
// );
// """.formatted(table)
// ).execute();
getLogger().warning("[IpacWhitelist] 暂时移除了非 SQLite 数据库的支持...");
}
// 使用 WAL 模式. 自动复用碎片空间
connection.prepareStatement("""
PRAGMA journal_mode = WAL;
PRAGMA auto_vacuum = 2;
"""
).execute();

// 重新连接数据库
reconnect();

// 是否启用大小写不敏感
String Name_COLLATE_NOCASE =
getPlugin().getConfig().getBoolean("sql.Name_COLLATE_NOCASE", true)
? "COLLATE NOCASE" : "";

// 加载数据表
connection.prepareStatement("""
CREATE TABLE IF NOT EXISTS "player" (
"ID" INTEGER NOT NULL,
"Type" INTEGER NOT NULL,
"Ban" INTEGER NOT NULL,
"UUID" TEXT NOT NULL,
"Name" TEXT NOT NULL %s,
"Time" INTEGER NOT NULL,
PRIMARY KEY("ID" AUTOINCREMENT)
);
CREATE INDEX IF NOT EXISTS IDX_Type ON "player" (Type);
CREATE INDEX IF NOT EXISTS IDX_Ban ON "player" (Ban );
CREATE INDEX IF NOT EXISTS IDX_UUID ON "player" (UUID);
CREATE INDEX IF NOT EXISTS IDX_Name ON "player" (Name);
CREATE INDEX IF NOT EXISTS IDX_Time ON "player" (Time);
""".formatted(Name_COLLATE_NOCASE)
).execute();

} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -154,7 +128,10 @@ public static PlayerData getPlayerData(Type inpDataType, String inpData){
try {
PreparedStatement sql = connection.prepareStatement(query);
sql.setString(1, inpData);
pd = packPlayerData(sql.executeQuery());
ResultSet results = sql.executeQuery();
if(results.next()){
pd = packPlayerData(results);
}
sql.close();
} catch (Exception e) {
getLogger().warning(e.getMessage());
Expand Down Expand Up @@ -259,10 +236,11 @@ public static void whileDataForList(Type type, int maxLine, whileDataForListInte
String query;
String limit = maxLine != -1 ? ("LIMIT "+ maxLine) : "";

query = switch (type) {
case ALL -> "SELECT * FROM `player` %s;".formatted(limit);
default -> "SELECT * FROM `player` WHERE (`Type` = %s) %s;".formatted(type.getID(), limit);
};
if(type == ALL){
query = "SELECT * FROM `player` %s;".formatted(limit);
}else{
query = "SELECT * FROM `player` WHERE (`Type` = %s) %s;".formatted(type.getID(), limit);
}

try {
PreparedStatement sql = connection.prepareStatement(query);
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# 数据库
sql:
# sqlite, 暂不支持 mysql
db: sqlite
# 使 Name 大小写不敏感, 同时影响白名单和指令. 仅可在建表时修改
Name_COLLATE_NOCASE: true

Expand Down

0 comments on commit cf083c4

Please sign in to comment.