Skip to content

Commit 43cfd1d

Browse files
committed
Refactor command sub-classes to use shared sendSuccess method
Introduced a protected static sendSuccess method in the base Command class to centralize success message handling. Updated all sub-commands to extend Command and use the new sendSuccess method, reducing code duplication and improving maintainability.
1 parent 78933ee commit 43cfd1d

File tree

10 files changed

+40
-24
lines changed

10 files changed

+40
-24
lines changed

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

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

3+
import java.util.function.Supplier;
4+
35
import com.mojang.brigadier.context.CommandContext;
46

57
import net.minecraft.commands.CommandSourceStack;
8+
import net.minecraft.network.chat.Component;
9+
import net.minecraft.world.entity.player.Player;
610

711
public class Command {
812
protected static boolean hasArgument(
@@ -11,4 +15,14 @@ protected static boolean hasArgument(
1115
) {
1216
return context.getNodes().stream().anyMatch(node -> node.getNode().getName().equals(argument));
1317
}
18+
19+
@SuppressWarnings("null")
20+
protected static void sendSuccess(
21+
CommandContext<CommandSourceStack> context,
22+
Supplier<Component> supplier
23+
) {
24+
if (context.getSource().getEntity() instanceof Player) {
25+
context.getSource().sendSuccess(supplier, false);
26+
}
27+
}
1428
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22

33
import com.mojang.brigadier.context.CommandContext;
44

5+
import dev.loat.command.Command;
56
import dev.loat.logging.Logger;
67
import net.minecraft.commands.CommandSourceStack;
78
import net.minecraft.network.chat.Component;
8-
import net.minecraft.world.entity.player.Player;
99

1010

11-
public final class Abort {
11+
public final class Abort extends Command {
1212
public static final String COMMAND = "abort";
1313

1414
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-
}
15+
String playerName = context.getSource().getTextName();
2216

2317
Logger.info("[%s] Aborting current action".formatted(playerName));
2418

25-
context.getSource().sendSuccess(() -> Component.literal("/backup abort"), false);
19+
Abort.sendSuccess(context, () -> Component.literal("/backup abort"));
20+
2621
return 1;
2722
}
2823
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
import com.mojang.brigadier.context.CommandContext;
44

5+
import dev.loat.command.Command;
56
import dev.loat.logging.Logger;
67
import net.minecraft.commands.CommandSourceStack;
78
import net.minecraft.network.chat.Component;
89

910

10-
public final class Cleanup {
11+
public final class Cleanup extends Command {
1112
public static final String COMMAND = "cleanup";
1213

1314
public static int execute(CommandContext<CommandSourceStack> context) {
1415
Logger.info("Running cleanup process");
1516

16-
context.getSource().sendSuccess(() -> Component.literal("/backup cleanup"), false);
17+
Cleanup.sendSuccess(context, () -> Component.literal("/backup cleanup"));
18+
1719
return 1;
1820
}
1921
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static int execute(CommandContext<CommandSourceStack> context) {
2626
Logger.info("[%s] Creating new backup".formatted(playerName));
2727
}
2828

29-
context.getSource().sendSuccess(() -> Component.literal("/backup create \"%s\"".formatted(comment)), false);
29+
Create.sendSuccess(context, () -> Component.literal("/backup create \"%s\"".formatted(comment)));
3030
return 1;
3131
}
3232
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import com.mojang.brigadier.arguments.StringArgumentType;
44
import com.mojang.brigadier.context.CommandContext;
55

6+
import dev.loat.command.Command;
67
import dev.loat.logging.Logger;
78
import net.minecraft.commands.CommandSourceStack;
89
import net.minecraft.network.chat.Component;
910

1011

11-
public final class Delete {
12+
public final class Delete extends Command {
1213
public static final String COMMAND = "delete";
1314
public static final String ARGUMENT = "name";
1415

@@ -19,7 +20,7 @@ public static int execute(CommandContext<CommandSourceStack> context) {
1920

2021
Logger.info("Deleting existing backup \"%s\"".formatted(name));
2122

22-
context.getSource().sendSuccess(() -> Component.literal("/backup delete \"%s\"".formatted(name)), false);
23+
Delete.sendSuccess(context, () -> Component.literal("/backup delete \"%s\"".formatted(name)));
2324
return 1;
2425
}
2526
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import com.mojang.brigadier.context.CommandContext;
44

5+
import dev.loat.command.Command;
56
import dev.loat.logging.Logger;
67
import net.minecraft.commands.CommandSourceStack;
78
import net.minecraft.network.chat.Component;
89

910

10-
public final class Help {
11+
public final class Help extends Command {
1112
public static final String COMMAND = "help";
1213

1314
public static int execute(CommandContext<CommandSourceStack> context) {
1415
Logger.info("Getting help");
1516

16-
context.getSource().sendSuccess(() -> Component.literal("/backup help"), false);
17+
Help.sendSuccess(context, () -> Component.literal("/backup help"));
1718
return 1;
1819
}
1920
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import com.mojang.brigadier.arguments.StringArgumentType;
44
import com.mojang.brigadier.context.CommandContext;
55

6+
import dev.loat.command.Command;
67
import dev.loat.logging.Logger;
78
import net.minecraft.commands.CommandSourceStack;
89
import net.minecraft.network.chat.Component;
910

1011

11-
public final class Info {
12+
public final class Info extends Command {
1213
public static final String COMMAND = "info";
1314
public static final String ARGUMENT = "name";
1415

@@ -18,7 +19,7 @@ public static int execute(CommandContext<CommandSourceStack> context) {
1819

1920
Logger.info("Getting info about existing backup \"%s\"".formatted(name));
2021

21-
context.getSource().sendSuccess(() -> Component.literal("/backup info \"%s\"".formatted(name)), false);
22+
Info.sendSuccess(context, () -> Component.literal("/backup info \"%s\"".formatted(name)));
2223
return 1;
2324
}
2425
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import com.mojang.brigadier.context.CommandContext;
44

5+
import dev.loat.command.Command;
56
import dev.loat.logging.Logger;
67
import net.minecraft.commands.CommandSourceStack;
78
import net.minecraft.network.chat.Component;
89

910

10-
public final class List {
11+
public final class List extends Command {
1112
public static final String COMMAND = "list";
1213

1314
public static int execute(CommandContext<CommandSourceStack> context) {
1415
Logger.info("Listing all backups");
1516

16-
context.getSource().sendSuccess(() -> Component.literal("/backup list"), false);
17+
List.sendSuccess(context, () -> Component.literal("/backup list"));
1718
return 1;
1819
}
1920
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import com.mojang.brigadier.context.CommandContext;
44

5+
import dev.loat.command.Command;
56
import dev.loat.logging.Logger;
67
import net.minecraft.commands.CommandSourceStack;
78
import net.minecraft.network.chat.Component;
89

910

10-
public final class Reload {
11+
public final class Reload extends Command {
1112
public static final String COMMAND = "reload";
1213

1314
public static int execute(CommandContext<CommandSourceStack> context) {
1415
Logger.info("Reloading config");
1516

16-
context.getSource().sendSuccess(() -> Component.literal("/backup reload"), false);
17+
Reload.sendSuccess(context, () -> Component.literal("/backup reload"));
1718
return 1;
1819
}
1920
}

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 restore \"%s\"".formatted(name)), false);
32+
Restore.sendSuccess(context, () -> Component.literal("/backup restore \"%s\"".formatted(name)));
3333
return 1;
3434
}
3535
}

0 commit comments

Comments
 (0)