Added BackupContext. This object gets passed instead of passing ServerCommandSource, MinecraftServer, etc... separately.
parent
51ecf54fb6
commit
fec162ae24
|
@ -24,6 +24,7 @@ import com.mojang.brigadier.context.CommandContext;
|
|||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.szum123321.textile_backup.TextileBackup;
|
||||
import net.szum123321.textile_backup.core.BackupContext;
|
||||
import net.szum123321.textile_backup.core.BackupHelper;
|
||||
|
||||
public class StartBackupCommand {
|
||||
|
@ -34,13 +35,17 @@ public class StartBackupCommand {
|
|||
).executes(ctx -> execute(ctx.getSource()));
|
||||
}
|
||||
|
||||
private static int executeWithComment(CommandContext<ServerCommandSource> source) {
|
||||
private static int executeWithComment(CommandContext<ServerCommandSource> ctx) {
|
||||
if(!TextileBackup.executorService.isShutdown())
|
||||
TextileBackup.executorService.submit(
|
||||
BackupHelper.create(
|
||||
source.getSource().getMinecraftServer(),
|
||||
source.getSource(),
|
||||
true,
|
||||
StringArgumentType.getString(source, "comment").replace("#", "")
|
||||
new BackupContext.Builder()
|
||||
.setCommandSource(ctx.getSource())
|
||||
.setServer(ctx.getSource().getMinecraftServer())
|
||||
.setComment(StringArgumentType.getString(ctx, "comment"))
|
||||
.guessInitiator()
|
||||
.setSave()
|
||||
.build()
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -48,12 +53,15 @@ public class StartBackupCommand {
|
|||
}
|
||||
|
||||
private static int execute(ServerCommandSource source){
|
||||
if(!TextileBackup.executorService.isShutdown())
|
||||
TextileBackup.executorService.submit(
|
||||
BackupHelper.create(
|
||||
source.getMinecraftServer(),
|
||||
source,
|
||||
true,
|
||||
null
|
||||
new BackupContext.Builder()
|
||||
.setCommandSource(source)
|
||||
.setServer(source.getMinecraftServer())
|
||||
.guessInitiator()
|
||||
.setSave()
|
||||
.build()
|
||||
)
|
||||
);
|
||||
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
package net.szum123321.textile_backup.core;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class BackupContext {
|
||||
private final MinecraftServer server;
|
||||
private final ServerCommandSource commandSource;
|
||||
private final BackupInitiator initiator;
|
||||
private final boolean save;
|
||||
private final String comment;
|
||||
|
||||
protected BackupContext(@NotNull MinecraftServer server, ServerCommandSource commandSource, @NotNull BackupInitiator initiator, boolean save, String comment) {
|
||||
this.server = server;
|
||||
this.commandSource = commandSource;
|
||||
this.initiator = initiator;
|
||||
this.save = save;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public MinecraftServer getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public ServerCommandSource getCommandSource() {
|
||||
return commandSource;
|
||||
}
|
||||
|
||||
public BackupInitiator getInitiator() {
|
||||
return initiator;
|
||||
}
|
||||
|
||||
public boolean startedByPlayer() {
|
||||
return initiator == BackupInitiator.Player;
|
||||
}
|
||||
|
||||
public boolean shouldSave() {
|
||||
return save;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private MinecraftServer server;
|
||||
private ServerCommandSource commandSource;
|
||||
private BackupInitiator initiator;
|
||||
private boolean save;
|
||||
private String comment;
|
||||
|
||||
private boolean guessInitiator;
|
||||
|
||||
public Builder() {
|
||||
this.server = null;
|
||||
this.commandSource = null;
|
||||
this.initiator = null;
|
||||
this.save = false;
|
||||
this.comment = null;
|
||||
|
||||
guessInitiator = false;
|
||||
}
|
||||
|
||||
public Builder setCommandSource(ServerCommandSource commandSource) {
|
||||
this.commandSource = commandSource;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setServer(MinecraftServer server) {
|
||||
this.server = server;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setInitiator(BackupInitiator initiator) {
|
||||
this.initiator = initiator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setComment(String comment) {
|
||||
this.comment = comment;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder guessInitiator() {
|
||||
this.guessInitiator = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setSave() {
|
||||
this.save = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BackupContext build() {
|
||||
if(guessInitiator) {
|
||||
initiator = commandSource.getEntity() == null ? BackupInitiator.ServerConsole : BackupInitiator.Player;
|
||||
} else if(initiator == null) {
|
||||
initiator = BackupInitiator.Null;
|
||||
}
|
||||
|
||||
if(server == null)
|
||||
setServer(commandSource.getMinecraftServer());
|
||||
|
||||
return new BackupContext(server, commandSource, initiator, save, comment);
|
||||
}
|
||||
}
|
||||
|
||||
public enum BackupInitiator {
|
||||
Player ("Player", "by: "),
|
||||
ServerConsole ("Server Console", "from: "),
|
||||
Timer ("Timer", "by: "),
|
||||
Shutdown ("Server Shutdown", "by: "),
|
||||
Null ("Null (That shouldn't have happened)", "form: ");
|
||||
|
||||
private final String name;
|
||||
private final String prefix;
|
||||
|
||||
BackupInitiator(String name, String prefix) {
|
||||
this.name = name;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,7 +19,6 @@
|
|||
package net.szum123321.textile_backup.core;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.szum123321.textile_backup.TextileBackup;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
@ -32,28 +31,30 @@ import java.util.Comparator;
|
|||
import java.util.Iterator;
|
||||
|
||||
public class BackupHelper {
|
||||
public static Runnable create(MinecraftServer server, ServerCommandSource ctx, boolean save, String comment) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
public static Runnable create(BackupContext ctx) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Backup started by: ");
|
||||
|
||||
if (ctx != null)
|
||||
builder.append(ctx.getName());
|
||||
else
|
||||
builder.append("SERVER");
|
||||
builder.append("Backup started ");
|
||||
|
||||
builder.append(ctx.getInitiator().getPrefix());
|
||||
|
||||
if(ctx.startedByPlayer()) {
|
||||
builder.append(ctx.getCommandSource().getDisplayName().getString());
|
||||
} else {
|
||||
builder.append(ctx.getInitiator().getName());
|
||||
}
|
||||
|
||||
builder.append(" on: ");
|
||||
builder.append(Utilities.getDateTimeFormatter().format(now));
|
||||
builder.append(Utilities.getDateTimeFormatter().format(LocalDateTime.now()));
|
||||
|
||||
Utilities.info(builder.toString(), null);
|
||||
|
||||
Utilities.info("Saving server...", ctx);
|
||||
if (ctx.shouldSave()) {
|
||||
Utilities.info("Saving server...", ctx.getCommandSource());
|
||||
ctx.getServer().save(true, true, false);
|
||||
}
|
||||
|
||||
if (save)
|
||||
server.save(true, true, false);
|
||||
|
||||
return new MakeBackupRunnable(server, ctx, comment);
|
||||
return new MakeBackupRunnable(ctx);
|
||||
}
|
||||
|
||||
public static void executeFileLimit(ServerCommandSource ctx, String worldName) {
|
||||
|
@ -92,6 +93,7 @@ public class BackupHelper {
|
|||
} else {
|
||||
Utilities.sendError("Something went wrong while deleting: " + f.getName(), ctx);
|
||||
}
|
||||
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,15 @@ public class BackupScheduler {
|
|||
if(TextileBackup.config.doBackupsOnEmptyServer || server.getPlayerManager().getCurrentPlayerCount() > 0) {
|
||||
if(scheduled) {
|
||||
if(nextBackup <= now) {
|
||||
TextileBackup.executorService.submit(BackupHelper.create(server, null, true, null));
|
||||
TextileBackup.executorService.submit(
|
||||
BackupHelper.create(
|
||||
new BackupContext.Builder()
|
||||
.setServer(server)
|
||||
.setInitiator(BackupContext.BackupInitiator.Timer)
|
||||
.setSave()
|
||||
.build()
|
||||
)
|
||||
);
|
||||
|
||||
nextBackup = now + TextileBackup.config.backupInterval;
|
||||
}
|
||||
|
@ -30,7 +38,15 @@ public class BackupScheduler {
|
|||
}
|
||||
} else if(!TextileBackup.config.doBackupsOnEmptyServer && server.getPlayerManager().getCurrentPlayerCount() == 0) {
|
||||
if(scheduled && nextBackup <= now) {
|
||||
TextileBackup.executorService.submit(BackupHelper.create(server, null, true, null));
|
||||
TextileBackup.executorService.submit(
|
||||
BackupHelper.create(
|
||||
new BackupContext.Builder()
|
||||
.setServer(server)
|
||||
.setInitiator(BackupContext.BackupInitiator.Timer)
|
||||
.setSave()
|
||||
.build()
|
||||
)
|
||||
);
|
||||
|
||||
scheduled = false;
|
||||
}
|
||||
|
|
|
@ -36,18 +36,18 @@ import java.time.LocalDateTime;
|
|||
|
||||
public class MakeBackupRunnable implements Runnable {
|
||||
private final MinecraftServer server;
|
||||
private final ServerCommandSource ctx;
|
||||
private final ServerCommandSource commandSource;
|
||||
private final String comment;
|
||||
|
||||
public MakeBackupRunnable(MinecraftServer server, ServerCommandSource ctx, String comment){
|
||||
this.server = server;
|
||||
this.ctx = ctx;
|
||||
this.comment = comment;
|
||||
public MakeBackupRunnable(BackupContext context){
|
||||
this.server = context.getServer();
|
||||
this.commandSource = context.getCommandSource();
|
||||
this.comment = context.getComment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Utilities.info("Starting backup", ctx);
|
||||
Utilities.info("Starting backup", commandSource);
|
||||
|
||||
File world = ((MinecraftServerSessionAccessor)server)
|
||||
.getSession()
|
||||
|
@ -70,7 +70,7 @@ public class MakeBackupRunnable implements Runnable {
|
|||
} catch (IOException e) {
|
||||
TextileBackup.LOGGER.error("An exception occurred when trying to create new backup file!", e);
|
||||
|
||||
Utilities.sendError("An exception occurred when trying to create new backup file!", ctx);
|
||||
Utilities.sendError("An exception occurred when trying to create new backup file!", commandSource);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -83,36 +83,36 @@ public class MakeBackupRunnable implements Runnable {
|
|||
coreCount = Math.min(TextileBackup.config.compressionCoreCountLimit, Runtime.getRuntime().availableProcessors());
|
||||
}
|
||||
|
||||
TextileBackup.LOGGER.trace("Running compression on {} threads", coreCount);
|
||||
TextileBackup.LOGGER.trace("Running compression on {} threads. Available cores = {}", coreCount, Runtime.getRuntime().availableProcessors());
|
||||
|
||||
switch (TextileBackup.config.format) {
|
||||
case ZIP:
|
||||
ParallelZipCompressor.createArchive(world, outFile, ctx, coreCount);
|
||||
ParallelZipCompressor.createArchive(world, outFile, commandSource, coreCount);
|
||||
break;
|
||||
|
||||
case BZIP2:
|
||||
ParallelBZip2Compressor.createArchive(world, outFile, ctx, coreCount);
|
||||
ParallelBZip2Compressor.createArchive(world, outFile, commandSource, coreCount);
|
||||
break;
|
||||
|
||||
case GZIP:
|
||||
ParallelGzipCompressor.createArchive(world, outFile, ctx, coreCount);
|
||||
ParallelGzipCompressor.createArchive(world, outFile, commandSource, coreCount);
|
||||
break;
|
||||
|
||||
case LZMA:
|
||||
LZMACompressor.createArchive(world, outFile, ctx); // Always single-threaded ):
|
||||
LZMACompressor.createArchive(world, outFile, commandSource); // Always single-threaded ):
|
||||
break;
|
||||
|
||||
default:
|
||||
TextileBackup.LOGGER.warn("Specified compressor ({}) is not supported! Zip will be used instead!", TextileBackup.config.format);
|
||||
Utilities.sendError("Error! No correct compression format specified! Using default compressor!", commandSource);
|
||||
|
||||
Utilities.sendError("Error! No correct compression format specified! using default compressor!", ctx);
|
||||
ParallelZipCompressor.createArchive(world, outFile, ctx, coreCount);
|
||||
ParallelZipCompressor.createArchive(world, outFile, commandSource, coreCount);
|
||||
break;
|
||||
}
|
||||
|
||||
BackupHelper.executeFileLimit(ctx, Utilities.getLevelName(server));
|
||||
BackupHelper.executeFileLimit(commandSource, Utilities.getLevelName(server));
|
||||
|
||||
Utilities.info("Done!", ctx);
|
||||
Utilities.info("Done!", commandSource);
|
||||
}
|
||||
|
||||
private String getFileName(){
|
||||
|
|
|
@ -20,6 +20,7 @@ package net.szum123321.textile_backup.mixin;
|
|||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.szum123321.textile_backup.TextileBackup;
|
||||
import net.szum123321.textile_backup.core.BackupContext;
|
||||
import net.szum123321.textile_backup.core.BackupHelper;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
|
@ -31,6 +32,14 @@ public abstract class MinecraftServerMixin {
|
|||
@Inject(method = "shutdown", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/server/MinecraftServer;save(ZZZ)Z"))
|
||||
public void onFinalWorldSave(CallbackInfo ci) {
|
||||
if (TextileBackup.config.shutdownBackup)
|
||||
TextileBackup.executorService.submit(BackupHelper.create((MinecraftServer) (Object) this, null, false, "shutdown"));
|
||||
TextileBackup.executorService.submit(
|
||||
BackupHelper.create(
|
||||
new BackupContext.Builder()
|
||||
.setServer((MinecraftServer)(Object) this)
|
||||
.setInitiator(BackupContext.BackupInitiator.Shutdown)
|
||||
.setComment("shutdown")
|
||||
.build()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue