-
Notifications
You must be signed in to change notification settings - Fork 1
Best Practices
RazorPlay01 edited this page May 14, 2025
·
2 revisions
This guide covers best practices, error handling, and troubleshooting for the Minecraft Networking API.
-
Unique Packet IDs: Ensure
getPacketId()returns a unique string for each packet type. - No-Args Constructor: Include a no-args constructor in packet classes for deserialization.
- Consistent Serialization: Read and write data in the same order to avoid mismatches.
-
Use Compression: For large packets, use
writeCompressed/readCompressedto reduce bandwidth. - Debug Logging: Use SLF4J to log packet activity for debugging.
The API provides specific exceptions:
-
PacketSerializationException: Issues during serialization/deserialization. -
PacketInstantiationException: Failure to create packet instances. -
PacketNotFoundException: Unregistered packet type. -
PacketRegistrationException: Duplicate IDs or invalid packet classes.
Example:
try {
byte[] data = PacketTCP.write(packet);
} catch (PacketSerializationException e) {
logger.error("Failed to serialize packet: {}", e.getMessage(), e);
}-
Packet Not Received:
- Verify packet registration on both client and server.
- Ensure channel names match (e.g.,
my_mod:packets_channel).
-
Serialization Errors:
- Check that
readandwritemethods use the same data order and types.
- Check that
-
ClassNotFoundException:
- Ensure the packet class is available on both client and server.
-
Large Packet Issues:
- Use compression and verify against
MAX_COMPRESSED_SIZE(1MB default).
- Use compression and verify against