Skip to content

Commit 74a5e34

Browse files
committed
refactor: api removed broadcast, becasue it was moved to typed MessageChannel
1 parent 34620cc commit 74a5e34

6 files changed

Lines changed: 70 additions & 43 deletions

File tree

README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,49 @@ CB has a public API module for other plugins to interact with the bridge network
103103
```java
104104
CommandBridgeAPI api = CommandBridgeProvider.get();
105105

106-
// send a command as console to a backend
107-
CommandChannel commands = api.channel(Channels.COMMAND);
108-
commands.console(Platform.BACKEND.target("survival-1"), "say hello");
106+
// get a typed message channel
107+
MessageChannel<CommandPayload> commands = api.channel(Channels.COMMAND);
109108

110-
// listen for incoming messages
111-
Subscription sub = commands.listen((ctx, payload) -> { /* ... */ });
109+
// send a command as console to a specific backend
110+
commands.send(Platform.BACKEND.target("survival-1"), CommandPayload.console("say hello"));
112111

113-
// server events
114-
api.onServerConnected(server -> { /* ... */ });
115-
api.onServerDisconnected(server -> { /* ... */ });
112+
// send a command as a player
113+
commands.send(Platform.BACKEND.target("survival-1"), CommandPayload.player("spawn", playerUuid));
116114

117-
// find a player across the network
118-
api.playerLocator().ifPresent(locator -> locator.locate(playerUuid));
115+
// broadcast a command to all connected servers
116+
commands.broadcast(CommandPayload.console("say maintenance in 5 minutes"));
119117

120-
// broadcast to all servers
121-
api.broadcast(commands, CommandPayload.console("say maintenance in 5 minutes"));
118+
// request-response (waits for the target to reply)
119+
commands.request(Platform.BACKEND.target("survival-1"), CommandPayload.console("list"))
120+
.thenAccept(response -> { /* handle response */ });
121+
122+
// listen for incoming messages on this channel
123+
Subscription sub = commands.listen((ctx, payload) -> {
124+
Platform.ServerTarget from = ctx.from();
125+
String command = payload.command();
126+
});
127+
sub.cancel(); // unsubscribe when done
128+
129+
// server lifecycle events
130+
api.onServerConnected(server -> { /* server joined the network */ });
131+
api.onServerDisconnected(server -> { /* server left the network */ });
132+
133+
// connection state monitoring
134+
api.onConnectionStateChanged(state -> {
135+
if (state.canSend()) { /* ready to send messages */ }
136+
});
137+
138+
// current server identity and state
139+
Platform.ServerTarget self = api.server();
140+
ConnectionState state = api.connectionState();
141+
142+
// list all connected servers (available on velocity)
143+
api.connectedServers().ifPresent(servers -> { /* Set<String> of server IDs */ });
144+
145+
// find which server a player is on (available on velocity)
146+
api.playerLocator().ifPresent(locator ->
147+
locator.locate(playerUuid).ifPresent(server -> { /* player is on this server */ })
148+
);
122149
```
123150

124151
---

api/src/main/java/dev/objz/commandbridge/api/CommandBridgeAPI.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import java.util.Optional;
1313
import java.util.Set;
14-
import java.util.concurrent.CompletableFuture;
1514
import java.util.function.Consumer;
1615

1716
/** Main entry point for interacting with the CommandBridge network. */
@@ -27,15 +26,6 @@ public interface CommandBridgeAPI {
2726
*/
2827
<T extends ChannelPayload, C extends MessageChannel<T>> C channel(ChannelType<T, C> type);
2928

30-
/**
31-
* Broadcasts a payload to all connected servers on a specific channel.
32-
*
33-
* @param channel the channel to broadcast on
34-
* @param payload the data to send
35-
* @return a future that completes when the broadcast is dispatched
36-
*/
37-
<P extends ChannelPayload> CompletableFuture<Void> broadcast(MessageChannel<P> channel, P payload);
38-
3929
/** @return the identity of the current server */
4030
Platform.ServerTarget server();
4131

api/src/main/java/dev/objz/commandbridge/api/channel/MessageChannel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public interface MessageChannel<P extends ChannelPayload> {
2323
*/
2424
CompletableFuture<Void> send(Platform.ServerTarget target, P payload);
2525

26+
/**
27+
* Broadcasts a payload to all connected servers.
28+
*
29+
* @param payload the data to send
30+
* @return a future that completes when the broadcast is dispatched
31+
*/
32+
CompletableFuture<Void> broadcast(P payload);
33+
2634
/**
2735
* Sends a request to a target server and waits for a response.
2836
*

backends/src/main/java/dev/objz/commandbridge/backends/api/BackendCommandBridgeImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ public <T extends ChannelPayload, C extends MessageChannel<T>> C channel(Channel
8383
return typeCast(channel);
8484
}
8585

86-
@Override
87-
public <P extends ChannelPayload> CompletableFuture<Void> broadcast(MessageChannel<P> channel, P payload) {
88-
Objects.requireNonNull(channel);
89-
Objects.requireNonNull(payload);
90-
return channel.send(Platform.BACKEND.target("*"), payload);
91-
}
92-
9386
@Override
9487
public Platform.ServerTarget server() {
9588
return Platform.BACKEND.target(localServerId());

core/src/main/java/dev/objz/commandbridge/net/channel/PluginMessageChannel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public CompletableFuture<Void> send(Platform.ServerTarget target, P payload) {
5353
return sendTransport.send(target, toPluginMessage(payload, false));
5454
}
5555

56+
@Override
57+
public CompletableFuture<Void> broadcast(P payload) {
58+
Objects.requireNonNull(payload);
59+
return sendTransport.send(Platform.BACKEND.target("*"), toPluginMessage(payload, false));
60+
}
61+
5662
@Override
5763
public CompletableFuture<P> request(Platform.ServerTarget target, P payload) {
5864
return request(target, payload, Duration.ofSeconds(15));

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ public <T extends ChannelPayload, C extends MessageChannel<T>> C channel(Channel
7171
return typeCast(channel);
7272
}
7373

74-
@Override
75-
public <P extends ChannelPayload> CompletableFuture<Void> broadcast(MessageChannel<P> channel, P payload) {
76-
Objects.requireNonNull(channel);
77-
Objects.requireNonNull(payload);
78-
List<CompletableFuture<Void>> futures = new ArrayList<>();
79-
for (ClientSession session : sessions) {
80-
if (!isRoutable(session)) {
81-
continue;
82-
}
83-
futures.add(channel.send(toPlatform(session.location()).target(session.id()), payload));
84-
}
85-
return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
86-
}
87-
8874
@Override
8975
public Platform.ServerTarget server() {
9076
return Platform.VELOCITY.target(serverId);
@@ -229,6 +215,10 @@ private <P extends ChannelPayload> Subscription registerListener(ChannelType<P,
229215
}
230216

231217
private CompletableFuture<Void> sendPluginMessage(Platform.ServerTarget target, PluginMessage payload) {
218+
if ("*".equals(target.id())) {
219+
return broadcastPluginMessage(payload);
220+
}
221+
232222
if (isLocalVelocity(target)) {
233223
Envelope local = Envelope.make(MessageType.PLUGIN_MESSAGE, serverId, serverId,
234224
Envelope.MAPPER.valueToTree(payload));
@@ -247,6 +237,19 @@ private CompletableFuture<Void> sendPluginMessage(Platform.ServerTarget target,
247237
return endpointServer.send(session.get().endpoint(), env).dispatch();
248238
}
249239

240+
private CompletableFuture<Void> broadcastPluginMessage(PluginMessage payload) {
241+
List<CompletableFuture<Void>> futures = new ArrayList<>();
242+
for (ClientSession session : sessions) {
243+
if (!isRoutable(session)) {
244+
continue;
245+
}
246+
Envelope env = Envelope.make(MessageType.PLUGIN_MESSAGE, serverId, session.id(),
247+
Envelope.MAPPER.valueToTree(payload));
248+
futures.add(endpointServer.send(session.endpoint(), env).dispatch());
249+
}
250+
return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
251+
}
252+
250253
private CompletableFuture<PluginMessage> requestPluginMessage(Platform.ServerTarget target,
251254
PluginMessage payload,
252255
Duration timeout) {

0 commit comments

Comments
 (0)