chunkedge_protocol/packets/play/
set_equipment_s2c.rs1use std::io::Write;
2
3use chunkedge_binary::{Decode, Encode, VarInt};
4use chunkedge_item::ItemStack;
5
6use crate::Packet;
7
8#[derive(Clone, PartialEq, Debug, Packet)]
9pub struct SetEquipmentS2c {
10 pub entity_id: VarInt,
11 pub equipment: Vec<EquipmentEntry>,
12}
13
14#[derive(Clone, PartialEq, Debug, Encode, Decode)]
15pub struct EquipmentEntry {
16 pub slot: EquipmentSlot,
17 pub item: ItemStack,
18}
19
20#[derive(Clone, Copy, PartialEq, Debug, Encode, Decode)]
21pub enum EquipmentSlot {
22 MainHand = 0,
23 OffHand = 1,
24 Boots = 2,
25 Leggings = 3,
26 Chestplate = 4,
27 Helmet = 5,
28 Body = 6,
29 Saddle = 7,
30}
31
32impl EquipmentSlot {
33 pub const fn number_of_members() -> usize {
34 8
36 }
37}
38
39impl From<u8> for EquipmentSlot {
40 fn from(value: u8) -> Self {
41 match value {
42 0 => EquipmentSlot::MainHand,
43 1 => EquipmentSlot::OffHand,
44 2 => EquipmentSlot::Boots,
45 3 => EquipmentSlot::Leggings,
46 4 => EquipmentSlot::Chestplate,
47 5 => EquipmentSlot::Helmet,
48 6 => EquipmentSlot::Body,
49 7 => EquipmentSlot::Saddle,
50 _ => panic!("Invalid equipment slot value: {value}"),
51 }
52 }
53}
54
55impl From<i8> for EquipmentSlot {
56 fn from(value: i8) -> Self {
57 match value {
58 0 => EquipmentSlot::MainHand,
59 1 => EquipmentSlot::OffHand,
60 2 => EquipmentSlot::Boots,
61 3 => EquipmentSlot::Leggings,
62 4 => EquipmentSlot::Chestplate,
63 5 => EquipmentSlot::Helmet,
64 6 => EquipmentSlot::Body,
65 7 => EquipmentSlot::Saddle,
66 _ => panic!("Invalid equipment slot value: {value}"),
67 }
68 }
69}
70
71impl Encode for SetEquipmentS2c {
72 fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
73 self.entity_id.encode(&mut w)?;
74
75 for i in 0..self.equipment.len() {
76 let slot = self.equipment[i].slot as i8;
77 if i != self.equipment.len() - 1 {
78 (slot | -128).encode(&mut w)?;
79 } else {
80 slot.encode(&mut w)?;
81 }
82 self.equipment[i].item.encode(&mut w)?;
83 }
84
85 Ok(())
86 }
87}
88
89impl<'a> Decode<'a> for SetEquipmentS2c {
90 fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
91 let entity_id = VarInt::decode(r)?;
92
93 let mut equipment = vec![];
94
95 loop {
96 let slot = i8::decode(r)?;
97 let item = ItemStack::decode(r)?;
98 equipment.push(EquipmentEntry {
99 slot: (slot & 127).into(),
100 item,
101 });
102 if slot & -128 == 0 {
103 break;
104 }
105 }
106
107 Ok(Self {
108 entity_id,
109 equipment,
110 })
111 }
112}