Skip to content

Commit 78933ee

Browse files
committed
feat: Add logging to backup command sub-commands
Introduced Logger.info calls to all backup command sub-commands for improved traceability of user actions. Fixed the 'info' command to execute the correct handler and corrected the restore command's success message. These changes enhance debugging and provide better feedback in server logs.
1 parent 2059552 commit 78933ee

File tree

9 files changed

+43
-7
lines changed

9 files changed

+43
-7
lines changed

src/main/java/dev/loat/command/CommandManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ public static void register() {
3232
.then(Commands.literal(Abort.COMMAND).executes(Abort::execute))
3333
.then(Commands.literal(List.COMMAND).executes(List::execute))
3434
.then(Commands.literal(Info.COMMAND)
35-
.then(Commands.argument(Info.ARGUMENT, StringArgumentType.string()).suggests(new BackupFiles()).executes(Restore::execute))
35+
.then(Commands.argument(Info.ARGUMENT, StringArgumentType.string()).suggests(new BackupFiles()).executes(Info::execute))
3636
)
3737
.then(Commands.literal(Cleanup.COMMAND).executes(Cleanup::execute))
3838
));
3939
}
4040
}
41-
42-

src/main/java/dev/loat/command/sub_command/Abort.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package dev.loat.command.sub_command;
22

33
import com.mojang.brigadier.context.CommandContext;
4+
5+
import dev.loat.logging.Logger;
46
import net.minecraft.commands.CommandSourceStack;
57
import net.minecraft.network.chat.Component;
8+
import net.minecraft.world.entity.player.Player;
69

710

811
public final class Abort {
912
public static final String COMMAND = "abort";
1013

1114
public static int execute(CommandContext<CommandSourceStack> context) {
15+
String playerName;
16+
17+
if (context.getSource().getEntity() instanceof Player) {
18+
playerName = context.getSource().getPlayer().getName().getString();
19+
} else {
20+
playerName = "Console";
21+
}
22+
23+
Logger.info("[%s] Aborting current action".formatted(playerName));
24+
1225
context.getSource().sendSuccess(() -> Component.literal("/backup abort"), false);
1326
return 1;
1427
}

src/main/java/dev/loat/command/sub_command/Cleanup.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.loat.command.sub_command;
22

33
import com.mojang.brigadier.context.CommandContext;
4+
5+
import dev.loat.logging.Logger;
46
import net.minecraft.commands.CommandSourceStack;
57
import net.minecraft.network.chat.Component;
68

@@ -9,6 +11,8 @@ public final class Cleanup {
911
public static final String COMMAND = "cleanup";
1012

1113
public static int execute(CommandContext<CommandSourceStack> context) {
14+
Logger.info("Running cleanup process");
15+
1216
context.getSource().sendSuccess(() -> Component.literal("/backup cleanup"), false);
1317
return 1;
1418
}

src/main/java/dev/loat/command/sub_command/Create.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ public final class Create extends Command {
1616
@SuppressWarnings("null")
1717
public static int execute(CommandContext<CommandSourceStack> context) {
1818
String comment;
19+
String playerName = context.getSource().getPlayer().getName().getString();
1920

2021
if(Create.hasArgument(context, Create.ARGUMENT)) {
2122
comment = StringArgumentType.getString(context, Create.ARGUMENT);
22-
Logger.info("Creating new backup with comment \"%s\"".formatted(comment));
23+
Logger.info("[%s] Creating new backup with comment \"%s\"".formatted(playerName, comment));
2324
} else {
2425
comment = "";
25-
Logger.info("Creating new backup");
26+
Logger.info("[%s] Creating new backup".formatted(playerName));
2627
}
2728

2829
context.getSource().sendSuccess(() -> Component.literal("/backup create \"%s\"".formatted(comment)), false);

src/main/java/dev/loat/command/sub_command/Help.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.loat.command.sub_command;
22

33
import com.mojang.brigadier.context.CommandContext;
4+
5+
import dev.loat.logging.Logger;
46
import net.minecraft.commands.CommandSourceStack;
57
import net.minecraft.network.chat.Component;
68

@@ -9,6 +11,8 @@ public final class Help {
911
public static final String COMMAND = "help";
1012

1113
public static int execute(CommandContext<CommandSourceStack> context) {
14+
Logger.info("Getting help");
15+
1216
context.getSource().sendSuccess(() -> Component.literal("/backup help"), false);
1317
return 1;
1418
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package dev.loat.command.sub_command;
22

3+
import com.mojang.brigadier.arguments.StringArgumentType;
34
import com.mojang.brigadier.context.CommandContext;
5+
6+
import dev.loat.logging.Logger;
47
import net.minecraft.commands.CommandSourceStack;
58
import net.minecraft.network.chat.Component;
69

@@ -9,8 +12,13 @@ public final class Info {
912
public static final String COMMAND = "info";
1013
public static final String ARGUMENT = "name";
1114

15+
@SuppressWarnings("null")
1216
public static int execute(CommandContext<CommandSourceStack> context) {
13-
context.getSource().sendSuccess(() -> Component.literal("/backup info"), false);
17+
String name = StringArgumentType.getString(context, Info.ARGUMENT);
18+
19+
Logger.info("Getting info about existing backup \"%s\"".formatted(name));
20+
21+
context.getSource().sendSuccess(() -> Component.literal("/backup info \"%s\"".formatted(name)), false);
1422
return 1;
1523
}
1624
}

src/main/java/dev/loat/command/sub_command/List.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.loat.command.sub_command;
22

33
import com.mojang.brigadier.context.CommandContext;
4+
5+
import dev.loat.logging.Logger;
46
import net.minecraft.commands.CommandSourceStack;
57
import net.minecraft.network.chat.Component;
68

@@ -9,6 +11,8 @@ public final class List {
911
public static final String COMMAND = "list";
1012

1113
public static int execute(CommandContext<CommandSourceStack> context) {
14+
Logger.info("Listing all backups");
15+
1216
context.getSource().sendSuccess(() -> Component.literal("/backup list"), false);
1317
return 1;
1418
}

src/main/java/dev/loat/command/sub_command/Reload.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.loat.command.sub_command;
22

33
import com.mojang.brigadier.context.CommandContext;
4+
5+
import dev.loat.logging.Logger;
46
import net.minecraft.commands.CommandSourceStack;
57
import net.minecraft.network.chat.Component;
68

@@ -9,6 +11,8 @@ public final class Reload {
911
public static final String COMMAND = "reload";
1012

1113
public static int execute(CommandContext<CommandSourceStack> context) {
14+
Logger.info("Reloading config");
15+
1216
context.getSource().sendSuccess(() -> Component.literal("/backup reload"), false);
1317
return 1;
1418
}

src/main/java/dev/loat/command/sub_command/Restore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static int execute(CommandContext<CommandSourceStack> context) {
2929

3030
Logger.info("Restoring existing backup \"%s\"".formatted(name));
3131

32-
context.getSource().sendSuccess(() -> Component.literal("/backup create \"%s\"".formatted(name)), false);
32+
context.getSource().sendSuccess(() -> Component.literal("/backup restore \"%s\"".formatted(name)), false);
3333
return 1;
3434
}
3535
}

0 commit comments

Comments
 (0)