Skip to content

Commit d52b19c

Browse files
feat(punishment): persist PunishmentTags to Redis with active punishments
1 parent 7656fb4 commit d52b19c

8 files changed

Lines changed: 42 additions & 36 deletions

File tree

commons/src/main/java/net/swofty/commons/protocol/objects/punishment/GetActivePunishmentProtocolObject.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import net.swofty.commons.protocol.ProtocolObject;
55
import net.swofty.commons.protocol.Serializer;
66
import net.swofty.commons.punishment.PunishmentReason;
7+
import net.swofty.commons.punishment.PunishmentTag;
78
import org.jetbrains.annotations.NotNull;
89
import org.jetbrains.annotations.Nullable;
910
import org.json.JSONObject;
1011

12+
import java.util.List;
1113
import java.util.UUID;
1214

1315
public class GetActivePunishmentProtocolObject
@@ -44,28 +46,36 @@ public Serializer<GetActivePunishmentResponse> getReturnSerializer() {
4446
return new Serializer<>() {
4547
@Override
4648
public String serialize(GetActivePunishmentResponse value) {
49+
Gson gson = new Gson();
4750
JSONObject json = new JSONObject();
4851
json.put("found", value.found());
4952
json.put("type", value.type());
5053
json.put("banId", value.banId());
51-
json.put("reason", value.reason() != null ? new Gson().toJson(value.reason()) : null);
54+
json.put("reason", value.reason() != null ? gson.toJson(value.reason()) : null);
5255
json.put("expiresAt", value.expiresAt());
56+
json.put("tags", value.tags() != null ? gson.toJson(value.tags()) : null);
5357
return json.toString();
5458
}
5559

5660
@Override
5761
public GetActivePunishmentResponse deserialize(String json) {
62+
Gson gson = new Gson();
5863
JSONObject obj = new JSONObject(json);
5964
boolean found = obj.getBoolean("found");
6065
if (!found) {
61-
return new GetActivePunishmentResponse(false, null, null, null, 0);
66+
return new GetActivePunishmentResponse(false, null, null, null, 0, List.of());
67+
}
68+
List<PunishmentTag> tags = List.of();
69+
if (!obj.isNull("tags")) {
70+
tags = List.of(gson.fromJson(obj.getString("tags"), PunishmentTag[].class));
6271
}
6372
return new GetActivePunishmentResponse(
6473
true,
6574
obj.optString("type", null),
6675
obj.optString("banId", null),
67-
obj.isNull("reason") ? null : new Gson().fromJson(obj.getString("reason"), PunishmentReason.class),
68-
obj.getLong("expiresAt")
76+
obj.isNull("reason") ? null : gson.fromJson(obj.getString("reason"), PunishmentReason.class),
77+
obj.getLong("expiresAt"),
78+
tags
6979
);
7080
}
7181

@@ -85,6 +95,7 @@ public record GetActivePunishmentResponse(
8595
@Nullable String type,
8696
@Nullable String banId,
8797
@Nullable PunishmentReason reason,
88-
long expiresAt
98+
long expiresAt,
99+
@NotNull List<PunishmentTag> tags
89100
) {}
90101
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package net.swofty.commons.punishment;
22

3-
public record ActivePunishment(String type, String banId, PunishmentReason reason, long expiresAt) {}
3+
import java.util.List;
4+
5+
public record ActivePunishment(String type, String banId, PunishmentReason reason, long expiresAt, List<PunishmentTag> tags) {}

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.time.Duration;
1111
import java.util.Map;
1212
import java.util.Optional;
13-
import java.util.Set;
1413
import java.util.UUID;
1514
import java.util.concurrent.CompletableFuture;
1615

@@ -61,17 +60,20 @@ public static boolean isInitialized() {
6160
return initialized && jedisPool != null && !jedisPool.isClosed();
6261
}
6362

64-
public static void saveActivePunishment(UUID playerId, String type, String id, PunishmentReason reason, long expiresAt) {
63+
public static void saveActivePunishment(UUID playerId, String type, String id, PunishmentReason reason, long expiresAt, java.util.List<PunishmentTag> tags) {
6564
try (Jedis jedis = jedisPool.getResource()) {
6665
String key = PREFIX + "active:" + playerId;
6766

6867
Gson gson = new Gson();
69-
Map<String, String> data = Map.of(
68+
java.util.HashMap<String, String> data = new java.util.HashMap<>(Map.of(
7069
"type", type,
7170
"banId", id,
72-
"reason", gson.toJson(reason), // most likely not optimal for performance
71+
"reason", gson.toJson(reason),
7372
"expiresAt", String.valueOf(expiresAt)
74-
);
73+
));
74+
if (tags != null && !tags.isEmpty()) {
75+
data.put("tags", gson.toJson(tags));
76+
}
7577

7678
jedis.hset(key, data);
7779
if (expiresAt > 0) {
@@ -100,9 +102,15 @@ public static Optional<ActivePunishment> getActive(UUID playerId) {
100102
}
101103

102104
Gson gson = new Gson();
103-
PunishmentReason reason = gson.fromJson(data.get("reason"), PunishmentReason.class); // most likely not optimal for performance
105+
PunishmentReason reason = gson.fromJson(data.get("reason"), PunishmentReason.class);
106+
107+
java.util.List<PunishmentTag> tags = java.util.List.of();
108+
String tagsJson = data.get("tags");
109+
if (tagsJson != null && !tagsJson.isBlank()) {
110+
tags = java.util.List.of(gson.fromJson(tagsJson, PunishmentTag[].class));
111+
}
104112

105-
return Optional.of(new ActivePunishment(type, banId, reason, expiresAt));
113+
return Optional.of(new ActivePunishment(type, banId, reason, expiresAt, tags));
106114
}
107115
}
108116

@@ -114,21 +122,4 @@ public static CompletableFuture<Long> revoke(UUID playerId) {
114122
}
115123
});
116124
}
117-
118-
public static Set<String> getAllBannedPlayerIds() {
119-
try (Jedis jedis = jedisPool.getResource()) {
120-
var cursor = "0";
121-
Set<String> result = new java.util.HashSet<>();
122-
var params = new redis.clients.jedis.params.ScanParams().match(PREFIX + "active:*").count(100);
123-
do {
124-
var scanResult = jedis.scan(cursor, params);
125-
for (String key : scanResult.getResult()) {
126-
result.add(key.substring((PREFIX + "active:").length()));
127-
}
128-
cursor = scanResult.getCursor();
129-
} while (!"0".equals(cursor));
130-
return result;
131-
}
132-
}
133-
134125
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ProtocolObject<GetActivePunishmentProtocolObject.GetActivePunishmentMessa
2222
public GetActivePunishmentProtocolObject.GetActivePunishmentResponse onMessage(ServiceProxyRequest message, GetActivePunishmentProtocolObject.GetActivePunishmentMessage messageObject) {
2323
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target());
2424
if (existing.isEmpty()) {
25-
return new GetActivePunishmentProtocolObject.GetActivePunishmentResponse(false, null, null, null, 0);
25+
return new GetActivePunishmentProtocolObject.GetActivePunishmentResponse(false, null, null, null, 0, java.util.List.of());
2626
}
2727

2828
ActivePunishment punishment = existing.get();
@@ -31,7 +31,8 @@ public GetActivePunishmentProtocolObject.GetActivePunishmentResponse onMessage(S
3131
punishment.type(),
3232
punishment.banId(),
3333
punishment.reason(),
34-
punishment.expiresAt()
34+
punishment.expiresAt(),
35+
punishment.tags()
3536
);
3637
}
3738
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public PunishPlayerProtocolObject.PunishPlayerResponse onMessage(ServiceProxyReq
5757
messageObject.type(),
5858
id.id(),
5959
reason,
60-
messageObject.expiresAt()
60+
messageObject.expiresAt(),
61+
messageObject.tags()
6162
);
6263
ProxyRedis.publishToProxy(ToProxyChannels.PUNISH_PLAYER, new JSONObject()
6364
.put("target", messageObject.target())

type.generic/src/main/java/net/swofty/type/generic/event/actions/ActionPlayerMute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void onPlayerChat(PlayerChatEvent event) {
2828
if (response instanceof GetActivePunishmentProtocolObject.GetActivePunishmentResponse r
2929
&& r.found() && PunishmentType.valueOf(r.type()) == PunishmentType.MUTE) {
3030
event.setCancelled(true);
31-
var punishment = new ActivePunishment(r.type(), r.banId(), r.reason(), r.expiresAt());
31+
var punishment = new ActivePunishment(r.type(), r.banId(), r.reason(), r.expiresAt(), r.tags());
3232
player.sendMessage(PunishmentMessages.muteMessage(punishment));
3333
}
3434
} catch (Exception ignored) {

velocity.extension/src/main/java/net/swofty/velocity/SkyBlockVelocity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public boolean punished(Player player) {
230230
}
231231

232232
ActivePunishment punishment = new ActivePunishment(
233-
r.type(), r.banId(), r.reason(), r.expiresAt());
233+
r.type(), r.banId(), r.reason(), r.expiresAt(), r.tags());
234234
PunishmentType type = PunishmentType.valueOf(r.type());
235235
if (type == PunishmentType.BAN) {
236236
player.disconnect(PunishmentMessages.banMessage(punishment));

velocity.extension/src/main/java/net/swofty/velocity/redis/listeners/ListenerPlayerPunished.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public JSONObject receivedMessage(JSONObject message, UUID serverUUID) {
5454
PunishmentType punishmentType = PunishmentType.valueOf(type);
5555
PunishmentReason finalReason = reason;
5656
SkyBlockVelocity.getServer().getPlayer(target).ifPresent((player) -> {
57-
ActivePunishment activePunishment = new ActivePunishment(type, id, finalReason, expiresAt);
57+
ActivePunishment activePunishment = new ActivePunishment(type, id, finalReason, expiresAt, java.util.List.of());
5858
switch (punishmentType) {
5959
case BAN -> {
6060
player.disconnect(PunishmentMessages.banMessage(activePunishment));

0 commit comments

Comments
 (0)