Skip to content

Commit

Permalink
Conversion work
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 16, 2024
1 parent 6dcc33a commit 60e84e4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 31 deletions.
83 changes: 62 additions & 21 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cjburkey.claimchunk.cmd.*;
import com.cjburkey.claimchunk.config.ClaimChunkWorldProfileHandler;
import com.cjburkey.claimchunk.config.ccconfig.*;
import com.cjburkey.claimchunk.data.conversion.IDataConverter;
import com.cjburkey.claimchunk.data.newdata.*;
import com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler;
import com.cjburkey.claimchunk.event.*;
Expand All @@ -32,6 +33,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.file.Files;

// TODO: Split this plugin up into services that users can use
// Services:
Expand Down Expand Up @@ -65,10 +67,14 @@

public final class ClaimChunk extends JavaPlugin implements IClaimChunkPlugin {

// The global instance of ClaimChunk on this server
// A plugin can only exist in one instance on any given server so it's ok to have a static
// instance
private static ClaimChunk instance;
/**
* External quick access to the main ClaimChunk class.
*
* <p>A plugin can only exist in one instance on any given server so it's ok to have a static
* instance I think. We don't actually internally use this
*/
@Getter private static ClaimChunk instance;

// Set once ClaimChunk has registered the `chunk-claim` flag with WorldGuard.
private static boolean worldGuardRegisteredFlag = false;

Expand Down Expand Up @@ -387,14 +393,62 @@ private boolean initDataHandler() {
: createJsonDataHandler();
}*/
if (dataHandler == null) {
dataHandler =
new SqLiteDataHandler(
new File(getDataFolder(), "/data/claimAndPlayerData.sqlite3"));
File sqliteFile = new File(getDataFolder(), "/data/claimAndPlayerData.sqlite3");

IClaimChunkDataHandler oldDataHandler = null;
// UGLY HACK!
// RANKS ARE INITIALIZED AFTER DATA HANDLER, SO IF RANK FILE
// DOESN'T EXIST, WE CAN ASSUME THIS IS A NEW INSTALL RATHER THAN
// CONVERSION!
// AFTER 0.0.25 releases, 0.0.26 doesn't need to include this (but
// WILL require players to install 0.0.25 FIRST to upgrade from
// pre-0.0.25 if, say, 0.0.26 comes out while they're on 0.0.24).
if (!sqliteFile.exists() && new File(getDataFolder(), "/ranks.json").exists()) {
oldDataHandler =
(config.getUseDatabase())
? ((config.getGroupRequests())
? new BulkMySQLDataHandler<>(
this,
this::createJsonDataHandler,
ignored -> {} /*JsonDataHandler::deleteFiles*/)
: new MySQLDataHandler<>(
this,
this::createJsonDataHandler,
ignored -> {} /*JsonDataHandler::deleteFiles*/))
: createJsonDataHandler();
}

dataHandler = new SqLiteDataHandler(sqliteFile);

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

File dataFolder = new File(getDataFolder(), "/data");
File oldClaimedFile = new File(dataFolder, "/claimedChunks.json");
File oldPlayerFile = new File(dataFolder, "/playerData.json");
if (oldClaimedFile.exists()) {
Files.move(
oldClaimedFile.toPath(),
new File(dataFolder, "/OLD_claimedChunks.json").toPath());
}
if (oldPlayerFile.exists()) {
Files.move(
oldClaimedFile.toPath(),
new File(dataFolder, "/OLD_playerData.json").toPath());
}
} catch (Exception e) {
throw new RuntimeException(
"Failed to initialize previous data handler to convert old data!");
}
}
}

Utils.debug("Using data handler \"%s\"", dataHandler.getClass().getName());
try {
// Initialize the data handler
dataHandler.init();
if (!dataHandler.getHasInit()) dataHandler.init();
return true;
} catch (Exception e) {
Utils.err(
Expand Down Expand Up @@ -669,19 +723,6 @@ public void onDisable() {
Utils.log("Finished disable.");
}

/**
* External quick access to the main ClaimChunk class.
*
* @return The current instance of ClaimChunk
* @see org.bukkit.plugin.PluginManager#getPlugin(String)
* @deprecated It is recommended to use {@code (ClaimChunk)
* Bukkit.getServer().getPluginManager().getPlugin("ClaimChunk")}
*/
@Deprecated
public static ClaimChunk getInstance() {
return instance;
}

public static class DataHandlerAlreadySetException extends Exception {

@Serial private static final long serialVersionUID = 49857948732L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,17 @@ public void addClaimedChunks(DataChunk[] chunks) {
public void removeClaimedChunk(ChunkPos pos) {
// Get the chunk ID
int chunkId;
try(PreparedStatement statement = prep(claimChunk, connection, String.format("SELECT `%s` FROM `%s` WHERE `%s`=? AND `%s`=? AND `%s`=?", CLAIMED_CHUNKS_ID, CLAIMED_CHUNKS_TABLE_NAME,
CLAIMED_CHUNKS_WORLD,
CLAIMED_CHUNKS_X,
CLAIMED_CHUNKS_Z))) {
try (PreparedStatement statement =
prep(
claimChunk,
connection,
String.format(
"SELECT `%s` FROM `%s` WHERE `%s`=? AND `%s`=? AND `%s`=?",
CLAIMED_CHUNKS_ID,
CLAIMED_CHUNKS_TABLE_NAME,
CLAIMED_CHUNKS_WORLD,
CLAIMED_CHUNKS_X,
CLAIMED_CHUNKS_Z))) {
statement.setString(1, pos.world());
statement.setInt(2, pos.x());
statement.setInt(3, pos.z());
Expand All @@ -226,8 +233,13 @@ public void removeClaimedChunk(ChunkPos pos) {
}

// Remove chunk accesses
try (PreparedStatement statement = prep(claimChunk, connection, String.format(
"DELETE FROM `%s` WHERE `%s`=?", ACCESS_TABLE_NAME, ACCESS_CHUNK_ID))) {
try (PreparedStatement statement =
prep(
claimChunk,
connection,
String.format(
"DELETE FROM `%s` WHERE `%s`=?",
ACCESS_TABLE_NAME, ACCESS_CHUNK_ID))) {
statement.setInt(1, chunkId);
statement.execute();
} catch (Exception e) {
Expand All @@ -237,10 +249,13 @@ public void removeClaimedChunk(ChunkPos pos) {
}

// Delete the chunk
try (PreparedStatement statement = prep(claimChunk, connection, String.format(
"DELETE FROM `%s` WHERE `%s`=?",
CLAIMED_CHUNKS_TABLE_NAME,
CLAIMED_CHUNKS_ID))) {
try (PreparedStatement statement =
prep(
claimChunk,
connection,
String.format(
"DELETE FROM `%s` WHERE `%s`=?",
CLAIMED_CHUNKS_TABLE_NAME, CLAIMED_CHUNKS_ID))) {
statement.setInt(1, chunkId);
statement.execute();
} catch (Exception e) {
Expand Down

0 comments on commit 60e84e4

Please sign in to comment.