Skip to content

Best Practices

RazorPlay01 edited this page May 14, 2025 · 2 revisions

Best Practices

This guide covers best practices, error handling, and troubleshooting for the Minecraft Networking API.

Best Practices

  • 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/readCompressed to reduce bandwidth.
  • Debug Logging: Use SLF4J to log packet activity for debugging.

Error Handling

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);
}

Troubleshooting

  • 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 read and write methods use the same data order and types.
  • 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).

Clone this wiki locally