Skip to content

Commit c48e755

Browse files
fix(punishment): update protocol objects and endpoints for per-type queries
- GetActivePunishmentMessage now requires a type field - UnpunishPlayerMessage now carries a type field (enables /unmute) - UnpunishPlayerEndpoint no longer hard-codes BAN-only - PunishPlayerEndpoint wraps save in try/catch returning DATABASE_ERROR - PunishPlayerEndpoint includes tags in pub/sub JSON payload - Fixed UnpunishPlayerProtocolObject null deserialization (optString -> isNull)
1 parent 1a54367 commit c48e755

5 files changed

Lines changed: 44 additions & 32 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ public Serializer<GetActivePunishmentMessage> getSerializer() {
2323
public String serialize(GetActivePunishmentMessage value) {
2424
JSONObject json = new JSONObject();
2525
json.put("target", value.target().toString());
26+
json.put("type", value.type());
2627
return json.toString();
2728
}
2829

2930
@Override
3031
public GetActivePunishmentMessage deserialize(String json) {
3132
JSONObject obj = new JSONObject(json);
3233
return new GetActivePunishmentMessage(
33-
UUID.fromString(obj.getString("target"))
34+
UUID.fromString(obj.getString("target")),
35+
obj.getString("type")
3436
);
3537
}
3638

@@ -87,7 +89,8 @@ public GetActivePunishmentResponse clone(GetActivePunishmentResponse value) {
8789
}
8890

8991
public record GetActivePunishmentMessage(
90-
@NotNull UUID target
92+
@NotNull UUID target,
93+
@NotNull String type
9194
) {}
9295

9396
public record GetActivePunishmentResponse(

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public String serialize(UnpunishPlayerMessage value) {
2020
JSONObject json = new JSONObject();
2121
json.put("target", value.target().toString());
2222
json.put("staff", value.staff().toString());
23+
json.put("type", value.type());
2324
return json.toString();
2425
}
2526

@@ -28,7 +29,8 @@ public UnpunishPlayerMessage deserialize(String json) {
2829
JSONObject obj = new JSONObject(json);
2930
return new UnpunishPlayerMessage(
3031
UUID.fromString(obj.getString("target")),
31-
UUID.fromString(obj.getString("staff"))
32+
UUID.fromString(obj.getString("staff")),
33+
obj.getString("type")
3234
);
3335
}
3436

@@ -55,7 +57,7 @@ public UnpunishPlayerResponse deserialize(String json) {
5557
JSONObject obj = new JSONObject(json);
5658
return new UnpunishPlayerResponse(
5759
obj.getBoolean("success"),
58-
obj.optString("errorMessage", null)
60+
obj.isNull("errorMessage") ? null : obj.getString("errorMessage")
5961
);
6062
}
6163

@@ -68,7 +70,8 @@ public UnpunishPlayerResponse clone(UnpunishPlayerResponse value) {
6870

6971
public record UnpunishPlayerMessage(
7072
@NotNull UUID target,
71-
@NotNull UUID staff
73+
@NotNull UUID staff,
74+
@NotNull String type
7275
) {}
7376

7477
public record UnpunishPlayerResponse(

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
@@ -7,6 +7,7 @@
77
import net.swofty.commons.punishment.PunishmentRedis;
88
import net.swofty.service.generic.redis.ServiceEndpoint;
99

10+
import java.util.List;
1011
import java.util.Optional;
1112

1213
public class GetActivePunishmentEndpoint implements ServiceEndpoint
@@ -20,9 +21,9 @@ public ProtocolObject<GetActivePunishmentProtocolObject.GetActivePunishmentMessa
2021

2122
@Override
2223
public GetActivePunishmentProtocolObject.GetActivePunishmentResponse onMessage(ServiceProxyRequest message, GetActivePunishmentProtocolObject.GetActivePunishmentMessage messageObject) {
23-
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target());
24+
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target(), messageObject.type());
2425
if (existing.isEmpty()) {
25-
return new GetActivePunishmentProtocolObject.GetActivePunishmentResponse(false, null, null, null, 0, java.util.List.of());
26+
return new GetActivePunishmentProtocolObject.GetActivePunishmentResponse(false, null, null, null, 0, List.of());
2627
}
2728

2829
ActivePunishment punishment = existing.get();

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.swofty.service.punishment.endpoints;
22

3+
import com.google.gson.Gson;
34
import net.swofty.commons.impl.ServiceProxyRequest;
45
import net.swofty.commons.protocol.ProtocolObject;
56
import net.swofty.commons.protocol.objects.punishment.PunishPlayerProtocolObject;
@@ -38,28 +39,32 @@ public PunishPlayerProtocolObject.PunishPlayerResponse onMessage(ServiceProxyReq
3839

3940
boolean hasOverwriteTag = messageObject.tags() != null && messageObject.tags().contains(PunishmentTag.OVERWRITE);
4041
if (!hasOverwriteTag) {
41-
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target());
42+
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target(), messageObject.type());
4243
if (existing.isPresent()) {
43-
ActivePunishment active = existing.get();
44-
PunishmentType existingType = PunishmentType.valueOf(active.type());
45-
if (existingType == punishmentType) {
46-
return new PunishPlayerProtocolObject.PunishPlayerResponse(false, null,
47-
PunishPlayerProtocolObject.ErrorCode.ALREADY_PUNISHED, active.banId());
48-
}
44+
return new PunishPlayerProtocolObject.PunishPlayerResponse(false, null,
45+
PunishPlayerProtocolObject.ErrorCode.ALREADY_PUNISHED, existing.get().banId());
4946
}
5047
}
5148

5249
PunishmentReason reason = messageObject.reason();
5350
PunishmentId id = PunishmentId.generateId();
5451

55-
PunishmentRedis.saveActivePunishment(
56-
messageObject.target(),
57-
messageObject.type(),
58-
id.id(),
59-
reason,
60-
messageObject.expiresAt(),
61-
messageObject.tags()
62-
);
52+
try {
53+
PunishmentRedis.saveActivePunishment(
54+
messageObject.target(),
55+
messageObject.type(),
56+
id.id(),
57+
reason,
58+
messageObject.expiresAt(),
59+
messageObject.tags()
60+
);
61+
} catch (Exception e) {
62+
Logger.error("Failed to save punishment to Redis", e);
63+
return new PunishPlayerProtocolObject.PunishPlayerResponse(false, null,
64+
PunishPlayerProtocolObject.ErrorCode.DATABASE_ERROR, "Failed to save punishment.");
65+
}
66+
67+
Gson gson = new Gson();
6368
ProxyRedis.publishToProxy(ToProxyChannels.PUNISH_PLAYER, new JSONObject()
6469
.put("target", messageObject.target())
6570
.put("type", messageObject.type())
@@ -69,6 +74,7 @@ public PunishPlayerProtocolObject.PunishPlayerResponse onMessage(ServiceProxyReq
6974
.put("staff", messageObject.staff())
7075
.put("issuedAt", now.toEpochMilli())
7176
.put("expiresAt", messageObject.expiresAt())
77+
.put("tags", messageObject.tags() != null ? gson.toJson(messageObject.tags()) : null)
7278
);
7379
Logger.info("Issued {} punishment to {} for reason '{}' (expires at: {})",
7480
messageObject.type(),

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.swofty.commons.protocol.objects.punishment.UnpunishPlayerProtocolObject;
66
import net.swofty.commons.punishment.ActivePunishment;
77
import net.swofty.commons.punishment.PunishmentRedis;
8-
import net.swofty.commons.punishment.PunishmentType;
98
import net.swofty.service.generic.redis.ServiceEndpoint;
109
import org.tinylog.Logger;
1110

@@ -22,20 +21,20 @@ public ProtocolObject<UnpunishPlayerProtocolObject.UnpunishPlayerMessage, Unpuni
2221

2322
@Override
2423
public UnpunishPlayerProtocolObject.UnpunishPlayerResponse onMessage(ServiceProxyRequest message, UnpunishPlayerProtocolObject.UnpunishPlayerMessage messageObject) {
25-
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target());
24+
Optional<ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target(), messageObject.type());
2625
if (existing.isEmpty()) {
27-
return new UnpunishPlayerProtocolObject.UnpunishPlayerResponse(false, "No active punishment found for this player.");
26+
return new UnpunishPlayerProtocolObject.UnpunishPlayerResponse(false, "No active " + messageObject.type().toLowerCase() + " found for this player.");
2827
}
2928

30-
ActivePunishment punishment = existing.get();
31-
PunishmentType type = PunishmentType.valueOf(punishment.type());
32-
if (type != PunishmentType.BAN) {
33-
return new UnpunishPlayerProtocolObject.UnpunishPlayerResponse(false, "Player is not banned (active punishment is " + type.name() + ").");
29+
try {
30+
PunishmentRedis.revoke(messageObject.target(), messageObject.type());
31+
} catch (Exception e) {
32+
Logger.error("Failed to revoke punishment", e);
33+
return new UnpunishPlayerProtocolObject.UnpunishPlayerResponse(false, "Failed to revoke punishment from database.");
3434
}
3535

36-
PunishmentRedis.revoke(messageObject.target()).join();
37-
Logger.info("Revoked ban for {} by staff {}",
38-
messageObject.target(), messageObject.staff());
36+
Logger.info("Revoked {} for {} by staff {}",
37+
messageObject.type(), messageObject.target(), messageObject.staff());
3938
return new UnpunishPlayerProtocolObject.UnpunishPlayerResponse(true, null);
4039
}
4140
}

0 commit comments

Comments
 (0)