From 85b4c4ff4acffd6d63a3b86c43044e970926adda Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Thu, 2 Jan 2020 16:42:33 +0100 Subject: [PATCH] Added AdminBlueprintRenameCommand Implements https://github.com/BentoBoxWorld/BentoBox/issues/1082 --- .../blueprints/AdminBlueprintCommand.java | 1 + .../AdminBlueprintRenameCommand.java | 72 +++++++++++++++++++ src/main/resources/locales/en-US.yml | 5 ++ 3 files changed, 78 insertions(+) create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintRenameCommand.java diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java index 107652366..97cef4cd8 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java @@ -46,6 +46,7 @@ public class AdminBlueprintCommand extends ConfirmableCommand { new AdminBlueprintOriginCommand(this); new AdminBlueprintCopyCommand(this); new AdminBlueprintSaveCommand(this); + new AdminBlueprintRenameCommand(this); new AdminBlueprintDeleteCommand(this); new AdminBlueprintPos1Command(this); new AdminBlueprintPos2Command(this); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintRenameCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintRenameCommand.java new file mode 100644 index 000000000..fdfd16926 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintRenameCommand.java @@ -0,0 +1,72 @@ +package world.bentobox.bentobox.api.commands.admin.blueprints; + +import world.bentobox.bentobox.api.commands.ConfirmableCommand; +import world.bentobox.bentobox.api.localization.TextVariables; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.blueprints.Blueprint; +import world.bentobox.bentobox.managers.BlueprintsManager; + +import java.io.File; +import java.util.List; +import java.util.Locale; + +/** + * Renames an existing blueprint. + * @author Poslovitch + * @since 1.10.0 + */ +public class AdminBlueprintRenameCommand extends ConfirmableCommand { + + public AdminBlueprintRenameCommand(AdminBlueprintCommand parent) { + super(parent, "rename"); + } + + @Override + public void setup() { + setParametersHelp("commands.admin.blueprint.rename.parameters"); + setDescription("commands.admin.blueprint.rename.description"); + } + + @Override + public boolean execute(User user, String label, List args) { + if (args.size() != 2) { + showHelp(this, user); + return false; + } + + AdminBlueprintCommand parent = (AdminBlueprintCommand) getParent(); + + // Check if the names are the same + String from = args.get(0).toLowerCase(Locale.ENGLISH); + String to = args.get(1).toLowerCase(Locale.ENGLISH); + + if (from.equals(to)) { + user.sendMessage("commands.admin.blueprint.rename.pick-different-name"); + return false; + } + + // Check if the 'from' file exists + File fromFile = new File(parent.getBlueprintsFolder(), from + BlueprintsManager.BLUEPRINT_SUFFIX); + if (!fromFile.exists()) { + user.sendMessage("commands.admin.blueprint.no-such-file"); + return false; + } + + // Check if the 'to' file exists + + File toFile = new File(parent.getBlueprintsFolder(), to + BlueprintsManager.BLUEPRINT_SUFFIX); + if (toFile.exists()) { + // Ask for confirmation to overwrite + askConfirmation(user, user.getTranslation("commands.admin.blueprint.file-exists"), () -> rename(user, from, to)); + } else { + askConfirmation(user, () -> rename(user, from, to)); + } + return true; + } + + private void rename(User user, String blueprintName, String newName) { + Blueprint blueprint = getPlugin().getBlueprintsManager().getBlueprints(getAddon()).get(blueprintName); + getPlugin().getBlueprintsManager().renameBlueprint(getAddon(), blueprint, newName); + user.sendMessage("commands.admin.blueprint.rename.success", "[old]", blueprintName, TextVariables.NAME, blueprint.getName()); + } +} diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index c78edf760..8d5e3d415 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -289,6 +289,11 @@ commands: save: parameters: "" description: "save the copied clipboard" + rename: + parameters: " " + description: "rename a blueprint" + success: "&a Blueprint &b [old] &a has been successfully renamed to &b [name]&a." + pick-different-name: "&c Please specify a name that is different from the blueprint's current name." management: back: "Back" instruction: "Click on blueprint then click here"