From 8f8afc014a5859aa2b5c75e943ec0a7da26cada7 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Fri, 24 May 2024 15:55:43 -0400 Subject: [PATCH] Finish up! --- .../com/cjburkey/claimchunk/ClaimChunk.java | 4 +- .../{IDataConverter.java => DataConvert.java} | 37 +++++++------------ .../data/newdata/MySQLDataHandler.java | 4 +- .../data/sqlite/SqLiteDataHandler.java | 5 +-- .../claimchunk/data/sqlite/SqLiteWrapper.java | 25 ++++++++++++- .../claimchunk/data/sqlite/SqlDataChunk.java | 11 ++---- .../claimchunk/data/sqlite/SqlDataPlayer.java | 3 -- 7 files changed, 46 insertions(+), 43 deletions(-) rename src/main/java/com/cjburkey/claimchunk/data/{IDataConverter.java => DataConvert.java} (50%) diff --git a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java index 1bd39e0..47c8fd7 100644 --- a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java +++ b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java @@ -6,7 +6,7 @@ import com.cjburkey.claimchunk.cmd.*; import com.cjburkey.claimchunk.config.ClaimChunkWorldProfileHandler; import com.cjburkey.claimchunk.config.ccconfig.*; -import com.cjburkey.claimchunk.data.IDataConverter; +import com.cjburkey.claimchunk.data.DataConvert; import com.cjburkey.claimchunk.data.newdata.*; import com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler; import com.cjburkey.claimchunk.event.*; @@ -416,7 +416,7 @@ private boolean initDataHandler() { if (oldDataHandler != null) { try { - IDataConverter.copyConvert(oldDataHandler, dataHandler); + DataConvert.copyConvert(oldDataHandler, dataHandler); oldDataHandler.exit(); if (oldClaimedFile.exists()) { diff --git a/src/main/java/com/cjburkey/claimchunk/data/IDataConverter.java b/src/main/java/com/cjburkey/claimchunk/data/DataConvert.java similarity index 50% rename from src/main/java/com/cjburkey/claimchunk/data/IDataConverter.java rename to src/main/java/com/cjburkey/claimchunk/data/DataConvert.java index ea7cb89..0782ee7 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/IDataConverter.java +++ b/src/main/java/com/cjburkey/claimchunk/data/DataConvert.java @@ -5,25 +5,23 @@ /** * Represents a class that may act as a converter between two different data systems. * - * @param The type from which the conversion may occur - * @param The type into which the provided handler will be converted * @since 0.0.13 */ -public interface IDataConverter< - From extends IClaimChunkDataHandler, To extends IClaimChunkDataHandler> { +public class DataConvert { + + private DataConvert() {} /** * Copies the data from the provided old data handler into the provided new data handler. This * does not update the old data handler. * - * @param oldDataHandler The old handler - * @param newDataHandler The new handler - * @param The type of the old data handler - * @param The type of the new data handler + * @param oldDataHandler The old handler, may or may not be initialized + * @param newDataHandler The new handler, may or may not be initialized * @since 0.0.13 */ - static void copyConvert( - A oldDataHandler, B newDataHandler) throws Exception { + public static void copyConvert( + IClaimChunkDataHandler oldDataHandler, IClaimChunkDataHandler newDataHandler) + throws Exception { // Initialize the old data handler if it hasn't been initialized yet if (!oldDataHandler.getHasInit()) oldDataHandler.init(); @@ -33,21 +31,12 @@ static void // Initialize the new data handler if it hasn't been initialized yet if (!newDataHandler.getHasInit()) newDataHandler.init(); + // Copy the player data from the old data handler to the new data handler. + // Make sure we do this before players! The SQLite data handler will make dummy players if + // there aren't proper players in the player data table already. + newDataHandler.addPlayers(oldDataHandler.getFullPlayerData()); + // Copy the chunks from the old data handler to the new data handler newDataHandler.addClaimedChunks(oldDataHandler.getClaimedChunks()); - - // Copy the player data from the old data handler to the new data handler - newDataHandler.addPlayers(oldDataHandler.getFullPlayerData()); } - - /** - * Converts one kind of data handler into the other kind. - * - * @param oldDataHandler The old data handler - * @return A new data handler containing the old data handler's data - * @throws Exception Any error that may occur during any phase of data conversion - * @since 0.0.13 - */ - @SuppressWarnings("unused") - To convert(From oldDataHandler) throws Exception; } diff --git a/src/main/java/com/cjburkey/claimchunk/data/newdata/MySQLDataHandler.java b/src/main/java/com/cjburkey/claimchunk/data/newdata/MySQLDataHandler.java index 205c9fa..797e125 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/newdata/MySQLDataHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/data/newdata/MySQLDataHandler.java @@ -7,7 +7,7 @@ import com.cjburkey.claimchunk.chunk.ChunkPlayerPermissions; import com.cjburkey.claimchunk.chunk.ChunkPos; import com.cjburkey.claimchunk.chunk.DataChunk; -import com.cjburkey.claimchunk.data.IDataConverter; +import com.cjburkey.claimchunk.data.DataConvert; import com.cjburkey.claimchunk.player.FullPlayerData; import com.cjburkey.claimchunk.player.SimplePlayerData; @@ -116,7 +116,7 @@ public void init() throws Exception { } if (oldDataHandler != null && claimChunk.getConfigHandler().getConvertOldData()) { - IDataConverter.copyConvert(oldDataHandler, this); + DataConvert.copyConvert(oldDataHandler, this); oldDataHandler.exit(); if (onCleanOld != null) { onCleanOld.accept(oldDataHandler); diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java index d206fc1..1459adf 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java @@ -20,10 +20,7 @@ * I've actually just decided that we're gonna do it this way: * - SQLite backing database *file* similar to current MySQL integration (which will * be removed and automatically converted). - * - Have some intermediary layer that can - * - Keep some regions in memory and unload when no players are within them - * for a minute or two. - * - Respond immediately and asynchronously update database. + * - Have some intermediary layer that can Respond immediately and asynchronously update database. */ /** diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java index 466d35a..4b0283d 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java @@ -65,6 +65,28 @@ public void close() { public void addClaimedChunk(DataChunk chunk) { SqlClosure.sqlExecute( connection -> { + // Make sure the player already exists! + // If there isn't a player with their UUID as a primary key, I'm pretty sure + // inserting into the chunk data would fail. This only really matters during + // loading, as there could be a chance the player is missing, somehow? + try (PreparedStatement statement = + connection.prepareStatement( + """ + INSERT OR IGNORE INTO player_data ( + player_uuid, + last_ign, + chunk_name, + last_online_time, + alerts_enabled, + extra_max_claims + ) VALUES ( + ?, "", NULL, 0, TRUE, 0 + ) + """)) { + statement.setString(1, chunk.player.toString()); + statement.execute(); + } + // Add the chunk try (PreparedStatement statement = connection.prepareStatement( @@ -149,13 +171,14 @@ public void removeClaimedChunk(ChunkPos chunk) { }); } + // The provided player data will replace an existing row public void addPlayer(FullPlayerData playerData) { SqlClosure.sqlExecute( connection -> { try (PreparedStatement statement = connection.prepareStatement( """ - INSERT INTO player_data ( + INSERT OR REPLACE INTO player_data ( player_uuid, last_ign, chunk_name, diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataChunk.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataChunk.java index de6b20f..dfb4af3 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataChunk.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataChunk.java @@ -6,17 +6,14 @@ public class SqlDataChunk { @Column(name = "chunk_world") - public String world; + String world; @Column(name = "chunk_x") - public int x; + int x; @Column(name = "chunk_z") - public int z; + int z; @Column(name = "owner_uuid") - public String uuid; - - @SuppressWarnings("unused") - public SqlDataChunk() {} + String uuid; } diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataPlayer.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataPlayer.java index 14df833..2a3892b 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataPlayer.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqlDataPlayer.java @@ -25,7 +25,4 @@ public class SqlDataPlayer { @Column(name = "extra_max_claims") public int extraMaxClaims; - - @SuppressWarnings("unused") - public SqlDataPlayer() {} }