diff --git a/Core/src/main/java/com/plotsquared/core/command/Backup.java b/Core/src/main/java/com/plotsquared/core/command/Backup.java index 8e16758bb..f9f45756e 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Backup.java +++ b/Core/src/main/java/com/plotsquared/core/command/Backup.java @@ -27,10 +27,12 @@ package com.plotsquared.core.command; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.backup.BackupProfile; +import com.plotsquared.core.backup.NullBackupProfile; import com.plotsquared.core.backup.PlayerBackupProfile; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; @@ -109,6 +111,29 @@ public final class Backup extends Command { public void save(final Command command, final PlotPlayer player, final String[] args, final RunnableVal3 confirm, final RunnableVal2 whenDone) { + final Plot plot = player.getCurrentPlot(); + if (plot == null) { + sendMessage(player, Captions.NOT_IN_PLOT); + } else if (!plot.hasOwner()) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "unowned"); + } else if (plot.isMerged()) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "merged"); + } else if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_BACKUP_OTHER)) { + sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_ADMIN_BACKUP_OTHER); + } else { + final BackupProfile backupProfile = Objects.requireNonNull(PlotSquared.imp()).getBackupManager().getProfile(plot); + if (backupProfile instanceof NullBackupProfile) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "other"); + } else { + backupProfile.createBackup().whenComplete((backup, throwable) -> { + if (throwable != null) { + sendMessage(player, Captions.BACKUP_FAILED, throwable.getMessage()); + } else { + sendMessage(player, Captions.BACKUP_SAVED); + } + }); + } + } } @CommandDeclaration(command = "list", @@ -120,6 +145,25 @@ public final class Backup extends Command { public void list(final Command command, final PlotPlayer player, final String[] args, final RunnableVal3 confirm, final RunnableVal2 whenDone) { + final Plot plot = player.getCurrentPlot(); + if (plot == null) { + sendMessage(player, Captions.NOT_IN_PLOT); + } else if (!plot.hasOwner()) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "unowned"); + } else if (plot.isMerged()) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "merged"); + } else if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_BACKUP_OTHER)) { + sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_ADMIN_BACKUP_OTHER); + } else { + final BackupProfile backupProfile = Objects.requireNonNull(PlotSquared.imp()).getBackupManager().getProfile(plot); + if (backupProfile instanceof NullBackupProfile) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "other"); + } else { + backupProfile.listBackups().whenComplete((backups, throwable) -> { + // TODO: List backups + }); + } + } } @CommandDeclaration(command = "load", @@ -131,6 +175,34 @@ public final class Backup extends Command { public void load(final Command command, final PlotPlayer player, final String[] args, final RunnableVal3 confirm, final RunnableVal2 whenDone) { + final Plot plot = player.getCurrentPlot(); + if (plot == null) { + sendMessage(player, Captions.NOT_IN_PLOT); + } else if (!plot.hasOwner()) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "unowned"); + } else if (plot.isMerged()) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "merged"); + } else if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_BACKUP_OTHER)) { + sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_ADMIN_BACKUP_OTHER); + } else if (args.length == 0) { + sendMessage(player, Captions.BACKUP_LOAD_USAGE); + } else { + final int number; + try { + number = Integer.parseInt(args[0]); + } catch (final Exception e) { + sendMessage(player, Captions.NOT_A_NUMBER, args[0]); + return; + } + final BackupProfile backupProfile = Objects.requireNonNull(PlotSquared.imp()).getBackupManager().getProfile(plot); + if (backupProfile instanceof NullBackupProfile) { + sendMessage(player, Captions.BACKUP_IMPOSSIBLE, "other"); + } else { + backupProfile.listBackups().whenComplete((backups, throwable) -> { + // TODO: Load backups + }); + } + } } } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java index d63019a21..c5f6bc8e3 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java @@ -184,6 +184,11 @@ public enum Captions implements Caption { PERMISSION_ALIAS_SET("plots.alias.set", "static.permissions"), PERMISSION_ALIAS_REMOVE("plots.alias.remove", "static.permissions"), PERMISSION_ADMIN_CHAT_BYPASS("plots.admin.chat.bypass", "static.permissions"), + PERMISSION_BACKUP("plots.backup", "static.permissions"), + PERMISSION_BACKUP_SAVE("plots.backup.save", "static.permissions"), + PERMISSION_BACUP_LIST("plots.backup.list", "static.permissions"), + PERMISSION_BACKUP_LOAD("plots.backup.load", "static.permissions"), + PERMISSION_ADMIN_BACKUP_OTHER("plots.admin.backup.other", "static.permissions"), // // CONSOLE_JAVA_OUTDATED( @@ -759,6 +764,10 @@ public enum Captions implements Caption { // BACKUP_USAGE("$1Usage: $2/plot backup save/list/load", "Backups"), + BACKUP_IMPOSSIBLE("$2Backups are not enabled for this plot: %s", "Backups"), + BACKUP_SAVED("$1The backup was created successfully", "Backups"), + BACKUP_FAILED("$2The backup could not be created: %s", "Backups"), + BACKUP_LOAD_USAGE("$1Usage: $2/plot backup load [#]", "Backups"), // /**