diff --git a/Core/src/main/java/com/plotsquared/core/backup/BackupProfile.java b/Core/src/main/java/com/plotsquared/core/backup/BackupProfile.java index 90b4d9694..663d684f6 100644 --- a/Core/src/main/java/com/plotsquared/core/backup/BackupProfile.java +++ b/Core/src/main/java/com/plotsquared/core/backup/BackupProfile.java @@ -62,4 +62,11 @@ public interface BackupProfile { */ @NotNull CompletableFuture createBackup(); + /** + * Restore a backup + * + * @return Backup to restore + */ + @NotNull CompletableFuture restoreBackup(@NotNull final Backup backup); + } diff --git a/Core/src/main/java/com/plotsquared/core/backup/NullBackupProfile.java b/Core/src/main/java/com/plotsquared/core/backup/NullBackupProfile.java index e4647592c..39c833579 100644 --- a/Core/src/main/java/com/plotsquared/core/backup/NullBackupProfile.java +++ b/Core/src/main/java/com/plotsquared/core/backup/NullBackupProfile.java @@ -54,4 +54,8 @@ public class NullBackupProfile implements BackupProfile { throw new UnsupportedOperationException("Cannot create backup of an unowned plot"); } + @Override @NotNull public CompletableFuture restoreBackup(@NotNull final Backup backup) { + return CompletableFuture.completedFuture(null); + } + } diff --git a/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java b/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java index f99bd8cd9..f86201424 100644 --- a/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java +++ b/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java @@ -25,8 +25,12 @@ */ package com.plotsquared.core.backup; +import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.schematic.Schematic; import com.plotsquared.core.util.SchematicHandler; +import com.plotsquared.core.util.task.RunnableVal; +import com.plotsquared.core.util.task.TaskManager; import lombok.RequiredArgsConstructor; import org.jetbrains.annotations.NotNull; @@ -111,4 +115,34 @@ public class PlayerBackupProfile implements BackupProfile { return future; } + @Override @NotNull public CompletableFuture restoreBackup(@NotNull final Backup backup) { + final CompletableFuture future = new CompletableFuture<>(); + if (backup.getFile() == null || !Files.exists(backup.getFile())) { + future.completeExceptionally(new IllegalArgumentException("The specific backup does not exist")); + } else { + TaskManager.runTaskAsync(() -> { + Schematic schematic = null; + try { + schematic = SchematicHandler.manager.getSchematic(backup.getFile().toFile()); + } catch (SchematicHandler.UnsupportedFormatException e) { + e.printStackTrace(); + } + if (schematic == null) { + future.completeExceptionally(new IllegalArgumentException("The backup is non-existent or not in the correct format")); + } else { + SchematicHandler.manager.paste(schematic, plot, 0, 1, 0, false, new RunnableVal() { + @Override public void run(Boolean value) { + if (value) { + future.complete(null); + } else { + future.completeExceptionally(new RuntimeException(Captions.SCHEMATIC_PASTE_FAILED.toString())); + } + } + }); + } + }); + } + return future; + } + }