diff --git a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java index 7848617e..d8321405 100644 --- a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java @@ -49,9 +49,7 @@ public class DelWarpCommand extends SavedPositionCommand { @Override public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) { - if (plugin.getSettings().getGeneral().isPermissionRestrictWarps() - && !executor.hasPermission(warp.getPermission()) - && !executor.hasPermission(Warp.getWildcardPermission())) { + if (plugin.getSettings().getGeneral().isPermissionRestrictWarps() && !warp.hasPermission(executor)) { plugin.getLocales().getLocale("error_no_permission") .ifPresent(executor::sendMessage); return; diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java index 596260c4..eeaf84de 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java @@ -54,13 +54,11 @@ public class WarpCommand extends SavedPositionCommand { @Override public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) { - if (plugin.getSettings().getGeneral().isPermissionRestrictWarps()) { - if (!executor.hasPermission(warp.getPermission()) - && !executor.hasPermission(Warp.getWildcardPermission())) { - plugin.getLocales().getLocale("error_no_permission") - .ifPresent(executor::sendMessage); - return; - } + // Check permission restricted warps + if (plugin.getSettings().getGeneral().isPermissionRestrictWarps() && !warp.hasPermission(executor)) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(executor::sendMessage); + return; } final Optional optionalTeleporter = resolveTeleporter(executor, args); diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java index 411c9814..27cac2c0 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java @@ -92,9 +92,8 @@ public class WarpListCommand extends ListCommand { @NotNull private List getItems(@NotNull CommandUser executor) { List warps = plugin.getDatabase().getWarps(); - if (plugin.getSettings().getGeneral().isPermissionRestrictWarps() - && !executor.hasPermission(Warp.getWildcardPermission())) { - warps = warps.stream().filter(warp -> executor.hasPermission(warp.getPermission())).toList(); + if (plugin.getSettings().getGeneral().isPermissionRestrictWarps()) { + warps = warps.stream().filter(warp -> warp.hasPermission(executor)).toList(); } return warps; } diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index fe3b782e..af996530 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -131,7 +131,7 @@ public final class Settings { + "if the user inherits multiple nodes.") private boolean stackPermissionLimits = false; - @Comment("Whether users require a permission (huskhomes.command.warp.) to use warps") + @Comment("Whether users require a permission (huskhomes.warp.) to use warps") private boolean permissionRestrictWarps = false; @Comment("How long a player has to stand still and not take damage for when teleporting (in seconds) ") diff --git a/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java b/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java index 16e137f0..629fe2bb 100644 --- a/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java +++ b/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java @@ -100,12 +100,11 @@ public class WarpsManager { @NotNull public List getUsableWarps(@NotNull CommandUser user) { - if (!plugin.getSettings().getGeneral().isPermissionRestrictWarps() - || user.hasPermission(Warp.getWildcardPermission())) { + if (!plugin.getSettings().getGeneral().isPermissionRestrictWarps()) { return getWarps(); } return warps.stream() - .filter(warp -> user.hasPermission(warp.getPermission())) + .filter(warp -> warp.hasPermission(user)) .map(Warp::getName) .toList(); } diff --git a/common/src/main/java/net/william278/huskhomes/position/Warp.java b/common/src/main/java/net/william278/huskhomes/position/Warp.java index fe70181f..ad9f56ea 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Warp.java +++ b/common/src/main/java/net/william278/huskhomes/position/Warp.java @@ -19,6 +19,7 @@ package net.william278.huskhomes.position; +import net.william278.huskhomes.user.CommandUser; import org.jetbrains.annotations.NotNull; import java.util.UUID; @@ -28,7 +29,8 @@ import java.util.UUID; */ public class Warp extends SavedPosition { - private static final String PERMISSION_PREFIX = "huskhomes.command.warp."; + private static final String USE_PERMISSION_FORMAT = "huskhomes.warp.%s"; + private static final String USE_PERMISSION_WILDCARD = "huskhomes.warp.*"; private Warp(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server, @NotNull PositionMeta positionMeta, @NotNull UUID uuid) { @@ -67,17 +69,21 @@ public class Warp extends SavedPosition { @NotNull public static String getPermission(@NotNull String warpName) { - return PERMISSION_PREFIX + warpName.toLowerCase(); - } - - @NotNull - public String getPermission() { - return getPermission(getName()); + return USE_PERMISSION_FORMAT.formatted(warpName); } @NotNull public static String getWildcardPermission() { - return PERMISSION_PREFIX + "*"; + return USE_PERMISSION_WILDCARD; + } + + @NotNull + public String getPermission() { + return Warp.getPermission(getName()); + } + + public boolean hasPermission(@NotNull CommandUser user) { + return user.hasPermission(Warp.getWildcardPermission()) || user.hasPermission(getPermission()); } } diff --git a/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java b/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java index 501ef86b..a9ee9314 100644 --- a/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java +++ b/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java @@ -32,7 +32,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.util.List; -import java.util.UUID; import java.util.stream.Stream; @DisplayName("Message Serialization Tests") diff --git a/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java b/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java index 0cafc0df..aee1d0fe 100644 --- a/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java +++ b/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java @@ -31,7 +31,6 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.UUID; @DisplayName("Normal Distribution Tests") public class NormalDistributionTests { diff --git a/docs/Commands.md b/docs/Commands.md index 7bea7a66..9b308aa7 100644 --- a/docs/Commands.md +++ b/docs/Commands.md @@ -433,7 +433,7 @@ This is a table of HuskHomes commands, how to use them, and their required permi ### Notes -† — If [Permission Restricted Warps](restricted-warps) are in use, the `huskhomes.command.warp.(warp_name)` permission node will also be required to be able to view and use warps. +† — If [Permission Restricted Warps](restricted-warps) are in use, the `huskhomes.warp.` permission node will also be required to be able to list, use, edit and delete (but not set) warps. The `huskhomes.warp.*` wildcard permission grants the ability to access all warps. ‡ — Requires `return_by_death` to be enabled in [`config.yml`](config-files). diff --git a/docs/Config-Files.md b/docs/Config-Files.md index 86eb7a64..ad05ab32 100644 --- a/docs/Config-Files.md +++ b/docs/Config-Files.md @@ -61,7 +61,7 @@ general: max_public_homes: 10 # Whether permission limits (i.e. huskhomes.max_homes.) should stack if the user inherits multiple nodes. stack_permission_limits: false - # Whether users require a permission (huskhomes.command.warp.) to use warps + # Whether users require a permission (huskhomes.warp.) to use warps permission_restrict_warps: false # How long a player has to stand still and not take damage for when teleporting (in seconds) teleport_warmup_time: 5 diff --git a/docs/Restricted-Warps.md b/docs/Restricted-Warps.md index 18cb8d40..98da5c4f 100644 --- a/docs/Restricted-Warps.md +++ b/docs/Restricted-Warps.md @@ -1,3 +1,28 @@ -HuskHomes supports restricting warps behind permission nodes. This means that players require a corresponding `huskhomes.command.warp.` node to view and use warps, else they will see an error message. +HuskHomes supports restricting warps behind permission nodes through the **Permission-Restrict Warps** feature. -To enable permission-restricted warps, set `permission_restrict_warps` to `true` under `general`. Don't forget to grant users the necessary permission nodes (`huskhomes.command.warp.`). \ No newline at end of file +## Permissions +With `permission_restrict_warps` enabled in [`config.yml`](config-files), players are required to have a corresponding `huskhomes.warp.` node. Note that this is a _different permission_ from the one required to use the `/warp` command, which is `huskhomes.command.warp`. + +With the `huskhomes.warp.` permission node, where `` is the name of the warp, users can: + +* Teleport to the warp with `/warp ` +* View the warp in the `/warplist`, or TAB-complete the warp +* Delete, or edit warps with that name, if they have access to the respective [[commands]] + +If users have permission to set warps, they will still be able to set warps with names they do not have permission for, but be advised they will not be able to edit or delete the warp once set without permission. If you're using permission restricted warps, we therefore recommend also granting the wildcard warp permission (see below). + +Note that the name of the warp is case-sensitive (i.e. the `huskhomes.warp.Home` permission node is different to the `huskhomes.warp.home` permission node). + +Users with the `huskhomes.warp.*` permission will be able to use all warps. + +## Configuring +To enable permission-restricted warps, set `permission_restrict_warps` to `true` under `general`. Don't forget to grant users the necessary permission nodes (`huskhomes.warp.`). + +
+Permission Restrict Warps – config.yml + +```yaml + # Whether users require a permission (huskhomes.warp.) to use warps + permission_restrict_warps: false +``` +
\ No newline at end of file