From 5def80739c3115652bdca552551f920bf5cbb905 Mon Sep 17 00:00:00 2001 From: zhangyuheng Date: Sun, 12 May 2024 23:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E4=BB=A5=E7=94=A8=E4=BA=8E=E5=AD=98=E5=82=A8=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 38 ++++++-- .../lunadeer/essentialsd/DatabaseManager.java | 92 +++++++++++++++++++ .../cn/lunadeer/essentialsd/EssentialsD.java | 3 + .../essentialsd/utils/ConfigManager.java | 66 +++++++++++++ src/main/resources/config.yml | 11 ++- 5 files changed, 200 insertions(+), 10 deletions(-) create mode 100644 src/main/java/cn/lunadeer/essentialsd/DatabaseManager.java diff --git a/pom.xml b/pom.xml index e8fa558..1f8045f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.lunadeer EssentialsD - 1.18.16 + 1.19.0 jar EssentialsD @@ -62,14 +62,34 @@ papermc https://repo.papermc.io/repository/maven-public/ + + jitpack.io + https://jitpack.io + - - - dev.folia - folia-api - 1.20.1-R0.1-SNAPSHOT - provided - - + + + dev.folia + folia-api + 1.20.1-R0.1-SNAPSHOT + provided + + + org.xerial + sqlite-jdbc + 3.41.2.2 + + + org.postgresql + postgresql + 42.7.2 + + + com.github.MilkBowl + VaultAPI + 1.7 + provided + + diff --git a/src/main/java/cn/lunadeer/essentialsd/DatabaseManager.java b/src/main/java/cn/lunadeer/essentialsd/DatabaseManager.java new file mode 100644 index 0000000..af61551 --- /dev/null +++ b/src/main/java/cn/lunadeer/essentialsd/DatabaseManager.java @@ -0,0 +1,92 @@ +package cn.lunadeer.essentialsd; + +import cn.lunadeer.essentialsd.utils.XLogger; + +import java.sql.*; + +public class DatabaseManager { + + public static Connection createConnection() { + try { + String connectionUrl; + if (EssentialsD.config.getDbType().equals("pgsql")) { + XLogger.info("正在连接到 PostgreSQL 数据库"); + Class.forName("org.postgresql.Driver"); + connectionUrl = "jdbc:postgresql://" + EssentialsD.config.getDbHost() + ":" + EssentialsD.config.getDbPort(); + connectionUrl += "/" + EssentialsD.config.getDbName(); + return DriverManager.getConnection(connectionUrl, EssentialsD.config.getDbUser(), EssentialsD.config.getDbPass()); + } else if (EssentialsD.config.getDbType().equals("sqlite")) { + XLogger.info("正在连接到 SQLite 数据库"); + Class.forName("org.sqlite.JDBC"); + connectionUrl = "jdbc:sqlite:" + EssentialsD.instance.getDataFolder() + "/" + EssentialsD.config.getDbName() + ".db"; + return DriverManager.getConnection(connectionUrl); + } else { + XLogger.err("=== 严重错误 ==="); + XLogger.err("数据库类型错误,只能为 pgsql 或 sqlite"); + XLogger.err("==============="); + return null; + } + } catch (ClassNotFoundException | SQLException e) { + XLogger.err("=== 严重错误 ==="); + XLogger.err("Database connection failed: " + e.getMessage()); + XLogger.err("==============="); + return null; + } + } + + public static ResultSet query(String sql) { + Connection conn = EssentialsD.dbConnection; + if (conn == null) { + return null; + } + try { + Statement stmt = conn.createStatement(); + if (sql.contains("SERIAL PRIMARY KEY") && EssentialsD.config.getDbType().equals("sqlite")) { + sql = sql.replace("SERIAL PRIMARY KEY", "INTEGER PRIMARY KEY AUTOINCREMENT"); + } + // if query with no result return null + if (stmt.execute(sql)) { + return stmt.getResultSet(); + } + } catch (SQLException e) { + handleDatabaseError("Database query failed: ", e, sql); + } + return null; + } + + private static void handleDatabaseError(String errorMessage, SQLException e, String sql) { + XLogger.err("=== 严重错误 ==="); + XLogger.err(errorMessage + e.getMessage()); + XLogger.err("SQL: " + sql); + XLogger.err("==============="); + } + + private static void addColumnIfNotExists(String tableName, String columnName, String columnDefinition) { + if (EssentialsD.config.getDbType().equals("pgsql")) { + String sql = "ALTER TABLE " + tableName + " ADD COLUMN IF NOT EXISTS " + columnName + " " + columnDefinition + ";"; + query(sql); + } else if (EssentialsD.config.getDbType().equals("sqlite")) { + try { + ResultSet rs = query("PRAGMA table_info(" + tableName + ");"); + boolean columnExists = false; + if (rs != null) { + while (rs.next()) { + if (columnName.equals(rs.getString("name"))) { + columnExists = true; + break; + } + } + } + if (!columnExists) { + query("ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDefinition + ";"); + } + } catch (SQLException e) { + handleDatabaseError("Database operation failed: ", e, ""); + } + } + } + + public static void migrate() { + String sql = ""; + } +} diff --git a/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java b/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java index 554252b..f9e2382 100644 --- a/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java +++ b/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java @@ -6,6 +6,7 @@ import cn.lunadeer.essentialsd.utils.*; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.sql.Connection; import java.util.Objects; public final class EssentialsD extends JavaPlugin { @@ -15,6 +16,7 @@ public final class EssentialsD extends JavaPlugin { // Plugin startup logic instance = this; config = new ConfigManager(instance); + dbConnection = DatabaseManager.createConnection(); scheduler = new Scheduler(this); tpManager = new TeleportManager(); @@ -88,5 +90,6 @@ public final class EssentialsD extends JavaPlugin { public static ConfigManager config; public static Scheduler scheduler; public static TeleportManager tpManager; + public static Connection dbConnection; private GiteaReleaseCheck releaseCheck; } diff --git a/src/main/java/cn/lunadeer/essentialsd/utils/ConfigManager.java b/src/main/java/cn/lunadeer/essentialsd/utils/ConfigManager.java index 06d0adf..143dacd 100644 --- a/src/main/java/cn/lunadeer/essentialsd/utils/ConfigManager.java +++ b/src/main/java/cn/lunadeer/essentialsd/utils/ConfigManager.java @@ -44,6 +44,16 @@ public class ConfigManager { _recipes_invisible_item_frame = _file.getBoolean("Recipes.InvisibleItemFrame", true); _recipes_light_block = _file.getBoolean("Recipes.LightBlock", true); _recipes_stacked_enchant_book = _file.getBoolean("Recipes.StackedEnchantBook", true); + _db_type = _file.getString("Database.Type", "sqlite"); + if (!_db_type.equals("pgsql") && !_db_type.equals("sqlite")) { + XLogger.err("当前数据库只支持 pgsql 或 sqlite,已重置为 sqlite"); + setDbType("sqlite"); + } + _db_host = _file.getString("Database.Host", "localhost"); + _db_port = _file.getString("Database.Port", "5432"); + _db_name = _file.getString("Database.Name", "dominion"); + _db_user = _file.getString("Database.User", "postgres"); + _db_pass = _file.getString("Database.Pass", "postgres"); } public Boolean isDebug() { @@ -199,6 +209,54 @@ public class ConfigManager { }, _chunk_operate_delay * 20); } + public String getDbType() { + return _db_type; + } + + public void setDbType(String db_type) { + _db_type = db_type; + _file.set("Database.Type", db_type); + _plugin.saveConfig(); + } + + public String getDbHost() { + return _db_host; + } + + public String getDbPort() { + return _db_port; + } + + public String getDbName() { + return _db_name; + } + + public void setDbUser(String db_user) { + _db_user = db_user; + _file.set("Database.User", db_user); + _plugin.saveConfig(); + } + + public String getDbUser() { + if (_db_user.contains("@")) { + setDbUser("'" + _db_user + "'"); + } + return _db_user; + } + + public void setDbPass(String db_pass) { + _db_pass = db_pass; + _file.set("Database.Pass", db_pass); + _plugin.saveConfig(); + } + + public String getDbPass() { + if (_db_pass.contains("@")) { + setDbPass("'" + _db_pass + "'"); + } + return _db_pass; + } + private final EssentialsD _plugin; private FileConfiguration _file; @@ -229,4 +287,12 @@ public class ConfigManager { private Boolean _recipes_invisible_item_frame; private Boolean _recipes_light_block; private Boolean _recipes_stacked_enchant_book; + + // database + private String _db_type; + private String _db_host; + private String _db_port; + private String _db_user; + private String _db_pass; + private String _db_name; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 357002d..c959429 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,8 +1,17 @@ +# 数据库配置 +Database: + Type: sqlite # pgsql, sqlite + Host: localhost # 数据库地址 + Port: 5432 # 数据库端口 + Name: essentials_d # 数据库名称 + User: essentials_d # 数据库用户名 + Pass: essentials_d # 数据库密码 + # 附魔瓶经验值倍率 ExpBottleRatio: 1.0 # 强加载区块列表 -ForceLoadChunks: [] +ForceLoadChunks: [ ] # - world:0:0 # - world_the_end:-12:12