1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::borrow::Cow;
use std::io::Write;

use bitfield_struct::bitfield;

use crate::chunk_section_pos::ChunkSectionPos;
use crate::{Decode, Encode, Packet, VarLong};

#[derive(Clone, Debug, Encode, Decode, Packet)]
pub struct ChunkDeltaUpdateS2c<'a> {
    pub chunk_sect_pos: ChunkSectionPos,
    pub blocks: Cow<'a, [ChunkDeltaUpdateEntry]>,
}

#[bitfield(u64)]
#[derive(PartialEq, Eq)]
pub struct ChunkDeltaUpdateEntry {
    #[bits(4)]
    pub off_y: u8,
    #[bits(4)]
    pub off_z: u8,
    #[bits(4)]
    pub off_x: u8,
    pub block_state: u32,
    #[bits(20)]
    _pad: u32,
}

impl Encode for ChunkDeltaUpdateEntry {
    fn encode(&self, w: impl Write) -> anyhow::Result<()> {
        VarLong(self.0 as i64).encode(w)
    }
}

impl Decode<'_> for ChunkDeltaUpdateEntry {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(ChunkDeltaUpdateEntry(VarLong::decode(r)?.0 as u64))
    }
}