Class SimpleChannel.MessageBuilder<MSG>
- Enclosing class:
- SimpleChannel
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
-
Field Summary
Modifier and TypeFieldDescriptionprivate SimpleChannel
private BiConsumer<MSG,
Supplier<NetworkEvent.Context>> private Function<FriendlyByteBuf,
MSG> private BiConsumer<MSG,
FriendlyByteBuf> private int
private BiConsumer<MSG,
Integer> private boolean
private Optional<NetworkDirection>
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add()
buildLoginPacketList
(Function<Boolean, List<org.apache.commons.lang3.tuple.Pair<String, MSG>>> loginPacketGenerators) consumerMainThread
(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) Set the message consumer, which is called once a message has been decoded.consumerNetworkThread
(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) Set the message consumer, which is called once a message has been decoded.consumerNetworkThread
(SimpleChannel.MessageBuilder.ToBooleanBiFunction<MSG, Supplier<NetworkEvent.Context>> handler) Function returning a boolean "packet handled" indication, for simpler channel building.decoder
(Function<FriendlyByteBuf, MSG> decoder) Set the message decoder, which reads the message from aFriendlyByteBuf
.encoder
(BiConsumer<MSG, FriendlyByteBuf> encoder) Set the message encoder, which writes this message to aFriendlyByteBuf
.private static <MSG> SimpleChannel.MessageBuilder<MSG>
forType
(SimpleChannel channel, Class<MSG> type, int id, NetworkDirection networkDirection) loginIndex
(Function<MSG, Integer> loginIndexGetter, BiConsumer<MSG, Integer> loginIndexSetter) Marks this packet as not needing a response when sent to the client
-
Field Details
-
channel
-
type
-
id
private int id -
encoder
-
decoder
-
consumer
-
loginIndexGetter
-
loginIndexSetter
-
loginPacketGenerators
-
networkDirection
-
needsResponse
private boolean needsResponse
-
-
Constructor Details
-
MessageBuilder
public MessageBuilder()
-
-
Method Details
-
forType
private static <MSG> SimpleChannel.MessageBuilder<MSG> forType(SimpleChannel channel, Class<MSG> type, int id, NetworkDirection networkDirection) -
encoder
Set the message encoder, which writes this message to aFriendlyByteBuf
.The encoder is called immediately when the packet is sent. This means encoding typically occurs on the main server/client thread rather than on the network thread.
However, this behaviour should not be relied on, and the encoder should try to be thread-safe and not interact with the current game state.
- Parameters:
encoder
- The message encoder.- Returns:
- This message builder, for chaining.
-
decoder
Set the message decoder, which reads the message from aFriendlyByteBuf
.The decoder is called when the message is received on the network thread. The decoder should not attempt to access or mutate any game state, deferring that until the the message is handled.
- Parameters:
decoder
- The message decoder.- Returns:
- The message builder, for chaining.
-
loginIndex
public SimpleChannel.MessageBuilder<MSG> loginIndex(Function<MSG, Integer> loginIndexGetter, BiConsumer<MSG, Integer> loginIndexSetter) -
buildLoginPacketList
-
markAsLoginPacket
-
noResponse
Marks this packet as not needing a response when sent to the client -
consumerNetworkThread
public SimpleChannel.MessageBuilder<MSG> consumerNetworkThread(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) Set the message consumer, which is called once a message has been decoded. This accepts the decoded message object and the message's context.The consumer is called on the network thread, and so should not interact with most game state by default.
NetworkEvent.Context.enqueueWork(Runnable)
can be used to handle the message on the main server or client thread. Alternatively one can useconsumerMainThread(BiConsumer)
to run the handler on the main thread.- Parameters:
consumer
- The message consumer.- Returns:
- The message builder, for chaining.
- See Also:
-
consumerMainThread
public SimpleChannel.MessageBuilder<MSG> consumerMainThread(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) Set the message consumer, which is called once a message has been decoded. This accepts the decoded message object and the message's context.Unlike
consumerNetworkThread(BiConsumer)
, the consumer is called on the main thread, and so can interact with most game state by default.- Parameters:
consumer
- The message consumer.- Returns:
- The message builder, for chaining.
- See Also:
-
consumerNetworkThread
public SimpleChannel.MessageBuilder<MSG> consumerNetworkThread(SimpleChannel.MessageBuilder.ToBooleanBiFunction<MSG, Supplier<NetworkEvent.Context>> handler) Function returning a boolean "packet handled" indication, for simpler channel building.- Parameters:
handler
- a handler- Returns:
- this
- See Also:
-
add
public void add()
-