Class PluginMessageProtocol

java.lang.Object
wtf.choco.veinminer.network.PluginMessageProtocol

public final class PluginMessageProtocol extends Object
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.
See Also:
  • Constructor Details

  • Method Details

    • getChannel

      @NotNull public @NotNull NamespacedKey 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-bound PluginMessage to the given MessageReceiver.
      Parameters:
      receiver - the receiver to which the message should be send
      message - the message to send
    • sendMessageToServer

      public void sendMessageToServer(@NotNull @NotNull MessageReceiver receiver, @NotNull @NotNull PluginMessage<?> message)
      Send a server-bound PluginMessage to the given MessageReceiver.
      Parameters:
      receiver - the receiver to which the message should be send
      message - the message to send
    • registerChannels

      public void registerChannels(@NotNull @NotNull ChannelRegistrar registrar)
      Register messaging channels with the given ChannelRegistrar.
      Parameters:
      registrar - the registrar
    • getPacketRegistry

      @NotNull @VisibleForTesting public @NotNull PluginMessageRegistry<?> getPacketRegistry(@NotNull @NotNull MessageDirection direction)
      Parameters:
      direction - the direction
      Returns:
      the registry
      API Note:
      THIS IS NOT API AND IS ONLY VISIBLE FOR DOCUMENTATION PURPOSES