Added newBackupContextBuilder function to BackupContext.Builder and simplified StartBackupCommand

This commit is contained in:
szymon 2020-12-02 21:05:16 +01:00
parent 06f80a9175
commit eba22e8464
3 changed files with 29 additions and 33 deletions

View File

@ -76,7 +76,8 @@ public class TextileBackup implements ModInitializer {
if (Statics.CONFIG.shutdownBackup && Statics.globalShutdownBackupFlag.get()) {
BackupHelper.create(
new BackupContext.Builder()
BackupContext.Builder
.newBackupContextBuilder()
.setServer(server)
.setInitiator(ActionInitiator.Shutdown)
.setComment("shutdown")

View File

@ -20,48 +20,41 @@ package net.szum123321.textile_backup.commands.create;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.Statics;
import net.szum123321.textile_backup.core.create.BackupContext;
import net.szum123321.textile_backup.core.create.BackupHelper;
import javax.annotation.Nullable;
public class StartBackupCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register() {
return CommandManager.literal("start")
.then(CommandManager.argument("comment", StringArgumentType.string())
.executes(StartBackupCommand::executeWithComment)
).executes(ctx -> execute(ctx.getSource()));
.executes(ctx -> execute(ctx.getSource(), StringArgumentType.getString(ctx, "comment")))
).executes(ctx -> execute(ctx.getSource(), null));
}
private static int executeWithComment(CommandContext<ServerCommandSource> ctx) {
if(!Statics.executorService.isShutdown())
Statics.executorService.submit(
BackupHelper.create(
new BackupContext.Builder()
.setCommandSource(ctx.getSource())
.setComment(StringArgumentType.getString(ctx, "comment"))
.guessInitiator()
.saveServer()
.build()
)
);
return 1;
}
private static int execute(ServerCommandSource source){
if(!Statics.executorService.isShutdown())
Statics.executorService.submit(
BackupHelper.create(
new BackupContext.Builder()
.setCommandSource(source)
.guessInitiator()
.saveServer()
.build()
)
);
private static int execute(ServerCommandSource source, @Nullable String comment) {
if(!Statics.executorService.isShutdown()) {
try {
Statics.executorService.submit(
BackupHelper.create(
BackupContext.Builder
.newBackupContextBuilder()
.setCommandSource(source)
.setComment(comment)
.guessInitiator()
.saveServer()
.build()
)
);
} catch (Exception e) {
Statics.LOGGER.error("Something went wrong while executing command!", e);
throw e;
}
}
return 1;
}

View File

@ -41,7 +41,8 @@ public class BackupScheduler {
if(nextBackup <= now) {
Statics.executorService.submit(
BackupHelper.create(
new BackupContext.Builder()
BackupContext.Builder
.newBackupContextBuilder()
.setServer(server)
.setInitiator(ActionInitiator.Timer)
.saveServer()
@ -59,7 +60,8 @@ public class BackupScheduler {
if(scheduled && nextBackup <= now) {
Statics.executorService.submit(
BackupHelper.create(
new BackupContext.Builder()
BackupContext.Builder
.newBackupContextBuilder()
.setServer(server)
.setInitiator(ActionInitiator.Timer)
.saveServer()