引入数据库以用于存储关键信息

This commit is contained in:
zhangyuheng 2024-05-12 23:54:00 +08:00
parent 2d10ead566
commit 5def80739c
5 changed files with 200 additions and 10 deletions

38
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>cn.lunadeer</groupId>
<artifactId>EssentialsD</artifactId>
<version>1.18.16</version>
<version>1.19.0</version>
<packaging>jar</packaging>
<name>EssentialsD</name>
@ -62,14 +62,34 @@
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.41.2.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.2</version>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -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 = "";
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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