Skip to content

Commit

Permalink
Finish up!
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 24, 2024
1 parent 1742869 commit 8f8afc0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -416,7 +416,7 @@ private boolean initDataHandler() {

if (oldDataHandler != null) {
try {
IDataConverter.copyConvert(oldDataHandler, dataHandler);
DataConvert.copyConvert(oldDataHandler, dataHandler);
oldDataHandler.exit();

if (oldClaimedFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@
/**
* Represents a class that may act as a converter between two different data systems.
*
* @param <From> The type from which the conversion may occur
* @param <To> 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 <A> The type of the old data handler
* @param <B> 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 <A extends IClaimChunkDataHandler, B extends IClaimChunkDataHandler> 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();

Expand All @@ -33,21 +31,12 @@ static <A extends IClaimChunkDataHandler, B extends IClaimChunkDataHandler> 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ public class SqlDataPlayer {

@Column(name = "extra_max_claims")
public int extraMaxClaims;

@SuppressWarnings("unused")
public SqlDataPlayer() {}
}

0 comments on commit 8f8afc0

Please sign in to comment.