Skip to content

Commit 7bb9b9b

Browse files
refactor(punishment): extract PunishmentMessages from PunishmentRedis (SRP)
Move message formatting (banMessage, muteMessage) into a dedicated PunishmentMessages class. PunishmentRedis now only handles connection management and data access. Also simplify SkyBlockVelocity.punished() to use early returns instead of AtomicBoolean.
1 parent 642d698 commit 7bb9b9b

5 files changed

Lines changed: 73 additions & 65 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package net.swofty.commons.punishment;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.swofty.commons.StringUtility;
5+
6+
public final class PunishmentMessages {
7+
private PunishmentMessages() {}
8+
9+
public static Component banMessage(PunishmentRedis.ActivePunishment punishment) {
10+
long expiresAt = punishment.expiresAt();
11+
PunishmentReason reason = punishment.reason();
12+
String banId = punishment.banId();
13+
14+
long timeLeft = expiresAt - System.currentTimeMillis();
15+
String prettyTimeLeft = StringUtility.formatTimeLeft(timeLeft);
16+
17+
String header;
18+
if (expiresAt <= 0) {
19+
header = "§cYou are permanently banned from this server!\n";
20+
} else {
21+
header = "§cYou are temporarily banned for §f" + prettyTimeLeft + " §cfrom this server!\n";
22+
}
23+
24+
String findOutMore = "";
25+
if (reason.getBanType() != null && reason.getBanType().getUrl() != null) {
26+
findOutMore = "§7Find out more: §b" + reason.getBanType().getUrl() + "\n";
27+
}
28+
29+
String footer = "§7Sharing your Ban ID may affect the processing of your appeal!";
30+
31+
return Component.text(header + "\n§7Reason: §f" + reason.getReasonString() + "\n" + findOutMore + "\n§7Ban ID: §f" + banId + "\n" + footer);
32+
}
33+
34+
public static Component muteMessage(PunishmentRedis.ActivePunishment punishment) {
35+
long expiresAt = punishment.expiresAt();
36+
PunishmentReason reason = punishment.reason();
37+
38+
long timeLeft = expiresAt - System.currentTimeMillis();
39+
String prettyTimeLeft = StringUtility.formatTimeLeft(timeLeft);
40+
41+
String line = "\n§c§m §r\n";
42+
43+
String header;
44+
if (expiresAt <= 0) {
45+
header = "§cYou are permanently muted on this server!\n";
46+
} else {
47+
header = "§cYou are currently muted for " + reason.getReasonString() + "\n";
48+
}
49+
String time = "§7Your mute will expire in §c" + prettyTimeLeft + "\n\n";
50+
51+
String urlInfo = "§7Find out more here: §fwww.hypixel.net/mutes\n";
52+
String footer = "§7Mute ID: §f" + punishment.banId();
53+
return Component.text(line + header + time + urlInfo + footer + line);
54+
}
55+
}

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package net.swofty.commons.punishment;
22

33
import com.google.gson.Gson;
4-
import net.kyori.adventure.text.Component;
5-
import net.swofty.commons.StringUtility;
64
import org.tinylog.Logger;
75
import redis.clients.jedis.Jedis;
86
import redis.clients.jedis.JedisPool;
@@ -119,53 +117,6 @@ public static CompletableFuture<Long> revoke(UUID playerId) {
119117

120118
public record ActivePunishment(String type, String banId, PunishmentReason reason, long expiresAt) {}
121119

122-
public static Component parseActivePunishmentBanMessage(ActivePunishment punishment) {
123-
long expiresAt = punishment.expiresAt();
124-
PunishmentReason reason = punishment.reason();
125-
String banId = punishment.banId();
126-
127-
long timeLeft = expiresAt - System.currentTimeMillis();
128-
String prettyTimeLeft = StringUtility.formatTimeLeft(timeLeft);
129-
130-
String header;
131-
if (expiresAt <= 0) {
132-
header = "§cYou are permanently banned from this server!\n";
133-
} else {
134-
header = "§cYou are temporarily banned for §f" + prettyTimeLeft + " §cfrom this server!\n";
135-
}
136-
137-
String findOutMore = "";
138-
if (reason.getBanType() != null && reason.getBanType().getUrl() != null) {
139-
findOutMore = "§7Find out more: §b" + reason.getBanType().getUrl() + "\n";
140-
}
141-
142-
String footer = "§7Sharing your Ban ID may affect the processing of your appeal!";
143-
144-
return Component.text(header + "\n§7Reason: §f" + reason.getReasonString() + "\n" + findOutMore + "\n§7Ban ID: §f" + banId + "\n" + footer);
145-
}
146-
147-
public static Component parseActivePunishmentMuteMessage(ActivePunishment punishment) {
148-
long expiresAt = punishment.expiresAt();
149-
PunishmentReason reason = punishment.reason();
150-
151-
long timeLeft = expiresAt - System.currentTimeMillis();
152-
String prettyTimeLeft = StringUtility.formatTimeLeft(timeLeft);
153-
154-
String line = "\n§c§m §r\n";
155-
156-
String header;
157-
if (expiresAt <= 0) {
158-
header = "§cYou are permanently muted on this server!\n";
159-
} else {
160-
header = "§cYou are currently muted for " + reason.getReasonString() + "\n";
161-
}
162-
String time = "§7Your mute will expire in §c" + prettyTimeLeft + "\n\n";
163-
164-
String urlInfo = "§7Find out more here: §fwww.hypixel.net/mutes\n";
165-
String footer = "§7Mute ID: §f" + punishment.banId();
166-
return Component.text(line + header + time + urlInfo + footer + line);
167-
}
168-
169120
public static Set<String> getAllBannedPlayerIds() {
170121
try (Jedis jedis = jedisPool.getResource()) {
171122
var cursor = "0";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.minestom.server.entity.Player;
44
import net.minestom.server.event.player.PlayerChatEvent;
5+
import net.swofty.commons.punishment.PunishmentMessages;
56
import net.swofty.commons.punishment.PunishmentRedis;
67
import net.swofty.commons.punishment.PunishmentType;
78
import net.swofty.type.generic.event.EventNodes;
@@ -22,7 +23,7 @@ public void onPlayerChat(PlayerChatEvent event) {
2223
return;
2324
}
2425
event.setCancelled(true);
25-
player.sendMessage(PunishmentRedis.parseActivePunishmentMuteMessage(punishment));
26+
player.sendMessage(PunishmentMessages.muteMessage(punishment));
2627
});
2728
}
2829

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import net.swofty.commons.config.ConfigProvider;
3737
import net.swofty.commons.config.Settings;
3838
import net.swofty.commons.proxy.FromProxyChannels;
39+
import net.swofty.commons.punishment.PunishmentMessages;
3940
import net.swofty.commons.punishment.PunishmentRedis;
4041
import net.swofty.commons.punishment.PunishmentType;
4142
import net.swofty.redisapi.api.RedisAPI;
@@ -69,7 +70,7 @@
6970
import java.util.Set;
7071
import java.util.concurrent.CompletableFuture;
7172
import java.util.concurrent.TimeUnit;
72-
import java.util.concurrent.atomic.AtomicBoolean;
73+
7374
import java.util.logging.Level;
7475
import java.util.logging.Logger;
7576
import java.util.stream.Collectors;
@@ -213,20 +214,19 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
213214
}
214215

215216
public boolean punished(Player player) {
216-
AtomicBoolean shouldConnect = new AtomicBoolean(true);
217217
Optional<PunishmentRedis.ActivePunishment> activePunishment = PunishmentRedis.getActive(player.getUniqueId());
218-
activePunishment.ifPresent(punishment -> {
219-
PunishmentType type = PunishmentType.valueOf(punishment.type());
220-
if (type == PunishmentType.BAN) {
221-
player.disconnect(PunishmentRedis.parseActivePunishmentBanMessage(punishment));
222-
shouldConnect.set(false);
223-
}
224-
if (type == PunishmentType.MUTE) {
225-
player.sendMessage(PunishmentRedis.parseActivePunishmentMuteMessage(punishment));
226-
}
227-
});
218+
if (activePunishment.isEmpty()) return false;
228219

229-
return !shouldConnect.get();
220+
PunishmentRedis.ActivePunishment punishment = activePunishment.get();
221+
PunishmentType type = PunishmentType.valueOf(punishment.type());
222+
if (type == PunishmentType.BAN) {
223+
player.disconnect(PunishmentMessages.banMessage(punishment));
224+
return true;
225+
}
226+
if (type == PunishmentType.MUTE) {
227+
player.sendMessage(PunishmentMessages.muteMessage(punishment));
228+
}
229+
return false;
230230
}
231231

232232
@Subscribe

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.swofty.commons.proxy.ToProxyChannels;
77
import net.swofty.commons.punishment.PunishmentId;
88
import net.swofty.commons.punishment.PunishmentReason;
9+
import net.swofty.commons.punishment.PunishmentMessages;
910
import net.swofty.commons.punishment.PunishmentRedis;
1011
import net.swofty.commons.punishment.PunishmentType;
1112
import net.swofty.commons.punishment.template.BanType;
@@ -56,10 +57,10 @@ public JSONObject receivedMessage(JSONObject message, UUID serverUUID) {
5657
PunishmentRedis.ActivePunishment activePunishment = new PunishmentRedis.ActivePunishment(type, id, finalReason, expiresAt);
5758
switch (punishmentType) {
5859
case BAN -> {
59-
player.disconnect(PunishmentRedis.parseActivePunishmentBanMessage(activePunishment));
60+
player.disconnect(PunishmentMessages.banMessage(activePunishment));
6061
}
6162
case MUTE -> {
62-
player.sendMessage(PunishmentRedis.parseActivePunishmentMuteMessage(activePunishment));
63+
player.sendMessage(PunishmentMessages.muteMessage(activePunishment));
6364
}
6465
case WARNING -> {
6566
player.sendMessage(Component.text("§c[WARNING] §7You have received a warning for the following reason: §e" + finalReason));

0 commit comments

Comments
 (0)