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 callingreceive(net.minecraft.util.Identifier, com.mojang.serialization.Decoder<D>, io.github.cottonmc.cotton.gui.networking.ScreenNetworking.MessageReceiver<D>)
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 callingsend(net.minecraft.util.Identifier, com.mojang.serialization.Encoder<D>, 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(() -> {
ScreenNetworking.of(this, NetworkSide.CLIENT).send(MESSAGE_KEY, 123);
});
- Since:
- 3.3.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA handler for received screen messages.static interfaceA listener forgetReadyEvent(). -
Method Summary
Modifier and TypeMethodDescriptionnet.fabricmc.fabric.api.event.Event<ScreenNetworking.ReadyListener> An event that is triggered when the networking handlers on both sides are ready to send and receive messages.static ScreenNetworkingof(SyncedGuiDescription description, NetworkSide networkSide) Deprecated, for removal: This API element is subject to removal in a future version.default <D> voidreceive(ScreenMessageKey<D> message, ScreenNetworking.MessageReceiver<D> receiver) Registers a message receiver for the message.<D> voidreceive(Identifier message, com.mojang.serialization.Decoder<D> decoder, ScreenNetworking.MessageReceiver<D> receiver) Registers a message receiver for the message.default <D> voidsend(ScreenMessageKey<D> message, D data) Sends a screen message to the other side of the connection.<D> voidsend(Identifier message, com.mojang.serialization.Encoder<D> encoder, D data) Sends a screen message to the other side of the connection.
-
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.UseSyncedGuiDescription.getNetworking(NetworkSide)instead.Gets a networking handler for the GUI description that is active on the specified side.- Parameters:
description- the GUI descriptionnetworkSide- the network side- Returns:
- the network handler
- Throws:
NullPointerException- if either parameter is null
-
receive
<D> void receive(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
RegistryOpsis available.- Type Parameters:
D- the message data type- Parameters:
message- the screen message IDdecoder- the message decoderreceiver- the message receiver- Throws:
IllegalStateException- if the message has already been registeredNullPointerException- if any parameter is null
-
receive
Registers a message receiver for the message.The codec can depend on registry data and
RegistryOpsis available.- Type Parameters:
D- the message data type- Parameters:
message- the screen message keyreceiver- the message receiver- Throws:
IllegalStateException- if the message has already been registeredNullPointerException- if any parameter is null- Since:
- 13.1.0
-
send
Sends a screen message to the other side of the connection.The encoder can depend on registry data and
RegistryOpsis available.- Type Parameters:
D- the message data type- Parameters:
message- the screen message IDencoder- the message encoderdata- the message data- Throws:
NullPointerException- if the message ID or the encoder is null
-
send
Sends a screen message to the other side of the connection.The codec can depend on registry data and
RegistryOpsis available.- Type Parameters:
D- the message data type- Parameters:
message- the screen message keydata- the message data- Throws:
NullPointerException- if the message key is null- Since:
- 13.1.0
-
getReadyEvent
net.fabricmc.fabric.api.event.Event<ScreenNetworking.ReadyListener> 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
-
SyncedGuiDescription.getNetworking(NetworkSide)instead.