Skip to content

Commit

Permalink
Add default chunk permissions column
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 30, 2024
1 parent 627ffc4 commit 05c3c91
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ private void handleAutoUnclaim() {

for (SimplePlayerData player : playerHandler.getJoinedPlayers()) {
// If the player has joined since time was recorded (that's 1s)
boolean playerJoinedSinceTimeRecordUpdate = player.lastOnlineTime > 1000;
boolean playerJoinedSinceTimeRecordUpdate = player.lastOnlineTime() > 1000;
// If the player hasn't been online recently enough
boolean playerBeenOfflineTooLong = player.lastOnlineTime < (time - (1000L * length));
boolean playerBeenOfflineTooLong = player.lastOnlineTime() < (time - (1000L * length));

if (playerJoinedSinceTimeRecordUpdate && playerBeenOfflineTooLong) {
// Get a list of all the player's chunks
ChunkPos[] claimedChunks = chunkHandler.getClaimedChunks(player.player);
ChunkPos[] claimedChunks = chunkHandler.getClaimedChunks(player.player());

if (claimedChunks.length > 0) {
// Unclaim all of the player's chunks
Expand All @@ -493,7 +493,7 @@ private void handleAutoUnclaim() {

Utils.log(
"Unclaimed all chunks of player \"%s\" (%s)",
player.lastIgn, player.player);
player.lastIgn(), player.player());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void addPlayer(
joinedPlayers.put(
player,
new FullPlayerData(
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims));
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims, new ChunkPlayerPermissions(0)));
}

@Override
Expand Down Expand Up @@ -410,7 +410,7 @@ private void loadPre0024Data() throws Exception {
player.chunkName,
player.lastOnlineTime,
player.alert,
0));
0, new ChunkPlayerPermissions()));
// Grant default permissions on all this player's chunks to all players in
// "permitted"
for (DataChunk chunk :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ public FullPlayerData[] getFullPlayerData() {
result.getString(3),
result.getLong(4),
result.getBoolean(5),
result.getInt(6)));
result.getInt(6), new ChunkPlayerPermissions(0)));
}
} catch (Exception e) {
Utils.err("Failed to retrieve all players data: %s", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void addPlayer(
int extraMaxClaims) {
addPlayer(
new FullPlayerData(
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims));
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims, new ChunkPlayerPermissions(0)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static void go() {
tryCreateTables();

// Call migration check methods here.
// Migration method naming scheme:
// migrate_{{MAJOR}}_{{MINOR}}_{{PATCH}}_{{DISCRIMINATOR}}
migrate_0_0_25_1();
}

private static void tryCreateTables() {
Expand All @@ -26,7 +29,8 @@ CREATE TABLE IF NOT EXISTS player_data (
chunk_name TEXT,
last_online_time INTEGER NOT NULL,
alerts_enabled INTEGER NOT NULL,
extra_max_claims INTEGER NOT NULL
extra_max_claims INTEGER NOT NULL,
default_chunk_permissions INTEGER NOT NULL
) STRICT
""");

Expand Down Expand Up @@ -59,6 +63,15 @@ FOREIGN KEY(other_player_uuid) REFERENCES player_data(player_uuid)
""");
}

private static void migrate_0_0_25_1() {
if (!columnExists("player_data", "default_chunk_permissions")) {
Q2Sql.executeUpdate("""
ALTER TABLE player_data
ADD default_chunk_permissions INTEGER NOT NULL DEFAULT 0
""");
}
}

// Use this method to determine if a column exists in a table to perform migrations
@SuppressWarnings("unused")
public static boolean columnExists(String tableName, String columnName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ INSERT OR REPLACE INTO player_data (
chunk_name,
last_online_time,
alerts_enabled,
extra_max_claims
extra_max_claims,
default_chunk_permissions
) VALUES (
?, ?, ?, ?, ?, ?
?, ?, ?, ?, ?, ?, ?
)
""")) {
statement.setString(1, playerData.player.toString());
Expand All @@ -195,6 +196,7 @@ INSERT OR REPLACE INTO player_data (
statement.setLong(4, playerData.lastOnlineTime);
statement.setBoolean(5, playerData.alert);
statement.setInt(6, playerData.extraMaxClaims);
statement.setInt(7, playerData.defaultChunkPermissions.permissionFlags);
statement.execute();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ public class SqlDataPlayer {

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

@Column(name = "default_chunk_permissions")
public int defaultChunkPermissions;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cjburkey.claimchunk.player;

import com.cjburkey.claimchunk.chunk.ChunkPlayerPermissions;
import com.cjburkey.claimchunk.data.sqlite.SqlDataPlayer;

import java.util.UUID;
Expand All @@ -12,20 +13,22 @@ public class FullPlayerData implements Cloneable {
public long lastOnlineTime;
public boolean alert;
public int extraMaxClaims;
public ChunkPlayerPermissions defaultChunkPermissions;

public FullPlayerData(
UUID player,
String lastIgn,
String chunkName,
long lastOnlineTime,
boolean alert,
int extraMaxClaims) {
int extraMaxClaims, ChunkPlayerPermissions defaultChunkPermissions) {
this.player = player;
this.lastIgn = lastIgn;
this.chunkName = chunkName;
this.lastOnlineTime = lastOnlineTime;
this.alert = alert;
this.extraMaxClaims = extraMaxClaims;
this.defaultChunkPermissions = defaultChunkPermissions;
}

public FullPlayerData(SqlDataPlayer player) {
Expand All @@ -35,7 +38,7 @@ public FullPlayerData(SqlDataPlayer player) {
player.chunkName,
player.lastOnlineTime,
player.alert,
player.extraMaxClaims);
player.extraMaxClaims, new ChunkPlayerPermissions(player.defaultChunkPermissions));
}

private FullPlayerData(FullPlayerData clone) {
Expand All @@ -45,7 +48,7 @@ private FullPlayerData(FullPlayerData clone) {
clone.chunkName,
clone.lastOnlineTime,
clone.alert,
clone.extraMaxClaims);
clone.extraMaxClaims, clone.defaultChunkPermissions);
}

public SimplePlayerData toSimplePlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public Collection<SimplePlayerData> getJoinedPlayers() {
public List<String> getJoinedPlayersFromName(String start) {
List<String> out = new ArrayList<>();
for (SimplePlayerData ply : dataHandler.getPlayers()) {
if (ply.lastIgn != null && ply.lastIgn.toLowerCase().startsWith(start.toLowerCase())) {
out.add(ply.lastIgn);
if (ply.lastIgn() != null && ply.lastIgn().toLowerCase().startsWith(start.toLowerCase())) {
out.add(ply.lastIgn());
}
}
return out;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
package com.cjburkey.claimchunk.player;

import java.util.Objects;
import java.util.UUID;

public final class SimplePlayerData {

public final UUID player;
public final String lastIgn;
public final long lastOnlineTime;

public SimplePlayerData(UUID player, String lastIgn, long lastOnlineTime) {
this.player = player;
this.lastIgn = lastIgn;
this.lastOnlineTime = lastOnlineTime;
}
public record SimplePlayerData(UUID player, String lastIgn, long lastOnlineTime) {

@Override
public boolean equals(Object o) {
Expand All @@ -25,8 +14,4 @@ public boolean equals(Object o) {
&& lastIgn.equals(that.lastIgn);
}

@Override
public int hashCode() {
return Objects.hash(player, lastIgn, lastOnlineTime);
}
}
49 changes: 42 additions & 7 deletions src/test/java/com/cjburkey/claimchunk/TestSQLPlease.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
import com.cjburkey.claimchunk.data.sqlite.SqLiteWrapper;
import com.cjburkey.claimchunk.player.FullPlayerData;

import com.zaxxer.q2o.Q2Sql;
import com.zaxxer.q2o.q2o;
import org.junit.jupiter.api.Test;
import org.sqlite.SQLiteDataSource;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,6 +37,37 @@ void ensureColumnExistsMethodWorks() {
}
}

@Test
void ensureMigrationWorks() throws IOException {
File dbFile = randomDbFile();

{
SQLiteDataSource dataSource = new SQLiteDataSource();
boolean ignored = dbFile.createNewFile();
dataSource.setUrl("jdbc:sqlite:" + dbFile);
q2o.initializeTxNone(dataSource);
dbFile.deleteOnExit();
// Old table format
Q2Sql.executeUpdate(
"""
CREATE TABLE IF NOT EXISTS player_data (
player_uuid TEXT PRIMARY KEY NOT NULL,
last_ign TEXT NOT NULL,
chunk_name TEXT,
last_online_time INTEGER NOT NULL,
alerts_enabled INTEGER NOT NULL,
extra_max_claims INTEGER NOT NULL
) STRICT
""");
q2o.deinitialize();
}

try (TestQlWrap ignoredWrapper = new TestQlWrap()) {
// Make sure the migration code added the column to the existing table
assert SqLiteTableMigrationManager.columnExists("player_data", "default_chunk_permissions");
}
}

@Test
void ensureNoDataLoss() {
try (TestQlWrap wrapper = new TestQlWrap()) {
Expand All @@ -41,15 +76,15 @@ void ensureNoDataLoss() {
UUID ply2Uuid = UUID.randomUUID();
wrapper.sql.addPlayer(
new FullPlayerData(
ply1Uuid, "SomeGuysName", null, System.currentTimeMillis(), true, 0));
ply1Uuid, "SomeGuysName", null, System.currentTimeMillis(), true, 0, new ChunkPlayerPermissions(0)));
wrapper.sql.addPlayer(
new FullPlayerData(
ply2Uuid,
"OtherPersonsName",
"queenshit",
System.currentTimeMillis(),
false,
0));
0, new ChunkPlayerPermissions(0)));

// Make fake accessors and permissions
UUID accessorUuid1 = UUID.randomUUID();
Expand Down Expand Up @@ -98,13 +133,13 @@ void multiplePermissions() {
// Add the players
wrapper.sql.addPlayer(
new FullPlayerData(
owner, "PersonHere", null, System.currentTimeMillis(), true, 0));
owner, "PersonHere", null, System.currentTimeMillis(), true, 0, new ChunkPlayerPermissions(0)));
wrapper.sql.addPlayer(
new FullPlayerData(
accessor1, "PersonThere", null, System.currentTimeMillis(), true, 0));
accessor1, "PersonThere", null, System.currentTimeMillis(), true, 0, new ChunkPlayerPermissions(0)));
wrapper.sql.addPlayer(
new FullPlayerData(
accessor2, "AnotherOne", null, System.currentTimeMillis(), true, 0));
accessor2, "AnotherOne", null, System.currentTimeMillis(), true, 0, new ChunkPlayerPermissions(0)));

// Add the chunk
wrapper.sql.addClaimedChunk(chunkData);
Expand All @@ -129,10 +164,10 @@ void insertOrUpdatePermission() {
// Add the players and the chunk
wrapper.sql.addPlayer(
new FullPlayerData(
owner, "PersonHere", null, System.currentTimeMillis(), true, 0));
owner, "PersonHere", null, System.currentTimeMillis(), true, 0, new ChunkPlayerPermissions(0)));
wrapper.sql.addPlayer(
new FullPlayerData(
accessor, "PersonThere", null, System.currentTimeMillis(), true, 0));
accessor, "PersonThere", null, System.currentTimeMillis(), true, 0, new ChunkPlayerPermissions(0)));
wrapper.sql.addClaimedChunk(new DataChunk(chunk, owner, new HashMap<>(), false));

// Insert the permission and check it
Expand Down

0 comments on commit 05c3c91

Please sign in to comment.