Skip to content

Commit

Permalink
Add permission type selection and permission modification screens
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 30, 2024
1 parent e730429 commit 65b406f
Show file tree
Hide file tree
Showing 18 changed files with 252 additions and 40 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 @@ -340,7 +340,7 @@ private void initAnonymousData() {
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@SuppressWarnings({"ResultOfMethodCallIgnored", "deprecation"})
private void initDataHandler() {
// Initialize the data handler if another plugin hasn't substituted one already
if (dataHandler == null) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public class ClaimChunkConfig {
@Getter private String guiMapMenuCenterUnclaimedItem;
@Getter private String guiMapMenuCenterSelfClaimedItem;
@Getter private String guiMapMenuCenterOtherClaimedItem;
@Getter private String guiPermSelectMenuItem;
@Getter private String guiPermModifyAllowItem;
@Getter private String guiPermModifyDenyItem;

/* Titles */

Expand Down Expand Up @@ -186,6 +189,9 @@ public void reload() {
guiMapMenuCenterUnclaimedItem = getString("gui", "mapMenuCenterUnclaimedItem");
guiMapMenuCenterSelfClaimedItem = getString("gui", "mapMenuCenterSelfClaimedItem");
guiMapMenuCenterOtherClaimedItem = getString("gui", "mapMenuCenterOtherClaimedItem");
guiPermSelectMenuItem = getString("gui", "permSelectMenuItem");
guiPermModifyAllowItem = getString("gui", "permModifyAllowItem");
guiPermModifyDenyItem = getString("gui", "permModifyDenyItem");

useTitlesInsteadOfChat = getBool("titles", "useTitlesInsteadOfChat");
useActionBar = getBool("titles", "useActionBar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;
import java.util.regex.Pattern;

// TODO: SWITCH THIS TO BACK UP THE .sqlite3 DATABASE FILE
public final class BackupCleaner {

public static boolean deleteBackups(
Expand Down Expand Up @@ -68,6 +69,7 @@ public static boolean deleteBackups(
} catch (Exception e) {
// Print the error
Utils.err("Failed to delete old backups in folder: %s", folder.toString());
//noinspection CallToPrintStackTrace
e.printStackTrace();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
*
* @param <T> The type of the backup data system.
* @since 0.0.16
* @deprecated We now use {@link com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler}
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public class BulkMySQLDataHandler<T extends IClaimChunkDataHandler> extends MySQLDataHandler<T>
implements IClaimChunkDataHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ default void addPlayer(UUID player, String lastIgn, boolean alerts, int defaultM
*/
void addPlayers(FullPlayerData[] players);

/**
* Get this player's default permission flags for non-overridden chunks.
*
* @param player The player's UUID to check.
* @return A map of this player's default permissions.
* @since 0.0.26
*/
@Nullable
Map<String, Boolean> 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 @@ -25,6 +25,11 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* @deprecated We now use {@link com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler}
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public class JsonDataHandler implements IClaimChunkDataHandler {

// Matches: `FILENAME_yyyy-MM-dd-HH-mm-ss-SSS.json`
Expand Down Expand Up @@ -168,6 +173,11 @@ public void addPlayers(FullPlayerData[] players) {
for (FullPlayerData player : players) addPlayer(player);
}

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

@Override
@Nullable
public String getPlayerUsername(UUID player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
*
* @param <T> The type of the backup data system.
* @since 0.0.13
* @deprecated We now use {@link com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler}
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public class MySQLDataHandler<T extends IClaimChunkDataHandler> implements IClaimChunkDataHandler {

static final String CLAIMED_CHUNKS_TABLE_NAME = "claimed_chunks";
Expand Down Expand Up @@ -424,6 +427,11 @@ public void addPlayers(FullPlayerData[] players) {
}
}

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

@Override
@Nullable
public String getPlayerUsername(UUID player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
import java.util.*;
import java.util.stream.Collectors;

/*
* I've actually just decided that we're gonna do it this way:
* - SQLite backing database *file* similar to current MySQL integration (which will
* be removed and automatically converted).
* - Have some intermediary layer that can Respond immediately and asynchronously update database.
*/

/**
* The SHINY, NEW........data handler that tries to fix the data loss issues by which this project
* has been plagued since its conception.
Expand Down Expand Up @@ -144,6 +137,12 @@ public void addPlayers(FullPlayerData[] players) {
Arrays.stream(players).forEach(this::addPlayer);
}

@Override
public @Nullable Map<String, Boolean> getDefaultPermissionsForPlayer(UUID player) {
FullPlayerData ply = joinedPlayers.get(player);
return ply == null ? null : ply.defaultChunkPermissions.toPermissionsMap();
}

@Override
public @Nullable String getPlayerUsername(UUID player) {
FullPlayerData ply = joinedPlayers.get(player);
Expand Down Expand Up @@ -254,7 +253,6 @@ public void takePlayerAccess(ChunkPos chunk, UUID accessor) {
@Override
public Map<UUID, ChunkPlayerPermissions> getPlayersWithAccess(ChunkPos chunk) {
DataChunk chunkData = claimedChunks.get(chunk);
if (chunkData != null) return chunkData.playerPermissions;
return null;
return chunkData == null ? null : chunkData.playerPermissions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,13 @@ public void setPlayerAccess(ChunkPos chunk, UUID accessor, int permissionFlags)
connection.prepareStatement(
chunkIdQuery(
"""
INSERT INTO chunk_permissions (
INSERT OR REPLACE 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());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public void onGuiClick(InventoryClickEvent e) {
UUID uuid = e.getWhoClicked().getUniqueId();
CCOpenGui openGui = openGuis.get(uuid);
if (openGui != null) {
if (e.getInventory().equals(openGui.inventory())) {
if (e.getInventory().equals(openGui.inventory())
&& e.getSlot() >= 0
&& e.getSlot() < e.getInventory().getSize()) {
openGui.gui()
.onClick(
openGui.inventory(), e.getSlot(), e.getClick(), e.getCurrentItem());
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -66,8 +67,6 @@ protected void addInteractiveButton(
@NotNull String itemName,
@NotNull List<String> itemLore,
@NotNull GuiItemAction action) {
Utils.debug("Made GUI stack in slot: " + slot);
Utils.debug("Max: " + this.actions.length);
if (slot >= 0 && slot < this.actions.length && itemType != Material.AIR) {
ItemStack stack = makeStack(itemType, itemName, itemLore);
if (stack != null) {
Expand Down Expand Up @@ -155,7 +154,7 @@ public Player getPlayer() {
}

/**
* Generate localized chunk owner name GUI text.
* Helper method to generate localized chunk owner name GUI text.
*
* @param chunkName The non-null name of the chunk owner. This replaces {@code %%NAME%%}
* verbatim.
Expand All @@ -169,7 +168,8 @@ public Player getPlayer() {
}

/**
* Get the name for this given chunk owner, or return the localized unknown player text.
* Helper method to get the name for this given chunk owner, or return the localized unknown
* player text.
*
* @param chunkOwner The non-null owner of the chunk.
* @return The name for the chunk's owner that can be shown to a player in the GUI.
Expand All @@ -178,4 +178,17 @@ public Player getPlayer() {
String chunkName = claimChunk.getPlayerHandler().getChunkName(chunkOwner);
return chunkName != null ? chunkName : claimChunk.getMessages().unknownChunkOwner;
}

protected static @NotNull List<String> splitLineLore(@NotNull String loreLine) {
return Arrays.asList(loreLine.split("\n"));
}

/**
* Helper method to open the given GUI for its respective player.
*
* @param gui The gui to open.
*/
protected void openGui(@NotNull ICCGui gui) {
claimChunk.getGuiHandler().openOrRefreshGui(gui);
}
}
12 changes: 4 additions & 8 deletions src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.UUID;

public class MainMenu extends GuiMenuScreen {
Expand Down Expand Up @@ -82,11 +81,8 @@ private void addMapItem(@NotNull Inventory inventory) {
4,
materialFromStr(claimChunk.getConfigHandler().getGuiMainMenuChunkMapItem()),
claimChunk.getMessages().guiMainMenuMapItemName,
Collections.singletonList(claimChunk.getMessages().guiMapDescription),
(clickType, stack) ->
claimChunk
.getGuiHandler()
.openOrRefreshGui(new MapMenu(claimChunk, getPlayer())));
splitLineLore(claimChunk.getMessages().guiMainMenuMapItemDesc),
(clickType, stack) -> openGui(new MapMenu(claimChunk, getPlayer())));
}

private void addPermsItem(@NotNull Inventory inventory) {
Expand All @@ -95,7 +91,7 @@ private void addPermsItem(@NotNull Inventory inventory) {
6,
materialFromStr(claimChunk.getConfigHandler().getGuiMainMenuPermFlagsItem()),
claimChunk.getMessages().guiMainMenuPermFlagsItemName,
Collections.singletonList(claimChunk.getMessages().guiMainMenuPermFlagsDescription),
(clickType, stack) -> {});
splitLineLore(claimChunk.getMessages().guiMainMenuPermFlagsDescription),
(clickType, stack) -> openGui(new PermSelectMenu(claimChunk, getPlayer())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MapMenu extends GuiMenuScreen {

public MapMenu(@NotNull ClaimChunk claimChunk, @NotNull Player player) {
// Offset to make it 6 tall so that we can have the top row be a menu bar.
super(claimChunk, player, MAP_HEIGHT + 1, claimChunk.getMessages().guiMainMenuTitle);
super(claimChunk, player, MAP_HEIGHT + 1, claimChunk.getMessages().guiMapMenuTitle);
}

@Override
Expand All @@ -41,10 +41,7 @@ public void onOpen(@NotNull Inventory inventory) {
materialFromStr(config.getGuiMenuBackButtonItem()),
messages.guiMenuBackButtonName,
Collections.singletonList(messages.guiMenuBackButtonDesc),
(clickType, stack) ->
claimChunk
.getGuiHandler()
.openOrRefreshGui(new MainMenu(claimChunk, getPlayer())));
(clickType, stack) -> openGui(new MainMenu(claimChunk, getPlayer())));

// Add the map items
// Inventory width is 9, so go 4 eastward and westward
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.cjburkey.claimchunk.gui.screens;

import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.ClaimChunkConfig;
import com.cjburkey.claimchunk.gui.GuiMenuScreen;
import com.cjburkey.claimchunk.i18n.V2JsonMessages;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class PermModifyMenu extends GuiMenuScreen {

public final boolean isAllChunks;

public PermModifyMenu(
@NotNull ClaimChunk claimChunk, @NotNull Player player, boolean isAllChunks) {
// Offset to make it 6 tall so that we can have the top row be a menu bar.
super(claimChunk, player, 4, claimChunk.getMessages().guiPermSelectMenuTitle);

this.isAllChunks = isAllChunks;
}

@Override
public void onOpen(@NotNull Inventory inventory) {
V2JsonMessages messages = claimChunk.getMessages();
ClaimChunkConfig config = claimChunk.getConfigHandler();
Material allowItem = materialFromStr(config.getGuiPermSelectMenuItem());
Material denyItem = materialFromStr(config.getGuiPermSelectMenuItem());

// Back button
addInteractiveButton(
inventory,
0,
materialFromStr(config.getGuiMenuBackButtonItem()),
messages.guiMenuBackButtonName,
splitLineLore(messages.guiMenuBackButtonDesc),
(clickType, stack) -> openGui(new PermSelectMenu(claimChunk, getPlayer())));

// Add permission buttons
// TODO: WE HAVE TO ADD DEFAULT PERMISSIONS TO CHUNK DATA!
Map<String, Boolean> permissions =
isAllChunks
? claimChunk
.getPlayerHandler()
.getDefaultPermissions(getPlayer().getUniqueId())
: null;
if (permissions == null) {
return;
}
int slot = 0;
for (Map.Entry<String, Boolean> permission : permissions.entrySet()) {
addInteractiveButton(
inventory,
9 + slot,
permission.getValue() ? allowItem : denyItem,
"&r&f" + permission.getKey(),
splitLineLore(messages.guiPermModifyPermDesc),
(clickType, stack) -> {});
slot++;
}
}

@Override
public @NotNull String getName() {
V2JsonMessages messages = claimChunk.getMessages();
return isAllChunks
? messages.guiPermModifyAllMenuTitle
: messages.guiPermModifyThisMenuTitle;
}
}
Loading

0 comments on commit 65b406f

Please sign in to comment.