Package wtf.choco.veinminer.network
Class PluginMessageProtocol
java.lang.Object
wtf.choco.veinminer.network.PluginMessageProtocol
Represents a protocol definition by which plugin messages ("custom packets") may be registered
and parsed in a more convenient and object-oriented way. Plugins can define their own protocols
and register custom
PluginMessage
implementations.
public static final PluginMessageProtocol PROTOCOL = new PluginMessageProtocol(new NamespacedKey("namespace", "key"), 1, serverRegistry -> serverRegistry .registerMessage(PluginMessageServerboundExampleOne.class, PluginMessageServerboundExampleOne::new) // 0x00 .registerMessage(PluginMessageServerboundExampleTwo.class, PluginMessageServerboundExampleTwo::new), // 0x01 clientRegistry -> clientRegistry .registerMessage(PluginMessageClientboundExampleOne.class, PluginMessageClientboundExampleOne::new) // 0x00 ); { // Somewhere in initialization, the channels have to be registered with a ChannelRegistrar PROTOCOL.registerChannels(new MyChannelRegisrarImplementation()); // Now things are ready to go and send messages PROTOCOL.sendServerMessage(messageReceiver, new PluginMessageServerboundExampleOne("some parameter", "whatever data you want in here", 10)); }The above may be used to send or receive client or server bound PluginMessages to and from third party software listening for Minecraft's custom payload packet.
-
Constructor Summary
ConstructorDescriptionPluginMessageProtocol
(@NotNull NamespacedKey channel, int version, @NotNull Consumer<PluginMessageRegistry<ServerboundPluginMessageListener>> serverboundMessageSupplier, @NotNull Consumer<PluginMessageRegistry<ClientboundPluginMessageListener>> clientboundMessageSupplier) Construct a newPluginMessageProtocol
. -
Method Summary
Modifier and TypeMethodDescription@NotNull NamespacedKey
Get the channel on which this protocol is listening.@NotNull PluginMessageRegistry<?>
getPacketRegistry
(@NotNull MessageDirection direction) Get thePluginMessageRegistry
for the givenMessageDirection
.int
Get the protocol version.void
registerChannels
(@NotNull ChannelRegistrar registrar) Register messaging channels with the givenChannelRegistrar
.void
sendMessageToClient
(@NotNull MessageReceiver receiver, @NotNull PluginMessage<?> message) Send a client-boundPluginMessage
to the givenMessageReceiver
.void
sendMessageToServer
(@NotNull MessageReceiver receiver, @NotNull PluginMessage<?> message) Send a server-boundPluginMessage
to the givenMessageReceiver
.
-
Constructor Details
-
PluginMessageProtocol
public PluginMessageProtocol(@NotNull @NotNull NamespacedKey channel, int version, @NotNull @NotNull Consumer<PluginMessageRegistry<ServerboundPluginMessageListener>> serverboundMessageSupplier, @NotNull @NotNull Consumer<PluginMessageRegistry<ClientboundPluginMessageListener>> clientboundMessageSupplier) Construct a newPluginMessageProtocol
.- Parameters:
channel
- the channel on which this protocol is registeredversion
- the protocol versionserverboundMessageSupplier
- the supplier to which server-bound messages should be registeredclientboundMessageSupplier
- the supplier to which client-bound messages should be registered
-
-
Method Details
-
getChannel
Get the channel on which this protocol is listening.- Returns:
- the channel
-
getVersion
public int getVersion()Get the protocol version.- Returns:
- the version
-
sendMessageToClient
public void sendMessageToClient(@NotNull @NotNull MessageReceiver receiver, @NotNull @NotNull PluginMessage<?> message) Send a client-boundPluginMessage
to the givenMessageReceiver
.- Parameters:
receiver
- the receiver to which the message should be sendmessage
- the message to send
-
sendMessageToServer
public void sendMessageToServer(@NotNull @NotNull MessageReceiver receiver, @NotNull @NotNull PluginMessage<?> message) Send a server-boundPluginMessage
to the givenMessageReceiver
.- Parameters:
receiver
- the receiver to which the message should be sendmessage
- the message to send
-
registerChannels
Register messaging channels with the givenChannelRegistrar
.- Parameters:
registrar
- the registrar
-
getPacketRegistry
@NotNull @VisibleForTesting public @NotNull PluginMessageRegistry<?> getPacketRegistry(@NotNull @NotNull MessageDirection direction) Get thePluginMessageRegistry
for the givenMessageDirection
.- Parameters:
direction
- the direction- Returns:
- the registry
- API Note:
- THIS IS NOT API AND IS ONLY VISIBLE FOR DOCUMENTATION PURPOSES
-