Skip to content

Instantly share code, notes, and snippets.

@axus
Created November 21, 2010 19:06
Show Gist options
  • Save axus/709032 to your computer and use it in GitHub Desktop.
Save axus/709032 to your computer and use it in GitHub Desktop.
Minecraft inventory packet parsing
//Update player inventory
size_t mcmm_client::getInventory(netpacket *pkt )
{
int32_t inv_type;
int16_t inv_count;
//Total length = 7 bytes + (4 shorts + health OR 36 shorts + health)
size_t packetsize = pkt->get_maxsize();
size_t minimumsize = SIZE_INVENTORY;
if (packetsize < minimumsize) {
//Not enough to parse
return 0;
}
//Read the inventory type and number of slots
pkt->read(inv_type);
pkt->read(inv_count);
//Check packet size before reading more
minimumsize += (inv_count * 2);
if (packetsize < minimumsize) { return 0; }
//Store item info in arrays
int16_t item_id[inv_count];
uint8_t item_count[inv_count];
uint16_t item_health[inv_count];
//Read the inventory items, and optionally health
int16_t index;
for (index = 0; index < inv_count; index++)
{
pkt->read(item_id[index]);
if (item_id[index] != -1) {
//Check new minimum size
minimumsize += 3;
if (packetsize < minimumsize) { return 0; }
//Read item count and health
pkt->read(item_count[index]);
pkt->read(item_health[index]);
}
}
netC.debugLog << "getInventory Type=" << inv_type << " Count=" << inv_count << endl;
//Return number of bytes consumed
return pkt->get_read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment