Skip to content

Commit

Permalink
Organize and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 23, 2024
1 parent 965c57b commit afd1958
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 167 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object DepData {
const val JAVAX_PERSISTENCE_VERSION = "2.1.0"
const val JAVAX_TRANSACTION_VERSION = "1.1"
const val SANS_ORM_VERSION = "3.17"
const val SLF4J_VERSION = "1.7.25"

// Directories
const val TEST_SERVER_DIR = "run"
Expand Down Expand Up @@ -301,6 +302,7 @@ dependencies {
implementation("javax.transaction:transaction-api:${DepData.JAVAX_TRANSACTION_VERSION}")
implementation("com.github.h-thurow:q2o:${DepData.SANS_ORM_VERSION}")

testImplementation("org.slf4j:slf4j-simple:${DepData.SLF4J_VERSION}")
testImplementation("org.junit.jupiter:junit-jupiter:${DepData.JUNIT_VERSION}")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:${DepData.JUNIT_LAUNCHER_VERSION}")
}
Expand Down
2 changes: 1 addition & 1 deletion 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.conversion.IDataConverter;
import com.cjburkey.claimchunk.data.IDataConverter;
import com.cjburkey.claimchunk.data.newdata.*;
import com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler;
import com.cjburkey.claimchunk.event.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cjburkey.claimchunk.data.conversion;
package com.cjburkey.claimchunk.data;

import com.cjburkey.claimchunk.data.newdata.IClaimChunkDataHandler;

Expand Down

This file was deleted.

This file was deleted.

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.conversion.IDataConverter;
import com.cjburkey.claimchunk.data.IDataConverter;
import com.cjburkey.claimchunk.player.FullPlayerData;
import com.cjburkey.claimchunk.player.SimplePlayerData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void givePlayerAccess(
DataChunk chunkData = claimedChunks.get(chunk);
if (chunkData != null) {
chunkData.playerPermissions.put(accessor, permissions);
sqLiteWrapper.updateOrInsertPlayerAccess(chunk, accessor, permissions.permissionFlags);
sqLiteWrapper.setPlayerAccess(chunk, accessor, permissions.permissionFlags);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ FOREIGN KEY(owner_uuid) REFERENCES player_data(player_uuid)
Q2Sql.executeUpdate(
"""
CREATE TABLE IF NOT EXISTS chunk_permissions (
perm_id INTEGER PRIMARY KEY,
chunk_id INTEGER NOT NULL,
other_player_uuid TEXT NOT NULL,
permission_bits INTEGER NOT NULL,
PRIMARY KEY(chunk_id, other_player_uuid)
FOREIGN KEY(chunk_id) REFERENCES chunk_data(chunk_id),
FOREIGN KEY(other_player_uuid) REFERENCES player_data(player_uuid)
) STRICT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,14 @@ INSERT INTO chunk_permissions (
""";

// Better way to do this?
String permsValsSql =
chunk.playerPermissions.entrySet().stream()
.map(
ignored ->
replaceChunkIdQuery(
"""
(%%SELECT_CHUNK_ID_SQL%%, ?, ?)
"""))
.collect(Collectors.joining(","));

try (PreparedStatement statement =
connection.prepareStatement(permsInsertPrefixSql + permsValsSql)) {
ArrayList<String> params = new ArrayList<>();
for (int i = 0; i < chunk.playerPermissions.size(); i++) {
params.add("(%%SELECT_CHUNK_ID_SQL%%, ?, ?)");
}
String finalSql =
chunkIdQuery(permsInsertPrefixSql + String.join(",", params));

try (PreparedStatement statement = connection.prepareStatement(finalSql)) {
int currentParam = 1;
for (Map.Entry<UUID, ChunkPlayerPermissions> entry :
chunk.playerPermissions.entrySet()) {
Expand All @@ -129,7 +125,7 @@ public void removeClaimedChunk(ChunkPos chunk) {
// Remove all granted permissions for the chunk
try (PreparedStatement statement =
connection.prepareStatement(
replaceChunkIdQuery(
chunkIdQuery(
"""
DELETE FROM chunk_permissions
WHERE chunk_id=%%SELECT_CHUNK_ID_SQL%%
Expand Down Expand Up @@ -254,60 +250,28 @@ public void setPlayerExtraMaxClaims(UUID player, int extraMaxClaims) {
});
}

public void updateOrInsertPlayerAccess(ChunkPos chunk, UUID accessor, int permissionFlags) {
// Check if the access already exists
public void setPlayerAccess(ChunkPos chunk, UUID accessor, int permissionFlags) {
SqlClosure.sqlExecute(
connection -> {
final boolean accessExists;
try (PreparedStatement statement =
connection.prepareStatement(
replaceChunkIdQuery(
chunkIdQuery(
"""
SELECT COUNT(*) FROM chunk_permissions
WHERE other_player_uuid=? AND chunk_id=%%SELECT_CHUNK_ID_SQL%%
"""))) {
statement.setString(1, accessor.toString());
setChunkPosParams(statement, 2, chunk);
ResultSet resultSet = statement.executeQuery();
accessExists = resultSet.next() && resultSet.getInt(1) > 0;
}

// If the entry already exists, update the permission bits
if (accessExists) {
try (PreparedStatement statement =
connection.prepareStatement(
replaceChunkIdQuery(
"""
UPDATE chunk_permissions
SET permission_bits=?
WHERE chunk_id=%%SELECT_CHUNK_ID_SQL%%
AND other_player_uuid=?
"""))) {
statement.setInt(1, permissionFlags);
int next = setChunkPosParams(statement, 2, chunk);
statement.setString(next, accessor.toString());
statement.execute();
}
} else {
try (PreparedStatement statement =
connection.prepareStatement(
replaceChunkIdQuery(
"""
INSERT INTO chunk_permissions (
chunk_id,
other_player_uuid,
permission_bits
) VALUES (
%%SELECT_CHUNK_ID_SQL%%, ?, ?
)
"""))) {
int next = setChunkPosParams(statement, 1, chunk);
statement.setString(next, accessor.toString());
statement.setInt(next + 1, permissionFlags);
statement.execute();
}
INSERT INTO chunk_permissions (
chunk_id,
other_player_uuid,
permission_bits
) VALUES (
%%SELECT_CHUNK_ID_SQL%%, ?, ?
)
ON CONFLICT(chunk_id, other_player_uuid) DO
UPDATE SET permission_bits=excluded.permission_bits
"""))) {
int next = setChunkPosParams(statement, 1, chunk);
statement.setString(next, accessor.toString());
statement.setInt(next + 1, permissionFlags);
statement.execute();
}

return null;
});
}
Expand All @@ -317,14 +281,15 @@ public void removePlayerAccess(ChunkPos chunk, UUID accessor) {
connection -> {
try (PreparedStatement statement =
connection.prepareStatement(
replaceChunkIdQuery(
chunkIdQuery(
"""
DELETE FROM chunk_permissions
WHERE chunk_id=%%SELECT_CHUNK_ID_SQL%%,
WHERE chunk_id=%%SELECT_CHUNK_ID_SQL%%
AND other_player_uuid=?
"""))) {
int next = setChunkPosParams(statement, 1, chunk);
statement.setString(next, accessor.toString());
statement.execute();
return null;
}
});
Expand Down Expand Up @@ -408,7 +373,7 @@ private int setChunkPosParams(
return worldParameterNum + 3;
}

private String replaceChunkIdQuery(String sql) {
private String chunkIdQuery(String sql) {
return SELECT_CHUNK_ID_SQL_PATTERN.matcher(sql).replaceAll(SELECT_CHUNK_ID_SQL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
@Table(name = "chunk_data")
public class SqlDataChunk {

@Id
@Column(name = "chunk_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int chunkId;

@Column(name = "chunk_world")
public String world;

Expand Down
Loading

0 comments on commit afd1958

Please sign in to comment.