Add permission restricted warps

This commit is contained in:
William committed 2022-08-20 18:41:30 +01:00
1 parent d4f4d5b0b0
commit 18673f3936
6 files changed
+28 -6

No files matched your search

@@ -158,8 +158,11 @@ public class Cache {
@NotNull @NotNull
public MineDown getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales, public MineDown getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
@NotNull List<Warp> warps, final int itemsPerPage, final int page) { @NotNull List<Warp> warps, final boolean permissionRestrictWarps,
final PaginatedList warpList = PaginatedList.of(warps.stream().map(warp -> final int itemsPerPage, final int page) {
final PaginatedList warpList = PaginatedList.of(warps.stream()
.filter(warp -> !permissionRestrictWarps || onlineUser.hasPermission(warp.getPermissionNode()))
.map(warp ->
locales.getRawLocale("warp_list_item", warp.meta.name) locales.getRawLocale("warp_list_item", warp.meta.name)
.orElse(warp.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage) .orElse(warp.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage)
.setHeaderFormat(locales.getRawLocale("warp_list_page_title", .setHeaderFormat(locales.getRawLocale("warp_list_page_title",
@@ -23,7 +23,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
return; return;
} }
onlineUser.sendMessage(plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps, onlineUser.sendMessage(plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
plugin.getSettings().listItemsPerPage, 1)); plugin.getSettings().permissionRestrictWarps, plugin.getSettings().listItemsPerPage, 1));
}); });
case 1 -> { case 1 -> {
final String warpName = args[0]; final String warpName = args[0];
@@ -47,7 +47,7 @@ public class WarpListCommand extends CommandBase implements ConsoleExecutable {
return; return;
} }
onlineUser.sendMessage(plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps, onlineUser.sendMessage(plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
plugin.getSettings().listItemsPerPage, pageNumber)); plugin.getSettings().permissionRestrictWarps, plugin.getSettings().listItemsPerPage, pageNumber));
}); });
} }
@@ -85,6 +85,9 @@ public class Settings {
@KeyPath("general.stack_permission_limits") @KeyPath("general.stack_permission_limits")
public boolean stackPermissionLimits = true; public boolean stackPermissionLimits = true;
@KeyPath("general.permission_restrict_warps")
public boolean permissionRestrictWarps = false;
@KeyPath("general.teleport_warmup_time") @KeyPath("general.teleport_warmup_time")
public int teleportWarmupTime = 5; public int teleportWarmupTime = 5;
@@ -24,4 +24,9 @@ public class Warp extends SavedPosition {
public Warp(@NotNull Position position, @NotNull PositionMeta meta) { public Warp(@NotNull Position position, @NotNull PositionMeta meta) {
super(position, meta); super(position, meta);
} }
@NotNull
public String getPermissionNode() {
return "huskhomes.warp." + meta.name.toLowerCase();
}
} }
@@ -91,13 +91,24 @@ public class TeleportManager {
} }
/** /**
* Attempt to teleport a {@link OnlineUser} to a server {@link Warp} * Attempt to teleport a {@link OnlineUser} to a server {@link Warp}.
* <p>
* If permission restricted warps are enabled, the user will not be teleported if they lack the
* required permission node ({@code huskhomes.warp.[warp_name]})
* *
* @param onlineUser the {@link OnlineUser} to teleport * @param onlineUser the {@link OnlineUser} to teleport
* @param warp the {@link Warp} to teleport to * @param warp the {@link Warp} to teleport to
*/ */
public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull Warp warp) { public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull Warp warp) {
//todo permission restricted warps // Check against warp permission restrictions if enabled (huskhomes.warp.<warp_name>)
if (plugin.getSettings().permissionRestrictWarps) {
if (!onlineUser.hasPermission(warp.getPermissionNode())) {
plugin.getLocales().getLocale("error_permission_restricted_warp", warp.meta.name)
.ifPresent(onlineUser::sendMessage);
return;
}
}
timedTeleport(onlineUser, warp).thenAccept(result -> finishTeleport(onlineUser, result)); timedTeleport(onlineUser, warp).thenAccept(result -> finishTeleport(onlineUser, result));
} }