Network Packet Module >=1.21.1
Availability
NetworkUtil (batch-sending utilities) was not synchronized in versions 1.21.2-1.21.11; it is only available in 1.21.1 and 26.1. All other APIs are consistent across all versions.
The package dev.anvilcraft.lib.v2.network provides a high-level abstraction over the NeoForge networking system to simplify packet definition, direction management, and automatic registration.
Documentation Index
| Document | Content |
|---|---|
| Packet Interfaces | IPacket, IClientboundPacket, IServerboundPacket, bidirectional packets |
| Registration System | @Network annotation, NetworkRegistrar, PacketProtocol |
| Sending Utilities | NetworkUtil batch-sending methods |
Core Workflow
- Define a packet class, implement the corresponding interface (e.g.,
IClientboundPacket), and declarestatic final Type<?>andStreamCodec - Add the
@Networkannotation topackage-info.java, specifying the protocol phase - Call
NetworkRegistrar.register(registrar, modId)insideRegisterPayloadHandlersEvent
Notes
- Packet classes must have a no-arg constructor (typically using records)
NetworkRegistrarlooks upTypeandStreamCodecstatic fields via reflection- If registration throws an exception, check whether the packet class is missing static fields or fails to implement the expected interface