Added RestoreContext for good measure
parent
7c07d2934c
commit
163626fb3d
|
@ -97,13 +97,13 @@ public class RestoreBackupCommand {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Statics.restoreAwaitThread == null || !Statics.restoreAwaitThread.isAlive()) {
|
Statics.restoreAwaitThread = RestoreHelper.create(
|
||||||
if(source.getEntity() != null)
|
RestoreContext.Builder.newRestoreContextBuilder()
|
||||||
Statics.LOGGER.info("Backup restoration was initiated by: {}", source.getName());
|
.setCommandSource(source)
|
||||||
else
|
.setFile(backupFile.get())
|
||||||
Statics.LOGGER.info("Backup restoration was initiated form Server Console");
|
.setComment(comment)
|
||||||
|
.build()
|
||||||
Statics.restoreAwaitThread = RestoreHelper.create(backupFile.get(), source.getMinecraftServer(), comment);
|
);
|
||||||
|
|
||||||
Statics.restoreAwaitThread.start();
|
Statics.restoreAwaitThread.start();
|
||||||
} else if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
|
} else if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
|
||||||
|
|
|
@ -20,6 +20,7 @@ package net.szum123321.textile_backup.core.restore;
|
||||||
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.szum123321.textile_backup.ConfigHandler;
|
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.core.LivingServer;
|
||||||
import net.szum123321.textile_backup.Statics;
|
import net.szum123321.textile_backup.Statics;
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
|
@ -31,34 +32,31 @@ import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class RestoreBackupRunnable implements Runnable {
|
public class RestoreBackupRunnable implements Runnable {
|
||||||
private final MinecraftServer server;
|
private final RestoreContext ctx;
|
||||||
private final RestoreHelper.RestoreableFile backupFile;
|
|
||||||
private final String finalBackupComment;
|
|
||||||
|
|
||||||
public RestoreBackupRunnable(MinecraftServer server, RestoreHelper.RestoreableFile backupFile, String finalBackupComment) {
|
public RestoreBackupRunnable(RestoreContext ctx) {
|
||||||
this.server = server;
|
this.ctx = ctx;
|
||||||
this.backupFile = backupFile;
|
|
||||||
this.finalBackupComment = finalBackupComment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Statics.LOGGER.info("Shutting down server...");
|
Statics.LOGGER.info("Shutting down server...");
|
||||||
|
|
||||||
server.stop(false);
|
ctx.getServer().stop(false);
|
||||||
awaitServerShutdown();
|
awaitServerShutdown();
|
||||||
|
|
||||||
if(Statics.CONFIG.backupOldWorlds) {
|
if(Statics.CONFIG.backupOldWorlds) {
|
||||||
BackupHelper.create(
|
BackupHelper.create(
|
||||||
new BackupContext.Builder()
|
BackupContext.Builder
|
||||||
.setServer(server)
|
.newBackupContextBuilder()
|
||||||
.setInitiator(BackupContext.BackupInitiator.Restore)
|
.setServer(ctx.getServer())
|
||||||
.setComment("Old_World" + (finalBackupComment != null ? "_" + finalBackupComment : ""))
|
.setInitiator(ActionInitiator.Restore)
|
||||||
|
.setComment("Old_World" + (ctx.getComment() != null ? "_" + ctx.getComment() : ""))
|
||||||
.build()
|
.build()
|
||||||
).run();
|
).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
File worldFile = Utilities.getWorldFolder(server);
|
File worldFile = Utilities.getWorldFolder(ctx.getServer());
|
||||||
|
|
||||||
Statics.LOGGER.info("Deleting old world...");
|
Statics.LOGGER.info("Deleting old world...");
|
||||||
|
|
||||||
|
@ -69,15 +67,15 @@ public class RestoreBackupRunnable implements Runnable {
|
||||||
|
|
||||||
Statics.LOGGER.info("Starting decompression...");
|
Statics.LOGGER.info("Starting decompression...");
|
||||||
|
|
||||||
if(backupFile.getArchiveFormat() == ConfigHandler.ArchiveFormat.ZIP)
|
if(ctx.getFile().getArchiveFormat() == ConfigHandler.ArchiveFormat.ZIP)
|
||||||
ZipDecompressor.decompress(backupFile.getFile(), worldFile);
|
ZipDecompressor.decompress(ctx.getFile().getFile(), worldFile);
|
||||||
else
|
else
|
||||||
GenericTarDecompressor.decompress(backupFile.getFile(), worldFile);
|
GenericTarDecompressor.decompress(ctx.getFile().getFile(), worldFile);
|
||||||
|
|
||||||
if(Statics.CONFIG.deleteOldBackupAfterRestore) {
|
if(Statics.CONFIG.deleteOldBackupAfterRestore) {
|
||||||
Statics.LOGGER.info("Deleting old backup");
|
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");
|
Statics.LOGGER.info("Something went wrong while deleting old backup");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +83,7 @@ public class RestoreBackupRunnable implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void awaitServerShutdown() {
|
private void awaitServerShutdown() {
|
||||||
while(((LivingServer)server).isAlive()) {
|
while(((LivingServer)ctx.getServer()).isAlive()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -52,17 +52,26 @@ public class RestoreHelper {
|
||||||
return optionalFile;
|
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();
|
MutableText message = Statics.LOGGER.getPrefixText().shallowCopy();
|
||||||
message.append("Warning! The server is going to shut down in " + Statics.CONFIG.restoreDelay + " seconds!");
|
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);
|
Statics.globalShutdownBackupFlag.set(false);
|
||||||
|
|
||||||
return new AwaitThread(
|
return new AwaitThread(
|
||||||
Statics.CONFIG.restoreDelay,
|
Statics.CONFIG.restoreDelay,
|
||||||
new RestoreBackupRunnable(server, backupFile, comment)
|
new RestoreBackupRunnable(ctx)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue