From 65cc90e79eb396ff70d8bac292238d9c255a5d33 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 31 Mar 2024 16:36:14 +0100 Subject: [PATCH] refactor: add more edithome/warp perms --- .../huskhomes/command/EditHomeCommand.java | 13 +--- .../huskhomes/command/EditWarpCommand.java | 7 +-- .../command/SavedPositionCommand.java | 62 +++++++++++-------- docs/Commands.md | 13 ++-- 4 files changed, 50 insertions(+), 45 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java index 077b79cf..3b25a8fa 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java @@ -30,17 +30,13 @@ import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.regex.Pattern; public class EditHomeCommand extends SavedPositionCommand { public EditHomeCommand(@NotNull HuskHomes plugin) { super("edithome", List.of(), Home.class, List.of("rename", "description", "relocate", "privacy"), plugin); - addAdditionalPermissions(arguments.stream().collect(HashMap::new, (m, e) -> m.put(e, false), HashMap::putAll)); } @Override @@ -60,14 +56,11 @@ public void execute(@NotNull CommandUser executor, @NotNull Home home, @NotNull .forEach(executor::sendMessage); return; } - - if (!arguments.contains(operation.get().toLowerCase())) { - plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) - .ifPresent(executor::sendMessage); + if (isInvalidOperation(operation.get(), executor)) { return; } - switch (operation.get().toLowerCase()) { + switch (operation.get().toLowerCase(Locale.ENGLISH)) { case "rename" -> setHomeName(executor, home, ownerEditing, args); case "description" -> setHomeDescription(executor, home, ownerEditing, args); case "relocate" -> setHomePosition(executor, home, ownerEditing); diff --git a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java index 3f41b722..64fbf79d 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java @@ -30,7 +30,6 @@ import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.regex.Pattern; @@ -40,7 +39,6 @@ public class EditWarpCommand extends SavedPositionCommand { public EditWarpCommand(@NotNull HuskHomes plugin) { super("editwarp", List.of(), Warp.class, List.of("rename", "description", "relocate"), plugin); setOperatorCommand(true); - addAdditionalPermissions(arguments.stream().collect(HashMap::new, (m, e) -> m.put(e, false), HashMap::putAll)); } @Override @@ -50,10 +48,7 @@ public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull getWarpEditorWindow(warp).forEach(executor::sendMessage); return; } - - if (!arguments.contains(operation.get().toLowerCase())) { - plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) - .ifPresent(executor::sendMessage); + if (isInvalidOperation(operation.get(), executor)) { return; } diff --git a/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java b/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java index c7e05618..588a30e5 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java @@ -31,10 +31,7 @@ import net.william278.huskhomes.util.TransactionResolver; import org.jetbrains.annotations.NotNull; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; +import java.util.*; public abstract class SavedPositionCommand extends Command implements TabProvider { @@ -48,6 +45,7 @@ protected SavedPositionCommand(@NotNull String name, @NotNull List alias this.arguments = arguments; addAdditionalPermissions(Map.of("other", true)); + addAdditionalPermissions(arguments.stream().collect(HashMap::new, (m, s) -> m.put(s, false), HashMap::putAll)); } @NotNull @@ -171,38 +169,52 @@ protected void teleport(@NotNull CommandUser executor, @NotNull Teleportable tel .buildAndComplete(executor.equals(teleporter), teleporter.getUsername()); } + protected boolean isInvalidOperation(String operation, CommandUser executor) { + if (!arguments.contains(operation.toLowerCase())) { + plugin.getLocales().getLocale("error_invalid_syntax", getUsage()) + .ifPresent(executor::sendMessage); + return true; + } + if (!executor.hasPermission(getPermission(operation.toLowerCase(Locale.ENGLISH)))) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(executor::sendMessage); + return true; + } + return false; + } + @NotNull - private static String formatUsage(List arguments) { + private static String formatUsage(Collection arguments) { return ((!arguments.isEmpty()) ? " [" + String.join("|", arguments) + "]" : ""); } @Override @NotNull public List suggest(@NotNull CommandUser executor, @NotNull String[] args) { - if (positionType == Home.class) { - return switch (args.length) { - case 0, 1 -> { - if (args.length == 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) { - if (executor.hasPermission(getOtherPermission())) { - yield filter(plugin.getManager().homes().getUserHomeIdentifiers(), args); - } - yield filter(plugin.getManager().homes().getUserHomeIdentifiers(), args); - } - if (executor instanceof OnlineUser user) { - yield filter(plugin.getManager().homes().getUserHomes().get(user.getUsername()), args); - } - yield filter(plugin.getManager().homes().getUserHomeIdentifiers(), args); - } - case 2 -> filter(arguments.stream().toList(), args); - default -> List.of(); - }; - } else { + if (positionType != Home.class) { return switch (args.length) { - case 0, 1 -> filter(plugin.getManager().warps().getUsableWarps(executor), args); - case 2 -> filter(arguments.stream().toList(), args); + case 0, 1 -> plugin.getManager().warps().getUsableWarps(executor); + case 2 -> arguments.stream().filter(a -> executor.hasPermission(getPermission(a))).toList(); default -> List.of(); }; } + + return switch (args.length) { + case 0, 1 -> { + if (args.length == 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) { + if (executor.hasPermission(getOtherPermission())) { + yield plugin.getManager().homes().getUserHomeIdentifiers(); + } + yield plugin.getManager().homes().getUserHomeIdentifiers(); + } + if (executor instanceof OnlineUser user) { + yield plugin.getManager().homes().getUserHomes().get(user.getUsername()); + } + yield plugin.getManager().homes().getUserHomeIdentifiers(); + } + case 2 -> arguments.stream().filter(a -> executor.hasPermission(getPermission(a))).toList(); + default -> List.of(); + }; } } diff --git a/docs/Commands.md b/docs/Commands.md index 2c1b6da3..2b4c67c2 100644 --- a/docs/Commands.md +++ b/docs/Commands.md @@ -85,26 +85,28 @@ This is a table of HuskHomes commands, how to use them, and their required permi /edithome /edithome <name> View and edit information about a home - huskhomes.command.edithome - ✅ + huskhomes.command.edithome + ✅ /edithome <name> rename <new_name> Rename a home + huskhomes.command.edithome.rename /edithome <name> description <text> Set a home's description + huskhomes.command.edithome.description /edithome <name> relocate Move a home to your current position + huskhomes.command.edithome.relocate /edithome <name> privacy [public|private] Set a home's privacy (make it public or private) huskhomes.command.edithome.privacy - ✅ /edithome <owner_name.home_name> [args] @@ -172,20 +174,23 @@ This is a table of HuskHomes commands, how to use them, and their required permi /editwarp /editwarp <name> View and edit information about a warp - huskhomes.command.editwarp + huskhomes.command.editwarp/editwarp <name> rename <new_name> Rename a warp + huskhomes.command.editwarp.rename /editwarp <name> description <text> Set a warp's description + huskhomes.command.editwarp.description /editwarp <name> relocate Move a warp to your current position + huskhomes.command.editwarp.relocate Spawn commands