Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Jan 14, 2025
2 parents e3b684a + b4825c4 commit d2df303
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 29 deletions.
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 @@ -47,6 +48,9 @@ public void initStorage(DataType type) {
createBlockStorageTables();
}
}

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

@Override
Expand Down Expand Up @@ -381,4 +385,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 @@ -49,6 +50,9 @@ public void initStorage(DataType type) {
createBlockStorageTables();
}
}

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

@Override
Expand Down Expand Up @@ -397,4 +401,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
@@ -1,13 +1,19 @@
package com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon;

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

import city.norain.slimefun4.timings.entry.SQLEntry;
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.implementation.Slimefun;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;

public abstract class SqlCommonAdapter<T extends ISqlCommonConfig> implements IDataSourceAdapter<T> {
protected HikariDataSource ds;
Expand All @@ -19,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 @@ -65,6 +72,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 @@ -85,4 +93,34 @@ public void shutdown() {
universalDataTable = null;
universalRecordTable = null;
}

public int getDatabaseVersion() {
return executeQuery("SELECT (" + FIELD_TABLE_VERSION + ") FROM " + tableInformationTable)
.getFirst()
.getInt(FieldKey.TABLE_VERSION);
}

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

switch (getDatabaseVersion()) {
case 0: {
patch = new DatabasePatchV1();
break;
}
}

if (patch == null) {
return;
}

try (var 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 @@ -65,6 +67,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 @@ -81,6 +84,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 @@ -35,6 +36,8 @@ public void initStorage(DataType type) {
case PLAYER_PROFILE -> createProfileTables();
case BLOCK_STORAGE -> createBlockStorageTables();
}

createTableInformationTable();
}

@Override
Expand Down Expand Up @@ -379,6 +382,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 @@ -12,7 +12,8 @@ public enum DataScope {
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});
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
Expand Up @@ -259,6 +259,10 @@ public SlimefunUniversalBlockData createUniversalBlock(Location l, String sfId)
uniData.setMenu(new UniversalMenu(preset, uuid, l));
}

if (Slimefun.getRegistry().getTickerBlocks().contains(sfId)) {
Slimefun.getTickerTask().enableTicker(l, uuid);
}

Slimefun.getDatabaseManager()
.getBlockDataController()
.saveUniversalData(uuid, sfId, Set.of(UniversalDataTrait.BLOCK, UniversalDataTrait.INVENTORY));
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;");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
public class StorageCacheUtils {
private static final Set<ADataContainer> loadingData = new HashSet<>();

@ParametersAreNonnullByDefault
public static boolean hasSlimefunBlock(Location l) {
return hasBlock(l) || hasUniversalBlock(l);
}

@ParametersAreNonnullByDefault
public static boolean hasBlock(Location l) {
return getBlock(l) != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ protected void dig(Block b, UniversalMenu menu, Block block) {
}

// We only want to break non-Slimefun blocks
if (!StorageCacheUtils.hasBlock(block.getLocation())
&& !StorageCacheUtils.hasUniversalBlock(block.getLocation())) {
if (!StorageCacheUtils.hasSlimefunBlock(block.getLocation())) {
breakBlock(menu, drops, block);
}
}
Expand All @@ -113,8 +112,7 @@ protected void moveAndDig(Block b, UniversalMenu menu, BlockFace face, Block blo
}

// We only want to break non-Slimefun blocks
if (!StorageCacheUtils.hasBlock(block.getLocation())
&& !StorageCacheUtils.hasUniversalBlock(block.getLocation())) {
if (!StorageCacheUtils.hasSlimefunBlock(block.getLocation())) {
breakBlock(menu, drops, block);
move(b, face, block);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,10 @@ protected void move(Block from, BlockFace face, Block to) {
Slimefun.getTickerTask().disableTicker(from.getLocation());

// Bro encountered a ghost 💀
if (StorageCacheUtils.hasBlock(to.getLocation())) {
var data = StorageCacheUtils.getBlock(to.getLocation());
if (StorageCacheUtils.hasSlimefunBlock(to.getLocation())) {
var data = StorageCacheUtils.getBlock(to.getLocation()) == null
? StorageCacheUtils.getBlock(to.getLocation())
: StorageCacheUtils.getUniversalBlock(to);
if (data != null && !data.isPendingRemove()) {
// Since it's a ghost, we just hunt it.
Slimefun.getDatabaseManager().getBlockDataController().removeBlock(to.getLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public BlockUseHandler getItemHandler() {
Block block = b.getRelative(BlockFace.UP);

// Just ignore slimefun block above.
if (StorageCacheUtils.hasBlock(block.getLocation())) return;
if (StorageCacheUtils.hasSlimefunBlock(block.getLocation())) return;

if (craft(p, input)) {
boolean water = Tag.LEAVES.isTagged(input.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedParticle;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -133,6 +132,9 @@ private boolean updateSaplingData(Block machine, Block block, BlockMenu inv, Sap
}

protected boolean isFertilizer(@Nullable ItemStack item) {
return SlimefunUtils.isItemSimilar(item, organicFertilizer, false, false);
var id = Slimefun.getItemDataService().getItemData(item);

return id.map(s -> s.startsWith("FERTILIZER") && SlimefunItems.FERTILIZER.getType() == item.getType())
.orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ public boolean canMine(@Nonnull Block block) {
Material type = block.getType();

if (type == Material.ANCIENT_DEBRIS) {
return canMineAncientDebris.getValue() && !StorageCacheUtils.hasBlock(block.getLocation());
return canMineAncientDebris.getValue() && !StorageCacheUtils.hasSlimefunBlock(block.getLocation());
} else if (version.isAtLeast(MinecraftVersion.MINECRAFT_1_17) && SlimefunTag.DEEPSLATE_ORES.isTagged(type)) {
return canMineDeepslateOres.getValue() && !StorageCacheUtils.hasBlock(block.getLocation());
return canMineDeepslateOres.getValue() && !StorageCacheUtils.hasSlimefunBlock(block.getLocation());
} else {
return SlimefunTag.INDUSTRIAL_MINER_ORES.isTagged(type) && !StorageCacheUtils.hasBlock(block.getLocation());
return SlimefunTag.INDUSTRIAL_MINER_ORES.isTagged(type)
&& !StorageCacheUtils.hasSlimefunBlock(block.getLocation());
}
}
}
Loading

0 comments on commit d2df303

Please sign in to comment.