Skip to content

Commit 2bfeb1b

Browse files
committed
doc: added comments
1 parent ac5da9f commit 2bfeb1b

17 files changed

Lines changed: 200 additions & 0 deletions

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,61 @@
1414
import java.util.concurrent.CompletableFuture;
1515
import java.util.function.Consumer;
1616

17+
/** Main entry point for interacting with the CommandBridge network. */
1718
public interface CommandBridgeAPI {
1819

20+
/**
21+
* Obtains a {@link MessageChannel} for the given {@link ChannelType}.
22+
*
23+
* @param type the channel type identity
24+
* @param <T> the payload type
25+
* @param <C> the channel interface type
26+
* @return the message channel
27+
*/
1928
<T extends ChannelPayload, C extends MessageChannel<T>> C channel(ChannelType<T, C> type);
2029

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+
*/
2137
<P extends ChannelPayload> CompletableFuture<Void> broadcast(MessageChannel<P> channel, P payload);
2238

39+
/** @return the identity of the current server */
2340
Platform.ServerTarget server();
2441

42+
/** @return the current connection state to the bridge network */
2543
ConnectionState connectionState();
2644

45+
/** @return the IDs of all currently connected servers, if available */
2746
Optional<Set<String>> connectedServers();
2847

48+
/** @return the player location lookup service, if available */
2949
Optional<PlayerLocator> playerLocator();
3050

51+
/**
52+
* Subscribes to server connection events.
53+
*
54+
* @param listener the listener to call when a server connects
55+
* @return a subscription handle to cancel the listener
56+
*/
3157
Subscription onServerConnected(ServerEventListener listener);
3258

59+
/**
60+
* Subscribes to server disconnection events.
61+
*
62+
* @param listener the listener to call when a server disconnects
63+
* @return a subscription handle to cancel the listener
64+
*/
3365
Subscription onServerDisconnected(ServerEventListener listener);
3466

67+
/**
68+
* Subscribes to connection state changes.
69+
*
70+
* @param listener the listener to call when the state changes
71+
* @return a subscription handle to cancel the listener
72+
*/
3573
Subscription onConnectionStateChanged(Consumer<ConnectionState> listener);
3674
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Objects;
44

5+
/** Static provider for accessing the {@link CommandBridgeAPI} instance. */
56
public final class CommandBridgeProvider {
67

78
private static volatile CommandBridgeAPI instance;
@@ -10,13 +11,25 @@ private CommandBridgeProvider() {
1011
throw new UnsupportedOperationException("Provider can't be instanced");
1112
}
1213

14+
/**
15+
* @return the registered API instance
16+
* @throws IllegalStateException if the API is not registered
17+
*/
1318
public static CommandBridgeAPI get() {
1419
if (instance == null) {
1520
throw new IllegalStateException("CommandBridge is not available. Is it installed and running?");
1621
}
1722
return instance;
1823
}
1924

25+
/**
26+
* Obtains the API instance cast to a specific type.
27+
*
28+
* @param type the API class type
29+
* @param <T> the API type
30+
* @return the cast API instance
31+
* @throws IllegalStateException if the instance is not available or compatible
32+
*/
2033
public static <T extends CommandBridgeAPI> T get(Class<T> type) {
2134
CommandBridgeAPI api = get();
2235
if (!type.isInstance(api)) {
@@ -25,6 +38,12 @@ public static <T extends CommandBridgeAPI> T get(Class<T> type) {
2538
return type.cast(api);
2639
}
2740

41+
/**
42+
* Registers the API implementation.
43+
*
44+
* @param impl the implementation to register
45+
* @throws IllegalStateException if an implementation is already registered
46+
*/
2847
public static void register(CommandBridgeAPI impl) {
2948
Objects.requireNonNull(impl);
3049
if (instance != null) {
@@ -33,6 +52,7 @@ public static void register(CommandBridgeAPI impl) {
3352
instance = impl;
3453
}
3554

55+
/** Unregisters the current API implementation. */
3656
public static void unregister() {
3757
instance = null;
3858
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package dev.objz.commandbridge.api.channel;
22

3+
/** Marker interface for data sent over a {@link MessageChannel}. */
34
public interface ChannelPayload {
45
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import java.util.Objects;
44

5+
/**
6+
* Identity and type information for a {@link MessageChannel}.
7+
*
8+
* @param <T> the payload type
9+
* @param <C> the channel interface type
10+
*/
511
public abstract class ChannelType<T extends ChannelPayload, C extends MessageChannel<T>> {
612

713
private final Class<T> type;
@@ -10,6 +16,7 @@ protected ChannelType(Class<T> type) {
1016
this.type = Objects.requireNonNull(type);
1117
}
1218

19+
/** @return the class of the payload handled by this channel */
1320
public Class<T> type() {
1421
return type;
1522
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import dev.objz.commandbridge.api.channel.command.CommandChannelType;
44

5+
/** Registry of built-in {@link ChannelType}s. */
56
public final class Channels {
67

8+
/** The default channel for executing commands. */
79
public static final CommandChannelType COMMAND = new CommandChannelType();
810

911
private Channels() {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,46 @@
77
import java.time.Duration;
88
import java.util.concurrent.CompletableFuture;
99

10+
/**
11+
* Communication pipe for sending and receiving {@link ChannelPayload}s.
12+
*
13+
* @param <P> the type of payload handled by this channel
14+
*/
1015
public interface MessageChannel<P extends ChannelPayload> {
1116

17+
/**
18+
* Sends a payload to a target server without expecting a response.
19+
*
20+
* @param target the destination server
21+
* @param payload the data to send
22+
* @return a future that completes when the message is sent
23+
*/
1224
CompletableFuture<Void> send(Platform.ServerTarget target, P payload);
1325

26+
/**
27+
* Sends a request to a target server and waits for a response.
28+
*
29+
* @param target the destination server
30+
* @param payload the request data
31+
* @return a future containing the response payload
32+
*/
1433
CompletableFuture<P> request(Platform.ServerTarget target, P payload);
1534

35+
/**
36+
* Sends a request to a target server with a custom timeout.
37+
*
38+
* @param target the destination server
39+
* @param payload the request data
40+
* @param timeout the maximum time to wait for a response
41+
* @return a future containing the response payload
42+
*/
1643
CompletableFuture<P> request(Platform.ServerTarget target, P payload, Duration timeout);
1744

45+
/**
46+
* Subscribes a listener to messages received on this channel.
47+
*
48+
* @param listener the listener to call for incoming messages
49+
* @return a subscription handle to cancel the listener
50+
*/
1851
Subscription listen(MessageListener<P> listener);
1952
}

api/src/main/java/dev/objz/commandbridge/api/channel/command/CommandChannel.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,40 @@
66
import java.util.UUID;
77
import java.util.concurrent.CompletableFuture;
88

9+
/** Specialized channel for dispatching commands to servers. */
910
public interface CommandChannel extends MessageChannel<CommandPayload> {
1011

12+
/**
13+
* Executes a command as the console.
14+
*
15+
* @param target the destination server
16+
* @param command the command string to run
17+
* @return a future that completes when the command is sent
18+
*/
1119
default CompletableFuture<Void> console(Platform.ServerTarget target, String command) {
1220
return send(target, CommandPayload.console(command));
1321
}
1422

23+
/**
24+
* Executes a command as a specific player.
25+
*
26+
* @param target the destination server
27+
* @param command the command string to run
28+
* @param player the UUID of the player to run as
29+
* @return a future that completes when the command is sent
30+
*/
1531
default CompletableFuture<Void> player(Platform.ServerTarget target, String command, UUID player) {
1632
return send(target, CommandPayload.player(command, player));
1733
}
1834

35+
/**
36+
* Executes a command with operator permissions, bypassing standard checks.
37+
*
38+
* @param target the destination server
39+
* @param command the command string to run
40+
* @param player the UUID of the player to run as
41+
* @return a future that completes when the command is sent
42+
*/
1943
default CompletableFuture<Void> operator(Platform.ServerTarget target, String command, UUID player) {
2044
return send(target, CommandPayload.operator(command, player));
2145
}

api/src/main/java/dev/objz/commandbridge/api/channel/command/CommandChannelType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import dev.objz.commandbridge.api.channel.ChannelType;
44

5+
/** Identity for the {@link CommandChannel}. */
56
public final class CommandChannelType extends ChannelType<CommandPayload, CommandChannel> {
67

8+
/** Creates a new command channel type. */
79
public CommandChannelType() {
810
super(CommandPayload.class);
911
}

api/src/main/java/dev/objz/commandbridge/api/channel/command/CommandPayload.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44

55
import java.util.UUID;
66

7+
/**
8+
* Payload for command execution.
9+
*
10+
* @param command the command string to run
11+
* @param runAs the execution mode
12+
* @param player the optional player context
13+
*/
714
public record CommandPayload(String command, RunAs runAs, UUID player) implements ChannelPayload {
815

16+
/** Creates a payload for console execution. */
917
public static CommandPayload console(String command) {
1018
return new CommandPayload(command, RunAs.CONSOLE, null);
1119
}
1220

21+
/** Creates a payload for player execution. */
1322
public static CommandPayload player(String command, UUID player) {
1423
return new CommandPayload(command, RunAs.PLAYER, player);
1524
}
1625

26+
/** Creates a payload for operator execution. */
1727
public static CommandPayload operator(String command, UUID player) {
1828
return new CommandPayload(command, RunAs.OPERATOR, player);
1929
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package dev.objz.commandbridge.api.channel.command;
22

3+
/** Defines the execution context for a command. */
34
public enum RunAs {
5+
/** Run as the server console. */
46
CONSOLE,
7+
/** Run as a specific player. */
58
PLAYER,
9+
/** Run as a player with temporary operator permissions. */
610
OPERATOR
711
}

0 commit comments

Comments
 (0)