Skip to content

Commit 7ec5460

Browse files
refactor(punishment): extract shared command helpers, reduce duplication
Add CONSOLE_UUID, resolvePlayerUuid(), and senderUuid() helpers to HypixelCommand base class. Refactor BanCommand and MuteCommand to use them, eliminating duplicated UUID resolution and sender extraction. Remove direct PunishmentRedis dependency from BanCommand.
1 parent 7bb9b9b commit 7ec5460

3 files changed

Lines changed: 46 additions & 95 deletions

File tree

type.generic/src/main/java/net/swofty/type/generic/command/HypixelCommand.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
import net.minestom.server.command.CommandSender;
55
import net.minestom.server.command.ConsoleSender;
66
import net.minestom.server.command.builder.Command;
7+
import net.minestom.server.entity.Player;
8+
import net.minestom.server.utils.mojang.MojangUtils;
79
import net.swofty.type.generic.data.HypixelDataHandler;
810
import net.swofty.type.generic.data.datapoints.DatapointRank;
911
import net.swofty.type.generic.user.HypixelPlayer;
1012

13+
import java.io.IOException;
1114
import java.util.ArrayList;
1215
import java.util.Arrays;
1316
import java.util.List;
17+
import java.util.UUID;
1418

1519
public abstract class HypixelCommand {
1620
public static final String COMMAND_SUFFIX = "Command";
21+
public static final UUID CONSOLE_UUID = new UUID(0, 0);
1722

1823
@Getter
1924
private final CommandParameters params;
@@ -40,6 +45,16 @@ protected HypixelCommand() {
4045

4146
public abstract void registerUsage(MinestomCommand command);
4247

48+
protected static UUID resolvePlayerUuid(CommandSender sender, String playerName, String action) throws IOException {
49+
UUID uuid = MojangUtils.getUUID(playerName);
50+
sender.sendMessage("§8Processing " + action + " for player §e" + playerName + "§7... (" + uuid + ")");
51+
return uuid;
52+
}
53+
54+
protected static UUID senderUuid(CommandSender sender) {
55+
return sender instanceof Player p ? p.getUuid() : CONSOLE_UUID;
56+
}
57+
4358
public boolean permissionCheck(CommandSender sender) {
4459
HypixelPlayer player = (HypixelPlayer) sender;
4560
HypixelDataHandler dataHandler = player.getDataHandler();

type.generic/src/main/java/net/swofty/type/generic/command/commands/BanCommand.java

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
import net.minestom.server.command.CommandSender;
55
import net.minestom.server.command.builder.arguments.*;
66
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
7-
import net.minestom.server.entity.Player;
8-
import net.minestom.server.utils.mojang.MojangUtils;
97
import net.swofty.commons.ServiceType;
108
import net.swofty.commons.StringUtility;
119
import net.swofty.commons.protocol.objects.punishment.PunishPlayerProtocolObject;
1210
import net.swofty.commons.punishment.PunishmentReason;
13-
import net.swofty.commons.punishment.PunishmentRedis;
1411
import net.swofty.commons.punishment.PunishmentTag;
1512
import net.swofty.commons.punishment.PunishmentType;
1613
import net.swofty.commons.punishment.template.BanType;
@@ -24,11 +21,9 @@
2421
import java.io.IOException;
2522
import java.util.ArrayList;
2623
import java.util.List;
27-
import java.util.Optional;
2824
import java.util.UUID;
2925
import java.util.concurrent.CompletableFuture;
3026
import java.util.concurrent.TimeUnit;
31-
import java.util.concurrent.atomic.AtomicBoolean;
3227

3328
@CommandParameters(
3429
aliases = "ban tempban banip tempbanip",
@@ -52,55 +47,35 @@ public void registerUsage(MinestomCommand command) {
5247
for (PunishmentTag tag : PunishmentTag.values()) {
5348
suggestion.addEntry(new SuggestionEntry("-" + tag.getShortCode(), Component.text("§e" + tag.getShortCode() + " §7| " + (tag.getDescription() != null ? tag.getDescription() : "No description"))));
5449
}
55-
}); // can be -O -U etc.
50+
});
5651

5752
command.addSyntax((sender, context) -> {
5853
String playerName = context.get(playerArg);
5954
String duration = context.get(durationArg);
6055
BanType type = BanType.valueOf(context.get(reasonArg));
6156

62-
63-
UUID targetUuid;
64-
try {
65-
targetUuid = MojangUtils.getUUID(playerName);
66-
sender.sendMessage("§8Processing ban for player §e" + playerName + "§7... (" + targetUuid + ")");
67-
} catch (IOException e) {
68-
sender.sendMessage("§cCould not find player: " + playerName);
69-
return;
70-
}
71-
72-
UUID senderUuid;
73-
if (sender instanceof Player player) {
74-
senderUuid = player.getUuid();
75-
} else {
76-
senderUuid = UUID.fromString("00000000-0000-0000-0000-000000000000");
77-
}
78-
79-
long actualTime = StringUtility.parseDuration(duration);
80-
long expiryTime = System.currentTimeMillis() + actualTime;
81-
8257
CompletableFuture.runAsync(() -> {
83-
banPlayer(sender, targetUuid, type, senderUuid, actualTime, expiryTime, playerName, null);
58+
try {
59+
UUID targetUuid = resolvePlayerUuid(sender, playerName, "ban");
60+
long actualTime = StringUtility.parseDuration(duration);
61+
long expiryTime = System.currentTimeMillis() + actualTime;
62+
banPlayer(sender, targetUuid, type, senderUuid(sender), actualTime, expiryTime, playerName, null);
63+
} catch (IOException e) {
64+
sender.sendMessage("§cCould not find player: " + playerName);
65+
}
8466
});
8567
}, playerArg, durationArg, reasonArg);
8668

87-
// permanent ban
8869
command.addSyntax((sender, context) -> {
8970
String playerName = context.get(playerArg);
9071
BanType reason = BanType.valueOf(context.get(reasonArg));
9172

9273
CompletableFuture.runAsync(() -> {
9374
try {
94-
banPlayer(sender,
95-
MojangUtils.getUUID(playerName),
96-
reason,
97-
sender instanceof Player player ? player.getUuid() : UUID.fromString("00000000-0000-0000-0000-000000000000"),
98-
0,
99-
-1,
100-
playerName, null);
75+
banPlayer(sender, resolvePlayerUuid(sender, playerName, "ban"), reason,
76+
senderUuid(sender), 0, -1, playerName, null);
10177
} catch (IOException e) {
10278
sender.sendMessage("§cCould not find player: " + playerName);
103-
return;
10479
}
10580
});
10681
}, playerArg, reasonArg);
@@ -112,13 +87,8 @@ public void registerUsage(MinestomCommand command) {
11287

11388
CompletableFuture.runAsync(() -> {
11489
try {
115-
banPlayer(sender,
116-
MojangUtils.getUUID(playerName),
117-
reason,
118-
sender instanceof Player player ? player.getUuid() : UUID.fromString("00000000-0000-0000-0000-000000000000"),
119-
0,
120-
-1,
121-
playerName, tags);
90+
banPlayer(sender, resolvePlayerUuid(sender, playerName, "ban"), reason,
91+
senderUuid(sender), 0, -1, playerName, tags);
12292
} catch (IOException e) {
12393
sender.sendMessage("§cCould not find player: " + playerName);
12494
}
@@ -128,7 +98,6 @@ public void registerUsage(MinestomCommand command) {
12898

12999
private List<PunishmentTag> parseTags(List<String> rawTags) {
130100
List<PunishmentTag> tags = new ArrayList<>();
131-
132101
for (String rawTag : rawTags) {
133102
if (rawTag.startsWith("-")) {
134103
String tagCode = rawTag.substring(1).toUpperCase();
@@ -140,26 +109,11 @@ private List<PunishmentTag> parseTags(List<String> rawTags) {
140109
}
141110
}
142111
}
143-
144112
return tags;
145113
}
146114

147-
private void banPlayer(CommandSender sender, UUID targetUuid, BanType type, UUID senderUuid, long actualTime, long expiryTime, String playerName, @Nullable List<PunishmentTag> tags) {
148-
if (tags != null && !tags.contains(PunishmentTag.OVERWRITE)) {
149-
Optional<PunishmentRedis.ActivePunishment> activePunishment = PunishmentRedis.getActive(targetUuid);
150-
AtomicBoolean alreadyBanned = new AtomicBoolean(false);
151-
activePunishment.ifPresent(punishment -> {
152-
PunishmentType t = PunishmentType.valueOf(punishment.type());
153-
if (t == PunishmentType.BAN) {
154-
sender.sendMessage("§cThis player is already banned. If you want to replace this ban use the tag -O, Punishment ID: §7" + punishment.banId());
155-
alreadyBanned.set(true);
156-
}
157-
});
158-
if (alreadyBanned.get()) {
159-
return;
160-
}
161-
}
162-
115+
private void banPlayer(CommandSender sender, UUID targetUuid, BanType type, UUID senderUuid,
116+
long actualTime, long expiryTime, String playerName, @Nullable List<PunishmentTag> tags) {
163117
ProxyService punishmentService = new ProxyService(ServiceType.PUNISHMENT);
164118
PunishmentReason reason = new PunishmentReason(type);
165119
ArrayList<PunishmentTag> tagList = (tags != null) ? new ArrayList<>(tags) : new ArrayList<>();
@@ -178,6 +132,8 @@ private void banPlayer(CommandSender sender, UUID targetUuid, BanType type, UUID
178132
if (result instanceof PunishPlayerProtocolObject.PunishPlayerResponse response) {
179133
if (response.success()) {
180134
sender.sendMessage("§aSuccessfully banned player §e" + playerName + "§a. §8Punishment ID: §7" + response.punishmentId());
135+
} else if (response.errorCode() == PunishPlayerProtocolObject.ErrorCode.ALREADY_PUNISHED) {
136+
sender.sendMessage("§cThis player is already banned. Use the tag -O to overwrite. Punishment ID: §7" + response.errorMessage());
181137
} else {
182138
sender.sendMessage("§cFailed to ban player: " + response.errorMessage());
183139
}

type.generic/src/main/java/net/swofty/type/generic/command/commands/MuteCommand.java

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import net.minestom.server.command.builder.arguments.ArgumentString;
77
import net.minestom.server.command.builder.arguments.ArgumentType;
88
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
9-
import net.minestom.server.entity.Player;
10-
import net.minestom.server.utils.mojang.MojangUtils;
119
import net.swofty.commons.ServiceType;
1210
import net.swofty.commons.StringUtility;
1311
import net.swofty.commons.protocol.objects.punishment.PunishPlayerProtocolObject;
@@ -49,55 +47,35 @@ public void registerUsage(MinestomCommand command) {
4947
String duration = context.get(durationArg);
5048
MuteType type = MuteType.valueOf(context.get(reasonArg));
5149

52-
53-
UUID targetUuid;
54-
try {
55-
targetUuid = MojangUtils.getUUID(playerName);
56-
sender.sendMessage("§8Processing mute for player §e" + playerName + "§7... (" + targetUuid + ")");
57-
} catch (IOException e) {
58-
sender.sendMessage("§cCould not find player: " + playerName);
59-
return;
60-
}
61-
62-
UUID senderUuid;
63-
if (sender instanceof Player player) {
64-
senderUuid = player.getUuid();
65-
} else {
66-
senderUuid = UUID.fromString("00000000-0000-0000-0000-000000000000");
67-
}
68-
69-
long actualTime = StringUtility.parseDuration(duration);
70-
long expiryTime = System.currentTimeMillis() + actualTime;
71-
7250
CompletableFuture.runAsync(() -> {
73-
mutePlayer(sender, targetUuid, type, senderUuid, actualTime, expiryTime, playerName);
51+
try {
52+
UUID targetUuid = resolvePlayerUuid(sender, playerName, "mute");
53+
long actualTime = StringUtility.parseDuration(duration);
54+
long expiryTime = System.currentTimeMillis() + actualTime;
55+
mutePlayer(sender, targetUuid, type, senderUuid(sender), actualTime, expiryTime, playerName);
56+
} catch (IOException e) {
57+
sender.sendMessage("§cCould not find player: " + playerName);
58+
}
7459
});
7560
}, playerArg, durationArg, reasonArg);
7661

77-
// permanent mute
7862
command.addSyntax((sender, context) -> {
7963
String playerName = context.get(playerArg);
8064
MuteType reason = MuteType.valueOf(context.get(reasonArg));
8165

8266
CompletableFuture.runAsync(() -> {
8367
try {
84-
mutePlayer(sender,
85-
MojangUtils.getUUID(playerName),
86-
reason,
87-
sender instanceof Player player ? player.getUuid() : UUID.fromString("00000000-0000-0000-0000-000000000000"),
88-
0,
89-
-1,
90-
playerName);
68+
mutePlayer(sender, resolvePlayerUuid(sender, playerName, "mute"), reason,
69+
senderUuid(sender), 0, -1, playerName);
9170
} catch (IOException e) {
9271
sender.sendMessage("§cCould not find player: " + playerName);
9372
}
9473
});
95-
96-
9774
}, playerArg, reasonArg);
9875
}
9976

100-
private void mutePlayer(CommandSender sender, UUID targetUuid, MuteType type, UUID senderUuid, long actualTime, long expiryTime, String playerName) {
77+
private void mutePlayer(CommandSender sender, UUID targetUuid, MuteType type, UUID senderUuid,
78+
long actualTime, long expiryTime, String playerName) {
10179
ProxyService punishmentService = new ProxyService(ServiceType.PUNISHMENT);
10280
PunishmentReason reason = new PunishmentReason(type);
10381
PunishPlayerProtocolObject.PunishPlayerMessage message = new PunishPlayerProtocolObject.PunishPlayerMessage(
@@ -113,6 +91,8 @@ private void mutePlayer(CommandSender sender, UUID targetUuid, MuteType type, UU
11391
if (result instanceof PunishPlayerProtocolObject.PunishPlayerResponse response) {
11492
if (response.success()) {
11593
sender.sendMessage("§aSuccessfully muted player §e" + playerName + "§a. §8Punishment ID: §7" + response.punishmentId());
94+
} else if (response.errorCode() == PunishPlayerProtocolObject.ErrorCode.ALREADY_PUNISHED) {
95+
sender.sendMessage("§cThis player already has an active punishment. Punishment ID: §7" + response.errorMessage());
11696
} else {
11797
sender.sendMessage("§cFailed to mute player: " + response.errorMessage());
11898
}

0 commit comments

Comments
 (0)