Added AdminBlueprintRenameCommand

Implements https://github.com/BentoBoxWorld/BentoBox/issues/1082
This commit is contained in:
Florian CUNY 2020-01-02 16:42:33 +01:00
parent fab6b295b6
commit 85b4c4ff4a
3 changed files with 78 additions and 0 deletions

View File

@ -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);

View File

@ -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<String> 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());
}
}

View File

@ -289,6 +289,11 @@ commands:
save:
parameters: "<blueprint name>"
description: "save the copied clipboard"
rename:
parameters: "<blueprint name> <new name>"
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"