1.16+: implement EntityEquipment_Array top-bit terminated variant (#439)

Fixes #408 1.16.1: EntityEquipment_VarInt : Main thread panic: Failed to read all of packet 0x47, had 363 bytes left - enchants
Fixes #421 1.16.1: EntityEquipment_VarInt : Player heads break things. (Failed to read all of packet 0x47, had 1292 bytes left)
This commit is contained in:
iceiix 2020-12-19 15:51:27 -08:00 committed by GitHub
parent 37f55e2c57
commit 433429e8a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 2 deletions

View File

@ -1738,6 +1738,10 @@ state_packets!(
/// EntityEquipment is sent to display an item on an entity, like a sword
/// or armor. Slot 0 is the held item and slots 1 to 4 are boots, leggings
/// chestplate and helmet respectively.
packet EntityEquipment_Array {
field entity_id: VarInt =,
field equipments: packet::EntityEquipments =,
}
packet EntityEquipment_VarInt {
field entity_id: VarInt =,
field slot: VarInt =,
@ -2505,6 +2509,57 @@ impl Serializable for CriterionProgress {
}
}
#[derive(Debug, Default)]
pub struct EntityEquipment {
pub slot: u8,
pub item: Option<item::Stack>,
}
impl Serializable for EntityEquipment {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
Ok(EntityEquipment {
slot: Serializable::read_from(buf)?,
item: Serializable::read_from(buf)?,
})
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
self.slot.write_to(buf)?;
self.item.write_to(buf)
}
}
// Top-bit terminated array of EntityEquipment
#[derive(Debug, Default)]
pub struct EntityEquipments {
pub equipments: Vec<EntityEquipment>,
}
impl Serializable for EntityEquipments {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
let mut equipments: Vec<EntityEquipment> = vec![];
loop {
let e: EntityEquipment = Serializable::read_from(buf)?;
equipments.push(EntityEquipment {
slot: e.slot & 0x7f,
item: e.item,
});
if e.slot & 0x80 == 0 {
break;
}
// TODO: detect infinite loop
}
Ok(EntityEquipments { equipments })
}
fn write_to<W: io::Write>(&self, _buf: &mut W) -> Result<(), Error> {
unimplemented!()
}
}
#[derive(Debug, Default)]
pub struct EntityProperty {
pub key: String,

View File

@ -128,7 +128,7 @@ protocol_packet_ids!(
0x44 => EntityMetadata
0x45 => EntityAttach
0x46 => EntityVelocity
0x47 => EntityEquipment_VarInt // TODO: changed to an array, but earlier than 1.16.1
0x47 => EntityEquipment_Array
0x48 => SetExperience
0x49 => UpdateHealth
0x4a => ScoreboardObjective

View File

@ -129,7 +129,7 @@ protocol_packet_ids!(
0x44 => EntityMetadata
0x45 => EntityAttach
0x46 => EntityVelocity
0x47 => EntityEquipment_VarInt // TODO: changed to an array, but earlier than 1.16.1
0x47 => EntityEquipment_Array
0x48 => SetExperience
0x49 => UpdateHealth
0x4a => ScoreboardObjective