Add BackupProfile#restoreBackup

This commit is contained in:
Alexander Söderberg 2020-05-10 14:58:45 +02:00
parent 8715a27a93
commit d0dbb495b0
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
3 changed files with 45 additions and 0 deletions

View file

@ -62,4 +62,11 @@ public interface BackupProfile {
*/
@NotNull CompletableFuture<Backup> createBackup();
/**
* Restore a backup
*
* @return Backup to restore
*/
@NotNull CompletableFuture<Void> restoreBackup(@NotNull final Backup backup);
}

View file

@ -54,4 +54,8 @@ public class NullBackupProfile implements BackupProfile {
throw new UnsupportedOperationException("Cannot create backup of an unowned plot");
}
@Override @NotNull public CompletableFuture<Void> restoreBackup(@NotNull final Backup backup) {
return CompletableFuture.completedFuture(null);
}
}

View file

@ -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<Void> restoreBackup(@NotNull final Backup backup) {
final CompletableFuture<Void> 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<Boolean>() {
@Override public void run(Boolean value) {
if (value) {
future.complete(null);
} else {
future.completeExceptionally(new RuntimeException(Captions.SCHEMATIC_PASTE_FAILED.toString()));
}
}
});
}
});
}
return future;
}
}