Interface ScreenNetworking


@NonExtendable public interface ScreenNetworking
ScreenNetworking handles screen-related network messages sent between the server and the client.

Registering a message receiver

Message receivers can be registered by calling receive(Identifier, Decoder, ScreenNetworking.MessageReceiver) on a ScreenNetworking for the receiving side. The message ID is a unique ID that matches between the sender and the receiver.

Message receivers should be registered in the constructor of a SyncedGuiDescription.

Sending messages

Messages can be sent by calling send(Identifier, Encoder, D) on a ScreenNetworking for the sending side. The message ID and codec should match up with a receiver registered on the opposite side.

Example

private static final ScreenMessageKey<Integer> MESSAGE_KEY = new ScreenMessageKey<>(
    Identifier.of("my_mod", "some_message"),
    Codec.INT
);

// Receiver
getNetworking(NetworkSide.SERVER).receive(MESSAGE_KEY, data -> {
	   // Example data: a lucky number as an int
    System.out.println("Your lucky number is " + data + "!");
});

// Sending

// We're sending from a button. The packet data is our lucky number, 123.
WButton button = ...;
button.setOnClick(() -> {
    getNetworking(NetworkSide.CLIENT).send(MESSAGE_KEY, 123);
});
Since:
3.3.0
  • Method Details

    • of

      @Deprecated(forRemoval=true, since="13.1.0") static ScreenNetworking of(SyncedGuiDescription description, NetworkSide networkSide)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Gets a networking handler for the GUI description that is active on the specified side.
      Parameters:
      description - the GUI description
      networkSide - the network side
      Returns:
      the network handler
      Throws:
      NullPointerException - if either parameter is null
    • receive

      <D> void receive(net.minecraft.resources.Identifier message, com.mojang.serialization.Decoder<D> decoder, ScreenNetworking.MessageReceiver<D> receiver)
      Registers a message receiver for the message.

      The decoder can depend on registry data and RegistryOps is available.

      Type Parameters:
      D - the message data type
      Parameters:
      message - the screen message ID
      decoder - the message decoder
      receiver - the message receiver
      Throws:
      IllegalStateException - if the message has already been registered
      NullPointerException - if any parameter is null
    • receive

      default <D> void receive(ScreenMessageKey<D> message, ScreenNetworking.MessageReceiver<D> receiver)
      Registers a message receiver for the message.

      The codec can depend on registry data and RegistryOps is available.

      Type Parameters:
      D - the message data type
      Parameters:
      message - the screen message key
      receiver - the message receiver
      Throws:
      IllegalStateException - if the message has already been registered
      NullPointerException - if any parameter is null
      Since:
      13.1.0
    • send

      <D> void send(net.minecraft.resources.Identifier message, com.mojang.serialization.Encoder<D> encoder, D data)
      Sends a screen message to the other side of the connection.

      The encoder can depend on registry data and RegistryOps is available.

      Type Parameters:
      D - the message data type
      Parameters:
      message - the screen message ID
      encoder - the message encoder
      data - the message data
      Throws:
      NullPointerException - if the message ID or the encoder is null
    • send

      default <D> void send(ScreenMessageKey<D> message, D data)
      Sends a screen message to the other side of the connection.

      The codec can depend on registry data and RegistryOps is available.

      Type Parameters:
      D - the message data type
      Parameters:
      message - the screen message key
      data - the message data
      Throws:
      NullPointerException - if the message key is null
      Since:
      13.1.0
    • getReadyEvent

      An event that is triggered when the networking handlers on both sides are ready to send and receive messages.

      For example, you can send initial GUI state to the client in a server-side ready event listener.

      Returns:
      the event
      Since:
      13.1.0