Skip to main content

nurtex_protocol/types/
player_skin.rs

1use nurtex_codec::Buffer;
2
3/// Отображаемые части скина
4#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
5pub struct DisplayedSkinParts {
6  pub cape: bool,
7  pub jacket: bool,
8  pub left_sleeve: bool,
9  pub right_sleeve: bool,
10  pub left_pants_leg: bool,
11  pub right_pants_leg: bool,
12  pub hat: bool,
13}
14
15impl Default for DisplayedSkinParts {
16  fn default() -> Self {
17    Self {
18      cape: true,
19      jacket: true,
20      left_sleeve: true,
21      right_sleeve: true,
22      left_pants_leg: true,
23      right_pants_leg: true,
24      hat: true,
25    }
26  }
27}
28
29impl DisplayedSkinParts {
30  /// Метод получения битовой маски из `DisplayedSkinParts`
31  pub fn to_mask(&self) -> u8 {
32    let mut mask = 0u8;
33
34    if self.cape {
35      mask |= 0x01;
36    }
37    if self.jacket {
38      mask |= 0x02;
39    }
40    if self.left_sleeve {
41      mask |= 0x04;
42    }
43    if self.right_sleeve {
44      mask |= 0x08;
45    }
46    if self.left_pants_leg {
47      mask |= 0x10;
48    }
49    if self.right_pants_leg {
50      mask |= 0x20;
51    }
52    if self.hat {
53      mask |= 0x40;
54    }
55
56    mask
57  }
58
59  /// Метод получения `DisplayedSkinParts` из битовой маски
60  pub fn from_mask(mask: u8) -> Self {
61    Self {
62      cape: (mask & 0x01) != 0,
63      jacket: (mask & 0x02) != 0,
64      left_sleeve: (mask & 0x04) != 0,
65      right_sleeve: (mask & 0x08) != 0,
66      left_pants_leg: (mask & 0x10) != 0,
67      right_pants_leg: (mask & 0x20) != 0,
68      hat: (mask & 0x40) != 0,
69    }
70  }
71}
72
73impl Buffer for DisplayedSkinParts {
74  fn read_buf(buffer: &mut std::io::Cursor<&[u8]>) -> Option<Self> {
75    Some(Self::from_mask(u8::read_buf(buffer)?))
76  }
77
78  fn write_buf(&self, buffer: &mut impl std::io::Write) -> std::io::Result<()> {
79    self.to_mask().write_buf(buffer)
80  }
81}