Interface IPayloadContext
- All Known Implementing Classes:
ClientPayloadContext
,ServerPayloadContext
@NonExtendable
public interface IPayloadContext
Common context interface for payload handlers.
-
Method Summary
Modifier and TypeMethodDescriptiondefault io.netty.channel.ChannelHandlerContext
Returns the channel handler context.default Connection
Returns the connection.default void
disconnect
(Component reason) Disconnects the player from the network.enqueueWork
(Runnable task) For handlers running on the network thread, submits the given task to be run on the main thread of the game.<T> CompletableFuture
<T> enqueueWork
(Supplier<T> task) void
Marks aConfigurationTask
as completed.flow()
Returns the flow of the received payload.void
handle
(CustomPacketPayload payload) Handles a payload using the current context.default void
Handles a packet using the current context.listener()
Retrieves the packet listener associated with this context.player()
Retrieves the player relevant to this payload.default ConnectionProtocol
protocol()
Returns the protocol of the connection.default void
reply
(CustomPacketPayload payload) Sends the given payload back to the sender.
-
Method Details
-
listener
ICommonPacketListener listener()Retrieves the packet listener associated with this context.For usability, this is typed to
ICommonPacketListener
, but can be downcast to the vanilla packet listeners if necessary. -
connection
Returns the connection.- Returns:
- the connection
-
player
Player player()Retrieves the player relevant to this payload. Players are only available in theConnectionProtocol.PLAY
phase.For server-bound payloads, retrieves the sending
ServerPlayer
.For client-bound payloads, retrieves the receiving
LocalPlayer
.- Throws:
UnsupportedOperationException
- when called during the configuration phase.
-
reply
Sends the given payload back to the sender.- Parameters:
payload
- The payload to send.
-
disconnect
Disconnects the player from the network. -
enqueueWork
For handlers running on the network thread, submits the given task to be run on the main thread of the game.For handlers running on the main thread, immediately executes the task.
On the network thread, the future will be automatically guarded against exceptions using
CompletableFuture.exceptionally(java.util.function.Function<java.lang.Throwable, ? extends T>)
. If you need to catch your own exceptions, use a try/catch block within your task.- Parameters:
task
- The task to run.
-
enqueueWork
- See Also:
-
flow
PacketFlow flow()Returns the flow of the received payload.- Returns:
- the flow of the received payload
-
protocol
Returns the protocol of the connection.- Returns:
- the protocol of the connection
-
handle
Handles a packet using the current context.Used to trigger vanilla handling when custom payloads may be transformed into a vanilla packet.
- Parameters:
packet
- The packet.
-
handle
Handles a payload using the current context.Used to handle sub-payloads if necessary.
- Parameters:
payload
- The payload.
-
finishCurrentTask
Marks aConfigurationTask
as completed.- Parameters:
type
- The type of task that was completed.- Throws:
UnsupportedOperationException
- if called on the client, or called on the server outside of the configuration phase.
-
channelHandlerContext
default io.netty.channel.ChannelHandlerContext channelHandlerContext()Returns the channel handler context.- Returns:
- the channel handler context
-