Skip to content

Commit

Permalink
Add bypass permissions for viewing worlds
Browse files Browse the repository at this point in the history
- `buildsystem.bypass.permission.public` for public worlds
- `buildsystem.bypass.permission.archive` for archived worlds
- `buildsystem.bypass.permission.private` for private worlds
  • Loading branch information
thomasmny committed Jan 15, 2025
1 parent 3a6911e commit d8c93ec
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public void execute(Player player, String[] args) {
return;
}

String permission = buildWorld.getData().permission().get();
if (player.hasPermission(permission) || permission.equalsIgnoreCase("-")) {
worldManager.teleport(player, buildWorld);
} else {
if (!worldManager.canEnter(player, buildWorld)) {
Messages.sendMessage(player, "worlds_tp_entry_forbidden");
return;
}

worldManager.teleport(player, buildWorld);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,7 @@ private boolean isValidWorld(Player player, BuildWorld buildWorld) {
return false;
}

if (player.hasPermission(BuildSystem.ADMIN_PERMISSION)) {
return true;
}

if (buildWorld.isCreator(player) || buildWorld.isBuilder(player)) {
return true;
}

String worldPermission = worldData.permission().get();
if (!worldPermission.equalsIgnoreCase("-") && !player.hasPermission(worldPermission)) {
if (!worldManager.canEnter(player, buildWorld)) {
return false;
}

Expand All @@ -227,9 +218,9 @@ public void onInventoryClick(InventoryClickEvent event) {

switch (event.getSlot()) {
case 45:
WorldSort newSort =
event.isLeftClick() ? worldDisplay.getWorldSort().getNext()
: worldDisplay.getWorldSort().getPrevious();
WorldSort newSort = event.isLeftClick()
? worldDisplay.getWorldSort().getNext()
: worldDisplay.getWorldSort().getPrevious();
worldDisplay.setWorldSort(newSort);
openInventory(player);
return;
Expand Down Expand Up @@ -275,11 +266,20 @@ public void onInventoryClick(InventoryClickEvent event) {
inventoryUtils.manageInventoryClick(event, player, itemStack);
}

/**
* The visibility settings for a {@link BuildWorld}.
*/
public enum Visibility {
PRIVATE,
PUBLIC,
IGNORE;

/**
* Matches the visibility based on whether the world is private.
*
* @param isPrivateWorld Whether the world is private
* @return The corresponding visibility
*/
public static Visibility matchVisibility(boolean isPrivateWorld) {
return isPrivateWorld ? PRIVATE : PUBLIC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,9 @@ public void manageInventoryClick(InventoryClickEvent event, Player player, ItemS
* <p>
* If the click is a...
* <ul>
* <l>...left click, the world is loaded (if previously unloaded) and the player is teleported to said world.</li>
* <li>...left click, the world is loaded (if previously unloaded) and the player is teleported to said world.</li>
* <li>...right click and the player is permitted to edit the world {@link WorldManager#isPermitted(Player, String, String)},
* the {@link EditInventory} for the world is opened for said player.
* If the player does not the the required permission the click is handled as a normal left click.</li>
* the {@link EditInventory} for the world is opened for said player. If the player does not the required permission the click is handled as a normal left click.</li>
* </ul>
*
* @param event The click event to modify
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,23 +661,64 @@ public boolean isSafeLocation(Location location) {
return ground.getType().isSolid();
}

/**
* Determines whether the player can enter the specified build world.
* <p>
* The following logic is applied to decide if the player can enter:
* <ul>
* <li>Does the player have the {@link BuildSystem#ADMIN_PERMISSION} or can bypass the world's permission?</li>
* <li>Is the player the creator of the build world?</li>
* <li>Is the player a builder for the build world?</li>
* <li>Does the build world have a specific permission, and does the player have that permission?</li>
* <li>If no specific permission is set (denoted by {@code "-"}), entry is allowed.</li>
* </ul>
*
* @param player The player whose access is being evaluated
* @param buildWorld The build world the player is attempting to enter
* @return {@code true} if the player can enter the build world, {@code false} otherwise
* @see #canBypassWorldPermission(Player, BuildWorld)
*/
public boolean canEnter(Player player, BuildWorld buildWorld) {
if (player.hasPermission(BuildSystem.ADMIN_PERMISSION)) {
if (player.hasPermission(BuildSystem.ADMIN_PERMISSION) || canBypassWorldPermission(player, buildWorld)) {
return true;
}

String permission = buildWorld.getData().permission().get();
if (permission.equals("-")) {
if (buildWorld.isCreator(player) || buildWorld.isBuilder(player)) {
return true;
}

if (buildWorld.isCreator(player) || buildWorld.isBuilder(player)) {
String permission = buildWorld.getData().permission().get();
if (permission.equals("-")) {
return true;
}

return player.hasPermission(permission);
}

/**
* Determines whether the player has permission to bypass restrictions for a specific build world.
* <p>
* <ul>
* <li>If the world is public, does the player have the permission {@code buildsystem.bypass.permission.public}?</li>
* <li>If the world is archived, does the player have the permission {@code buildsystem.bypass.permission.archive}?</li>
* <li>If the world is private, does the player have the permission {@code buildsystem.bypass.permission.private}?</li>
* </ul>
*
* @param player The player whose permissions will be checked
* @param buildWorld The build world for which bypass permissions are evaluated
* @return {@code true} if the player has bypass permissions for the specified world, {@code false} otherwise
*/
private boolean canBypassWorldPermission(Player player, BuildWorld buildWorld) {
WorldData worldData = buildWorld.getData();
if (worldData.status().get() == WorldStatus.ARCHIVE) {
return player.hasPermission("buildsystem.bypass.permission.archive");
}

return worldData.privateWorld().get()
? player.hasPermission("buildsystem.bypass.permission.private")
: player.hasPermission("buildsystem.bypass.permission.public");
}

public boolean canBypassBuildRestriction(Player player) {
return player.hasPermission(BuildSystem.ADMIN_PERMISSION) || plugin.getPlayerManager().isInBuildMode(player);
}
Expand Down

0 comments on commit d8c93ec

Please sign in to comment.