Skip to main content

nurtex_protocol/types/
player_action.rs

1use nurtex_codec::Buffer;
2use nurtex_codec::types::variable::VarI32;
3
4/// Действие игрока
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
6pub enum PlayerAction {
7  StartedDigging,
8  CancelledDigging,
9  FinishedDigging,
10  DropItemStack,
11  DropItem,
12  FinishUsingItem,
13  SwapItem,
14}
15
16impl Buffer for PlayerAction {
17  fn read_buf(buffer: &mut std::io::Cursor<&[u8]>) -> Option<Self> {
18    let id = i32::read_var(buffer)?;
19
20    Some(match id {
21      0 => Self::StartedDigging,
22      1 => Self::CancelledDigging,
23      2 => Self::FinishedDigging,
24      3 => Self::DropItemStack,
25      4 => Self::DropItem,
26      5 => Self::FinishUsingItem,
27      6 => Self::SwapItem,
28      _ => return None,
29    })
30  }
31
32  fn write_buf(&self, buffer: &mut impl std::io::Write) -> std::io::Result<()> {
33    let id = match self {
34      Self::StartedDigging => 0,
35      Self::CancelledDigging => 1,
36      Self::FinishedDigging => 2,
37      Self::DropItemStack => 3,
38      Self::DropItem => 4,
39      Self::FinishUsingItem => 5,
40      Self::SwapItem => 6,
41    };
42
43    id.write_var(buffer)?;
44
45    Ok(())
46  }
47}