Skip to content

Commit

Permalink
Prepare for permission revolution
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Jun 2, 2024
1 parent f5ec144 commit 3bcebc8
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 74 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private void initEcon() {
Utils.log("Economy not enabled.");
}

@SuppressWarnings("unused")
@SuppressWarnings("deprecation")
private JsonDataHandler createJsonDataHandler() {
// Create the basic JSON data handler
return new JsonDataHandler(
Expand Down Expand Up @@ -560,7 +560,7 @@ private void setupNewCommands() {
private void doUpdateCheck() {
try {
// Get the latest online plugin version
availableVersion = UpdateChecker.getLatestRelease("cjburkey01", "ClaimChunk");
availableVersion = UpdateChecker.getLatestRelease();

// Make sure the latest available version is valid
if (availableVersion == null) {
Expand All @@ -570,8 +570,7 @@ private void doUpdateCheck() {

if (availableVersion.isNewerThan(version)) {
// If the latest available version is newer than the current plugin version, the
// server
// should be updated
// server version should be updated
updateAvailable = true;
Utils.log(
"An update for ClaimChunk is available! Your version: %s | Latest version:"
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/cjburkey/claimchunk/chunk/DataChunk.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package com.cjburkey.claimchunk.chunk;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

public class DataChunk {

/** The position of the chunk. */
public final ChunkPos chunk;
public final @NotNull ChunkPos chunk;

/** The UUID of the owning player. */
public final UUID player;

/** Whether TNT can explode in this chunk if TNT is disabled in the config. */
// Assignment because I'm not sure if GSON will handle it?
@SuppressWarnings("UnusedAssignment")
public boolean tnt = true;
public final @NotNull UUID player;

/** The other players that have access to the chunk, and their permissions * */
public Map<UUID, ChunkPlayerPermissions> playerPermissions;
public @NotNull Map<UUID, ChunkPlayerPermissions> playerPermissions;

/** The default access that players will have in this chunk */
public @Nullable ChunkPlayerPermissions defaultPermissions;

/**
* Create an instance of chunk data that links a chunk's position and the owning player.
*
* @param chunk The position of chunk.
* @param player The UUID of the owning player.
* @param tnt Whether TNT is enabled in this chunk.
*/
public DataChunk(
ChunkPos chunk,
UUID player,
Map<UUID, ChunkPlayerPermissions> playerPermissions,
boolean tnt) {
@NotNull ChunkPos chunk,
@NotNull UUID player,
@NotNull Map<UUID, ChunkPlayerPermissions> playerPermissions,
@Nullable ChunkPlayerPermissions defaultPermissions) {
this.chunk = chunk;
this.player = player;
this.playerPermissions = playerPermissions;
this.tnt = tnt;
this.defaultPermissions = defaultPermissions;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import com.cjburkey.claimchunk.player.PlayerHandler;
import com.cjburkey.claimchunk.rank.RankHandler;
import com.cjburkey.claimchunk.service.prereq.claim.*;

import org.bukkit.*;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cjburkey.claimchunk.player.FullPlayerData;
import com.cjburkey.claimchunk.player.SimplePlayerData;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
Expand Down Expand Up @@ -113,39 +114,37 @@ public interface IClaimChunkDataHandler {
UUID getChunkOwner(ChunkPos pos);

/**
* Retrieves all claimed chunks and their owners across all worlds.
* Sets the given chunk's default permission flags for all players without specific access flags
* granted. These permissions will override the player's default chunk permissions.
*
* @return An array of all claimed chunks
* @since 0.0.13
* @param pos The position of the chunk to modify. Nothing happens if the chunk is not currently
* claimed.
* @param chunkPermissions The permissions to set as default, or {@code null} to clear the
* permissions for this chunk.
* @since 0.0.26
*/
DataChunk[] getClaimedChunks();
void setDefaultChunkPermissions(
@NotNull ChunkPos pos, @Nullable ChunkPlayerPermissions chunkPermissions);

/**
* Toggles whether TNT can explode in the given chunk.
* Get the given chunk's default permissions for players that haven't been granted special
* access.
*
* @param ignoredPos The position of the chunk
* @return Whether TNT is now enabled in the provided chunk
* @since 0.0.16
* @deprecated Unused.
* @param pos The position of the chunk.
* @return The default permissions for the chunk, or {@code null} if the chunk is not claimed or
* the chunk doesn't have any specific default access permissions.
* @since 0.0.26
*/
@Deprecated
default boolean toggleTnt(ChunkPos ignoredPos) {
return false;
}
@Nullable
ChunkPlayerPermissions getDefaultChunkPermissions(@NotNull ChunkPos pos);

/**
* Retrieves whether TNT can explode in the given chunk (regardless of whether TNT is disabled
* in the config).
* Retrieves all claimed chunks and their owners across all worlds.
*
* @param ignoredPos The position of the chunk
* @return Whether TNT is enabled in the provided chunk
* @since 0.0.16
* @deprecated Unused.
* @return An array of all claimed chunks
* @since 0.0.13
*/
@Deprecated
default boolean isTntEnabled(ChunkPos ignoredPos) {
return false;
}
DataChunk[] getClaimedChunks();

// -- PLAYERS -- //

Expand Down Expand Up @@ -204,6 +203,17 @@ default void addPlayer(UUID player, String lastIgn, boolean alerts, int defaultM
*/
void addPlayers(FullPlayerData[] players);

/**
* Set the given player's default permission flags in chunks that don't have any specific
* permissions granted.
*
* @param player The player's default to modify
* @param permissions The permissions to grant by default
* @since 0.0.26
*/
void setDefaultPermissionsForPlayer(
@NotNull UUID player, @NotNull ChunkPlayerPermissions permissions);

/**
* Get this player's default permission flags for non-overridden chunks.
*
Expand All @@ -212,7 +222,7 @@ default void addPlayer(UUID player, String lastIgn, boolean alerts, int defaultM
* @since 0.0.26
*/
@Nullable
Map<String, Boolean> getDefaultPermissionsForPlayer(UUID player);
ChunkPlayerPermissions getDefaultPermissionsForPlayer(UUID player);

/**
* Retrieves the username for the given player UUID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void load() throws Exception {
}
}

//noinspection ConstantValue
if (claimedChunks.values().stream().allMatch(c -> c.playerPermissions == null)) {
// If all playerPermissions are null, then the JSON files are in the pre 0.0.24 format
loadPre0024Data();
Expand All @@ -102,12 +103,12 @@ void clearData() {

@Override
public void addClaimedChunk(ChunkPos pos, UUID player) {
claimedChunks.put(pos, new DataChunk(pos, player, new HashMap<>(), false));
claimedChunks.put(pos, new DataChunk(pos, player, new HashMap<>(), null));
}

private void addClaimedChunkWithPerms(
ChunkPos pos, UUID player, Map<UUID, ChunkPlayerPermissions> playerPermissions) {
claimedChunks.put(pos, new DataChunk(pos, player, playerPermissions, false));
claimedChunks.put(pos, new DataChunk(pos, player, playerPermissions, null));
}

@Override
Expand Down Expand Up @@ -135,6 +136,17 @@ public UUID getChunkOwner(ChunkPos pos) {
return null;
}

// Implemented by SqLiteDataHandler
@Override
public void setDefaultChunkPermissions(
@NotNull ChunkPos pos, @Nullable ChunkPlayerPermissions chunkPermissions) {}

// Implemented by SqLiteDataHandler
@Override
public @Nullable ChunkPlayerPermissions getDefaultChunkPermissions(@NotNull ChunkPos pos) {
return null;
}

@Override
public DataChunk[] getClaimedChunks() {
return this.claimedChunks.entrySet().stream()
Expand All @@ -144,7 +156,7 @@ public DataChunk[] getClaimedChunks() {
claimedChunk.getKey(),
claimedChunk.getValue().player,
claimedChunk.getValue().playerPermissions,
claimedChunk.getValue().tnt))
null))
.toArray(DataChunk[]::new);
}

Expand Down Expand Up @@ -173,8 +185,14 @@ public void addPlayers(FullPlayerData[] players) {
for (FullPlayerData player : players) addPlayer(player);
}

// Implemented by SqLiteDataHandler
@Override
public void setDefaultPermissionsForPlayer(
@NotNull UUID player, @NotNull ChunkPlayerPermissions permissions) {}

// Implemented by SqLiteDataHandler
@Override
public @Nullable Map<String, Boolean> getDefaultPermissionsForPlayer(UUID player) {
public @Nullable ChunkPlayerPermissions getDefaultPermissionsForPlayer(UUID player) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.cjburkey.claimchunk.player.FullPlayerData;
import com.cjburkey.claimchunk.player.SimplePlayerData;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.sql.Connection;
Expand Down Expand Up @@ -318,6 +319,17 @@ public UUID getChunkOwner(ChunkPos pos) {
return null;
}

// Implemented by SqLiteDataHandler
@Override
public void setDefaultChunkPermissions(
@NotNull ChunkPos pos, @Nullable ChunkPlayerPermissions chunkPermissions) {}

// Implemented by SqLiteDataHandler
@Override
public @Nullable ChunkPlayerPermissions getDefaultChunkPermissions(@NotNull ChunkPos pos) {
return null;
}

@Override
public DataChunk[] getClaimedChunks() {
String sql =
Expand All @@ -343,7 +355,7 @@ public DataChunk[] getClaimedChunks() {
result.getString(2), result.getInt(3), result.getInt(4)),
UUID.fromString(result.getString(6)),
allChunkPermissions.getOrDefault(result.getInt(1), new HashMap<>()),
result.getBoolean(5)));
null));
}
} catch (Exception e) {
Utils.err("Failed to get all claimed chunks: %s", e.getMessage());
Expand Down Expand Up @@ -427,8 +439,14 @@ public void addPlayers(FullPlayerData[] players) {
}
}

// Implemented by SqLiteDataHandler
@Override
public void setDefaultPermissionsForPlayer(
@NotNull UUID player, @NotNull ChunkPlayerPermissions permissions) {}

// Implemented by SqLiteDataHandler
@Override
public @Nullable Map<String, Boolean> getDefaultPermissionsForPlayer(UUID player) {
public @Nullable ChunkPlayerPermissions getDefaultPermissionsForPlayer(UUID player) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void load() throws Exception {

@Override
public void addClaimedChunk(ChunkPos pos, UUID player) {
DataChunk chunk = new DataChunk(pos, player, new HashMap<>(), false);
DataChunk chunk = new DataChunk(pos, player, new HashMap<>(), null);
claimedChunks.put(pos, chunk);
sqLiteWrapper.addClaimedChunk(chunk);
}
Expand All @@ -101,6 +101,22 @@ public boolean isChunkClaimed(ChunkPos pos) {
return chunk == null ? null : chunk.player;
}

@Override
public void setDefaultChunkPermissions(
@NotNull ChunkPos pos, @Nullable ChunkPlayerPermissions chunkPermissions) {
DataChunk chunk = claimedChunks.get(pos);
if (chunk != null) {
chunk.defaultPermissions = chunkPermissions;
}
sqLiteWrapper.setDefaultChunkPermissions(pos, chunkPermissions);
}

@Override
public @Nullable ChunkPlayerPermissions getDefaultChunkPermissions(@NotNull ChunkPos pos) {
DataChunk chunk = claimedChunks.get(pos);
return chunk == null ? null : chunk.defaultPermissions;
}

@Override
public DataChunk[] getClaimedChunks() {
return claimedChunks.values().toArray(new DataChunk[0]);
Expand Down Expand Up @@ -138,9 +154,19 @@ public void addPlayers(FullPlayerData[] players) {
}

@Override
public @Nullable Map<String, Boolean> getDefaultPermissionsForPlayer(UUID player) {
public void setDefaultPermissionsForPlayer(
@NotNull UUID player, @NotNull ChunkPlayerPermissions permissions) {
FullPlayerData ply = joinedPlayers.get(player);
if (ply != null) {
ply.defaultChunkPermissions = permissions;
}
sqLiteWrapper.setDefaultPermissionsForPlayer(player, permissions);
}

@Override
public @Nullable ChunkPlayerPermissions getDefaultPermissionsForPlayer(UUID player) {
FullPlayerData ply = joinedPlayers.get(player);
return ply == null ? null : ply.defaultChunkPermissions.toPermissionsMap();
return ply == null ? null : ply.defaultChunkPermissions;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CREATE TABLE IF NOT EXISTS chunk_data (
chunk_x INTEGER NOT NULL,
chunk_z INTEGER NOT NULL,
owner_uuid TEXT NOT NULL,
default_local_permissions INTEGER,
FOREIGN KEY(owner_uuid) REFERENCES player_data(player_uuid)
) STRICT
Expand Down Expand Up @@ -78,6 +79,14 @@ private static void migrate_0_0_25() {
ADD default_chunk_permissions INTEGER NOT NULL DEFAULT 0
""");
}

if (!columnExists("chunk_data", "default_local_permissions")) {
Q2Sql.executeUpdate(
"""
ALTER TABLE chunk_data
ADD default_local_permissions INTEGER DEFAULT NULL
""");
}
}

// Use this method to determine if a column exists in a table to perform migrations
Expand Down
Loading

0 comments on commit 3bcebc8

Please sign in to comment.