Skip to content

Implement Packet Context API.#5164

Open
Patbox wants to merge 10 commits intoFabricMC:26.1from
Patbox:packet_context
Open

Implement Packet Context API.#5164
Patbox wants to merge 10 commits intoFabricMC:26.1from
Patbox:packet_context

Conversation

@Patbox
Copy link
Member

@Patbox Patbox commented Jan 25, 2026

Packet Context is a simple way to store and access values between networking stages (login, configuration, play) and between handler/config sync/whatever and packet serialization (for example changing the vanilla packet format depending on client having XYZ).
PacketContext can be sourced from Connection and *PacketListernerImpl classes for usage outside of serialization and with PacketContext.get() (or orElseThrow()) inside.

Values can be then simply retriever with a getValue(Key) method or stored with setValue(Key, T) (or updateValues for changing multiple at the same time).
There are also provided method to run something with PacketContext.get() set up outside of vanilla packet serialization for handling the edge cases where it is needed.

The idea for this api is based from https://github.com/NucleoidMC/packet-tweaker, which is used by few mods/libraries for this feature. In case of Fabric API itself it will be useful for handling of vanilla compatibility in case of extended (stream) codec formats.

This pr is fully functional. Only things left to consider is what/if fabric should provide some contexts by default.

@Patbox Patbox added enhancement New feature or request module: networking api Pull requests and issues related to the networking api labels Jan 25, 2026
@sylv256 sylv256 added the tests PR has tests label Jan 26, 2026
Copy link
Member

@modmuss50 modmuss50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just a few nit picks.

Comment on lines 89 to 91
@Nullable
static PacketContext get() {
return PacketContextImpl.VALUE.get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ScopedValue.get() throws if its not bound, in what case will this return null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops, will probably fix that then

Comment on lines +31 to +32
* This class allow to easily pass context between multiple packet listeners and packet serialization.
* All connections get their own unique context object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A simple code example or something to show how to use it may be useful.

Comment on lines 70 to 75
* Stores the value.
*
* @param key unique key under which value is stored
* @param value value to store
*/
<T> void set(Key<T> key, T value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not document that T can be nullable, to remove.

import net.fabricmc.fabric.test.networking.NetworkingTestmods;

public class PacketContextTest implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger(PacketContextTest.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mega nit pick: loggers should be private.

@modmuss50
Copy link
Member

You could likely quite easily write a unit test to cover the basic get+set logic of this. Ill leave that up to you.

@modmuss50 modmuss50 added the status: last call If you care, make yourself heard right away! label Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request module: networking api Pull requests and issues related to the networking api status: last call If you care, make yourself heard right away! tests PR has tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants