|
13 | 13 | import java.util.function.Consumer; |
14 | 14 |
|
15 | 15 | /** |
16 | | - * Main entry point for interacting with the CommandBridge network. |
| 16 | + * Primary interface for interacting with the CommandBridge network from a third-party plugin. |
17 | 17 | * |
18 | | - * <p>Methods returning {@link java.util.Optional} indicate proxy-only features; |
19 | | - * they return {@code Optional.empty()} on backend servers and a present value on the Velocity proxy. |
| 18 | + * <p>Use {@link #channel(Class)} to obtain typed message channels for sending and receiving |
| 19 | + * payloads. {@link #server()} and {@link #connectionState()} expose the current server's identity |
| 20 | + * and connection status. {@link #connectedServers()}, {@link #playerLocator()}, |
| 21 | + * {@link #onServerConnected(ServerEventListener)}, and |
| 22 | + * {@link #onServerDisconnected(ServerEventListener)} are available only on the Velocity proxy; |
| 23 | + * they return {@code Optional.empty()} on backend servers. |
| 24 | + * |
| 25 | + * <p>Obtain an instance via {@link CommandBridgeProvider#get()}. The following example shows |
| 26 | + * common usage patterns: |
| 27 | + * |
| 28 | + * <pre>{@code |
| 29 | + * CommandBridgeAPI api = CommandBridgeProvider.get(); |
| 30 | + * MessageChannel<CommandPayload> channel = api.channel(CommandPayload.class); |
| 31 | + * |
| 32 | + * // Send a command to a backend |
| 33 | + * channel.to(List.of(Platform.backend("survival-1"))) |
| 34 | + * .send(new CommandPayload("say hello", RunAs.CONSOLE)); |
| 35 | + * |
| 36 | + * // Broadcast to all connected servers |
| 37 | + * channel.toAll().send(new CommandPayload("say maintenance soon", RunAs.CONSOLE)); |
| 38 | + * |
| 39 | + * // Subscribe to server connection events (proxy only) |
| 40 | + * api.onServerConnected(server -> |
| 41 | + * System.out.println("Connected: " + server.id()) |
| 42 | + * ).ifPresent(subscriptions::add); |
| 43 | + * |
| 44 | + * // React to connection state changes (all platforms) |
| 45 | + * api.onConnectionStateChanged(state -> { |
| 46 | + * if (state.isActive()) { |
| 47 | + * // ready to send |
| 48 | + * } |
| 49 | + * }); |
| 50 | + * }</pre> |
| 51 | + * |
| 52 | + * @see CommandBridgeProvider |
| 53 | + * @see dev.objz.commandbridge.api.channel.MessageChannel |
20 | 54 | */ |
21 | 55 | public interface CommandBridgeAPI { |
22 | 56 |
|
23 | 57 | /** |
24 | | - * Obtains a {@link MessageChannel} for the given payload type. |
| 58 | + * Obtains a typed {@link dev.objz.commandbridge.api.channel.MessageChannel} for the given |
| 59 | + * payload type. |
25 | 60 | * |
26 | | - * @param type the payload class that identifies the channel |
27 | | - * @param <T> the payload type |
28 | | - * @return the message channel |
| 61 | + * @param <T> the payload type; must extend {@link dev.objz.commandbridge.api.channel.ChannelPayload} |
| 62 | + * @param type the {@link Class} token identifying the payload type; used for channel routing |
| 63 | + * @return the message channel for sending and receiving payloads of type {@code T} |
| 64 | + * @see dev.objz.commandbridge.api.channel.MessageChannel |
29 | 65 | */ |
30 | 66 | <T extends ChannelPayload> MessageChannel<T> channel(Class<T> type); |
31 | 67 |
|
32 | | - /** @return the identity of the current server */ |
| 68 | + /** |
| 69 | + * Returns the identity of the server this API instance is running on. |
| 70 | + * |
| 71 | + * @return the {@link dev.objz.commandbridge.api.platform.Platform.ServerTarget} of the current |
| 72 | + * server, containing its configured identifier and platform type |
| 73 | + */ |
33 | 74 | Platform.ServerTarget server(); |
34 | 75 |
|
35 | | - /** @return the current connection state to the bridge network */ |
| 76 | + /** |
| 77 | + * Returns the current connection state of this server to the CommandBridge network. |
| 78 | + * |
| 79 | + * @return the current {@link ConnectionState} |
| 80 | + * @see ConnectionState#isActive() |
| 81 | + */ |
36 | 82 | ConnectionState connectionState(); |
37 | 83 |
|
38 | | - /** @return the IDs of all currently connected servers, or empty if not available on this platform */ |
| 84 | + /** |
| 85 | + * Returns the identifiers of all servers currently connected to the bridge network. |
| 86 | + * |
| 87 | + * <p>This method is available only on the Velocity proxy. On backend servers, it returns |
| 88 | + * {@code Optional.empty()}. |
| 89 | + * |
| 90 | + * @return an {@link java.util.Optional} containing a snapshot of the connected server IDs if |
| 91 | + * called on the Velocity proxy, or an empty {@code Optional} on backend servers |
| 92 | + * @see #onServerConnected(ServerEventListener) |
| 93 | + */ |
39 | 94 | Optional<Set<String>> connectedServers(); |
40 | 95 |
|
41 | | - /** @return the player location lookup service, or empty if not available on this platform */ |
| 96 | + /** |
| 97 | + * Returns the player location service for resolving which server a player is connected to. |
| 98 | + * |
| 99 | + * <p>This method is available only on the Velocity proxy. On backend servers, it returns |
| 100 | + * {@code Optional.empty()}. |
| 101 | + * |
| 102 | + * @return an {@link java.util.Optional} containing the {@link PlayerLocator} if called on the |
| 103 | + * Velocity proxy, or an empty {@code Optional} on backend servers |
| 104 | + * @see dev.objz.commandbridge.api.platform.PlayerLocator#locate(java.util.UUID) |
| 105 | + */ |
42 | 106 | Optional<PlayerLocator> playerLocator(); |
43 | 107 |
|
44 | 108 | /** |
45 | 109 | * Subscribes to server connection events. |
46 | 110 | * |
47 | | - * <p>Returns {@code Optional.empty()} on platforms where server connection events are not |
48 | | - * available (e.g. backend servers). On the Velocity proxy, returns a present {@link Optional} |
49 | | - * containing a {@link Subscription} that can be cancelled via {@link Subscription#cancel()}. |
50 | | - * Use {@link java.util.Optional#ifPresent(java.util.function.Consumer)} to handle safely. |
| 111 | + * <p>This method is available only on the Velocity proxy. On backend servers, it returns |
| 112 | + * {@code Optional.empty()} and the listener is not registered. |
51 | 113 | * |
52 | | - * @param listener the listener to call when a server connects |
53 | | - * @return a present subscription handle on the proxy, or empty on backends |
| 114 | + * @param listener the listener to invoke when a backend server connects to the bridge network; |
| 115 | + * must not be {@code null} |
| 116 | + * @return an {@link java.util.Optional} containing the {@link Subscription} on the proxy, or |
| 117 | + * an empty {@code Optional} on backends |
| 118 | + * @see #onServerDisconnected(ServerEventListener) |
| 119 | + * @see dev.objz.commandbridge.api.message.Subscription#cancel() |
54 | 120 | */ |
55 | 121 | Optional<Subscription> onServerConnected(ServerEventListener listener); |
56 | 122 |
|
57 | 123 | /** |
58 | 124 | * Subscribes to server disconnection events. |
59 | 125 | * |
60 | | - * <p>Returns {@code Optional.empty()} on platforms where server disconnection events are not |
61 | | - * available (e.g. backend servers). On the Velocity proxy, returns a present {@link Optional} |
62 | | - * containing a {@link Subscription} that can be cancelled via {@link Subscription#cancel()}. |
63 | | - * Use {@link java.util.Optional#ifPresent(java.util.function.Consumer)} to handle safely. |
| 126 | + * <p>This method is available only on the Velocity proxy. On backend servers, it returns |
| 127 | + * {@code Optional.empty()} and the listener is not registered. |
64 | 128 | * |
65 | | - * @param listener the listener to call when a server disconnects |
66 | | - * @return a present subscription handle on the proxy, or empty on backends |
| 129 | + * @param listener the listener to invoke when a backend server disconnects from the bridge |
| 130 | + * network; must not be {@code null} |
| 131 | + * @return an {@link java.util.Optional} containing the {@link Subscription} on the proxy, or |
| 132 | + * an empty {@code Optional} on backends |
| 133 | + * @see #onServerConnected(ServerEventListener) |
| 134 | + * @see dev.objz.commandbridge.api.message.Subscription#cancel() |
67 | 135 | */ |
68 | 136 | Optional<Subscription> onServerDisconnected(ServerEventListener listener); |
69 | 137 |
|
70 | 138 | /** |
71 | | - * Subscribes to connection state changes. |
| 139 | + * Subscribes to connection state changes on this server. |
| 140 | + * |
| 141 | + * <p>Available on all platforms, unlike the server event methods. The listener is invoked on |
| 142 | + * every state transition. |
72 | 143 | * |
73 | | - * @param listener the listener to call when the state changes |
74 | | - * @return a subscription handle to cancel the listener |
| 144 | + * @param listener the consumer to call with the new {@link ConnectionState} on each transition; |
| 145 | + * must not be {@code null} |
| 146 | + * @return a {@link Subscription} that can be cancelled to stop receiving state change events |
| 147 | + * @see ConnectionState |
75 | 148 | */ |
76 | 149 | Subscription onConnectionStateChanged(Consumer<ConnectionState> listener); |
77 | 150 | } |
0 commit comments