Package dev.jorel.commandapi.network
Class CommandAPIMessenger<InputChannel,OutputChannel>
java.lang.Object
dev.jorel.commandapi.network.CommandAPIMessenger<InputChannel,OutputChannel>
- Type Parameters:
InputChannel- The type of objects that might send a packet to this pluginOutputChannel- The type of objects this plugin might send messages to
Handles sending and receiving
CommandAPIPackets between instances of the CommandAPI plugin on different servers.
This class should be extended on each platform to hook into its specific plugin-messaging scheme.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCommandAPIMessenger(CommandAPIPacketHandlerProvider<InputChannel> packetHandlerProvider) Creates a newCommandAPIMessenger. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose()Disables this messenger, unregistering any events and such that let it send/receive packets before the plugin disables.abstract intGets theCommandAPIProtocol.PROTOCOL_VERSIONon the other side of an outgoing connection.protected voidmessageReceived(CommandAPIProtocol protocol, InputChannel sender, byte[] input) Handles a message sent to this plugin.voidsendPacket(OutputChannel target, CommandAPIPacket packet) Sends a packet to the given target.protected abstract voidsendRawBytes(CommandAPIProtocol protocol, OutputChannel target, byte[] bytes) Sends an array of bytes to the given target.
-
Constructor Details
-
CommandAPIMessenger
Creates a newCommandAPIMessenger.- Parameters:
packetHandlerProvider- TheCommandAPIPacketHandlerProviderthat defines the various implementations ofCommandAPIPacketHandlerthat should be used to handle incoming packets.
-
-
Method Details
-
close
public abstract void close()Disables this messenger, unregistering any events and such that let it send/receive packets before the plugin disables. -
getConnectionProtocolVersion
Gets theCommandAPIProtocol.PROTOCOL_VERSIONon the other side of an outgoing connection. This is determined by the value in theSetVersionPacketsent by this connection, or 0 if they never sent that packet.- Parameters:
target- TheCommandAPIMessengerto get the version for.- Returns:
- The
CommandAPIProtocol.PROTOCOL_VERSIONsupported by this target.
-
messageReceived
Handles a message sent to this plugin. The given byte array will be decoded into aCommandAPIPacketaccording to the givenCommandAPIProtocol, then handled appropriately. Nothing happens if the given byte array is empty.- Parameters:
protocol- The protocol to handle the message with.sender- The source of the message.input- The byte array of the message.- Throws:
IllegalStateException- if the packet is malformed and cannot be read correctly.
-
sendPacket
Sends a packet to the given target. The given packet will be converted into a byte array and sent usingsendRawBytes(CommandAPIProtocol, Object, byte[]). The packet must be registered inCommandAPIProtocolto be sent.- Parameters:
target- The connection to send the packet to.packet- TheCommandAPIPacketto send.- Throws:
IllegalStateException- if the given packet is not registered inCommandAPIProtocol.
-
sendRawBytes
protected abstract void sendRawBytes(CommandAPIProtocol protocol, OutputChannel target, byte[] bytes) Sends an array of bytes to the given target.- Parameters:
protocol- TheCommandAPIProtocolto use to send this packet.target- The connection to send this packet to.bytes- The array of bytes to send.
-