From eba22e846427184937f854a42356b3846d546b59 Mon Sep 17 00:00:00 2001 From: szymon Date: Wed, 2 Dec 2020 21:05:16 +0100 Subject: [PATCH] Added newBackupContextBuilder function to BackupContext.Builder and simplified StartBackupCommand --- .../textile_backup/TextileBackup.java | 3 +- .../commands/create/StartBackupCommand.java | 53 ++++++++----------- .../core/create/BackupScheduler.java | 6 ++- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/TextileBackup.java b/src/main/java/net/szum123321/textile_backup/TextileBackup.java index 791a9fa..bd18788 100644 --- a/src/main/java/net/szum123321/textile_backup/TextileBackup.java +++ b/src/main/java/net/szum123321/textile_backup/TextileBackup.java @@ -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") diff --git a/src/main/java/net/szum123321/textile_backup/commands/create/StartBackupCommand.java b/src/main/java/net/szum123321/textile_backup/commands/create/StartBackupCommand.java index 5ed374b..bf24c41 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/create/StartBackupCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/create/StartBackupCommand.java @@ -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 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 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; } diff --git a/src/main/java/net/szum123321/textile_backup/core/create/BackupScheduler.java b/src/main/java/net/szum123321/textile_backup/core/create/BackupScheduler.java index fc2b417..8ff78a9 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/BackupScheduler.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/BackupScheduler.java @@ -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()