Added newBackupContextBuilder function to BackupContext.Builder and simplified StartBackupCommand

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

View File

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