From c6943502e2f4ee0159151b141367d3336566f284 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Thu, 22 Aug 2024 02:56:36 -0400 Subject: [PATCH] Get flag data saving, loading does NOT work --- .../com/cjburkey/claimchunk/ClaimChunk.java | 2 +- .../data/sqlite/SqLiteDataHandler.java | 2 +- .../sqlite/SqLiteTableMigrationManager.java | 8 +- .../claimchunk/data/sqlite/SqLiteWrapper.java | 187 +++++++++--------- .../com/cjburkey/claimchunk/flag/CCFlags.java | 5 + .../claimchunk/flag/CCInteractClasses.java | 3 + .../cjburkey/claimchunk/flag/CCPermFlags.java | 20 +- .../sub/ply/flags => flag}/FlagHandler.java | 7 +- .../claimchunk/i18n/V2JsonMessages.java | 24 +++ .../smartcommand/ClaimChunkBaseCommand.java | 56 ++++-- .../smartcommand/sub/ply/HelpCmd.java | 3 - .../sub/ply/flags/CCPlyAccessCmd.java | 36 ++++ .../sub/ply/flags/CmdAddPermFlag.java | 18 -- .../sub/ply/flags/CmdClearPermFlag.java | 43 ++++ .../sub/ply/flags/CmdSetPermFlag.java | 43 ++++ .../sub/ply/flags/CmdViewPermFlag.java | 43 ++++ .../sub/ply/flags/ShowFlagsHerePlayer.java | 38 ---- .../cjburkey/claimchunk/TestSQLPlease.java | 102 ++++++++-- 18 files changed, 439 insertions(+), 201 deletions(-) rename src/main/java/com/cjburkey/claimchunk/{smartcommand/sub/ply/flags => flag}/FlagHandler.java (76%) create mode 100644 src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CCPlyAccessCmd.java delete mode 100644 src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdAddPermFlag.java create mode 100644 src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdClearPermFlag.java create mode 100644 src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdSetPermFlag.java create mode 100644 src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdViewPermFlag.java delete mode 100644 src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/ShowFlagsHerePlayer.java diff --git a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java index 6c827ed..e2fa5db 100644 --- a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java +++ b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java @@ -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; @@ -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; diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java index e68767e..aabdc21 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteDataHandler.java @@ -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); } } diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteTableMigrationManager.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteTableMigrationManager.java index 67acfe3..c7369f0 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteTableMigrationManager.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteTableMigrationManager.java @@ -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, @@ -69,7 +70,6 @@ REFERENCES chunk_data(chunk_id) """); } - @SuppressWarnings("unused") public static boolean tableExists(String tableName) { return SqlClosure.sqlExecute( connection -> { diff --git a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java index 56ddbe6..6ee2f1b 100644 --- a/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java +++ b/src/main/java/com/cjburkey/claimchunk/data/sqlite/SqLiteWrapper.java @@ -248,7 +248,53 @@ public void setPermissionFlags( @NotNull UUID owner, @Nullable UUID accessor, @Nullable ChunkPos chunk, - @NotNull HashMap newFlags) {} + @NotNull HashMap 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 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, @@ -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; } }); } @@ -356,15 +347,15 @@ public List 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")); @@ -378,8 +369,13 @@ public List 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); @@ -394,7 +390,7 @@ public List getAllPlayers() { return playerData.values().stream().toList(); } - public static Collection getAllChunks() { + public Collection getAllChunks() { return SqlClosure.sqlExecute( connection -> { HashMap chunks = new HashMap<>(); @@ -426,8 +422,8 @@ public static Collection 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()) { @@ -447,25 +443,28 @@ public static Collection 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); diff --git a/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java b/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java index 0958ea5..231791c 100644 --- a/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/CCFlags.java @@ -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 :} @@ -44,6 +47,8 @@ public boolean doesProtect(boolean isFlagEnabled) { } } + public record ProtectingFlag(String name, CCFlags.FlagData flagData) {} + public record FlagData( ProtectWhen protectWhen, @NotNull Set include, @NotNull Set exclude) {} diff --git a/src/main/java/com/cjburkey/claimchunk/flag/CCInteractClasses.java b/src/main/java/com/cjburkey/claimchunk/flag/CCInteractClasses.java index 138eced..320703c 100644 --- a/src/main/java/com/cjburkey/claimchunk/flag/CCInteractClasses.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/CCInteractClasses.java @@ -10,6 +10,9 @@ import java.util.*; import java.util.stream.Collectors; +/** + * @since 0.0.26 + */ public final class CCInteractClasses { private final HashMap> classBlocks = new HashMap<>(); diff --git a/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java b/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java index 566b525..6e7e0f8 100644 --- a/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/CCPermFlags.java @@ -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, @@ -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); } } } @@ -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 enabledContextFlags) { + public @Nullable CCFlags.ProtectingFlag getProtectingFlag( + EntityType entityType, CCFlags.EntityFlagType interactionType) { // Loop through each flag for (Map.Entry entityControllingFlag : entityControls.entrySet()) { @@ -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); } } } diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/FlagHandler.java b/src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java similarity index 76% rename from src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/FlagHandler.java rename to src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java index fcd0433..e4a5f37 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/FlagHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/flag/FlagHandler.java @@ -1,8 +1,13 @@ -package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; +package com.cjburkey.claimchunk.flag; import com.cjburkey.claimchunk.ClaimChunk; import com.cjburkey.claimchunk.data.newdata.IClaimChunkDataHandler; +/** + * Interface between flag data and the rest of the plugin :) + * + * @since 0.0.26 + */ public class FlagHandler { private final IClaimChunkDataHandler dataHandler; diff --git a/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java b/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java index 3b8c618..5751dc8 100644 --- a/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java +++ b/src/main/java/com/cjburkey/claimchunk/i18n/V2JsonMessages.java @@ -248,6 +248,30 @@ &r&fpermissions in your chunk(s) public String cmdRevokeAccess = "Revoke all permissions for [player] in either the current chunk," + " or across your claimed territory"; + + // New flag commands + public String cmdPermFlagGlobalSet = "Set the default protection flags in your claims"; + public String cmdPermFlagPlySet = + "Set the default protection flags for a certain player in your claims"; + public String cmdPermFlagPlyChunkSet = + "Set protection flags for a certain player in the chunk you're standing in"; + public String cmdPermFlagChunkSet = + "Set the default protection flags for the you're standing in"; + public String cmdPermFlagGlobalClear = "Clear certain default protection flags in your claims"; + public String cmdPermFlagPlyClear = + "Clear certain default protection flags for a certain player in your claims"; + public String cmdPermFlagPlyChunkClear = + "Clear certain flags for a certain player in the chunk you're standing in"; + public String cmdPermFlagChunkClear = + "Clear certain default protection flags for the you're standing in"; + public String cmdPermFlagGlobalList = "List enabled/disabled default flags in your claims"; + public String cmdPermFlagPlyList = + "List enabled/disabled default flags for a player in your claims"; + public String cmdPermFlagPlyChunkList = + "List enabled/disabled flags for a player in the chunk you're standing in"; + public String cmdPermFlagChunkList = + "List enabled/disabled default flags for players in the chunk you're standing in"; + public String cmdAdminUnclaim = "Unclaim the chunk you're standing in whether or not you are the owner"; public String cmdAlert = diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java index 9e63b64..49e852e 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java @@ -8,6 +8,9 @@ import com.cjburkey.claimchunk.Utils; import com.cjburkey.claimchunk.smartcommand.sub.admin.*; import com.cjburkey.claimchunk.smartcommand.sub.ply.*; +import com.cjburkey.claimchunk.smartcommand.sub.ply.flags.CmdClearPermFlag; +import com.cjburkey.claimchunk.smartcommand.sub.ply.flags.CmdSetPermFlag; +import com.cjburkey.claimchunk.smartcommand.sub.ply.flags.CmdViewPermFlag; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -43,22 +46,48 @@ public ClaimChunkBaseCommand(ClaimChunk claimChunk) { // Player commands registerCmds( - // TODO: - // `/chunk access` - /*new CommandStr(new AccessCmd(claimChunk), "access"), - // `/chunk checkaccess` - new CommandStr(new CheckAccessCmd(claimChunk), "checkaccess"), - // `/chunk revokeaccess` - new CommandStr(new RevokeAccessCmd(claimChunk), "revokeaccess"),*/ - + // `/chunk flags player here set/clear` + // `/chunk flags player set/clear` + // `/chunk flags here set/clear` + // `/chunk flags set/clear` // `/chunk flags show player here` // `/chunk flags show player` // `/chunk flags show here` // `/chunk flags show` - // `/chunk flags player here grant/revoke` - // `/chunk flags player grant/revoke` - // `/chunk flags here grant/revoke` - // `/chunk flags grant/revoke` + new CommandStr( + new CmdSetPermFlag(claimChunk, true, true), + "flags", + "player", + "here", + "set"), + new CommandStr( + new CmdSetPermFlag(claimChunk, true, false), "flags", "player", "set"), + new CommandStr(new CmdSetPermFlag(claimChunk, false, true), "flags", "here", "set"), + new CommandStr(new CmdSetPermFlag(claimChunk, false, false), "flags", "set"), + // Clear flag commands + new CommandStr( + new CmdClearPermFlag(claimChunk, true, true), + "flags", + "player", + "here", + "clear"), + new CommandStr( + new CmdClearPermFlag(claimChunk, true, false), "flags", "player", "clear"), + new CommandStr( + new CmdClearPermFlag(claimChunk, false, true), "flags", "here", "clear"), + new CommandStr(new CmdClearPermFlag(claimChunk, false, false), "flags", "clear"), + // Show flag commands + new CommandStr( + new CmdViewPermFlag(claimChunk, true, true), + "flags", + "player", + "here", + "list"), + new CommandStr( + new CmdViewPermFlag(claimChunk, true, false), "flags", "player", "list"), + new CommandStr( + new CmdViewPermFlag(claimChunk, false, true), "flags", "here", "list"), + new CommandStr(new CmdViewPermFlag(claimChunk, false, false), "flags", "list"), // `/chunk alert` new CommandStr(new AlertCmd(claimChunk), "alert"), @@ -111,8 +140,7 @@ private void registerCmds(CommandStr... commands) { try { registerSubCommand(cmd.cmd, cmd.args); } catch (CommandNotValidException e) { - // Hopefully won't occur, but compile-time safety isn't one of - // Java's strong-suits + // Hopefully won't occur... Utils.err("Failed to initialize subcommand: /chunk %s", String.join(" ", cmd.args)); //noinspection CallToPrintStackTrace e.printStackTrace(); diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java index a4edd15..b4e702b 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java @@ -48,10 +48,7 @@ public int getRequiredArguments() { @Override public boolean onCall(@NotNull String cmdUsed, @NotNull CommandSender player, String[] args) { - Utils.err("%s", (Object) args); if (args.length == 0) { - Utils.err("HELP ME"); - // Display the help command header messageChat(player, claimChunk.getMessages().helpHeader); diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CCPlyAccessCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CCPlyAccessCmd.java new file mode 100644 index 0000000..97bca55 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CCPlyAccessCmd.java @@ -0,0 +1,36 @@ +package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; + +import claimchunk.dependency.de.goldmensch.commanddispatcher.Executor; + +import com.cjburkey.claimchunk.ClaimChunk; +import com.cjburkey.claimchunk.smartcommand.CCSubCommand; + +import org.jetbrains.annotations.NotNull; + +public abstract class CCPlyAccessCmd extends CCSubCommand { + + public final boolean isForPly; + public final boolean isForChunk; + + public CCPlyAccessCmd(@NotNull ClaimChunk claimChunk, boolean isForPly, boolean isForChunk) { + super(claimChunk, Executor.PLAYER, true, "player", "access"); + this.isForPly = isForPly; + this.isForChunk = isForChunk; + } + + @Override + public abstract @NotNull String getDescription(); + + @Override + public CCArg[] getPermittedArguments() { + if (isForPly) { + return new CCArg[] {new CCArg("otherPlayer", CCAutoComplete.OFFLINE_PLAYER)}; + } + return new CCArg[0]; + } + + @Override + public int getRequiredArguments() { + return isForPly ? 1 : 0; + } +} diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdAddPermFlag.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdAddPermFlag.java deleted file mode 100644 index c48233c..0000000 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdAddPermFlag.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; - -import claimchunk.dependency.de.goldmensch.commanddispatcher.Executor; - -import com.cjburkey.claimchunk.ClaimChunk; -import com.cjburkey.claimchunk.smartcommand.CCSubCommand; - -import org.jetbrains.annotations.NotNull; - -public abstract class CmdAddPermFlag extends CCSubCommand { - - public CmdAddPermFlag(@NotNull ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, true, "player", "access"); - } - - // TODO: MAKE GENERAL FORM AND 4 IMPLEMENTORS - -} diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdClearPermFlag.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdClearPermFlag.java new file mode 100644 index 0000000..0368ce4 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdClearPermFlag.java @@ -0,0 +1,43 @@ +package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; + +import com.cjburkey.claimchunk.ClaimChunk; +import com.cjburkey.claimchunk.i18n.V2JsonMessages; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +/** + * @since 0.0.26 + */ +public class CmdClearPermFlag extends CCPlyAccessCmd { + + public CmdClearPermFlag(@NotNull ClaimChunk claimChunk, boolean isForPly, boolean isForChunk) { + super(claimChunk, isForPly, isForChunk); + } + + @Override + public @NotNull String getDescription() { + V2JsonMessages msg = claimChunk.getMessages(); + if (isForPly) { + if (isForChunk) { + return msg.cmdPermFlagPlyChunkClear; + } else { + return msg.cmdPermFlagPlyClear; + } + } else { + if (isForChunk) { + return msg.cmdPermFlagChunkClear; + } else { + return msg.cmdPermFlagGlobalClear; + } + } + } + + @Override + public boolean onCall( + @NotNull String cmdUsed, @NotNull CommandSender executor, @NotNull String[] args) { + // TODO: DO THIS + + return false; + } +} diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdSetPermFlag.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdSetPermFlag.java new file mode 100644 index 0000000..51ce057 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdSetPermFlag.java @@ -0,0 +1,43 @@ +package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; + +import com.cjburkey.claimchunk.ClaimChunk; +import com.cjburkey.claimchunk.i18n.V2JsonMessages; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +/** + * @since 0.0.26 + */ +public class CmdSetPermFlag extends CCPlyAccessCmd { + + public CmdSetPermFlag(@NotNull ClaimChunk claimChunk, boolean isForPly, boolean isForChunk) { + super(claimChunk, isForPly, isForChunk); + } + + @Override + public @NotNull String getDescription() { + V2JsonMessages msg = claimChunk.getMessages(); + if (isForPly) { + if (isForChunk) { + return msg.cmdPermFlagPlyChunkSet; + } else { + return msg.cmdPermFlagPlySet; + } + } else { + if (isForChunk) { + return msg.cmdPermFlagChunkSet; + } else { + return msg.cmdPermFlagGlobalSet; + } + } + } + + @Override + public boolean onCall( + @NotNull String cmdUsed, @NotNull CommandSender executor, @NotNull String[] args) { + // TODO: DO THIS + + return false; + } +} diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdViewPermFlag.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdViewPermFlag.java new file mode 100644 index 0000000..c45fe33 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/CmdViewPermFlag.java @@ -0,0 +1,43 @@ +package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; + +import com.cjburkey.claimchunk.ClaimChunk; +import com.cjburkey.claimchunk.i18n.V2JsonMessages; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +/** + * @since 0.0.26 + */ +public class CmdViewPermFlag extends CCPlyAccessCmd { + + public CmdViewPermFlag(@NotNull ClaimChunk claimChunk, boolean isForPly, boolean isForChunk) { + super(claimChunk, isForPly, isForChunk); + } + + @Override + public @NotNull String getDescription() { + V2JsonMessages msg = claimChunk.getMessages(); + if (isForPly) { + if (isForChunk) { + return msg.cmdPermFlagPlyChunkList; + } else { + return msg.cmdPermFlagPlyList; + } + } else { + if (isForChunk) { + return msg.cmdPermFlagChunkList; + } else { + return msg.cmdPermFlagGlobalList; + } + } + } + + @Override + public boolean onCall( + @NotNull String cmdUsed, @NotNull CommandSender executor, @NotNull String[] args) { + // TODO: DO THIS + + return false; + } +} diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/ShowFlagsHerePlayer.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/ShowFlagsHerePlayer.java deleted file mode 100644 index 6deb253..0000000 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/flags/ShowFlagsHerePlayer.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.cjburkey.claimchunk.smartcommand.sub.ply.flags; - -import claimchunk.dependency.de.goldmensch.commanddispatcher.Executor; - -import com.cjburkey.claimchunk.ClaimChunk; -import com.cjburkey.claimchunk.smartcommand.CCSubCommand; - -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; - -import java.util.Set; - -public class ShowFlagsHerePlayer extends CCSubCommand { - - public ShowFlagsHerePlayer(@NotNull ClaimChunk claimChunk, @NotNull Executor executor) { - super(claimChunk, executor, true, "player", "access"); - } - - @Override - public CCArg[] getPermittedArguments() { - return new CCArg[] { - new CCArg(claimChunk.getMessages().argPlayer, CCAutoComplete.OFFLINE_PLAYER), - }; - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public boolean onCall( - @NotNull String cmdUsed, @NotNull CommandSender executor, @NotNull String[] args) { - Set allFlags = claimChunk.getPermFlags().getAllFlags(); - - return false; - } -} diff --git a/src/test/java/com/cjburkey/claimchunk/TestSQLPlease.java b/src/test/java/com/cjburkey/claimchunk/TestSQLPlease.java index 82c560e..c2d16ee 100644 --- a/src/test/java/com/cjburkey/claimchunk/TestSQLPlease.java +++ b/src/test/java/com/cjburkey/claimchunk/TestSQLPlease.java @@ -1,11 +1,19 @@ package com.cjburkey.claimchunk; +import static org.junit.jupiter.api.Assertions.*; + +import com.cjburkey.claimchunk.chunk.ChunkPos; +import com.cjburkey.claimchunk.chunk.DataChunk; import com.cjburkey.claimchunk.data.sqlite.SqLiteTableMigrationManager; import com.cjburkey.claimchunk.data.sqlite.SqLiteWrapper; +import com.cjburkey.claimchunk.player.FullPlayerData; import org.junit.jupiter.api.Test; import java.io.File; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; import java.util.UUID; class TestSQLPlease { @@ -14,12 +22,85 @@ class TestSQLPlease { void ensureColumnExistsMethodWorks() { // Must create the wrapper to initialize (and deinitialize) connection try (TestQlWrap ignoredWrapper = new TestQlWrap()) { - assert SqLiteTableMigrationManager.columnExists("player_data", "player_uuid"); - assert SqLiteTableMigrationManager.columnExists("chunk_data", "owner_uuid"); - assert SqLiteTableMigrationManager.tableExists("permission_flags"); - assert !SqLiteTableMigrationManager.tableExists("bob_the_builder_no_we_cant"); - assert !SqLiteTableMigrationManager.columnExists("chunk_hell", "permission_bits"); - assert !SqLiteTableMigrationManager.columnExists("player_data", "fake_col"); + assertTrue(SqLiteTableMigrationManager.columnExists("player_data", "player_uuid")); + assertTrue(SqLiteTableMigrationManager.columnExists("chunk_data", "owner_uuid")); + assertTrue(SqLiteTableMigrationManager.tableExists("permission_flags")); + assertFalse(SqLiteTableMigrationManager.tableExists("bob_the_builder_no_we_cant")); + assertFalse(SqLiteTableMigrationManager.columnExists("chunk_hell", "permission_bits")); + assertFalse(SqLiteTableMigrationManager.columnExists("player_data", "fake_col")); + } + } + + @SuppressWarnings("OptionalGetWithoutIsPresent") + @Test + void test0026BasicsWork() { + try (TestQlWrap wrapper = new TestQlWrap()) { + assertTrue(SqLiteTableMigrationManager.tableExists("player_data")); + assertTrue(SqLiteTableMigrationManager.tableExists("chunk_data")); + assertTrue(SqLiteTableMigrationManager.tableExists("permission_flags")); + + FullPlayerData examplePlayer1 = + new FullPlayerData( + UUID.randomUUID(), "BobMarley", "Chris Farley", 924789, true, 0); + FullPlayerData examplePlayer2 = + new FullPlayerData( + UUID.randomUUID(), "HisBrother", "Tommy Boy", 63425, false, 3); + wrapper.sql.addPlayer(examplePlayer1); + wrapper.sql.addPlayer(examplePlayer2); + + DataChunk exampleChunk1 = + new DataChunk(new ChunkPos("world", 39, -91), examplePlayer1.player); + DataChunk exampleChunk2 = + new DataChunk(new ChunkPos("world_the_nether", -17, 1), examplePlayer2.player); + wrapper.sql.addClaimedChunk(exampleChunk1); + wrapper.sql.addClaimedChunk(exampleChunk2); + + HashMap perms1 = new HashMap<>(); + perms1.put("doThis", true); + perms1.put("doThat", false); + wrapper.sql.setPermissionFlags(examplePlayer1.player, null, null, perms1); + + HashMap perms2 = new HashMap<>(); + perms2.put("dontDoThis", true); + perms2.put("dontDoThat", false); + perms2.put("dontAtAll", false); + wrapper.sql.setPermissionFlags( + examplePlayer1.player, null, exampleChunk2.chunk(), perms2); + + HashMap perms3 = new HashMap<>(); + perms3.put("alpha", true); + wrapper.sql.setPermissionFlags( + examplePlayer1.player, examplePlayer2.player, exampleChunk2.chunk(), perms3); + + HashMap perms4 = new HashMap<>(); + perms4.put("alAsphalt", false); + wrapper.sql.setPermissionFlags( + examplePlayer1.player, examplePlayer2.player, null, perms4); + + List loadedPlayers = wrapper.sql.getAllPlayers(); + Collection loadedChunks = wrapper.sql.getAllChunks(); + + assertEquals(2, loadedPlayers.size()); + assertEquals(2, loadedChunks.size()); + + FullPlayerData firstPly = + loadedPlayers.stream() + .filter(s -> s.player.equals(examplePlayer1.player)) + .findFirst() + .get(); + DataChunk firstPlysChunk = + loadedChunks.stream() + .filter(s -> s.player().equals(examplePlayer1.player)) + .findFirst() + .get(); + + assertEquals(2, firstPly.globalFlags.size()); + assertEquals(false, firstPly.globalFlags.get("doThat")); + assertEquals(true, firstPly.globalFlags.get("doThis")); + assertEquals(false, firstPlysChunk.defaultFlags().get("dontDoThat")); + assertEquals( + true, firstPlysChunk.specificFlags().get(examplePlayer2.player).get("alpha")); + assertEquals(false, firstPly.playerFlags.get(examplePlayer2.player).get("alAsphalt")); } } @@ -168,13 +249,8 @@ static class TestQlWrap implements AutoCloseable { File dbFile; TestQlWrap() { - try { - dbFile = randomDbFile(); - sql = new SqLiteWrapper(dbFile, false); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); - } + dbFile = randomDbFile(); + sql = new SqLiteWrapper(dbFile, false); } @Override