Skip to content

Commit

Permalink
Start working on loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 16, 2024
1 parent fcafaf5 commit 2b78d73
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 142 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,8 +6,8 @@
import com.cjburkey.claimchunk.cmd.*;
import com.cjburkey.claimchunk.config.ClaimChunkWorldProfileHandler;
import com.cjburkey.claimchunk.config.ccconfig.*;
import com.cjburkey.claimchunk.data.journaled.JournaledDataHandler;
import com.cjburkey.claimchunk.data.newdata.*;
import com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler;
import com.cjburkey.claimchunk.event.*;
import com.cjburkey.claimchunk.i18n.V2JsonMessages;
import com.cjburkey.claimchunk.layer.PlaceholderInitLayer;
Expand Down Expand Up @@ -388,7 +388,7 @@ private boolean initDataHandler() {
}*/
if (dataHandler == null) {
dataHandler =
new JournaledDataHandler(
new SqLiteDataHandler(
new File(getDataFolder(), "/data/claimAndPlayerData.sqlite3"));
}
Utils.debug("Using data handler \"%s\"", dataHandler.getClass().getName());
Expand Down
49 changes: 26 additions & 23 deletions src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public ChunkHandler(IClaimChunkDataHandler dataHandler, ClaimChunk claimChunk) {
* Result returned by the {@link #fillClaimInto(String, int, int, int, int, UUID, Collection)}
* and {@link #fillClaim(String, int, int, int, UUID)} methods.
*/
public static enum FloodClaimResult {
public enum FloodClaimResult {

/** The method completed without issues. */
SUCCESSFULL,
SUCCESSFUL,

/** The method recursed too many times and aborted due to that. */
TOO_MANY_RECUSIONS,
TOO_MANY_RECURSIONS,

/** The collection got too big and the method aborted due to that. */
COLLECTION_TOO_BIG,
Expand All @@ -43,13 +43,13 @@ public static enum FloodClaimResult {
* The algorithm hit a claimed chunk that did not belong to the player and aborted due to
* this
*/
HIT_NONPLAYER_CLAIM;
HIT_NONPLAYER_CLAIM,
}

/**
* Claims several chunks at once for a player. This method is very unsafe at it's own as it does
* Claims several chunks at once for a player. This method is very unsafe at its own as it does
* not test whether the player can actually claim that chunk. This means that ownership can be
* overridden. And the player can go over it's quota as well
* overridden. And the player can go over its quota as well
*
* @param chunks The chunks to claim
* @param player The player that claims these chunks
Expand Down Expand Up @@ -110,19 +110,19 @@ public ChunkPos claimChunk(String world, int x, int z, UUID player) {
- getClaimed(player));
Map.Entry<Collection<ChunkPos>, FloodClaimResult> result =
fillClaim(world, x - 1, z, maxArea, player);
if (result.getValue() == FloodClaimResult.SUCCESSFULL) {
if (result.getValue() == FloodClaimResult.SUCCESSFUL) {
claimAll(result.getKey(), player);
} else {
result = fillClaim(world, x + 1, z, maxArea, player);
if (result.getValue() == FloodClaimResult.SUCCESSFULL) {
if (result.getValue() == FloodClaimResult.SUCCESSFUL) {
claimAll(result.getKey(), player);
} else {
result = fillClaim(world, x, z - 1, maxArea, player);
if (result.getValue() == FloodClaimResult.SUCCESSFULL) {
if (result.getValue() == FloodClaimResult.SUCCESSFUL) {
claimAll(result.getKey(), player);
} else {
result = fillClaim(world, x, z + 1, maxArea, player);
if (result.getValue() == FloodClaimResult.SUCCESSFULL) {
if (result.getValue() == FloodClaimResult.SUCCESSFUL) {
claimAll(result.getKey(), player);
}
}
Expand Down Expand Up @@ -161,35 +161,35 @@ private FloodClaimResult fillClaimInto(
UUID player,
Collection<ChunkPos> collector) {
if (recursions == 0) {
return FloodClaimResult.TOO_MANY_RECUSIONS;
return FloodClaimResult.TOO_MANY_RECURSIONS;
}
if (collector.size() > maxSize) {
return FloodClaimResult.COLLECTION_TOO_BIG;
}
ChunkPos claimingPosition = new ChunkPos(world, x, z);
if (collector.contains(claimingPosition)) {
return FloodClaimResult.SUCCESSFULL;
return FloodClaimResult.SUCCESSFUL;
}
UUID owner = getOwner(claimingPosition);
if (owner != null) {
if (owner.equals(player)) {
return FloodClaimResult.SUCCESSFULL; // Hit player claim, do not claim it
return FloodClaimResult.SUCCESSFUL; // Hit player claim, do not claim it
} else {
return FloodClaimResult.HIT_NONPLAYER_CLAIM; // Hit player claim, do not claim it
}
}
collector.add(claimingPosition);
FloodClaimResult result =
fillClaimInto(world, x - 1, z, --recursions, maxSize, player, collector);
if (result != FloodClaimResult.SUCCESSFULL) {
if (result != FloodClaimResult.SUCCESSFUL) {
return result;
}
result = fillClaimInto(world, x + 1, z, recursions, maxSize, player, collector);
if (result != FloodClaimResult.SUCCESSFULL) {
if (result != FloodClaimResult.SUCCESSFUL) {
return result;
}
result = fillClaimInto(world, x, z - 1, recursions, maxSize, player, collector);
if (result != FloodClaimResult.SUCCESSFULL) {
if (result != FloodClaimResult.SUCCESSFUL) {
return result;
}
return fillClaimInto(world, x, z + 1, recursions, maxSize, player, collector);
Expand Down Expand Up @@ -418,6 +418,7 @@ public boolean isOwner(World world, int x, int z, Player ply) {
* @param ply The UUID of the player.
* @return Whether this player owns this chunk.
*/
@SuppressWarnings("unused")
public boolean isOwner(Chunk chunk, UUID ply) {
return isOwner(chunk.getWorld(), chunk.getX(), chunk.getZ(), ply);
}
Expand Down Expand Up @@ -470,22 +471,24 @@ public UUID getOwner(ChunkPos pos) {
/**
* Toggles whether TNT is enabled in the provided chunk.
*
* @param chunk The Spigot chunk position.
* @param ignoredChunk The Spigot chunk position.
* @return Whether TNT is now (after the toggle) enabled in this chunk.
* @deprecated DOES NOTHING!
*/
public boolean toggleTnt(Chunk chunk) {
return dataHandler.toggleTnt(new ChunkPos(chunk));
@Deprecated
public boolean toggleTnt(Chunk ignoredChunk) {
return false;
}

/**
* Checks whether TNT is enabled in the provided chunk.
*
* @deprecated Must make use of new
* @param chunk The Spigot chunk position.
* @param ignoredChunk The Spigot chunk position.
* @return Whether TNT is currently enabled in this chunk.
* @deprecated DOES NOTHING!
*/
@Deprecated
public boolean isTntEnabled(Chunk chunk) {
return dataHandler.isTntEnabled(new ChunkPos(chunk));
public boolean isTntEnabled(Chunk ignoredChunk) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ public DataChunk[] getClaimedChunks() {
return dataHandler.getClaimedChunks();
}

@Override
public boolean toggleTnt(ChunkPos pos) {
return dataHandler.toggleTnt(pos);
}

@Override
public boolean isTntEnabled(ChunkPos pos) {
return dataHandler.isTntEnabled(pos);
}

@Override
public void addPlayer(
UUID player,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public interface IClaimChunkDataHandler {
UUID getChunkOwner(ChunkPos pos);

/**
* Retrives all claimed chunks and their owners across all worlds.
* Retrieves all claimed chunks and their owners across all worlds.
*
* @return An array of all claimed chunks
* @since 0.0.13
Expand All @@ -123,25 +123,29 @@ public interface IClaimChunkDataHandler {
/**
* Toggles whether TNT can explode in the given chunk.
*
* @param pos The position of the chunk
* @param ignoredPos The position of the chunk
* @return Whether TNT is now enabled in the provided chunk
* @since 0.0.16
* @deprecated Unused.
*/
@Deprecated
boolean toggleTnt(ChunkPos pos);
default boolean toggleTnt(ChunkPos ignoredPos) {
return false;
}

/**
* Retrieves whether TNT can explode in the given chunk (regardless of whether TNT is disabled
* in the config).
*
* @param pos The position of the chunk
* @param ignoredPos The position of the chunk
* @return Whether TNT is enabled in the provided chunk
* @since 0.0.16
* @deprecated Unused.
*/
@Deprecated
boolean isTntEnabled(ChunkPos pos);
default boolean isTntEnabled(ChunkPos ignoredPos) {
return false;
}

// -- PLAYERS -- //

Expand All @@ -151,7 +155,7 @@ public interface IClaimChunkDataHandler {
* @param player The UUID of the player
* @param lastIgn The in-game name of the player
* @param chunkName The display name for this player's chunks
* @param lastOnlineTime The last time (in ms since January 1, 1970 UTC) that the player was
* @param lastOnlineTime The last time (in ms since January 1, 1970, UTC) that the player was
* online
* @param alerts Whether to send this player alerts when someone enters their chunks
* @since 0.0.24
Expand Down Expand Up @@ -221,7 +225,7 @@ default void addPlayer(UUID player, String lastIgn, boolean alerts, int defaultM
UUID getPlayerUUID(String username);

/**
* Set the last time (in ms since January 1, 1970 UTC) that the player was online.
* Set the last time (in ms since January 1, 1970, UTC) that the player was online.
*
* @param player The player whose time should be updated
* @param time The new time since the player was last online
Expand All @@ -233,7 +237,7 @@ default void addPlayer(UUID player, String lastIgn, boolean alerts, int defaultM
* Sets the given player's chunk's display name.
*
* @param player The player whose chunks should have the new display name
* @param name The new display name for this players chunks or {@code null} to clear it
* @param name The new display name for this player's chunks or {@code null} to clear it
* @since 0.0.13
*/
void setPlayerChunkName(UUID player, @Nullable String name);
Expand All @@ -242,7 +246,7 @@ default void addPlayer(UUID player, String lastIgn, boolean alerts, int defaultM
* Retrieves the given player's chunk's display name.
*
* @param player The player whose chunks' name to check
* @return The new display name for this players chunks or {@code null} if the player has not
* @return The new display name for this player's chunks or {@code null} if the player has not
* joined or the chunks are unnamed
* @since 0.0.13
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ public void load() throws Exception {
}
}

public void deleteFiles() {
if (claimedChunksFile != null && !claimedChunksFile.delete())
Utils.err("Failed to delete claimed chunks file");
if (joinedPlayersFile != null && !joinedPlayersFile.delete())
Utils.err("Failed to delete joined players file");
}

void clearData() {
claimedChunks.clear();
joinedPlayers.clear();
Expand Down Expand Up @@ -150,18 +143,6 @@ public DataChunk[] getClaimedChunks() {
.toArray(DataChunk[]::new);
}

@Override
public boolean toggleTnt(ChunkPos pos) {
DataChunk chunk = claimedChunks.get(pos);
if (chunk == null) return false;
return (chunk.tnt = !chunk.tnt);
}

@Override
public boolean isTntEnabled(ChunkPos pos) {
return claimedChunks.containsKey(pos) && claimedChunks.get(pos).tnt;
}

@Override
public void addPlayer(
UUID player,
Expand Down Expand Up @@ -446,7 +427,9 @@ private void loadPre0024Data() throws Exception {

// Backup existing files
doBackup(joinedPlayersFile);
doBackup(claimedChunksFile);
if (claimedChunksFile != null && claimedChunksFile.exists()) {
doBackup(claimedChunksFile);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,57 +308,6 @@ public DataChunk[] getClaimedChunks() {
return chunks.toArray(new DataChunk[0]);
}

@Override
public boolean toggleTnt(ChunkPos pos) {
boolean current = isTntEnabled(pos);
String sql =
String.format(
"UPDATE `%s` SET `%s`=? WHERE (`%s`=?) AND (`%s`=?) AND (`%s`=?)",
CLAIMED_CHUNKS_TABLE_NAME,
CLAIMED_CHUNKS_TNT,
CLAIMED_CHUNKS_WORLD,
CLAIMED_CHUNKS_X,
CLAIMED_CHUNKS_Z);
try (PreparedStatement statement = prep(claimChunk, connection, sql)) {
statement.setBoolean(1, !current);
statement.setString(2, pos.world());
statement.setInt(3, pos.x());
statement.setInt(4, pos.z());
statement.execute();
return !current;
} catch (Exception e) {
Utils.err("Failed to update tnt enabled in chunk: %s", e.getMessage());
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
return current;
}

@Override
public boolean isTntEnabled(ChunkPos pos) {
String sql =
String.format(
"SELECT `%s` FROM `%s` WHERE (`%s`=?) AND (`%s`=?) AND (`%s`=?)",
CLAIMED_CHUNKS_TNT,
CLAIMED_CHUNKS_TABLE_NAME,
CLAIMED_CHUNKS_WORLD,
CLAIMED_CHUNKS_X,
CLAIMED_CHUNKS_Z);
try (PreparedStatement statement = prep(claimChunk, connection, sql)) {
statement.setString(1, pos.world());
statement.setInt(2, pos.x());
statement.setInt(3, pos.z());
try (ResultSet result = statement.executeQuery()) {
if (result.next()) return result.getBoolean(1);
}
} catch (Exception e) {
Utils.err("Failed to retrieve tnt enabled in chunk: %s", e.getMessage());
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
return false;
}

@Override
public void addPlayer(
UUID player,
Expand Down
Loading

0 comments on commit 2b78d73

Please sign in to comment.