azalea_protocol/packets/game/
c_set_equipment.rs1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, BufReadError};
4use azalea_core::entity_id::MinecraftEntityId;
5use azalea_inventory::{ItemStack, components::EquipmentSlot};
6use azalea_protocol_macros::ClientboundGamePacket;
7
8#[derive(AzBuf, ClientboundGamePacket, Clone, Debug, PartialEq)]
9pub struct ClientboundSetEquipment {
10 #[var]
11 pub entity_id: MinecraftEntityId,
12 pub slots: EquipmentSlots,
13}
14
15#[derive(Clone, Debug, PartialEq)]
16pub struct EquipmentSlots {
17 pub slots: Vec<(EquipmentSlot, ItemStack)>,
18}
19
20impl AzBuf for EquipmentSlots {
21 fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
22 let mut slots = vec![];
23
24 loop {
25 let equipment_byte = u8::azalea_read(buf)?;
26 let equipment_slot =
27 EquipmentSlot::from_byte(equipment_byte & 127).ok_or_else(|| {
28 BufReadError::UnexpectedEnumVariant {
29 id: equipment_byte.into(),
30 }
31 })?;
32 let item = ItemStack::azalea_read(buf)?;
33 slots.push((equipment_slot, item));
34 if equipment_byte & 128 == 0 {
35 break;
36 };
37 }
38
39 Ok(EquipmentSlots { slots })
40 }
41 fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
42 for i in 0..self.slots.len() {
43 let (equipment_slot, item) = &self.slots[i];
44 let mut equipment_byte = *equipment_slot as u8;
45 if i != self.slots.len() - 1 {
46 equipment_byte |= 128;
47 }
48 equipment_byte.azalea_write(buf)?;
49 item.azalea_write(buf)?;
50 }
51
52 Ok(())
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use std::io::Cursor;
59
60 use azalea_buf::AzBuf;
61
62 use super::*;
63
64 #[test]
65 fn test_read_lifesteal_net_set_equipment() {
66 let contents = [1, 128, 0, 129, 0, 130, 0, 131, 0, 132, 0, 133, 0, 7, 0];
67 let mut buf = Cursor::new(contents.as_slice());
68 let packet = ClientboundSetEquipment::azalea_read(&mut buf).unwrap();
69 println!("{packet:?}");
70
71 assert_eq!(buf.position(), contents.len() as u64);
72 }
73}