Interface PacketContext
@NonExtendable
public interface PacketContext
This class allow to easily pass context between multiple packet listeners and packet serialization.
All connections get their own unique context object.
When using outside of packet serialization, you can retrieve instance of PacketContext
by calling the PacketContextProvider.getPacketContext() method on vanilla packet listeners.
When inside of packet serialization, whatever it's within StreamCodec or networking-used Codec
you can retrievie the instance with get() or orElseThrow().
Example usage:
PacketContext.Key<TriState> TATER_MESSAGE = PacketContext.key(Identifier.fromNamespaceAndPath("mod", "tater_message"));
ServerConfigurationNetworking.registerGlobalReceiver(ServerboundModConfig.TYPE, (packet, context) -> {
context.packetListener().getPacketContext().set(TATER_MESSAGE, packet.taterMessage());
});
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
if (handler.getPacketContext().orElse(TATER_MESSAGE, TriState.DEFAULT).orElse(ModConfig.taterMessage)) {
handler.getPlayer().sendSystemMessage(Component.literal("I am a Tiny Potato and I believe in you!"), false);
}
});
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfacestatic interface -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PacketContext.ReadKey<@NonNull net.minecraft.network.Connection> The connection that owns this packet context.static final PacketContext.ReadKey<com.mojang.authlib.GameProfile> The Game Profile attached to this connection.static final PacketContext.ReadKey<net.minecraft.server.MinecraftServer> The server instance that handles this connection. -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable PacketContextget()Returns currently set packet context.<T> @Nullable Tget(PacketContext.ReadKey<T> key) Returns currently stored value.static <T> PacketContext.Key<T> key(net.minecraft.resources.Identifier key) Creates a new key to be used with the packet context.default <T> TorElse(PacketContext.ReadKey<T> key, T defaultValue) Returns currently stored value.static PacketContextReturns currently set packet context.default <T> TorElseThrow(PacketContext.ReadKey<T> key) Returns currently stored value.static voidrunWithContext(PacketContextProvider provider, Runnable runnable) Runs specified runnable under a packet context.<T> voidset(PacketContext.Key<T> key, @Nullable T value) Stores the value.static <T> TsupplyWithContext(PacketContextProvider provider, Supplier<T> supplier) Runs specified runnable under a packet context, returning a value.
-
Field Details
-
SERVER_INSTANCE
The server instance that handles this connection. Only present on clientbound connections. This value is set once theServerHandshakePacketListenerImplis constructed. -
GAME_PROFILE
The Game Profile attached to this connection. This value is set on both server and client, once the login process succeeds. -
CONNECTION
The connection that owns this packet context. This value is always present.
-
-
Method Details
-
get
Returns currently stored value.- Parameters:
key- unique key under which value is stored- Returns:
- stored value or null if not set.
-
orElseThrow
Returns currently stored value. In case of it not being stored earlier, this method will throw.- Parameters:
key- unique key under which value is stored- Returns:
- stored value
- Throws:
NullPointerException- if not set
-
orElse
Returns currently stored value. In case of it not being stored earlier, this method will return provided default value.- Parameters:
key- unique key under which value is storeddefaultValue- value to return if no value is set- Returns:
- stored value if present, defaultValue otherwise
-
set
Stores the value.- Parameters:
key- unique key under which value is storedvalue- value to store, if null it will remove it instead
-
get
Returns currently set packet context.- Returns:
- current context or null
-
orElseThrow
Returns currently set packet context. In case of context missing, this method will throw.- Returns:
- current context or null
-
runWithContext
Runs specified runnable under a packet context.- Parameters:
provider- provider of the contextrunnable- runnable to execute
-
supplyWithContext
Runs specified runnable under a packet context, returning a value.- Parameters:
provider- provider of the contextsupplier- supplier to execute- Returns:
- result of supplier
-
key
Creates a new key to be used with the packet context.- Parameters:
key- identifier for this key- Returns:
- a unique key
-