Skip to content

Commit

Permalink
Clean up and make sure player online time is saved at login and logoff
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 15, 2024
1 parent 8eb8392 commit fcafaf5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 205 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public class JournaledDataHandler implements IClaimChunkDataHandler {
private HashMap<UUID, FullPlayerData> joinedPlayers;
private SqLiteWrapper sqLiteWrapper;

// Don't need this shit actually
// private HashMap<RegionPos, ClaimRegion> claimRegions;

public JournaledDataHandler(@NotNull File claimChunkDb) {
this.claimChunkDb = claimChunkDb;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,35 @@
import java.sql.*;
import java.util.UUID;

public record SqLiteWrapper(File dbFile) {
public class SqLiteWrapper {

public SqLiteWrapper(@NotNull File dbFile) {
public final File dbFile;
private Connection liveConnection;

public SqLiteWrapper(@NotNull File dbFile) throws RuntimeException {
this.dbFile = dbFile;

try {
TableMigrationManager.go(this::connectionOrDie);
// Make sure the SQLite driver exists and get it in the classpath
// for the DriverManager to search.
Class.forName("org.sqlite.JDBC");

// Initialize the tables and perform any changes to them
TableMigrationManager.go(this::connectionOrException);
} catch (ClassNotFoundException e) {
throw new RuntimeException(
"Cannot find SQLite JDBC class? Not sure how this can happen. Please submit an"
+ " issue on GitHub",
e);
} catch (SQLException e) {
throw new RuntimeException("Failed to initialize tables!", e);
throw new RuntimeException("Failed to initialize tables! This is fatal!", e);
}
}

// -- DATABASE INTEGRATIONS! -- //

public void addClaimedChunk(DataChunk chunk) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
// Use the nested select query to get the user's row ID as the
// owner's id
try (PreparedStatement statement =
Expand Down Expand Up @@ -55,7 +68,7 @@ INSERT INTO chunk_data (
}

public void removeClaimedChunk(ChunkPos chunk) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
// Get chunk ID
final int chunkId;
try (PreparedStatement statement =
Expand Down Expand Up @@ -99,7 +112,7 @@ public void removeClaimedChunk(ChunkPos chunk) {

// TODO: TEST
public void addPlayer(FullPlayerData playerData) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
try (PreparedStatement statement =
connection.prepareStatement(
"""
Expand All @@ -126,7 +139,7 @@ INSERT INTO player_data (
}

public void setPlayerLastOnline(UUID player, long time) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
// Use the nested select query to get the user's row ID as the
// owner's id
try (PreparedStatement statement =
Expand All @@ -146,7 +159,7 @@ public void setPlayerLastOnline(UUID player, long time) {
}

public void setPlayerChunkName(UUID player, String chunkName) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
// Use the nested select query to get the user's row ID as the
// owner's id
try (PreparedStatement statement =
Expand All @@ -166,7 +179,7 @@ public void setPlayerChunkName(UUID player, String chunkName) {
}

public void setPlayerReceiveAlerts(UUID player, boolean receiveAlerts) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
// Use the nested select query to get the user's row ID as the
// owner's id
try (PreparedStatement statement =
Expand All @@ -186,7 +199,7 @@ public void setPlayerReceiveAlerts(UUID player, boolean receiveAlerts) {
}

public void setPlayerExtraMaxClaims(UUID player, int extraMaxClaims) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
// Use the nested select query to get the user's row ID as the
// owner's id
try (PreparedStatement statement =
Expand All @@ -206,7 +219,7 @@ public void setPlayerExtraMaxClaims(UUID player, int extraMaxClaims) {
}

public void addPlayerAccess(ChunkPos chunk, UUID accessor, int permissionFlags) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
try (PreparedStatement statement =
connection.prepareStatement(
"""
Expand Down Expand Up @@ -241,7 +254,7 @@ INSERT INTO chunk_permissions (
}

public void updatePlayerAccess(ChunkPos chunk, UUID accessor, int permissionFlags) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
try (PreparedStatement statement =
connection.prepareStatement(
"""
Expand Down Expand Up @@ -273,7 +286,7 @@ public void updatePlayerAccess(ChunkPos chunk, UUID accessor, int permissionFlag
}

public void removePlayerAccess(ChunkPos chunk, UUID accessor) {
try (Connection connection = ensureConnection()) {
try (Connection connection = connectionOrException()) {
try (PreparedStatement statement =
connection.prepareStatement(
"""
Expand Down Expand Up @@ -304,29 +317,23 @@ public void removePlayerAccess(ChunkPos chunk, UUID accessor) {

// -- Connection stuff -- //

public @NotNull Connection ensureConnection() throws RuntimeException, SQLException {
try {
public @NotNull Connection connection() throws SQLException, IOException {
if (liveConnection == null || liveConnection.isClosed()) {
if (!dbFile.exists() && dbFile.createNewFile()) {
Utils.warn("Created empty database file");
}

Class.forName("org.sqlite.JDBC");
return DriverManager.getConnection("jdbc:sqlite:" + dbFile);
} catch (IOException e) {
throw new RuntimeException("Failed to create new file " + dbFile, e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(
"Cannot find SQLite JDBC class? Not sure how this can happen. Please submit an"
+ " issue on GitHub",
e);
liveConnection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);
}
return liveConnection;
}

public @NotNull Connection connectionOrDie() {
public @NotNull Connection connectionOrException() throws RuntimeException {
try {
return ensureConnection();
return connection();
} catch (SQLException e) {
throw new RuntimeException("SQL Exception", e);
throw new RuntimeException("SQLException on connection creation", e);
} catch (IOException e) {
throw new RuntimeException("Failed to create new file " + dbFile, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public void onPlayerJoin(PlayerJoinEvent e) {
public void onPlayerLeave(PlayerQuitEvent e) {
claimChunk.getAdminOverrideHandler().remove(e.getPlayer().getUniqueId());
AutoClaimHandler.disable(e.getPlayer());
claimChunk.getPlayerHandler().setLastJoinedTime(e.getPlayer().getUniqueId(), System.currentTimeMillis());
claimChunk
.getPlayerHandler()
.setLastJoinedTime(e.getPlayer().getUniqueId(), System.currentTimeMillis());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ public int getMaxClaims(UUID player) {
}

public void onJoin(Player ply) {
if (!dataHandler.hasPlayer(ply.getUniqueId())) {
UUID uuid = ply.getUniqueId();
if (dataHandler.hasPlayer(uuid)) {
dataHandler.setPlayerLastOnline(uuid, System.currentTimeMillis());
} else {
dataHandler.addPlayer(
ply.getUniqueId(),
uuid,
ply.getName(),
claimChunk.getConfigHandler().getDefaultSendAlertsToOwner(),
0);
Expand Down

0 comments on commit fcafaf5

Please sign in to comment.