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);
     }
});
  • Field Details

    • SERVER_INSTANCE

      static final PacketContext.ReadKey<net.minecraft.server.MinecraftServer> SERVER_INSTANCE
      The server instance that handles this connection. Only present on clientbound connections. This value is set once the ServerHandshakePacketListenerImpl is constructed.
    • GAME_PROFILE

      static final PacketContext.ReadKey<com.mojang.authlib.GameProfile> GAME_PROFILE
      The Game Profile attached to this connection. This value is set on both server and client, once the login process succeeds.
    • CONNECTION

      static final PacketContext.ReadKey<@NonNull net.minecraft.network.Connection> CONNECTION
      The connection that owns this packet context. This value is always present.
  • Method Details

    • get

      <T> @Nullable T get(PacketContext.ReadKey<T> key)
      Returns currently stored value.
      Parameters:
      key - unique key under which value is stored
      Returns:
      stored value or null if not set.
    • orElseThrow

      default <T> T orElseThrow(PacketContext.ReadKey<T> key)
      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

      default <T> T orElse(PacketContext.ReadKey<T> key, T defaultValue)
      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 stored
      defaultValue - value to return if no value is set
      Returns:
      stored value if present, defaultValue otherwise
    • set

      <T> void set(PacketContext.Key<T> key, @Nullable T value)
      Stores the value.
      Parameters:
      key - unique key under which value is stored
      value - value to store, if null it will remove it instead
    • get

      static @Nullable PacketContext get()
      Returns currently set packet context.
      Returns:
      current context or null
    • orElseThrow

      static PacketContext orElseThrow()
      Returns currently set packet context. In case of context missing, this method will throw.
      Returns:
      current context or null
    • runWithContext

      static void runWithContext(PacketContextProvider provider, Runnable runnable)
      Runs specified runnable under a packet context.
      Parameters:
      provider - provider of the context
      runnable - runnable to execute
    • supplyWithContext

      static <T> T supplyWithContext(PacketContextProvider provider, Supplier<T> supplier)
      Runs specified runnable under a packet context, returning a value.
      Parameters:
      provider - provider of the context
      supplier - supplier to execute
      Returns:
      result of supplier
    • key

      static <T> PacketContext.Key<T> key(net.minecraft.resources.Identifier key)
      Creates a new key to be used with the packet context.
      Parameters:
      key - identifier for this key
      Returns:
      a unique key