Skip to content

Commit

Permalink
Get flag data saving, loading does NOT work
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Aug 22, 2024
1 parent 446fda4 commit c694350
Show file tree
Hide file tree
Showing 18 changed files with 439 additions and 201 deletions.
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 @@ -11,6 +11,7 @@
import com.cjburkey.claimchunk.event.*;
import com.cjburkey.claimchunk.flag.CCInteractClasses;
import com.cjburkey.claimchunk.flag.CCPermFlags;
import com.cjburkey.claimchunk.flag.FlagHandler;
import com.cjburkey.claimchunk.gui.CCGuiHandler;
import com.cjburkey.claimchunk.i18n.V2JsonMessages;
import com.cjburkey.claimchunk.layer.PlaceholderInitLayer;
Expand All @@ -19,7 +20,6 @@
import com.cjburkey.claimchunk.rank.RankHandler;
import com.cjburkey.claimchunk.service.prereq.claim.*;
import com.cjburkey.claimchunk.smartcommand.CCBukkitCommand;
import com.cjburkey.claimchunk.smartcommand.sub.ply.flags.FlagHandler;
import com.cjburkey.claimchunk.transition.FromPre0023;
import com.cjburkey.claimchunk.update.*;
import com.cjburkey.claimchunk.worldguard.WorldGuardHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void load() throws Exception {
for (FullPlayerData player : sqLiteWrapper.getAllPlayers()) {
joinedPlayers.putIfAbsent(player.player, player);
}
for (DataChunk chunk : SqLiteWrapper.getAllChunks()) {
for (DataChunk chunk : sqLiteWrapper.getAllChunks()) {
claimedChunks.putIfAbsent(chunk.chunk(), chunk);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ REFERENCES player_data(player_uuid)
Q2Sql.executeUpdate(
"""
CREATE TABLE IF NOT EXISTS permission_flags (
rowid INTEGER PRIMARY KEY,
player_uuid TEXT NOT NULL,
other_player_uuid TEXT,
chunk_id INTEGER,
other_player_uuid TEXT NOT NULL,
chunk_id INTEGER NOT NULL,
flag_name TEXT NOT NULL,
allow_deny INTEGER NOT NULL,
PRIMARY KEY(player_uuid, other_player_uuid, chunk_id, flag_name)
FOREIGN KEY(player_uuid)
REFERENCES player_data(player_uuid)
ON DELETE CASCADE,
Expand All @@ -69,7 +70,6 @@ REFERENCES chunk_data(chunk_id)
""");
}

@SuppressWarnings("unused")
public static boolean tableExists(String tableName) {
return SqlClosure.sqlExecute(
connection -> {
Expand Down
187 changes: 93 additions & 94 deletions src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,53 @@ public void setPermissionFlags(
@NotNull UUID owner,
@Nullable UUID accessor,
@Nullable ChunkPos chunk,
@NotNull HashMap<String, Boolean> newFlags) {}
@NotNull HashMap<String, Boolean> newFlags) {
String valueList =
newFlags.values().stream()
.map(
ignored ->
"(?,?,"
+ (chunk != null ? SELECT_CHUNK_ID_SQL : "-1")
+ ",?,?)")
.collect(Collectors.joining(","));
String sql =
"""
INSERT INTO permission_flags (
player_uuid,
other_player_uuid,
chunk_id,
flag_name,
allow_deny
) VALUES \s"""
+ valueList
+ """
ON CONFLICT (
player_uuid,
other_player_uuid,
chunk_id,
flag_name
) DO UPDATE SET allow_deny=excluded.allow_deny
""";

SqlClosure.sqlExecute(
connection -> {
try (PreparedStatement statement = connection.prepareStatement(sql)) {
int param = 1;
for (Map.Entry<String, Boolean> entry : newFlags.entrySet()) {
statement.setString(param++, owner.toString());
statement.setString(
param++, accessor == null ? "" : accessor.toString());
if (chunk != null) {
param = setChunkPosParams(statement, param, chunk);
}
statement.setString(param++, entry.getKey());
statement.setBoolean(param++, entry.getValue());
}
statement.execute();
return null;
}
});
}

public void clearPermissionFlags(
@NotNull UUID owner,
Expand All @@ -259,88 +305,33 @@ public void clearPermissionFlags(
Arrays.stream(flagNames)
.map(ignored -> "flag_name=?")
.collect(Collectors.joining(" OR "));
String where =
"WHERE player_uuid="
+ (accessor == null ? "''" : "?")
+ " AND chunk_id="
+ (chunk == null ? "-1" : SELECT_CHUNK_ID_SQL)
+ " AND ("
+ clauses
+ ")";

SqlClosure.sqlExecute(
connection -> {
if (accessor != null && chunk != null) {
try (PreparedStatement statement =
connection.prepareStatement(
chunkIdQuery(
"""
DELETE FROM permission_flags
WHERE player_uuid=?
AND chunk_id=%%SELECT_CHUNK_ID_SQL%%
AND (
"""
+ clauses
+ ")"))) {
int param = 1;
statement.setString(param++, owner.toString());
param = setChunkPosParams(statement, param, chunk);
for (String flagName : flagNames) {
statement.setString(param++, flagName);
}
statement.execute();
return null;
}
} else if (chunk != null) {
try (PreparedStatement statement =
connection.prepareStatement(
chunkIdQuery(
"""
DELETE FROM permission_flags
WHERE player_uuid=NULL
AND chunk_id=%%SELECT_CHUNK_ID_SQL%%
AND (
"""
+ clauses
+ ")"))) {
int param = setChunkPosParams(statement, 1, chunk);
for (String flagName : flagNames) {
statement.setString(param++, flagName);
}
statement.execute();
return null;
try (PreparedStatement statement =
connection.prepareStatement(
chunkIdQuery("DELETE FROM permission_flags " + where))) {
int param = 1;
statement.setString(param++, owner.toString());
if (accessor != null) {
statement.setString(param, accessor.toString());
}
} else if (accessor != null) {
try (PreparedStatement statement =
connection.prepareStatement(
chunkIdQuery(
"""
DELETE FROM permission_flags
WHERE player_uuid=?
AND chunk_id=NULL
AND (
"""
+ clauses
+ ")"))) {
int param = 1;
statement.setString(param++, owner.toString());
for (String flagName : flagNames) {
statement.setString(param++, flagName);
}
statement.execute();
return null;
if (chunk != null) {
param = setChunkPosParams(statement, param, chunk);
}
} else {
try (PreparedStatement statement =
connection.prepareStatement(
chunkIdQuery(
"""
DELETE FROM permission_flags
WHERE player_uuid=NULL
AND chunk_id=NULL
AND (
"""
+ clauses
+ ")"))) {
int param = 1;
for (String flagName : flagNames) {
statement.setString(param++, flagName);
}
statement.execute();
return null;
for (String flagName : flagNames) {
statement.setString(param++, flagName);
}
statement.execute();
return null;
}
});
}
Expand All @@ -356,15 +347,15 @@ public List<FullPlayerData> getAllPlayers() {
(acc, ply) -> acc.put(ply.player, ply),
HashMap::putAll);

// TODO: LOAD PLAYER PERMISSION FLAGS
SqlClosure.sqlExecute(
connection -> {
try (PreparedStatement statement =
connection.prepareStatement(
"""
SELECT player_uuid, other_player_uuid, flag_name, allow_deny
FROM permission_flags
""")) {
SELECT player_uuid, other_player_uuid, flag_name, allow_deny, chunk_id
FROM permission_flags
WHERE chunk_id=-1
""")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
UUID playerUuid = UUID.fromString(resultSet.getString("player_uuid"));
Expand All @@ -378,8 +369,13 @@ public List<FullPlayerData> getAllPlayers() {
continue;
}

UUID otherPly = null;
try {
otherPly = UUID.fromString(otherPlyUuid);
} catch (Exception ignored) {
}

if (otherPlyUuid != null) {
UUID otherPly = UUID.fromString(otherPlyUuid);
thisPly.playerFlags
.computeIfAbsent(otherPly, ignored -> new HashMap<>())
.put(flagName, allowDeny);
Expand All @@ -394,7 +390,7 @@ public List<FullPlayerData> getAllPlayers() {
return playerData.values().stream().toList();
}

public static Collection<DataChunk> getAllChunks() {
public Collection<DataChunk> getAllChunks() {
return SqlClosure.sqlExecute(
connection -> {
HashMap<ChunkPos, DataChunk> chunks = new HashMap<>();
Expand Down Expand Up @@ -426,8 +422,8 @@ public static Collection<DataChunk> getAllChunks() {
chunk_world, chunk_x, chunk_z,
flag_name, allow_deny
FROM permission_flags
WHERE permission_flags.chunk_id IS NOT NULL
LEFT JOIN chunk_data ON permission_flags.chunk_id=chunk_data.chunk_id
WHERE permission_flags.chunk_id!=-1
""")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Expand All @@ -447,25 +443,28 @@ public static Collection<DataChunk> getAllChunks() {
ignoredChunkOwnerUuid != null && chunkWorld != null
? new ChunkPos(chunkWorld, chunkX, chunkZ)
: null;
// May be null!
String otherPlayerUuid = resultSet.getString("other_player_uuid");
// May be empty!
String otherPlyUuid = resultSet.getString("other_player_uuid");
// Never null
String flagName = resultSet.getString("flag_name");
boolean allowDeny = resultSet.getBoolean("allow_deny");

DataChunk chunk = chunks.get(Objects.requireNonNull(chunkPos));
if (chunk == null) {
Utils.err(
"Tried to load permissions for unclaimed chunk at %s",
chunkPos);
continue;
throw new RuntimeException(
"Tried to load permissions for unclaimed chunk at "
+ chunkPos);
}

UUID otherPlayer = null;
try {
otherPlayer = UUID.fromString(otherPlyUuid);
} catch (Exception ignored) {
}

if (otherPlayerUuid != null) {
if (otherPlayer != null) {
chunk.specificFlags()
.computeIfAbsent(
UUID.fromString(otherPlayerUuid),
ignored -> new HashMap<>())
.computeIfAbsent(otherPlayer, ignored -> new HashMap<>())
.put(flagName, allowDeny);
} else {
chunk.defaultFlags().put(flagName, allowDeny);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import java.util.Set;

/**
* @since 0.0.26
*/
public final class CCFlags {

// Methods named such that they may align with record getters :}
Expand Down Expand Up @@ -44,6 +47,8 @@ public boolean doesProtect(boolean isFlagEnabled) {
}
}

public record ProtectingFlag(String name, CCFlags.FlagData flagData) {}

public record FlagData(
ProtectWhen protectWhen, @NotNull Set<String> include, @NotNull Set<String> exclude) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.*;
import java.util.stream.Collectors;

/**
* @since 0.0.26
*/
public final class CCInteractClasses {

private final HashMap<String, HashSet<Material>> classBlocks = new HashMap<>();
Expand Down
20 changes: 6 additions & 14 deletions src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void load(
* @return The name of the flag that should protect the block, or {@code null} if no flags
* prohibit this action.
*/
public @Nullable String getProtectingFlag(
public @Nullable CCFlags.ProtectingFlag getProtectingFlag(
Material blockType, CCFlags.BlockFlagType interactionType) {
// Loop through each flag
// Maybe separate flags by interaction type to make this lookup cheaper,
Expand All @@ -99,7 +99,7 @@ public void load(
// enabled/disabled state
if (flagApplies(
blockType, this::typeMatches, flagData.include(), flagData.exclude())) {
return flagName;
return new CCFlags.ProtectingFlag(flagName, flagData);
}
}
}
Expand All @@ -115,14 +115,11 @@ public void load(
*
* @param entityType The Bukkit type of the entity to query.
* @param interactionType The type of entity operation to check.
* @param enabledContextFlags A set of all flags enabled for this current context.
* @return The name of the flag that should protect the entity, or {@code null} if no flags
* prohibit this action.
*/
public @Nullable String getProtectingFlag(
EntityType entityType,
CCFlags.EntityFlagType interactionType,
Set<String> enabledContextFlags) {
public @Nullable CCFlags.ProtectingFlag getProtectingFlag(
EntityType entityType, CCFlags.EntityFlagType interactionType) {
// Loop through each flag
for (Map.Entry<String, CCFlags.EntityFlagData> entityControllingFlag :
entityControls.entrySet()) {
Expand All @@ -133,13 +130,8 @@ public void load(
if (flagType == interactionType) {
// Check whether this flag protects the entity
if (flagApplies(
entityType,
this::typeMatches,
flagData.include(),
flagData.exclude())
&& flagData.protectWhen()
.doesProtect(enabledContextFlags.contains(flagName))) {
return flagName;
entityType, this::typeMatches, flagData.include(), flagData.exclude())) {
return new CCFlags.ProtectingFlag(flagName, flagData);
}
}
}
Expand Down
Loading

0 comments on commit c694350

Please sign in to comment.