Skip to content

Commit 5df6d47

Browse files
fix(punishment): fix runtime bugs in punishment system
Add no-arg constructor to PunishmentReason for Gson deserialization, fix "muted for muted for" typo, replace jedis.keys() with SCAN, make getAllBannedPlayerIds synchronous so UnBanCommand suggestions populate, use SecureRandom for PunishmentId generation, fix permanent ban expiry validation in PunishPlayerEndpoint.
1 parent 6d76fb8 commit 5df6d47

5 files changed

Lines changed: 29 additions & 25 deletions

File tree

commons/src/main/java/net/swofty/commons/punishment/PunishmentId.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package net.swofty.commons.punishment;
22

3+
import java.security.SecureRandom;
4+
35
public record PunishmentId(String id) {
6+
private static final SecureRandom RANDOM = new SecureRandom();
47

58
public static PunishmentId generateId() {
69
StringBuilder idBuilder = new StringBuilder("#");
710
String hexChars = "0123456789ABCDEF";
811
for (int i = 0; i < 8; i++) {
9-
int randomIndex = (int) (Math.random() * hexChars.length());
10-
idBuilder.append(hexChars.charAt(randomIndex));
12+
idBuilder.append(hexChars.charAt(RANDOM.nextInt(hexChars.length())));
1113
}
1214
return new PunishmentId(idBuilder.toString());
1315
}

commons/src/main/java/net/swofty/commons/punishment/PunishmentReason.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package net.swofty.commons.punishment;
22

33
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
45
import lombok.NonNull;
56
import net.swofty.commons.punishment.template.BanType;
67
import net.swofty.commons.punishment.template.MuteType;
7-
import net.swofty.commons.punishment.template.UnpunishReason;
88
import org.jetbrains.annotations.Nullable;
99

1010
@Getter
11+
@NoArgsConstructor
1112
public class PunishmentReason {
1213
@Nullable
1314
private BanType banType;

commons/src/main/java/net/swofty/commons/punishment/PunishmentRedis.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.Set;
1616
import java.util.UUID;
1717
import java.util.concurrent.CompletableFuture;
18-
import java.util.stream.Collectors;
1918

2019
public class PunishmentRedis {
2120
private static final String PREFIX = "punish:";
@@ -158,7 +157,7 @@ public static Component parseActivePunishmentMuteMessage(ActivePunishment punish
158157
if (expiresAt <= 0) {
159158
header = "§cYou are permanently muted on this server!\n";
160159
} else {
161-
header = "§cYou are currently muted for muted for " + reason.getReasonString() + "\n";
160+
header = "§cYou are currently muted for " + reason.getReasonString() + "\n";
162161
}
163162
String time = "§7Your mute will expire in §c" + prettyTimeLeft + "\n\n";
164163

@@ -167,16 +166,20 @@ public static Component parseActivePunishmentMuteMessage(ActivePunishment punish
167166
return Component.text(line + header + time + urlInfo + footer + line);
168167
}
169168

170-
// lookup for all BANNED players
171-
public static CompletableFuture<Set<String>> getAllBannedPlayerIds() {
172-
return CompletableFuture.supplyAsync(() -> {
173-
try (Jedis jedis = jedisPool.getResource()) {
174-
Set<String> keys = jedis.keys(PREFIX + "active:*");
175-
return keys.stream()
176-
.map(key -> key.substring((PREFIX + "active:").length()))
177-
.collect(Collectors.toSet());
178-
}
179-
});
169+
public static Set<String> getAllBannedPlayerIds() {
170+
try (Jedis jedis = jedisPool.getResource()) {
171+
var cursor = "0";
172+
Set<String> result = new java.util.HashSet<>();
173+
var params = new redis.clients.jedis.params.ScanParams().match(PREFIX + "active:*").count(100);
174+
do {
175+
var scanResult = jedis.scan(cursor, params);
176+
for (String key : scanResult.getResult()) {
177+
result.add(key.substring((PREFIX + "active:").length()));
178+
}
179+
cursor = scanResult.getCursor();
180+
} while (!"0".equals(cursor));
181+
return result;
182+
}
180183
}
181184

182185
}

service.punishment/src/main/java/net/swofty/service/punishment/endpoints/PunishPlayerEndpoint.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public PunishPlayerProtocolObject.PunishPlayerResponse onMessage(ServiceProxyReq
3636
PunishmentId id = PunishmentId.generateId();
3737

3838
Instant now = Instant.now();
39-
Instant expiresAt = Instant.ofEpochMilli(messageObject.expiresAt());
40-
if (expiresAt.isBefore(Instant.now())) {
39+
if (messageObject.expiresAt() > 0 && Instant.ofEpochMilli(messageObject.expiresAt()).isBefore(now)) {
4140
return new PunishPlayerProtocolObject.PunishPlayerResponse(false, null, PunishPlayerProtocolObject.ErrorCode.INVALID_EXPIRY, "The expiration time provided is invalid.");
4241
}
4342

@@ -62,7 +61,7 @@ public PunishPlayerProtocolObject.PunishPlayerResponse onMessage(ServiceProxyReq
6261
messageObject.type(),
6362
messageObject.target(),
6463
reason.getReasonString(),
65-
expiresAt.toString()
64+
messageObject.expiresAt()
6665
);
6766
return new PunishPlayerProtocolObject.PunishPlayerResponse(true, id.id(), null, null);
6867
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.swofty.type.generic.command.commands;
22

33
import net.minestom.server.command.builder.arguments.Argument;
4-
import net.minestom.server.command.builder.arguments.ArgumentString;
54
import net.minestom.server.command.builder.arguments.ArgumentType;
65
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
76
import net.minestom.server.utils.mojang.MojangUtils;
@@ -11,6 +10,7 @@
1110
import net.swofty.type.generic.user.categories.Rank;
1211

1312
import java.io.IOException;
13+
import java.util.Set;
1414
import java.util.UUID;
1515

1616
@CommandParameters(
@@ -25,12 +25,11 @@ public class UnBanCommand extends HypixelCommand {
2525
@Override
2626
public void registerUsage(MinestomCommand command) {
2727
Argument<String> argument = ArgumentType.String("player").setSuggestionCallback((sender, context, suggestion) -> {
28-
PunishmentRedis.getAllBannedPlayerIds().thenAccept((id) -> {
29-
for (String playerName : id) {
30-
UUID playerUuid = UUID.fromString(playerName);
31-
suggestion.addEntry(new SuggestionEntry(playerUuid.toString()));
32-
}
33-
});
28+
if (!PunishmentRedis.isInitialized()) return;
29+
Set<String> ids = PunishmentRedis.getAllBannedPlayerIds();
30+
for (String playerId : ids) {
31+
suggestion.addEntry(new SuggestionEntry(playerId));
32+
}
3433
});
3534

3635
command.addSyntax((sender, context) -> {

0 commit comments

Comments
 (0)