Skip to content

Commit a89de5f

Browse files
Merge pull request #688 from ArikSquad/feat/punishments
feat: punishments
2 parents 1db2135 + d400b0a commit a89de5f

44 files changed

Lines changed: 1801 additions & 18 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

commons/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ dependencies {
2525
exclude(group = "org.jboss.shrinkwrap.resolver", module = "shrinkwrap-resolver-depchain")
2626
}
2727

28+
// Must match AtlasRedisAPI's Jedis version to avoid conflicts
29+
implementation("redis.clients:jedis:4.2.3")
30+
2831
implementation("org.spongepowered:configurate-yaml:4.2.0")
2932
}

commons/src/main/java/net/swofty/commons/ServiceType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public enum ServiceType {
99
PARTY,
1010
DARK_AUCTION,
1111
ORCHESTRATOR,
12-
FRIEND
12+
FRIEND,
13+
PUNISHMENT,
1314
;
1415
}

commons/src/main/java/net/swofty/commons/StringUtility.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,21 @@ public static String ntify(int i) {
300300
};
301301
};
302302
}
303+
304+
public static long parseDuration(String duration) {
305+
long totalMillis = 0;
306+
Pattern pattern = Pattern.compile("(\\d+)([dhms])");
307+
Matcher matcher = pattern.matcher(duration);
308+
while (matcher.find()) {
309+
int value = Integer.parseInt(matcher.group(1));
310+
char unit = matcher.group(2).charAt(0);
311+
switch (unit) {
312+
case 'd' -> totalMillis += TimeUnit.DAYS.toMillis(value);
313+
case 'h' -> totalMillis += TimeUnit.HOURS.toMillis(value);
314+
case 'm' -> totalMillis += TimeUnit.MINUTES.toMillis(value);
315+
case 's' -> totalMillis += TimeUnit.SECONDS.toMillis(value);
316+
}
317+
}
318+
return totalMillis;
319+
}
303320
}

commons/src/main/java/net/swofty/commons/config/ConfigProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ public class ConfigProvider {
4343
throw new RuntimeException("Failed to load configuration", e);
4444
}
4545
}
46+
4647
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package net.swofty.commons.protocol.objects.punishment;
2+
3+
import com.google.gson.Gson;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.Serializer;
6+
import net.swofty.commons.punishment.PunishmentReason;
7+
import net.swofty.commons.punishment.PunishmentTag;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
import org.json.JSONObject;
11+
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
public class GetActivePunishmentProtocolObject
16+
extends ProtocolObject<GetActivePunishmentProtocolObject.GetActivePunishmentMessage,
17+
GetActivePunishmentProtocolObject.GetActivePunishmentResponse> {
18+
19+
@Override
20+
public Serializer<GetActivePunishmentMessage> getSerializer() {
21+
return new Serializer<>() {
22+
@Override
23+
public String serialize(GetActivePunishmentMessage value) {
24+
JSONObject json = new JSONObject();
25+
json.put("target", value.target().toString());
26+
json.put("type", value.type());
27+
return json.toString();
28+
}
29+
30+
@Override
31+
public GetActivePunishmentMessage deserialize(String json) {
32+
JSONObject obj = new JSONObject(json);
33+
return new GetActivePunishmentMessage(
34+
UUID.fromString(obj.getString("target")),
35+
obj.getString("type")
36+
);
37+
}
38+
39+
@Override
40+
public GetActivePunishmentMessage clone(GetActivePunishmentMessage value) {
41+
return value;
42+
}
43+
};
44+
}
45+
46+
@Override
47+
public Serializer<GetActivePunishmentResponse> getReturnSerializer() {
48+
return new Serializer<>() {
49+
@Override
50+
public String serialize(GetActivePunishmentResponse value) {
51+
Gson gson = new Gson();
52+
JSONObject json = new JSONObject();
53+
json.put("found", value.found());
54+
json.put("type", value.type());
55+
json.put("banId", value.banId());
56+
json.put("reason", value.reason() != null ? gson.toJson(value.reason()) : null);
57+
json.put("expiresAt", value.expiresAt());
58+
json.put("tags", value.tags() != null ? gson.toJson(value.tags()) : null);
59+
return json.toString();
60+
}
61+
62+
@Override
63+
public GetActivePunishmentResponse deserialize(String json) {
64+
Gson gson = new Gson();
65+
JSONObject obj = new JSONObject(json);
66+
boolean found = obj.getBoolean("found");
67+
if (!found) {
68+
return new GetActivePunishmentResponse(false, null, null, null, 0, List.of());
69+
}
70+
List<PunishmentTag> tags = List.of();
71+
if (!obj.isNull("tags")) {
72+
tags = List.of(gson.fromJson(obj.getString("tags"), PunishmentTag[].class));
73+
}
74+
return new GetActivePunishmentResponse(
75+
true,
76+
obj.optString("type", null),
77+
obj.optString("banId", null),
78+
obj.isNull("reason") ? null : gson.fromJson(obj.getString("reason"), PunishmentReason.class),
79+
obj.getLong("expiresAt"),
80+
tags
81+
);
82+
}
83+
84+
@Override
85+
public GetActivePunishmentResponse clone(GetActivePunishmentResponse value) {
86+
return value;
87+
}
88+
};
89+
}
90+
91+
public record GetActivePunishmentMessage(
92+
@NotNull UUID target,
93+
@NotNull String type
94+
) {}
95+
96+
public record GetActivePunishmentResponse(
97+
boolean found,
98+
@Nullable String type,
99+
@Nullable String banId,
100+
@Nullable PunishmentReason reason,
101+
long expiresAt,
102+
@NotNull List<PunishmentTag> tags
103+
) {}
104+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package net.swofty.commons.protocol.objects.punishment;
2+
3+
import com.google.gson.Gson;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.Serializer;
6+
import net.swofty.commons.punishment.PunishmentReason;
7+
import net.swofty.commons.punishment.PunishmentTag;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
import org.json.JSONObject;
11+
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
public class PunishPlayerProtocolObject
16+
extends ProtocolObject<PunishPlayerProtocolObject.PunishPlayerMessage,
17+
PunishPlayerProtocolObject.PunishPlayerResponse> {
18+
19+
@Override
20+
public Serializer<PunishPlayerMessage> getSerializer() {
21+
return new Serializer<>() {
22+
23+
@Override
24+
public String serialize(PunishPlayerMessage value) {
25+
JSONObject json = new JSONObject();
26+
json.put("target", value.target().toString());
27+
json.put("type", value.type());
28+
json.put("reason", new Gson().toJson(value.reason()));
29+
json.put("expiresAt", value.expiresAt());
30+
json.put("tags", new Gson().toJson(value.tags()));
31+
json.put("staff", value.staff().toString());
32+
return json.toString();
33+
}
34+
35+
@Override
36+
public PunishPlayerMessage deserialize(String json) {
37+
JSONObject obj = new JSONObject(json);
38+
39+
return new PunishPlayerMessage(
40+
UUID.fromString(obj.getString("target")),
41+
obj.getString("type"),
42+
new Gson().fromJson(obj.getString("reason"), PunishmentReason.class),
43+
UUID.fromString(obj.getString("staff")),
44+
List.of(new Gson().fromJson(obj.getString("tags"), PunishmentTag[].class)),
45+
obj.getLong("expiresAt")
46+
);
47+
}
48+
49+
@Override
50+
public PunishPlayerMessage clone(PunishPlayerMessage value) {
51+
return value; // immutable
52+
}
53+
};
54+
}
55+
56+
@Override
57+
public Serializer<PunishPlayerResponse> getReturnSerializer() {
58+
return new Serializer<>() {
59+
60+
@Override
61+
public String serialize(PunishPlayerResponse value) {
62+
JSONObject json = new JSONObject();
63+
json.put("success", value.success());
64+
json.put("punishmentId", value.punishmentId());
65+
json.put("errorCode", value.errorCode());
66+
json.put("errorMessage", value.errorMessage());
67+
return json.toString();
68+
}
69+
70+
@Override
71+
public PunishPlayerResponse deserialize(String json) {
72+
JSONObject obj = new JSONObject(json);
73+
74+
return new PunishPlayerResponse(
75+
obj.getBoolean("success"),
76+
obj.optString("punishmentId", null),
77+
obj.optString("errorCode", null) != null ? ErrorCode.valueOf(obj.getString("errorCode")) : null,
78+
obj.optString("errorMessage", null)
79+
);
80+
}
81+
82+
@Override
83+
public PunishPlayerResponse clone(PunishPlayerResponse value) {
84+
return value; // immutable
85+
}
86+
};
87+
}
88+
89+
public record PunishPlayerMessage(
90+
@NotNull
91+
UUID target,
92+
@NotNull
93+
String type,
94+
@NotNull
95+
PunishmentReason reason,
96+
@NotNull
97+
UUID staff,
98+
List<PunishmentTag> tags,
99+
long expiresAt
100+
) {
101+
}
102+
103+
public record PunishPlayerResponse(
104+
boolean success,
105+
@Nullable
106+
String punishmentId,
107+
@Nullable
108+
ErrorCode errorCode,
109+
@Nullable
110+
String errorMessage
111+
) {
112+
}
113+
114+
public enum ErrorCode {
115+
INVALID_TYPE,
116+
DATABASE_ERROR,
117+
INVALID_EXPIRY,
118+
ALREADY_PUNISHED,
119+
UNKNOWN_ERROR
120+
}
121+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package net.swofty.commons.protocol.objects.punishment;
2+
3+
import net.swofty.commons.protocol.ProtocolObject;
4+
import net.swofty.commons.protocol.Serializer;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
import org.json.JSONObject;
8+
9+
import java.util.UUID;
10+
11+
public class UnpunishPlayerProtocolObject
12+
extends ProtocolObject<UnpunishPlayerProtocolObject.UnpunishPlayerMessage,
13+
UnpunishPlayerProtocolObject.UnpunishPlayerResponse> {
14+
15+
@Override
16+
public Serializer<UnpunishPlayerMessage> getSerializer() {
17+
return new Serializer<>() {
18+
@Override
19+
public String serialize(UnpunishPlayerMessage value) {
20+
JSONObject json = new JSONObject();
21+
json.put("target", value.target().toString());
22+
json.put("staff", value.staff().toString());
23+
json.put("type", value.type());
24+
return json.toString();
25+
}
26+
27+
@Override
28+
public UnpunishPlayerMessage deserialize(String json) {
29+
JSONObject obj = new JSONObject(json);
30+
return new UnpunishPlayerMessage(
31+
UUID.fromString(obj.getString("target")),
32+
UUID.fromString(obj.getString("staff")),
33+
obj.getString("type")
34+
);
35+
}
36+
37+
@Override
38+
public UnpunishPlayerMessage clone(UnpunishPlayerMessage value) {
39+
return value;
40+
}
41+
};
42+
}
43+
44+
@Override
45+
public Serializer<UnpunishPlayerResponse> getReturnSerializer() {
46+
return new Serializer<>() {
47+
@Override
48+
public String serialize(UnpunishPlayerResponse value) {
49+
JSONObject json = new JSONObject();
50+
json.put("success", value.success());
51+
json.put("errorMessage", value.errorMessage());
52+
return json.toString();
53+
}
54+
55+
@Override
56+
public UnpunishPlayerResponse deserialize(String json) {
57+
JSONObject obj = new JSONObject(json);
58+
return new UnpunishPlayerResponse(
59+
obj.getBoolean("success"),
60+
obj.isNull("errorMessage") ? null : obj.getString("errorMessage")
61+
);
62+
}
63+
64+
@Override
65+
public UnpunishPlayerResponse clone(UnpunishPlayerResponse value) {
66+
return value;
67+
}
68+
};
69+
}
70+
71+
public record UnpunishPlayerMessage(
72+
@NotNull UUID target,
73+
@NotNull UUID staff,
74+
@NotNull String type
75+
) {}
76+
77+
public record UnpunishPlayerResponse(
78+
boolean success,
79+
@Nullable String errorMessage
80+
) {}
81+
}

commons/src/main/java/net/swofty/commons/proxy/ToProxyChannels.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public enum ToProxyChannels {
1717
REGISTER_TEST_FLOW("register-test-flow", new RegisterTestFlowRequirements()),
1818
TEST_FLOW_SERVER_READY("test-flow-server-ready", new TestFlowServerReadyRequirements()),
1919
STAFF_CHAT("staff-chat", new StaffChatRequirements()),
20+
PUNISH_PLAYER("punish-player", new KickPlayerRequirements())
2021
;
2122

2223
@Getter
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.swofty.commons.proxy.requirements.to;
2+
3+
import net.swofty.commons.proxy.ProxyChannelRequirements;
4+
5+
import java.util.List;
6+
7+
public class KickPlayerRequirements extends ProxyChannelRequirements {
8+
@Override
9+
public List<RequiredKey> getRequiredKeysForProxy() {
10+
return List.of();
11+
}
12+
13+
@Override
14+
public List<RequiredKey> getRequiredKeysForServer() {
15+
return List.of(
16+
new RequiredKey("player_uuid"),
17+
new RequiredKey("message")
18+
);
19+
}
20+
}

commons/src/main/java/net/swofty/commons/proxy/requirements/to/PlayerHandlerRequirements.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public enum PlayerHandlerActions {
2828
REFRESH_COOP_DATA,
2929
MESSAGE,
3030
TRANSFER_WITH_UUID,
31-
GET_SERVER
31+
GET_SERVER,
32+
LIMBO,
3233
}
3334
}

0 commit comments

Comments
 (0)