Skip to content

Commit 87752e0

Browse files
refactor(punishment): route all queries through punishment service
Remove direct PunishmentRedis access from proxy and game server. Both now query the punishment service via protocol objects instead. - Fix ServerOutboundMessage.sendMessageToService to accept non-UUID filter IDs, enabling proxy-to-service communication - Register protocol objects in proxy before startListeners - Update SkyBlockVelocity.punished() to use GetActivePunishment - Update ActionPlayerMute to use GetActivePunishment - Update UnBanCommand suggestions to use GetAllBannedIds - Remove PunishmentRedis.connect() from HypixelGenericLoader
1 parent 827a2af commit 87752e0

5 files changed

Lines changed: 64 additions & 42 deletions

File tree

proxy.api/src/main/java/net/swofty/proxyapi/redis/ServerOutboundMessage.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,16 @@ public static void sendMessageToService(ServiceType service,
7474
Object rawMessage,
7575
Consumer<String> response) {
7676
UUID requestId = UUID.randomUUID();
77-
UUID toCallback = null;
78-
try {
79-
toCallback = UUID.fromString(RedisAPI.getInstance().getFilterId());
80-
} catch (Exception e) {
81-
return;
82-
}
77+
String callbackId = RedisAPI.getInstance().getFilterId();
78+
if (callbackId == null) return;
79+
8380
redisMessageListeners.put(requestId, response);
8481

8582
String message = specification.translateToString(rawMessage);
8683

8784
RedisAPI.getInstance().publishMessage(service.name(),
8885
ChannelRegistry.getFromName(specification.channel()),
89-
new ServiceProxyRequest(requestId, toCallback.toString(),
86+
new ServiceProxyRequest(requestId, callbackId,
9087
specification.channel(), message).toJSON().toString());
9188
}
9289

type.generic/src/main/java/net/swofty/type/generic/HypixelGenericLoader.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import net.swofty.commons.CustomWorlds;
2222
import net.swofty.commons.ServerType;
2323
import net.swofty.commons.config.ConfigProvider;
24-
import net.swofty.commons.punishment.PunishmentRedis;
2524
import net.swofty.type.generic.block.PlayerHeadBlockHandler;
2625
import net.swofty.type.generic.block.SignBlockHandler;
2726
import net.swofty.type.generic.command.HypixelCommand;
@@ -195,9 +194,6 @@ public void initialize(MinecraftServer server) {
195194
// Initialize leaderboard service (uses Redis for O(log N) leaderboard operations)
196195
LeaderboardService.connect(ConfigProvider.settings().getRedisUri());
197196

198-
// Initialize punishment Redis connection
199-
PunishmentRedis.connect(ConfigProvider.settings().getRedisUri());
200-
201197
// Load achievement and quest registries from YAML configuration
202198
AchievementRegistry.loadFromConfiguration();
203199
QuestRegistry.loadFromConfiguration();

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import net.minestom.server.command.builder.arguments.Argument;
44
import net.minestom.server.command.builder.arguments.ArgumentType;
55
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
6-
import net.minestom.server.utils.mojang.MojangUtils;
76
import net.swofty.commons.ServiceType;
7+
import net.swofty.commons.protocol.objects.punishment.GetAllBannedIdsProtocolObject;
88
import net.swofty.commons.protocol.objects.punishment.UnpunishPlayerProtocolObject;
9-
import net.swofty.commons.punishment.PunishmentRedis;
109
import net.swofty.proxyapi.ProxyService;
1110
import net.swofty.type.generic.command.CommandParameters;
1211
import net.swofty.type.generic.command.HypixelCommand;
@@ -29,10 +28,17 @@ public class UnBanCommand extends HypixelCommand {
2928
@Override
3029
public void registerUsage(MinestomCommand command) {
3130
Argument<String> argument = ArgumentType.String("player").setSuggestionCallback((sender, context, suggestion) -> {
32-
if (!PunishmentRedis.isInitialized()) return;
33-
Set<String> ids = PunishmentRedis.getAllBannedPlayerIds();
34-
for (String playerId : ids) {
35-
suggestion.addEntry(new SuggestionEntry(playerId));
31+
try {
32+
ProxyService service = new ProxyService(ServiceType.PUNISHMENT);
33+
var response = service.handleRequest(new GetAllBannedIdsProtocolObject.GetAllBannedIdsMessage())
34+
.orTimeout(2, TimeUnit.SECONDS)
35+
.join();
36+
if (response instanceof GetAllBannedIdsProtocolObject.GetAllBannedIdsResponse r) {
37+
for (String playerId : r.ids()) {
38+
suggestion.addEntry(new SuggestionEntry(playerId));
39+
}
40+
}
41+
} catch (Exception ignored) {
3642
}
3743
});
3844

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@
22

33
import net.minestom.server.entity.Player;
44
import net.minestom.server.event.player.PlayerChatEvent;
5+
import net.swofty.commons.ServiceType;
6+
import net.swofty.commons.protocol.objects.punishment.GetActivePunishmentProtocolObject;
57
import net.swofty.commons.punishment.PunishmentMessages;
68
import net.swofty.commons.punishment.PunishmentRedis;
79
import net.swofty.commons.punishment.PunishmentType;
10+
import net.swofty.proxyapi.ProxyService;
811
import net.swofty.type.generic.event.EventNodes;
912
import net.swofty.type.generic.event.HypixelEvent;
1013
import net.swofty.type.generic.event.HypixelEventClass;
1114

12-
import java.util.Optional;
15+
import java.util.concurrent.TimeUnit;
1316

1417
public class ActionPlayerMute implements HypixelEventClass {
1518

1619
@HypixelEvent(node = EventNodes.PLAYER, requireDataLoaded = false)
1720
public void onPlayerChat(PlayerChatEvent event) {
1821
Player player = event.getPlayer();
19-
Optional<PunishmentRedis.ActivePunishment> activePunishment = PunishmentRedis.getActive(player.getUuid());
20-
activePunishment.ifPresent(punishment -> {
21-
PunishmentType type = PunishmentType.valueOf(punishment.type());
22-
if (type != PunishmentType.MUTE) {
23-
return;
22+
try {
23+
var response = new ProxyService(ServiceType.PUNISHMENT)
24+
.handleRequest(new GetActivePunishmentProtocolObject.GetActivePunishmentMessage(player.getUuid()))
25+
.orTimeout(2, TimeUnit.SECONDS)
26+
.join();
27+
28+
if (response instanceof GetActivePunishmentProtocolObject.GetActivePunishmentResponse r
29+
&& r.found() && PunishmentType.valueOf(r.type()) == PunishmentType.MUTE) {
30+
event.setCancelled(true);
31+
var punishment = new PunishmentRedis.ActivePunishment(r.type(), r.banId(), r.reason(), r.expiresAt());
32+
player.sendMessage(PunishmentMessages.muteMessage(punishment));
2433
}
25-
event.setCancelled(true);
26-
player.sendMessage(PunishmentMessages.muteMessage(punishment));
27-
});
34+
} catch (Exception ignored) {
35+
}
2836
}
29-
3037
}

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@
3232
import io.netty.channel.ChannelPipeline;
3333
import lombok.Getter;
3434
import net.kyori.adventure.text.Component;
35+
import net.swofty.commons.ServiceType;
3536
import net.swofty.commons.ServerType;
3637
import net.swofty.commons.config.ConfigProvider;
3738
import net.swofty.commons.config.Settings;
39+
import net.swofty.commons.protocol.ProtocolObject;
40+
import net.swofty.commons.protocol.objects.punishment.GetActivePunishmentProtocolObject;
3841
import net.swofty.commons.proxy.FromProxyChannels;
3942
import net.swofty.commons.punishment.PunishmentMessages;
4043
import net.swofty.commons.punishment.PunishmentRedis;
4144
import net.swofty.commons.punishment.PunishmentType;
45+
import net.swofty.proxyapi.ProxyService;
46+
import net.swofty.proxyapi.redis.ServerOutboundMessage;
4247
import net.swofty.redisapi.api.RedisAPI;
4348
import net.swofty.velocity.command.ProtocolVersionCommand;
4449
import net.swofty.velocity.command.ServerStatusCommand;
@@ -66,7 +71,6 @@
6671
import java.nio.file.Path;
6772
import java.time.Duration;
6873
import java.util.List;
69-
import java.util.Optional;
7074
import java.util.Set;
7175
import java.util.concurrent.CompletableFuture;
7276
import java.util.concurrent.TimeUnit;
@@ -204,30 +208,42 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
204208
for (FromProxyChannels channel : FromProxyChannels.values()) {
205209
RedisMessage.registerProxyToServer(channel);
206210
}
211+
loopThroughPackage("net.swofty.commons.protocol.objects", ProtocolObject.class)
212+
.forEach(ServerOutboundMessage::registerFromProtocolObject);
207213
RedisAPI.getInstance().startListeners();
208214

209215
/**
210216
* Setup GameManager
211217
*/
212218
GameManager.loopServers(server);
213-
PunishmentRedis.connect(ConfigProvider.settings().getRedisUri());
214219
}
215220

216221
public boolean punished(Player player) {
217-
Optional<PunishmentRedis.ActivePunishment> activePunishment = PunishmentRedis.getActive(player.getUniqueId());
218-
if (activePunishment.isEmpty()) return false;
219-
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));
222+
try {
223+
var response = new ProxyService(ServiceType.PUNISHMENT)
224+
.handleRequest(new GetActivePunishmentProtocolObject.GetActivePunishmentMessage(player.getUniqueId()))
225+
.orTimeout(3, TimeUnit.SECONDS)
226+
.join();
227+
228+
if (!(response instanceof GetActivePunishmentProtocolObject.GetActivePunishmentResponse r) || !r.found()) {
229+
return false;
230+
}
231+
232+
PunishmentRedis.ActivePunishment punishment = new PunishmentRedis.ActivePunishment(
233+
r.type(), r.banId(), r.reason(), r.expiresAt());
234+
PunishmentType type = PunishmentType.valueOf(r.type());
235+
if (type == PunishmentType.BAN) {
236+
player.disconnect(PunishmentMessages.banMessage(punishment));
237+
return true;
238+
}
239+
if (type == PunishmentType.MUTE) {
240+
player.sendMessage(PunishmentMessages.muteMessage(punishment));
241+
}
242+
return false;
243+
} catch (Exception e) {
244+
return false;
228245
}
229-
return false;
230-
}
246+
}
231247

232248
@Subscribe
233249
public void onPlayerJoin(PlayerChooseInitialServerEvent event) {

0 commit comments

Comments
 (0)