From 6fb63c9609a0305075231b8b95ab144a1e0f17de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= <sauilitired@gmail.com>
Date: Sun, 10 May 2020 16:53:19 +0200
Subject: [PATCH] Implement backup restoration command. This might work, idk,
 we'll see.

---
 .../com/plotsquared/core/command/Backup.java  | 27 ++++++++++++++++---
 .../core/configuration/Captions.java          |  7 +++--
 2 files changed, 29 insertions(+), 5 deletions(-)

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 5f9522194..60cba6d6c 100644
--- a/Core/src/main/java/com/plotsquared/core/command/Backup.java
+++ b/Core/src/main/java/com/plotsquared/core/command/Backup.java
@@ -36,6 +36,7 @@ import com.plotsquared.core.util.Permissions;
 import com.plotsquared.core.util.task.RunnableVal2;
 import com.plotsquared.core.util.task.RunnableVal3;
 
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -127,9 +128,9 @@ public final class Backup extends Command {
             } else {
                 backupProfile.createBackup().whenComplete((backup, throwable) -> {
                     if (throwable != null) {
-                        sendMessage(player, Captions.BACKUP_FAILED, throwable.getMessage());
+                        sendMessage(player, Captions.BACKUP_SAVE_FAILED, throwable.getMessage());
                     } else {
-                        sendMessage(player, Captions.BACKUP_SAVED);
+                        sendMessage(player, Captions.BACKUP_SAVE_SUCCESS);
                     }
                 });
             }
@@ -200,7 +201,27 @@ public final class Backup extends Command {
                 sendMessage(player, Captions.BACKUP_IMPOSSIBLE, Captions.GENERIC_OTHER.getTranslated());
             } else {
                 backupProfile.listBackups().whenComplete((backups, throwable) -> {
-                    // TODO: Load backups
+                    if (throwable != null) {
+                        sendMessage(player, Captions.BACKUP_LOAD_FAILURE, throwable.getMessage());
+                    } else {
+                        if (number < 1 || number > backups.size()) {
+                            sendMessage(player, Captions.BACKUP_LOAD_FAILURE, Captions.GENERIC_INVALID_CHOICE.getTranslated());
+                        } else {
+                            final com.plotsquared.core.backup.Backup backup = backups.get(number - 1);
+                            if (backup == null || backup.getFile() == null || !Files.exists(backup.getFile())) {
+                                sendMessage(player, Captions.BACKUP_LOAD_FAILURE, Captions.GENERIC_INVALID_CHOICE.getTranslated());
+                            } else {
+                                CmdConfirm.addPending(player, "/plot backup load " + number, () ->
+                                    backupProfile.restoreBackup(backup).whenComplete((n, error) -> {
+                                   if (error != null) {
+                                       sendMessage(player, Captions.BACKUP_LOAD_FAILURE, error.getMessage());
+                                   } else {
+                                       sendMessage(player, Captions.BACKUP_LOAD_SUCCESS);
+                                   }
+                                }));
+                            }
+                        }
+                    }
                 });
             }
         }
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 20d6193ea..486a4db90 100644
--- a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java
+++ b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java
@@ -765,8 +765,10 @@ public enum Captions implements Caption {
     //<editor-fold desc="Backups">
     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_SAVE_SUCCESS("$1The backup was created successfully", "Backups"),
+    BACKUP_SAVE_FAILED("$2The backup could not be created: %s", "Backups"),
+    BACKUP_LOAD_SUCCESS("$1The backup was restored successfully", "Backups"),
+    BACKUP_LOAD_FAILURE("$2The backup could not be restored: %s", "Backups"),
     BACKUP_LOAD_USAGE("$1Usage: $2/plot backup load [#]", "Backups"),
     //</editor-fold>
 
@@ -774,6 +776,7 @@ public enum Captions implements Caption {
     GENERIC_OTHER("other", "Generic"),
     GENERIC_MERGED("merged", "Generic"),
     GENERIC_UNOWNED("unowned", "Generic"),
+    GENERIC_INVALID_CHOICE("invalid choice", "Generic"),
     //</editor-fold>
 
     /**