summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
blob: 2d0c403c54761e616e5ca346cbfae169bb21e9f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.keuin.kbackupfabric.operation;

import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.worker.RestoreWorker;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;

import java.io.File;

import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*;

class RestoreOperation extends AbstractConfirmableOperation {

    //private static final Logger LOGGER = LogManager.getLogger();
    private final String backupName;
    private final CommandContext<ServerCommandSource> context;

    RestoreOperation(CommandContext<ServerCommandSource> context, String backupName) {
        this.backupName = backupName;
        this.context = context;
    }

    @Override
    public boolean confirm() {
        // do restore to backupName
        MinecraftServer server = context.getSource().getMinecraftServer();
        PrintUtil.msgInfo(context, String.format("Restoring to previous world %s ...", backupName), true);

        String backupFileName = getBackupFileName(backupName);
        PrintUtil.debug("Backup file name: " + backupFileName);
        File backupFile = new File(getBackupSaveDirectory(server), backupFileName);

        PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depended on your world size and the disk speed, the restore progress may take seconds or minutes.", true);
        PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true);
        PrintUtil.msgInfo(context, "After it shuts down, please restart the server manually.", true);
        final int WAIT_SECONDS = 10;
        for (int i = 0; i < WAIT_SECONDS; ++i) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignored) {
            }
        }
        PrintUtil.msgInfo(context, "Shutting down ...", true);
        RestoreWorker.invoke(server, backupFile.getPath(), getLevelPath(server));
        return true;
    }

    @Override
    public String toString() {
        return String.format("restoration from %s", backupName);
    }
}