Skip to content

Commit

Permalink
Merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Jan 15, 2025
2 parents c16f312 + 3e68e4a commit dce65ad
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/city/norain/slimefun4/VaultIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected static void register(Slimefun plugin) {
plugin.getLogger().log(Level.WARNING, "无法接入 Vault. 如果你是 CMI 用户, 请至配置文件启用经济系统");
}
} else {
plugin.getLogger().log(Level.WARNING, "无法接入 Vault. 你必须先安装 Vault!");
plugin.getLogger().log(Level.WARNING, "无法接入 Vault, 使用游戏币解锁研究功能将无法使用");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ default List<RecordSet> getData(RecordKey key) {
List<RecordSet> getData(RecordKey key, boolean distinct);

void deleteData(RecordKey key);

void patch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;

Expand Down Expand Up @@ -48,6 +49,9 @@ public void initStorage(DataType type) {
createBlockStorageTables();
}
}

tableInformationTable = SqlUtils.mapTable(DataScope.TABLE_INFORMATION, config.tablePrefix());
createTableInformationTable();
}

@Override
Expand Down Expand Up @@ -382,4 +386,13 @@ private void createUniversalDataTable() {
+ ")"
+ ");");
}

private void createTableInformationTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ tableInformationTable
+ "("
+ FIELD_TABLE_VERSION
+ " INT UNIQUE NOT NULL DEFAULT '0'"
+ ");");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;

Expand Down Expand Up @@ -48,6 +49,9 @@ public void initStorage(DataType type) {
createBlockStorageTables();
}
}

tableInformationTable = SqlUtils.mapTable(DataScope.TABLE_INFORMATION, config.tablePrefix());
createTableInformationTable();
}

@Override
Expand Down Expand Up @@ -396,4 +400,13 @@ private void createUniversalDataTable() {
+ ")"
+ ");");
}

private void createTableInformationTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ tableInformationTable
+ "("
+ FIELD_TABLE_VERSION
+ " INT UNIQUE NOT NULL DEFAULT '0'"
+ ");");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.IDataSourceAdapter;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataScope;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.FieldKey;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import com.xzavier0722.mc.plugin.slimefun4.storage.patch.DatabasePatch;
import com.xzavier0722.mc.plugin.slimefun4.storage.patch.DatabasePatchV1;
import com.zaxxer.hikari.HikariDataSource;
import io.github.thebusybiscuit.slimefun4.api.ErrorReport;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;

public abstract class SqlCommonAdapter<T extends ISqlCommonConfig> implements IDataSourceAdapter<T> {
protected HikariDataSource ds;
Expand All @@ -22,6 +25,7 @@ public abstract class SqlCommonAdapter<T extends ISqlCommonConfig> implements ID
chunkDataTable,
blockInvTable,
universalInvTable;
protected String tableInformationTable;
protected T config;

@Override
Expand Down Expand Up @@ -62,6 +66,7 @@ protected String mapTable(DataScope scope) {
case UNIVERSAL_INVENTORY -> universalInvTable;
case UNIVERSAL_RECORD -> universalRecordTable;
case UNIVERSAL_DATA -> universalDataTable;
case TABLE_INFORMATION -> tableInformationTable;
case NONE -> throw new IllegalArgumentException("NONE cannot be a storage data scope!");
};
}
Expand All @@ -82,4 +87,36 @@ public void shutdown() {
universalDataTable = null;
universalRecordTable = null;
}

public int getDatabaseVersion() {
List<RecordSet> query = executeQuery("SELECT (" + SqlConstants.FIELD_TABLE_VERSION + ") FROM "
+ (tableInformationTable == null ? SqlConstants.TABLE_NAME_TABLE_INFORMATION : tableInformationTable));

if (query.isEmpty()) {
return 0;
} else {
return query.get(0).getInt(FieldKey.TABLE_VERSION);
}
}

@Override
public void patch() {
DatabasePatch patch = null;

if (getDatabaseVersion() == 0) {
patch = new DatabasePatchV1();
}

if (patch == null) {
return;
}

try (Connection conn = ds.getConnection()) {
Slimefun.logger().log(Level.INFO, "正在更新数据库版本至 " + patch.getVersion() + ", 可能需要一段时间...");
patch.patch(conn.createStatement(), config);
Slimefun.logger().log(Level.INFO, "更新完成. ");
} catch (SQLException e) {
Slimefun.logger().log(Level.SEVERE, "更新数据库时出现问题!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface SqlConstants {
String TABLE_NAME_UNIVERSAL_INVENTORY = "universal_inventory";
String TABLE_NAME_UNIVERSAL_RECORD = "universal_record";
String TABLE_NAME_UNIVERSAL_DATA = "universal_data";
String TABLE_NAME_TABLE_INFORMATION = "table_information";

String FIELD_PLAYER_UUID = "p_uuid";
String FIELD_PLAYER_NAME = "p_name";
Expand All @@ -36,4 +37,6 @@ public interface SqlConstants {
String FIELD_UNIVERSAL_UUID = "universal_uuid";

String FIELD_UNIVERSAL_TRAITS = "universal_traits";

String FIELD_TABLE_VERSION = "table_version";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_BACKPACK;
Expand All @@ -24,6 +25,7 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_CHUNK_DATA;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_PLAYER_PROFILE;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_PLAYER_RESEARCH;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_TABLE_INFORMATION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_UNIVERSAL_DATA;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_UNIVERSAL_INVENTORY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_UNIVERSAL_RECORD;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class SqlUtils {
fieldMap.put(FieldKey.DATA_VALUE, FIELD_DATA_VALUE);
fieldMap.put(FieldKey.UNIVERSAL_UUID, FIELD_UNIVERSAL_UUID);
fieldMap.put(FieldKey.UNIVERSAL_TRAITS, FIELD_UNIVERSAL_TRAITS);
fieldMap.put(FieldKey.TABLE_VERSION, FIELD_TABLE_VERSION);
mapper = new FieldMapper<>(fieldMap);
}

Expand All @@ -74,6 +77,7 @@ public static String mapTable(DataScope scope) {
case UNIVERSAL_INVENTORY -> TABLE_NAME_UNIVERSAL_INVENTORY;
case UNIVERSAL_RECORD -> TABLE_NAME_UNIVERSAL_RECORD;
case UNIVERSAL_DATA -> TABLE_NAME_UNIVERSAL_DATA;
case TABLE_INFORMATION -> TABLE_NAME_TABLE_INFORMATION;
case NONE -> throw new IllegalArgumentException("NONE cannot be a storage data scope!");
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;

Expand All @@ -32,6 +33,8 @@ public void initStorage(DataType type) {
case PLAYER_PROFILE -> createProfileTables();
case BLOCK_STORAGE -> createBlockStorageTables();
}

createTableInformationTable();
}

@Override
Expand Down Expand Up @@ -376,6 +379,16 @@ private void createUniversalDataTable() {
+ ");");
}

private void createTableInformationTable() {
var table = SqlUtils.mapTable(DataScope.TABLE_INFORMATION);
executeSql("CREATE TABLE IF NOT EXISTS "
+ table
+ "("
+ FIELD_TABLE_VERSION
+ " INT UNIQUE NOT NULL DEFAULT '0'"
+ ");");
}

public synchronized void executeSql(String sql) {
super.executeSql(sql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
public enum DataScope {
NONE,
PLAYER_RESEARCH,
PLAYER_PROFILE(new FieldKey[]{FieldKey.PLAYER_UUID}),
BACKPACK_PROFILE(new FieldKey[]{FieldKey.BACKPACK_ID}),
BACKPACK_INVENTORY(new FieldKey[]{FieldKey.BACKPACK_ID, FieldKey.INVENTORY_SLOT}),
BLOCK_RECORD(new FieldKey[]{FieldKey.LOCATION}),
BLOCK_DATA(new FieldKey[]{FieldKey.LOCATION, FieldKey.DATA_KEY}),
CHUNK_DATA(new FieldKey[]{FieldKey.CHUNK, FieldKey.DATA_KEY}),
BLOCK_INVENTORY(new FieldKey[]{FieldKey.LOCATION, FieldKey.INVENTORY_SLOT}),
UNIVERSAL_RECORD(new FieldKey[]{FieldKey.UNIVERSAL_UUID}),
UNIVERSAL_DATA(new FieldKey[]{FieldKey.UNIVERSAL_UUID, FieldKey.DATA_KEY}),
UNIVERSAL_INVENTORY(new FieldKey[]{FieldKey.UNIVERSAL_UUID, FieldKey.INVENTORY_SLOT});
PLAYER_PROFILE(new FieldKey[] {FieldKey.PLAYER_UUID}),
BACKPACK_PROFILE(new FieldKey[] {FieldKey.BACKPACK_ID}),
BACKPACK_INVENTORY(new FieldKey[] {FieldKey.BACKPACK_ID, FieldKey.INVENTORY_SLOT}),
BLOCK_RECORD(new FieldKey[] {FieldKey.LOCATION}),
BLOCK_DATA(new FieldKey[] {FieldKey.LOCATION, FieldKey.DATA_KEY}),
CHUNK_DATA(new FieldKey[] {FieldKey.CHUNK, FieldKey.DATA_KEY}),
BLOCK_INVENTORY(new FieldKey[] {FieldKey.LOCATION, FieldKey.INVENTORY_SLOT}),
UNIVERSAL_RECORD(new FieldKey[] {FieldKey.UNIVERSAL_UUID}),
UNIVERSAL_DATA(new FieldKey[] {FieldKey.UNIVERSAL_UUID, FieldKey.DATA_KEY}),
UNIVERSAL_INVENTORY(new FieldKey[] {FieldKey.UNIVERSAL_UUID, FieldKey.INVENTORY_SLOT}),
TABLE_INFORMATION;

private final FieldKey[] primaryKeys;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public enum FieldKey {
*/
UNIVERSAL_UUID,

UNIVERSAL_TRAITS;
UNIVERSAL_TRAITS,

TABLE_VERSION;

private final boolean isNumType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected ADataController(DataType dataType) {
public void init(IDataSourceAdapter<?> dataAdapter, int maxReadThread, int maxWriteThread) {
this.dataAdapter = dataAdapter;
dataAdapter.initStorage(dataType);
dataAdapter.patch();
readExecutor = Executors.newFixedThreadPool(maxReadThread, threadFactory);
writeExecutor = Executors.newFixedThreadPool(maxWriteThread, threadFactory);
callbackExecutor = Executors.newCachedThreadPool(threadFactory);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.xzavier0722.mc.plugin.slimefun4.storage.patch;

import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.ISqlCommonConfig;
import java.sql.SQLException;
import java.sql.Statement;
import lombok.Getter;

@Getter
public abstract class DatabasePatch {
protected int version;

public abstract void patch(Statement stmt, ISqlCommonConfig config) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.xzavier0722.mc.plugin.slimefun4.storage.patch;

import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_INVENTORY_ITEM;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;

import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.mysql.MysqlConfig;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.ISqlCommonConfig;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlCommonConfig;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlUtils;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataScope;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabasePatchV1 extends DatabasePatch {
public DatabasePatchV1() {
this.version = 1;
}

@Override
public void patch(Statement stmt, ISqlCommonConfig config) throws SQLException {
var table = SqlUtils.mapTable(
DataScope.TABLE_INFORMATION, config instanceof SqlCommonConfig scc ? scc.tablePrefix() : "");

stmt.execute("UPDATE " + table + " SET " + FIELD_TABLE_VERSION + " = '1' LIMIT 1;");

if (config instanceof MysqlConfig mysqlConf) {
var uniInvTable = SqlUtils.mapTable(DataScope.UNIVERSAL_INVENTORY, mysqlConf.tablePrefix());
stmt.execute("ALTER TABLE " + uniInvTable + " MODIFY COLUMN " + FIELD_INVENTORY_ITEM + " TEXT;");
}
}
}

0 comments on commit dce65ad

Please sign in to comment.