Skip to content

Commit e83e21d

Browse files
committed
feat(velocity): wire whenOnline queue with player join and disconnect lifecycle
1 parent 65c1c86 commit e83e21d

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

velocity/src/main/java/dev/objz/commandbridge/velocity/Main.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public void onProxyInitialization(ProxyInitializeEvent e) {
160160
pluginMessageQueue = new PluginMessageQueue();
161161
sessions.onRemove(session -> {
162162
playerTracker.remove(session.id());
163+
pluginMessageQueue.removeByServer(session.id());
163164
if (api != null) {
164165
api.onServerDisconnected(session);
165166
}
@@ -211,6 +212,14 @@ public void onProxyInitialization(ProxyInitializeEvent e) {
211212

212213
api = new VelocityCommandBridgeImpl(sessions, playerTracker, cfg.serverId(), endpointServer, pluginMessageQueue);
213214

215+
playerTracker.onPlayerJoin((clientId, uuid) -> {
216+
if (api != null) {
217+
for (var msg : pluginMessageQueue.drain(clientId, uuid)) {
218+
api.replayQueuedMessage(msg);
219+
}
220+
}
221+
});
222+
214223
installRoutes();
215224

216225
command = new CBCommand(

velocity/src/main/java/dev/objz/commandbridge/velocity/api/PluginMessageQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final class PluginMessageQueue {
2121
private static final long TTL_MS = TimeUnit.MINUTES.toMillis(5);
2222
private static final int MAX_TOTAL = 1000;
2323

24-
record QueuedMessage(Platform.ServerTarget target, PluginMessage message, String from, long queuedAt) {
24+
public record QueuedMessage(Platform.ServerTarget target, PluginMessage message, String from, long queuedAt) {
2525
}
2626

2727
private final ConcurrentHashMap<UUID, Queue<QueuedMessage>> queues = new ConcurrentHashMap<>();

velocity/src/main/java/dev/objz/commandbridge/velocity/api/VelocityCommandBridgeImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,27 @@ private void sendErrorResponse(Envelope originalEnv, String error) {
394394
});
395395
}
396396

397+
public void replayQueuedMessage(PluginMessageQueue.QueuedMessage msg) {
398+
if (msg.from() == null) {
399+
sendPluginMessage(msg.target(), msg.message()).exceptionally(ex -> {
400+
Log.warn("Failed to replay queued plugin message: {}", ex.getMessage());
401+
return null;
402+
});
403+
return;
404+
}
405+
Optional<ClientSession> targetSession = sessions.findSession(msg.target().id(),
406+
toLocation(msg.target().type()));
407+
if (targetSession.isEmpty() || !isRoutable(targetSession.get())) {
408+
return;
409+
}
410+
Envelope env = Envelope.make(MessageType.PLUGIN_MESSAGE, msg.from(),
411+
msg.target().id(), Envelope.MAPPER.valueToTree(msg.message()));
412+
endpointServer.send(targetSession.get().endpoint(), env).dispatch().exceptionally(ex -> {
413+
Log.warn("Failed to replay queued plugin message from {}: {}", msg.from(), ex.getMessage());
414+
return null;
415+
});
416+
}
417+
397418
private PluginMessage readPluginMessage(Envelope env) {
398419
try {
399420
return Envelope.MAPPER.treeToValue(env.payload(), PluginMessage.class);

0 commit comments

Comments
 (0)