From ceafcf3f38033799608c9c60d5c37bbf3de1e5bf Mon Sep 17 00:00:00 2001 From: William Date: Tue, 23 Aug 2022 00:38:45 +0100 Subject: [PATCH] Show edithome privacy toggle buttons only if user has permission, close #157 --- .../huskhomes/command/EditHomeCommand.java | 36 +++++++++++-------- .../william278/huskhomes/config/Locales.java | 2 +- common/src/main/resources/locales/en-gb.yml | 3 +- 3 files changed, 25 insertions(+), 16 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 8addc8f7..13a6f888 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java @@ -2,6 +2,7 @@ package net.william278.huskhomes.command; import de.themoep.minedown.MineDown; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.position.Home; @@ -88,7 +89,8 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons if (editOperation == null) { getHomeEditorWindow(home, true, otherOwner, - !otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node)) + !otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node), + editor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node)) .forEach(editor::sendMessage); return; } @@ -178,7 +180,8 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons // Show the menu if the menu flag is set if (showMenuFlag.get()) { getHomeEditorWindow(home, false, otherOwner, - !otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node)) + !otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node), + editor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node)) .forEach(editor::sendMessage); } })); @@ -242,7 +245,8 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons // Show the menu if the menu flag is set if (showMenuFlag.get()) { getHomeEditorWindow(home, false, otherOwner, - !otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node)) + !otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node), + editor.hasPermission(Permission.COMMAND_EDIT_HOME_PRIVACY.node)) .forEach(editor::sendMessage); } } @@ -267,15 +271,16 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons /** * Get a formatted home editor chat window for a supplied {@link Home} * - * @param home The home to display - * @param showTitle Whether to show the menu title - * @param otherViewer If the viewer of the editor is not the homeowner - * @param showTeleportButton Whether to show the teleport "use" button + * @param home The home to display + * @param showTitle Whether to show the menu title + * @param otherViewer If the viewer of the editor is not the homeowner + * @param showTeleportButton Whether to show the teleport "use" button + * @param showPrivacyToggleButton Whether to show the home privacy toggle button * @return List of {@link MineDown} messages to send to the editor that form the menu */ @NotNull - private List getHomeEditorWindow(@NotNull Home home, final boolean showTitle, - final boolean otherViewer, final boolean showTeleportButton) { + private List getHomeEditorWindow(@NotNull Home home, final boolean showTitle, final boolean otherViewer, + final boolean showTeleportButton, final boolean showPrivacyToggleButton) { return new ArrayList<>() {{ if (showTitle) { if (!otherViewer) { @@ -303,9 +308,11 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons } if (!plugin.getSettings().crossServer) { - plugin.getLocales().getLocale("edit_home_menu_world", home.world.name).ifPresent(this::add); + plugin.getLocales().getLocale("edit_home_menu_world", home.world.name) + .ifPresent(this::add); } else { - plugin.getLocales().getLocale("edit_home_menu_world_server", home.world.name, home.server.name).ifPresent(this::add); + plugin.getLocales().getLocale("edit_home_menu_world_server", home.world.name, home.server.name) + .ifPresent(this::add); } plugin.getLocales().getLocale("edit_home_menu_coordinates", @@ -319,9 +326,10 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons formattedName) .ifPresent(this::add); } - plugin.getLocales().getLocale("edit_home_menu_manage_buttons", - formattedName) - .ifPresent(this::add); + plugin.getLocales().getRawLocale("edit_home_menu_manage_buttons", + Locales.escapeMineDown(formattedName), showPrivacyToggleButton ? plugin.getLocales() + .getRawLocale("edit_home_menu_toggle_privacy_button").orElse("") : "") + .map(MineDown::new).ifPresent(this::add); plugin.getLocales().getLocale("edit_home_menu_meta_edit_buttons", formattedName) .ifPresent(this::add); 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 d5e475e9..f302de64 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Locales.java +++ b/common/src/main/java/net/william278/huskhomes/config/Locales.java @@ -103,7 +103,7 @@ public class Locales { * @return The escaped string */ @NotNull - private static String escapeMineDown(@NotNull String string) { + public static String escapeMineDown(@NotNull String string) { final StringBuilder value = new StringBuilder(); for (int i = 0; i < string.length(); ++i) { char c = string.charAt(i); diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index 1d1b6dfa..ec214839 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -17,7 +17,8 @@ edit_home_menu_world: '[⭘ %1%](#3b76ff-#5286ff show_text=b76ff&World:\n&8Th edit_home_menu_world_server: '[⭘ %1%](#3b76ff-#477fff show_text=b76ff&World:\n&8The world where the home is set) [☁ %2%](#477fff-#5286ff show_text=b76ff&Server:\n&8The server where the home is set)' edit_home_menu_coordinates: '[⚐ x: %1%, y: %2%, z: %3%](#ffc43b-#f5c962 show_text=&#ffc43b&Coordinates:\n&8Location of the home in the world\n&8yaw: %4%, pitch: %5%)\n' edit_home_menu_use_buttons: '[Use:](gray) [[⏩ Teleport…]](#00fb9a show_text=&7Click to teleport to this home run_command=/huskhomes:home %1%)' -edit_home_menu_manage_buttons: '[Manage:](gray) [[❌ Delete…]](#ff3300 show_text=&7Click to delete this home\n&#ff3300&⚠ This cannot be undone! run_command=/huskhomes:delhome %1%) [[⚐ Relocate…]](#ffc43b-#f5c962 show_text=&7Click to relocate this home to your position run_command=/huskhomes:edithome %1% relocate -m) [[☀ Public/Private…]](#6fff00 show_text=&7Click to toggle the privacy of this home run_command=/huskhomes:edithome %1% privacy -m)' +edit_home_menu_manage_buttons: '[Manage:](gray) [[❌ Delete…]](#ff3300 show_text=&7Click to delete this home\n&#ff3300&⚠ This cannot be undone! run_command=/huskhomes:delhome %1%) [[⚐ Relocate…]](#ffc43b-#f5c962 show_text=&7Click to relocate this home to your position run_command=/huskhomes:edithome %1% relocate -m) %2%' +edit_home_menu_toggle_privacy_button: '[[☀ Public/Private…]](#6fff00 show_text=&7Click to toggle the privacy of this home run_command=/huskhomes:edithome %1% privacy -m)' 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)'