Added AdminBlueprintDeleteCommand

Implements https://github.com/BentoBoxWorld/BentoBox/issues/1048

Also added BlueprintsManager#deleteBlueprint(GameModeAddon, String)
Fixed a code smell in AdminBlueprintSaveCommand
This commit is contained in:
Florian CUNY 2019-12-01 11:54:23 +01:00
parent 7c3e81cc5e
commit 13fab3173e
5 changed files with 87 additions and 1 deletions

View File

@ -46,6 +46,7 @@ public class AdminBlueprintCommand extends ConfirmableCommand {
new AdminBlueprintOriginCommand(this);
new AdminBlueprintCopyCommand(this);
new AdminBlueprintSaveCommand(this);
new AdminBlueprintDeleteCommand(this);
new AdminBlueprintPos1Command(this);
new AdminBlueprintPos2Command(this);
new AdminBlueprintListCommand(this);

View File

@ -0,0 +1,55 @@
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 java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
/**
* Command that deletes a Blueprint.
* @author Poslovitch
* @since 1.9.0
*/
public class AdminBlueprintDeleteCommand extends ConfirmableCommand {
public AdminBlueprintDeleteCommand(AdminBlueprintCommand parent) {
super(parent, "delete", "remove");
}
@Override
public void setup() {
setParametersHelp("commands.admin.blueprint.delete.parameters");
setDescription("commands.admin.blueprint.delete.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.size() != 1) {
showHelp(this, user);
return false;
}
String blueprintName = args.get(0).toLowerCase(Locale.ENGLISH);
// Check if blueprint exist
if (getPlugin().getBlueprintsManager().getBlueprints(getAddon()).containsKey(blueprintName)) {
askConfirmation(user, user.getTranslation("commands.admin.blueprint.delete.confirmation"), () -> {
getPlugin().getBlueprintsManager().deleteBlueprint(getAddon(), blueprintName);
user.sendMessage("commands.admin.blueprint.delete.success", TextVariables.NAME, blueprintName);
});
return true;
} else {
user.sendMessage("commands.admin.blueprint.delete.no-blueprint", TextVariables.NAME, blueprintName);
return false;
}
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
return Optional.of(new LinkedList<>(getPlugin().getBlueprintsManager().getBlueprints(getAddon()).keySet()));
}
}

View File

@ -56,7 +56,7 @@ public class AdminBlueprintSaveCommand extends ConfirmableCommand {
parent.hideClipboard(user);
boolean result = new BlueprintClipboardManager(getPlugin(), parent.getBlueprintsFolder(), clipboard).save(user, name);
if (result) {
getPlugin().getBlueprintsManager().addBlueprint((GameModeAddon)getAddon(), clipboard.getBlueprint());
getPlugin().getBlueprintsManager().addBlueprint(getAddon(), clipboard.getBlueprint());
}
return result;
}

View File

@ -392,6 +392,28 @@ public class BlueprintsManager {
return blueprints.get(addon).stream().collect(Collectors.toMap(Blueprint::getName, b -> b));
}
/**
* Unregisters the Blueprint from the manager and deletes the file.
* @param addon game mode addon
* @param name name of the Blueprint to delete
* @since 1.9.0
*/
public void deleteBlueprint(GameModeAddon addon, String name) {
List<Blueprint> addonBlueprints = blueprints.get(addon);
addonBlueprints.stream().filter(b -> b.getName().equals(name)).forEach(b -> {
addonBlueprints.remove(b);
blueprints.put(addon, addonBlueprints);
File file = new File(getBlueprintsFolder(addon), name + BLUEPRINT_SUFFIX);
// Delete the file
try {
Files.deleteIfExists(file.toPath());
} catch (IOException e) {
plugin.logError("Could not delete Blueprint " + e.getLocalizedMessage());
}
});
}
/**
* Paste the islands to world
*

View File

@ -255,6 +255,14 @@ commands:
copy:
parameters: "[air]"
description: "copy the clipboard set by pos1 and pos2 and optionally the air blocks"
delete:
parameters: "<name>"
description: "delete the blueprint"
no-blueprint: "&b [name] &c does not exist."
confirmation: |
&c Are you sure you want to delete this blueprint?
&c Once deleted, there is no way to recover it.
success: "&a Successfully deleted blueprint &b [name]&a ."
load:
parameters: "<name>"
description: "load blueprint into the clipboard"