From 629188a6c230b270dd36ef0f82c3d9dc1a479aef Mon Sep 17 00:00:00 2001 From: William Date: Wed, 17 Aug 2022 23:09:04 +0100 Subject: [PATCH] Add editwarp and associated locales --- .../huskhomes/command/BukkitCommandType.java | 1 + bukkit/src/main/resources/plugin.yml | 2 + .../huskhomes/command/EditWarpCommand.java | 220 ++++++++++++++++++ .../william278/huskhomes/config/Locales.java | 9 + common/src/main/resources/locales/en-gb.yml | 32 ++- 5 files changed, 253 insertions(+), 11 deletions(-) create mode 100644 common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java index 3d047d14..9b2d107b 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java @@ -19,6 +19,7 @@ public enum BukkitCommandType { SET_WARP_COMMAND(new SetWarpCommand(BukkitHuskHomes.getInstance())), WARP_LIST_COMMAND(new WarpListCommand(BukkitHuskHomes.getInstance())), DEL_WARP_COMMAND(new DelWarpCommand(BukkitHuskHomes.getInstance())), + EDIT_WARP_COMMAND(new EditWarpCommand(BukkitHuskHomes.getInstance())), BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())); public final CommandBase commandBase; diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 544372db..befb7600 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -33,5 +33,7 @@ commands: usage: /warplist [page] delwarp: usage: /delwarp + editwarp: + usage: /editwarp [rename|description|relocate] [args] back: usage: /back \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java new file mode 100644 index 00000000..81f8abde --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java @@ -0,0 +1,220 @@ +package net.william278.huskhomes.command; + +import de.themoep.minedown.MineDown; +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.position.Warp; +import net.william278.huskhomes.position.PositionMeta; +import net.william278.huskhomes.util.Permission; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +public class EditWarpCommand extends CommandBase implements TabCompletable, ConsoleExecutable { + + private final String[] EDIT_WARP_COMPLETIONS = {"rename", "description", "relocate"}; + + public EditWarpCommand(@NotNull HuskHomes implementor) { + super("editwarp", Permission.COMMAND_EDIT_WARP, implementor); + } + + @Override + public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + if (args.length >= 1) { + final String warpName = args[0]; + final String editOperation = args.length >= 2 ? args[1] : null; + final String editArgs = getEditArguments(args); + + plugin.getDatabase().getWarp(warpName).thenAccept(optionalWarp -> { + if (optionalWarp.isEmpty()) { + plugin.getLocales().getLocale("error_warp_invalid", warpName) + .ifPresent(onlineUser::sendMessage); + return; + } + editWarp(optionalWarp.get(), onlineUser, editOperation, editArgs); + }); + } else { + plugin.getLocales().getLocale("error_invalid_syntax", + "/editwarp [" + String.join("|", EDIT_WARP_COMPLETIONS) + "] [args]") + .ifPresent(onlineUser::sendMessage); + } + } + + /** + * Perform the specified EditOperation on the specified warp + * + * @param warp The warp to edit + * @param editor The player who is editing the warp + * @param editOperation The edit operation to perform + * @param editArgs Arguments for the edit operation + */ + private void editWarp(@NotNull Warp warp, @NotNull OnlineUser editor, + @Nullable String editOperation, @Nullable String editArgs) { + final AtomicBoolean showMenuFlag = new AtomicBoolean(false); + + if (editOperation == null) { + getWarpEditorWindow(warp, true, editor.hasPermission(Permission.COMMAND_WARP.node)) + .forEach(editor::sendMessage); + return; + } + if (editArgs != null) { + String argToCheck = editArgs; + if (editArgs.contains(Pattern.quote(" "))) { + argToCheck = editArgs.split(Pattern.quote(" "))[0]; + } + if (argToCheck.equals("-m")) { + showMenuFlag.set(true); + editArgs = editArgs.replaceFirst("-m", ""); + } + } + + switch (editOperation.toLowerCase()) { + case "rename" -> { + if (editArgs == null || editArgs.contains(Pattern.quote(" "))) { + plugin.getLocales().getLocale("error_invalid_syntax", + "/editwarp rename ") + .ifPresent(editor::sendMessage); + return; + } + + final String oldWarpName = warp.meta.name; + final String newWarpName = editArgs; + plugin.getSavedPositionManager().updateWarpMeta(warp, new PositionMeta(newWarpName, warp.meta.description)) + .thenAccept(renameResult -> (switch (renameResult.resultType()) { + case SUCCESS -> + plugin.getLocales().getLocale("edit_warp_update_name", oldWarpName, newWarpName); + case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_warp_name_taken"); + case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_warp_name_length"); + case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_warp_name_characters"); + default -> Optional.of(new MineDown("")); + }).ifPresent(editor::sendMessage)); + } + case "description" -> { + if (editArgs == null) { + plugin.getLocales().getLocale("error_invalid_syntax", + "/editwarp description ") + .ifPresent(editor::sendMessage); + return; + } + + final String oldWarpDescription = warp.meta.description; + final String newDescription = editArgs; + + plugin.getSavedPositionManager().updateWarpMeta(warp, new PositionMeta(warp.meta.name, newDescription)) + .thenAccept(descriptionUpdateResult -> (switch (descriptionUpdateResult.resultType()) { + case SUCCESS -> plugin.getLocales().getLocale("edit_warp_update_description", + warp.meta.name, oldWarpDescription, newDescription); + case FAILED_DESCRIPTION_LENGTH -> + plugin.getLocales().getLocale("error_warp_description_length"); + case FAILED_DESCRIPTION_CHARACTERS -> + plugin.getLocales().getLocale("error_warp_description_characters"); + default -> Optional.of(new MineDown("")); + }).ifPresent(editor::sendMessage)); + } + case "relocate" -> editor.getPosition().thenAccept(position -> + plugin.getSavedPositionManager().updateWarpPosition(warp, position).thenRun(() -> { + editor.sendMessage(plugin.getLocales().getLocale("edit_warp_update_location", + warp.meta.name).orElse(new MineDown(""))); + + // Show the menu if the menu flag is set + if (showMenuFlag.get()) { + getWarpEditorWindow(warp, false, editor.hasPermission(Permission.COMMAND_WARP.node)) + .forEach(editor::sendMessage); + } + })); + default -> plugin.getLocales().getLocale("error_invalid_syntax", + "/editwarp [" + String.join("|", EDIT_WARP_COMPLETIONS) + "] [args]") + .ifPresent(editor::sendMessage); + } + } + + @Nullable + private String getEditArguments(@NotNull String[] args) { + if (args.length > 2) { + final StringJoiner joiner = new StringJoiner(" "); + for (int i = 2; i < args.length; i++) { + joiner.add(args[i]); + } + return joiner.toString(); + } + return null; + } + + /** + * Get a formatted warp editor chat window for a supplied {@link Warp} + * + * @param warp The warp to display + * @param showTitle Whether to show the menu title + * @param showTeleportButton Whether to show the teleport "use" button + * @return List of {@link MineDown} messages to send to the editor that form the menu + */ + @NotNull + private List getWarpEditorWindow(@NotNull Warp warp, final boolean showTitle, + final boolean showTeleportButton) { + return new ArrayList<>() {{ + if (showTitle) { + plugin.getLocales().getLocale("edit_warp_menu_title", warp.meta.name) + .ifPresent(this::add); + } + + plugin.getLocales().getLocale("edit_warp_menu_metadata", + DateTimeFormatter.ofPattern("MMM dd yyyy, HH:mm") + .format(warp.meta.creationTime.atZone(ZoneId.systemDefault())), + warp.uuid.toString().split(Pattern.quote("-"))[0], + warp.uuid.toString()) + .ifPresent(this::add); + + if (warp.meta.description.length() > 0) { + plugin.getLocales().getLocale("edit_warp_menu_description", + warp.meta.description.length() > 50 + ? warp.meta.description.substring(0, 49).trim() + "…" : warp.meta.description, + String.join("\n", warp.meta.description.split("(?<=\\G.{40,}\\s)"))) + .ifPresent(this::add); + } + + if (!plugin.getSettings().crossServer) { + plugin.getLocales().getLocale("edit_warp_menu_world", warp.world.name).ifPresent(this::add); + } else { + plugin.getLocales().getLocale("edit_warp_menu_world_server", warp.world.name, warp.server.name).ifPresent(this::add); + } + + plugin.getLocales().getLocale("edit_warp_menu_coordinates", + String.format("%.1f", warp.x), String.format("%.1f", warp.y), String.format("%.1f", warp.z), + String.format("%.2f", warp.yaw), String.format("%.2f", warp.pitch)) + .ifPresent(this::add); + + if (showTeleportButton) { + plugin.getLocales().getLocale("edit_warp_menu_use_buttons", warp.meta.name) + .ifPresent(this::add); + } + plugin.getLocales().getLocale("edit_warp_menu_manage_buttons", warp.meta.name) + .ifPresent(this::add); + plugin.getLocales().getLocale("edit_warp_menu_meta_edit_buttons", warp.meta.name) + .ifPresent(this::add); + }}; + } + + @Override + public void onConsoleExecute(@NotNull String[] args) { + //todo + } + + @Override + public @NotNull List onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + return switch (args.length) { + case 0, 1 -> plugin.getCache().warps.stream() + .filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : "")) + .sorted().collect(Collectors.toList()); + case 2 -> Arrays.stream(EDIT_WARP_COMPLETIONS) + .filter(s -> s.toLowerCase().startsWith(args[1].toLowerCase())) + .sorted().collect(Collectors.toList()); + default -> Collections.emptyList(); + }; + } +} diff --git a/common/src/main/java/net/william278/huskhomes/config/Locales.java b/common/src/main/java/net/william278/huskhomes/config/Locales.java index 57cda6cd..d5e475e9 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Locales.java +++ b/common/src/main/java/net/william278/huskhomes/config/Locales.java @@ -93,6 +93,15 @@ public class Locales { return rawLocale; } + /** + * Escape a string from {@link MineDown} formatting for use in a MineDown-formatted locale + *

+ * Although MineDown provides {@link MineDown#escape(String)}, that method fails to escape events + * properly when using the escaped string in a replacement, so this is used instead + * + * @param string The string to escape + * @return The escaped string + */ @NotNull private static String escapeMineDown(@NotNull String string) { final StringBuilder value = new StringBuilder(); diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index f3a2aa4a..8dc41fb9 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -7,11 +7,8 @@ teleporting_complete: '[Teleportation complete!](#00fb9a)' teleporting_action_bar_countdown: '[Teleporting in](#00fb9a) [%1%](#00fb9a bold)[…](#00fb9a)' teleporting_action_bar_cancelled: '[Teleportation cancelled!](#ff7e5e)' teleporting_please_stand_still: '[Teleportation will be cancelled if you move or take damage.](gray)' -home_deleted: '[Successfully deleted your home,](#00fb9a) [%1%](#00fb9a bold)' -home_deleted_other: '[Successfully deleted %1%''s home,](#00fb9a) [%2%](#00fb9a bold)' -warp_deleted: '[Successfully deleted the warp,](#00fb9a) [%1%](#00fb9a bold)' -edit_home_menu_title: '[Viewing home details for](#00fb9a) [%1%](#00fb9a bold)[:](#00fb9a)' +edit_home_menu_title: '[Viewing details for your home,](#00fb9a) [%1%](#00fb9a bold)[:](#00fb9a)' edit_home_menu_title_other: '[Viewing details for %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[:](#00fb9a)' edit_home_menu_metadata_private: '[⌚ %1%](#999999-#ababab show_text=&7Set Timestamp:\n&8When the home was set) [⚡ %2%](#ababab-#c7c7c7 show_text=&7Home ID:\n&8%3%) [● Private Home](#ff9f0f show_text=&#ff9f0f&Privacy:\n&8The home is private to the owner)' edit_home_menu_metadata_public: '[⌚ %1%](#999999-#ababab show_text=&7Set Timestamp:\n&8When the home was set) [⚡ %2%](#ababab-#c7c7c7 show_text=&7Home ID:\n&8%3%) [☀ Public Home](#6fff00 show_text=fff00&Privacy:\n&8Anyone can teleport to the home)' @@ -24,20 +21,29 @@ edit_home_menu_manage_buttons: '[Manage:](gray) [[❌ Delete…]](#ff3300 show_t edit_home_menu_meta_edit_buttons: '[Edit:](gray) [[✎ Rename…]](#c160ff-#bb50ff show_text=&7Click to suggest the rename command suggest_command=/huskhomes:edithome %1% rename ) [[ℹ Description…]](#bb50ff-#b43fff show_text=&7Click to suggest the edit description command suggest_command=/huskhomes:edithome %1% description )' edit_home_privacy_public_success: '[Successfully made your home,](#00fb9a) [%1%](#00fb9a bold)[, open to the public.](#00fb9a)' -edit_home_privacy_private_success: '[Successfully made your home,](#00fb9a) [%1%](#00fb9a bold)[, private.](#00fb9a)' edit_home_privacy_public_success_other: '[Successfully made %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[, open to the public.](#00fb9a)' +edit_home_privacy_private_success: '[Successfully made your home,](#00fb9a) [%1%](#00fb9a bold)[, private.](#00fb9a)' edit_home_privacy_private_success_other: '[Successfully made %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[, private.](#00fb9a)' -edit_home_update_location: '[Re-located](#00fb9a) [%1%](#00fb9a bold) [to your current position.](#00fb9a)' +edit_home_update_location: '[Re-located your home](#00fb9a) [%1%](#00fb9a bold) [to your current position.](#00fb9a)' edit_home_update_location_other: '[Re-located %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[, to your current position.](#00fb9a)' edit_home_update_description: '[Successfully changed the description of your home, %1%:](#00fb9a) \n[←](white) [Old:](#00fb9a) ["%2%"](gray)\n[→](white) [New:](#00fb9a) ["%3%" ](gray)' edit_home_update_description_other: '[Successfully changed the description of %1%''s home, %2%:](#00fb9a) \n[←](white) [Old:](#00fb9a) ["%3%"](gray)\n[→](white) [New:](#00fb9a) ["%4%" ](gray)' -edit_home_update_name: '[Successfully changed your home''s name: %1% →](#00fb9a) [%2%](#00fb9a bold)' +edit_home_update_name: '[Successfully renamed your home: %1% →](#00fb9a) [%2%](#00fb9a bold)' edit_home_update_name_other: '[Successfully renamed %1%''s home: %2% →](#00fb9a) [%3%](#00fb9a bold)' -edit_warp_title: '\n[Edit details for the warp](#00fb9a) [%1%](#00fb9a bold)[:](#00fb9a)' -edit_warp_options: '\n[[Teleport]](green show_text=&7Teleport to this warp run_command=/huskhomes:warp %1%) [[Delete]](red show_text=&7Delete this warp run_command=/huskhomes:delwarp %1%) [[Relocate]](blue show_text=&7Update this warp''s location run_command=/huskhomes:editwarp %1% location)\n[[Rename]](yellow show_text=&7Change the name of this warp suggest_command=/huskhomes:editwarp %1% rename ) [[Edit Description]](gold show_text=&7Update this warp''s description suggest_command=/huskhomes:editwarp %1% description )\n' + +edit_warp_menu_title: '[Viewing details for the warp,](#00fb9a) [%1%](#00fb9a bold)[:](#00fb9a)' +edit_home_menu_metadata: '[⌚ %1%](#999999-#ababab show_text=&7Set Timestamp:\n&8When the warp was set) [⚡ %2%](#ababab-#c7c7c7 show_text=&7Warp ID:\n&8%3%)' +edit_warp_menu_description: '[ℹ %1%](#c160ff-#b43fff show_text=&#c160ff&Description:\n&8%2%)\n' +edit_warp_menu_world: '[⭘ %1%](#3b76ff-#5286ff show_text=b76ff&World:\n&8The world where the warp is set)' +edit_warp_menu_world_server: '[⭘ %1%](#3b76ff-#477fff show_text=b76ff&World:\n&8The world where the warp is set) [☁ %2%](#477fff-#5286ff show_text=b76ff&Server:\n&8The server where the warp is set)' +edit_warp_menu_coordinates: '[⚐ x: %1%, y: %2%, z: %3%](#ffc43b-#f5c962 show_text=&#ffc43b&Coordinates:\n&8Location of the warp in the world\n&8yaw: %4%, pitch: %5%)\n' +edit_warp_menu_use_buttons: '[Use:](gray) [[⏩ Teleport…]](#00fb9a show_text=&7Click to teleport to this home run_command=/huskhomes:warp %1%)' +edit_warp_menu_manage_buttons: '[Manage:](gray) [[❌ Delete…]](#ff3300 show_text=&7Click to delete this warp\n&#ff3300&⚠ This cannot be undone! run_command=/huskhomes:delwarp %1%) [[⚐ Relocate…]](#ffc43b-#f5c962 show_text=&7Click to relocate this warp to your position run_command=/huskhomes:editwarp %1% relocate -m)' +edit_warp_menu_meta_edit_buttons: '[Edit:](gray) [[✎ Rename…]](#c160ff-#bb50ff show_text=&7Click to suggest the rename command suggest_command=/huskhomes:editwarp %1% rename ) [[ℹ Description…]](#bb50ff-#b43fff show_text=&7Click to suggest the edit description command suggest_command=/huskhomes:editwarp %1% description )' + edit_warp_update_location: '[Re-located the warp](#00fb9a) [%1%](#00fb9a bold) [to your current position.](#00fb9a)' -edit_warp_update_description: '[Changed the description for the warp](#00fb9a) [%1%](#00fb9a bold) [to "%2%".](#00fb9a)' -edit_warp_update_name: '[Successfully changed a warp''s name: %1% →](#00fb9a) [%2%](#00fb9a bold)' +edit_warp_update_description: '[Successfully changed the description of the warp, %1%:](#00fb9a) \n[←](white) [Old:](#00fb9a) ["%2%"](gray)\n[→](white) [New:](#00fb9a) ["%3%" ](gray)' +edit_warp_update_name: '[Successfully renamed the warp: %1% →](#00fb9a) [%2%](#00fb9a bold)' home_list_page_title: '[%1%''s Homes:](#00fb9a) [(%2%-%3% of](#00fb9a) [%4%](#00fb9a bold)[)](#00fb9a)' public_home_list_page_title: '[☀ Public Homes:](#00fb9a) [(%1%-%2% of](#00fb9a) [%3%](#00fb9a bold)[)](#00fb9a)' @@ -59,6 +65,10 @@ list_page_jumper_current_page: '[%1%](#00fb9a)' list_page_jumper_separator: ' ' list_page_jumper_group_separator: '…' +home_deleted: '[Successfully deleted your home,](#00fb9a) [%1%](#00fb9a bold)' +home_deleted_other: '[Successfully deleted %1%''s home,](#00fb9a) [%2%](#00fb9a bold)' +warp_deleted: '[Successfully deleted the warp,](#00fb9a) [%1%](#00fb9a bold)' + set_home_spent_money: '[Successfully spent %1% to set an additional home!](gray)' set_home_used_free_slots: '[You''ve used all of your free set home slots (%1%); setting additional homes will cost %2% each.](gray)' set_home_success: '[Successfully set a new home called](#00fb9a) [%1%](#00fb9a bold)'