azalea_protocol/packets/game/
s_set_command_block.rs

1use std::io::Cursor;
2
3use azalea_buf::{AzBuf, AzaleaRead, BufReadError};
4use azalea_core::{bitset::FixedBitSet, position::BlockPos};
5use azalea_protocol_macros::ServerboundGamePacket;
6
7use crate::packets::AzaleaWrite;
8
9#[derive(Clone, Debug, ServerboundGamePacket)]
10pub struct ServerboundSetCommandBlock {
11    pub pos: BlockPos,
12    pub command: String,
13    pub mode: Mode,
14
15    pub track_output: bool,
16    pub conditional: bool,
17    pub automatic: bool,
18}
19
20#[derive(AzBuf, Clone, Copy, Debug)]
21pub enum Mode {
22    Sequence = 0,
23    Auto = 1,
24    Redstone = 2,
25}
26
27impl AzaleaRead for ServerboundSetCommandBlock {
28    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
29        let pos = BlockPos::azalea_read(buf)?;
30        let command = String::azalea_read(buf)?;
31        let mode = Mode::azalea_read(buf)?;
32
33        let set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::azalea_read(buf)?;
34        Ok(Self {
35            pos,
36            command,
37            mode,
38            track_output: set.index(0),
39            conditional: set.index(1),
40            automatic: set.index(2),
41        })
42    }
43}
44
45impl AzaleaWrite for ServerboundSetCommandBlock {
46    fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
47        self.pos.azalea_write(buf)?;
48        self.command.azalea_write(buf)?;
49        self.mode.azalea_write(buf)?;
50
51        let mut set = FixedBitSet::<{ 3_usize.div_ceil(8) }>::new();
52        if self.track_output {
53            set.set(0);
54        }
55        if self.conditional {
56            set.set(1);
57        }
58        if self.automatic {
59            set.set(2);
60        }
61        set.azalea_write(buf)
62    }
63}