Skip to main content

nurtex_protocol/types/
player_action.rs

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