diff --git a/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java b/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java index aa098e1..257b11a 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java @@ -97,13 +97,13 @@ public class RestoreBackupCommand { return 0; } - if(Statics.restoreAwaitThread == null || !Statics.restoreAwaitThread.isAlive()) { - if(source.getEntity() != null) - Statics.LOGGER.info("Backup restoration was initiated by: {}", source.getName()); - else - Statics.LOGGER.info("Backup restoration was initiated form Server Console"); - - Statics.restoreAwaitThread = RestoreHelper.create(backupFile.get(), source.getMinecraftServer(), comment); + Statics.restoreAwaitThread = RestoreHelper.create( + RestoreContext.Builder.newRestoreContextBuilder() + .setCommandSource(source) + .setFile(backupFile.get()) + .setComment(comment) + .build() + ); Statics.restoreAwaitThread.start(); } else if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) { diff --git a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java index 2b70cc5..2318106 100644 --- a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java +++ b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreBackupRunnable.java @@ -20,6 +20,7 @@ package net.szum123321.textile_backup.core.restore; import net.minecraft.server.MinecraftServer; import net.szum123321.textile_backup.ConfigHandler; +import net.szum123321.textile_backup.core.ActionInitiator; import net.szum123321.textile_backup.core.LivingServer; import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.core.Utilities; @@ -31,34 +32,31 @@ import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor; import java.io.File; public class RestoreBackupRunnable implements Runnable { - private final MinecraftServer server; - private final RestoreHelper.RestoreableFile backupFile; - private final String finalBackupComment; + private final RestoreContext ctx; - public RestoreBackupRunnable(MinecraftServer server, RestoreHelper.RestoreableFile backupFile, String finalBackupComment) { - this.server = server; - this.backupFile = backupFile; - this.finalBackupComment = finalBackupComment; + public RestoreBackupRunnable(RestoreContext ctx) { + this.ctx = ctx; } @Override public void run() { Statics.LOGGER.info("Shutting down server..."); - server.stop(false); + ctx.getServer().stop(false); awaitServerShutdown(); if(Statics.CONFIG.backupOldWorlds) { BackupHelper.create( - new BackupContext.Builder() - .setServer(server) - .setInitiator(BackupContext.BackupInitiator.Restore) - .setComment("Old_World" + (finalBackupComment != null ? "_" + finalBackupComment : "")) + BackupContext.Builder + .newBackupContextBuilder() + .setServer(ctx.getServer()) + .setInitiator(ActionInitiator.Restore) + .setComment("Old_World" + (ctx.getComment() != null ? "_" + ctx.getComment() : "")) .build() ).run(); } - File worldFile = Utilities.getWorldFolder(server); + File worldFile = Utilities.getWorldFolder(ctx.getServer()); Statics.LOGGER.info("Deleting old world..."); @@ -69,15 +67,15 @@ public class RestoreBackupRunnable implements Runnable { Statics.LOGGER.info("Starting decompression..."); - if(backupFile.getArchiveFormat() == ConfigHandler.ArchiveFormat.ZIP) - ZipDecompressor.decompress(backupFile.getFile(), worldFile); + if(ctx.getFile().getArchiveFormat() == ConfigHandler.ArchiveFormat.ZIP) + ZipDecompressor.decompress(ctx.getFile().getFile(), worldFile); else - GenericTarDecompressor.decompress(backupFile.getFile(), worldFile); + GenericTarDecompressor.decompress(ctx.getFile().getFile(), worldFile); if(Statics.CONFIG.deleteOldBackupAfterRestore) { Statics.LOGGER.info("Deleting old backup"); - if(!backupFile.getFile().delete()) + if(!ctx.getFile().getFile().delete()) Statics.LOGGER.info("Something went wrong while deleting old backup"); } @@ -85,7 +83,7 @@ public class RestoreBackupRunnable implements Runnable { } private void awaitServerShutdown() { - while(((LivingServer)server).isAlive()) { + while(((LivingServer)ctx.getServer()).isAlive()) { try { Thread.sleep(100); } catch (InterruptedException e) { diff --git a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreContext.java b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreContext.java new file mode 100644 index 0000000..b7352a2 --- /dev/null +++ b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreContext.java @@ -0,0 +1,106 @@ +/* + * A simple backup mod for Fabric + * Copyright (C) 2020 Szum123321 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.szum123321.textile_backup.core.restore; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.ServerCommandSource; +import net.szum123321.textile_backup.core.ActionInitiator; + +import javax.annotation.Nullable; + +public class RestoreContext { + private final RestoreHelper.RestoreableFile file; + private final MinecraftServer server; + @Nullable + private final String comment; + private final ActionInitiator initiator; + private final ServerCommandSource commandSource; + + public RestoreContext(RestoreHelper.RestoreableFile file, MinecraftServer server, @Nullable String comment, ActionInitiator initiator, ServerCommandSource commandSource) { + this.file = file; + this.server = server; + this.comment = comment; + this.initiator = initiator; + this.commandSource = commandSource; + } + + public RestoreHelper.RestoreableFile getFile() { + return file; + } + + public MinecraftServer getServer() { + return server; + } + + @Nullable + public String getComment() { + return comment; + } + + public ActionInitiator getInitiator() { + return initiator; + } + + public ServerCommandSource getCommandSource() { + return commandSource; + } + + public static final class Builder { + private RestoreHelper.RestoreableFile file; + private MinecraftServer server; + private String comment; + private ServerCommandSource serverCommandSource; + + private Builder() { + } + + public static Builder newRestoreContextBuilder() { + return new Builder(); + } + + public Builder setFile(RestoreHelper.RestoreableFile file) { + this.file = file; + return this; + } + + public Builder setServer(MinecraftServer server) { + this.server = server; + return this; + } + + public Builder setComment(@Nullable String comment) { + this.comment = comment; + return this; + } + + public Builder setCommandSource(ServerCommandSource commandSource) { + this.serverCommandSource = commandSource; + return this; + } + + public RestoreContext build() { + if(server == null) server = serverCommandSource.getMinecraftServer(); + + ActionInitiator initiator = serverCommandSource.getEntity() instanceof PlayerEntity ? ActionInitiator.Player : ActionInitiator.ServerConsole; + + return new RestoreContext(file, server, comment, initiator, serverCommandSource); + } + } +} diff --git a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java index 95599eb..2c1db5d 100644 --- a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java +++ b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java @@ -52,17 +52,26 @@ public class RestoreHelper { return optionalFile; } - public static AwaitThread create(RestoreableFile backupFile, MinecraftServer server, String comment) { + public static AwaitThread create(RestoreContext ctx) { + if(ctx.getInitiator() == ActionInitiator.Player) + Statics.LOGGER.info("Backup restoration was initiated by: {}", ctx.getCommandSource().getName()); + else + Statics.LOGGER.info("Backup restoration was initiated form Server Console"); + MutableText message = Statics.LOGGER.getPrefixText().shallowCopy(); message.append("Warning! The server is going to shut down in " + Statics.CONFIG.restoreDelay + " seconds!"); - server.getPlayerManager().broadcastChatMessage(message, MessageType.GAME_INFO, Util.NIL_UUID); + ctx.getServer().getPlayerManager().broadcastChatMessage( + message, + MessageType.GAME_INFO, + ctx.getInitiator() == ActionInitiator.Player ? ctx.getCommandSource().getEntity().getUuid() : Util.NIL_UUID + ); Statics.globalShutdownBackupFlag.set(false); return new AwaitThread( Statics.CONFIG.restoreDelay, - new RestoreBackupRunnable(server, backupFile, comment) + new RestoreBackupRunnable(ctx) ); }