mcproto-types 0.1.4-beta.1

All types for Minecraft.
Documentation
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use derive_more::with_trait::{Into, Deref, DerefMut, From};
use crate::{Codec, TypeCodecError};
use mcproto_codec::{VarIntRead, VarIntWrite};
use uuid::Uuid;

const MAX_TEXT_COMPONENT_DECODE_CHARS: usize = 262_144;
const MAX_TEXT_COMPONENT_ENCODE_CHARS: usize = 32_767;
const POSITION_XZ_MIN: i32 = -33_554_432;
const POSITION_XZ_MAX: i32 = 33_554_431;
const POSITION_Y_MIN: i32 = -2_048;
const POSITION_Y_MAX: i32 = 2_047;


#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, From, Into, Deref, DerefMut)]
pub struct Angle(pub u8);

impl Angle {
    /// 从角度值创建 Angle (0.0 - 360.0)
    pub fn from_degrees(degrees: f32) -> Self {
        let normalized = degrees.rem_euclid(360.0);
        let steps = (normalized / 360.0 * 256.0).round() as u8;
        Angle(steps)
    }

    /// 转换为角度值 (0.0 - 360.0)
    pub fn to_degrees(self) -> f32 {
        (self.0 as f32) / 256.0 * 360.0
    }

    /// 从弧度创建 Angle
    pub fn from_radians(radians: f32) -> Self {
        Self::from_degrees(radians.to_degrees())
    }

    /// 转换为弧度
    pub fn to_radians(self) -> f32 {
        self.to_degrees().to_radians()
    }
}

impl Codec for Angle {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        buf.push(self.0);
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        if buf.is_empty() {
            return Err(TypeCodecError::EmptyBuffer);
        }
        let byte = buf[0];
        *buf = &buf[1..];
        Ok(Angle(byte))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, From, Into, Deref, DerefMut)]
pub struct TextComponent(pub String);

impl Codec for TextComponent {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        self.0.encode(buf)
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        String::decode(buf).map(TextComponent)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, From, Into, Deref, DerefMut)]
pub struct JsonTextComponent(pub String);

impl Codec for JsonTextComponent {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        let char_count = self.0.chars().count();
        if char_count > MAX_TEXT_COMPONENT_ENCODE_CHARS {
            return Err(TypeCodecError::InvalidTextComponentLength(char_count));
        }
        self.0.encode(buf)
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        let text = String::decode(buf)?;
        let char_count = text.chars().count();
        if char_count > MAX_TEXT_COMPONENT_DECODE_CHARS {
            return Err(TypeCodecError::InvalidTextComponentLength(char_count));
        }
        Ok(JsonTextComponent(text))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Position {
    pub x: i32,
    pub y: i32,
    pub z: i32,
}

impl Position {
    pub fn new(x: i32, y: i32, z: i32) -> Result<Self, TypeCodecError> {
        if !(POSITION_XZ_MIN..=POSITION_XZ_MAX).contains(&x) {
            return Err(TypeCodecError::InvalidPositionValue("x", x));
        }
        if !(POSITION_Y_MIN..=POSITION_Y_MAX).contains(&y) {
            return Err(TypeCodecError::InvalidPositionValue("y", y));
        }
        if !(POSITION_XZ_MIN..=POSITION_XZ_MAX).contains(&z) {
            return Err(TypeCodecError::InvalidPositionValue("z", z));
        }
        Ok(Self { x, y, z })
    }

    const fn decode_signed(value: u64, bits: u32) -> i32 {
        let shift = 64 - bits;
        ((value << shift) as i64 >> shift) as i32
    }
}

impl Codec for Position {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        Self::new(self.x, self.y, self.z)?;

        let x = (self.x as i64 & 0x3ff_ffff) as u64;
        let z = (self.z as i64 & 0x3ff_ffff) as u64;
        let y = (self.y as i64 & 0xfff) as u64;
        let packed = (x << 38) | (z << 12) | y;

        buf.extend_from_slice(&packed.to_be_bytes());
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        let bytes: [u8; 8] = buf
            .get(..8)
            .ok_or(TypeCodecError::EndOfBuffer(buf.len(), 8))?
            .try_into()
            .map_err(|_| TypeCodecError::EndOfBuffer(buf.len(), 8))?;
        *buf = &buf[8..];

        let packed = u64::from_be_bytes(bytes);
        let x = Self::decode_signed(packed >> 38, 26);
        let z = Self::decode_signed((packed >> 12) & 0x3ff_ffff, 26);
        let y = Self::decode_signed(packed & 0xfff, 12);

        Self::new(x, y, z)
    }
}

// LpVec3
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LpVec3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

// Bit Set
#[derive(Debug, Clone, PartialEq, Eq, Hash, From, Into, Deref, DerefMut)]
pub struct BitSet(pub Vec<i64>);

impl BitSet {
    pub fn new(data: Vec<i64>) -> Self {
        Self(data)
    }

    pub fn get(&self, index: usize) -> bool {
        let long_index = index / 64;
        let bit_index = index % 64;
        self.0
            .get(long_index)
            .map(|value| ((*value as u64) & (1u64 << bit_index)) != 0)
            .unwrap_or(false)
    }

    pub fn set(&mut self, index: usize, value: bool) {
        let long_index = index / 64;
        let bit_index = index % 64;
        if long_index >= self.0.len() {
            self.0.resize(long_index + 1, 0);
        }
        let mut bits = self.0[long_index] as u64;
        let mask = 1u64 << bit_index;
        if value {
            bits |= mask;
        } else {
            bits &= !mask;
        }
        self.0[long_index] = bits as i64;
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, From, Into, Deref, DerefMut)]
pub struct FixedBitSet<const BYTES: usize>(pub [u8; BYTES]);

impl<const BYTES: usize> FixedBitSet<BYTES> {
    pub const fn new(data: [u8; BYTES]) -> Self {
        Self(data)
    }

    pub const fn byte_len() -> usize {
        BYTES
    }

    pub const fn bit_capacity() -> usize {
        BYTES * 8
    }

    pub fn get(&self, index: usize) -> bool {
        if index >= Self::bit_capacity() {
            return false;
        }
        let byte_index = index / 8;
        let bit_index = index % 8;
        (self.0[byte_index] & (1 << bit_index)) != 0
    }

    pub fn set(&mut self, index: usize, value: bool) {
        if index >= Self::bit_capacity() {
            return;
        }
        let byte_index = index / 8;
        let bit_index = index % 8;
        let mask = 1u8 << bit_index;
        if value {
            self.0[byte_index] |= mask;
        } else {
            self.0[byte_index] &= !mask;
        }
    }
}

impl<const BYTES: usize> Codec for FixedBitSet<BYTES> {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        buf.extend_from_slice(&self.0);
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        let bytes: [u8; BYTES] = buf
            .get(..BYTES)
            .ok_or(TypeCodecError::EndOfBuffer(buf.len(), BYTES))?
            .try_into()
            .map_err(|_| TypeCodecError::EndOfBuffer(buf.len(), BYTES))?;
        *buf = &buf[BYTES..];
        Ok(Self(bytes))
    }
}

impl Codec for BitSet {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        let len = i32::try_from(self.0.len())
            .map_err(|_| TypeCodecError::InvalidArrayLength(i32::MAX))?;
        buf.write_varint(len)?;
        for value in &self.0 {
            buf.extend_from_slice(&value.to_be_bytes());
        }
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        let len = buf.read_varint()?;
        if len < 0 {
            return Err(TypeCodecError::InvalidArrayLength(len));
        }
        let len = len as usize;
        let mut data = Vec::with_capacity(len);

        for _ in 0..len {
            let bytes: [u8; 8] = buf
                .get(..8)
                .ok_or(TypeCodecError::EndOfBuffer(buf.len(), 8))?
                .try_into()
                .map_err(|_| TypeCodecError::EndOfBuffer(buf.len(), 8))?;
            *buf = &buf[8..];
            data.push(i64::from_be_bytes(bytes));
        }

        Ok(Self(data))
    }
}

impl LpVec3 {
    pub const fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }
}

impl Codec for LpVec3 {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        const MIN_THRESHOLD: f64 = 3.051_944_088_384_301e-5;
        const MAX_COORDINATE: f64 = 17_179_869_183.0;
        const MAX_QUANTIZED_VALUE: f64 = 32_766.0;

        let x = self.x.clamp(-MAX_COORDINATE, MAX_COORDINATE);
        let y = self.y.clamp(-MAX_COORDINATE, MAX_COORDINATE);
        let z = self.z.clamp(-MAX_COORDINATE, MAX_COORDINATE);

        if !x.is_finite() || !y.is_finite() || !z.is_finite() {
            buf.push(0);
            return Ok(());
        }

        let max_coordinate = x.abs().max(y.abs()).max(z.abs());
        if max_coordinate < MIN_THRESHOLD {
            buf.push(0);
            return Ok(());
        }

        let max_coordinate_i = max_coordinate as i64;
        let scale_factor = if max_coordinate > max_coordinate_i as f64 {
            max_coordinate_i + 1
        } else {
            max_coordinate_i
        };

        let need_continuation = (scale_factor & 3) != scale_factor;
        let packed_scale = if need_continuation {
            (scale_factor & 3) | 4
        } else {
            scale_factor
        };

        let pack = |v: f64| -> u64 {
            let normalized = v / scale_factor as f64;
            let quantized = ((normalized * 0.5 + 0.5) * MAX_QUANTIZED_VALUE).round();
            (quantized as u64) & 0x7fff
        };

        let packed_x = pack(x) << 3;
        let packed_y = pack(y) << 18;
        let packed_z = pack(z) << 33;
        let packed = packed_z | packed_y | packed_x | (packed_scale as u64);

        buf.push(packed as u8);
        buf.push((packed >> 8) as u8);
        buf.extend_from_slice(&((packed >> 16) as u32).to_be_bytes());
        if need_continuation {
            buf.write_varint((scale_factor >> 2) as i32)?;
        }
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        const MAX_QUANTIZED_VALUE: f64 = 32_766.0;

        let byte1 = *buf
            .first()
            .ok_or(TypeCodecError::EndOfBuffer(buf.len(), 1))?;
        *buf = &buf[1..];

        if byte1 == 0 {
            return Ok(Self::new(0.0, 0.0, 0.0));
        }

        let byte2 = *buf
            .first()
            .ok_or(TypeCodecError::EndOfBuffer(buf.len(), 1))?;
        *buf = &buf[1..];

        let bytes3_to_6: [u8; 4] = buf
            .get(..4)
            .ok_or(TypeCodecError::EndOfBuffer(buf.len(), 4))?
            .try_into()
            .map_err(|_| TypeCodecError::EndOfBuffer(buf.len(), 4))?;
        *buf = &buf[4..];

        let bytes3_to_6 = u32::from_be_bytes(bytes3_to_6) as u64;
        let packed = (bytes3_to_6 << 16) | ((byte2 as u64) << 8) | (byte1 as u64);

        let mut scale_factor = (byte1 & 3) as u64;
        if (byte1 & 4) == 4 {
            scale_factor |= ((buf.read_varint()? as u32 as u64) << 2) & 0xffff_ffff_ffff_fffc;
        }

        let unpack = |v: u64| -> f64 {
            let q = (v & 0x7fff).min(32_766) as f64;
            q * 2.0 / MAX_QUANTIZED_VALUE - 1.0
        };

        let sf = scale_factor as f64;
        Ok(Self {
            x: unpack(packed >> 3) * sf,
            y: unpack(packed >> 18) * sf,
            z: unpack(packed >> 33) * sf,
        })
    }
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, From, Into, Deref, DerefMut)]
pub struct UUID(pub Uuid);

impl Codec for UUID {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        buf.extend_from_slice(self.0.as_bytes());
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        let bytes: [u8; 16] = buf
            .get(..16)
            .ok_or(TypeCodecError::EndOfBuffer(buf.len(), 16))?
            .try_into()
            .map_err(|_| TypeCodecError::EndOfBuffer(buf.len(), 16))?;
        *buf = &buf[16..];

        Ok(UUID(Uuid::from_bytes(bytes)))
    }
}
// TODO: EntityMetadata
// TODO: Slot
// TODO: Hashed Slot

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Nbt(Vec<u8>);
impl Codec for Nbt {
    fn encode(&self, buf: &mut Vec<u8>) -> Result<(), TypeCodecError> {
        buf.extend_from_slice(&self.0);
        Ok(())
    }

    fn decode(buf: &mut &[u8]) -> Result<Self, TypeCodecError> {
        let start = *buf;
        let mut pos = 0;
        let mut depth: i32 = 1;

        while depth > 0 {
            let tag_id = start.get(pos).ok_or(TypeCodecError::EndOfBuffer(pos, pos + 1))?;
            pos += 1;

            if *tag_id == 0x00 {
                depth -= 1;
                continue;
            }

            // 跳过名字(u16 长度 + 字节)
            let name_len = u16::from_be_bytes([
                start[pos], start[pos + 1]
            ]) as usize;
            pos += 2 + name_len;

            // 根据类型跳过值
            pos = skip_value(start, pos, *tag_id)?;

            if *tag_id == 0x0A {
                depth += 1;
            }
        }

        *buf = &buf[pos..];
        Ok(Nbt(start[..pos].to_vec()))
    }
}

fn skip_value(buf: &[u8], pos: usize, tag_id: u8) -> Result<usize, TypeCodecError> {
    match tag_id {
        0x01 => Ok(pos + 1),                          // Byte
        0x02 | 0x08 => {                               // Short | String (skip len + bytes)
            let len = u16::from_be_bytes([buf[pos], buf[pos + 1]]) as usize;
            Ok(pos + 2 + if tag_id == 0x08 { len } else { 2 })
        }
        0x03 | 0x05 | 0x07 | 0x0B => {                 // Int | Float | ByteArray | IntArray
            let len = i32::from_be_bytes([buf[pos], buf[pos+1], buf[pos+2], buf[pos+3]]);
            if len < 0 { return Err(TypeCodecError::InvalidArrayLength(len)); }
            let size = if tag_id == 0x07 { len } else if tag_id == 0x0B { len * 4 } else { 4 };
            Ok(pos + 4 + size as usize)
        }
        0x04 | 0x06 | 0x0C => {                         // Long | Double | LongArray
            let len = if tag_id == 0x0C {
                let l = i32::from_be_bytes([buf[pos], buf[pos+1], buf[pos+2], buf[pos+3]]);
                if l < 0 { return Err(TypeCodecError::InvalidArrayLength(l)); }
                l * 8
            } else { 8 };
            Ok(pos + (if tag_id == 0x0C { 4 } else { 0 }) + len as usize)
        }
        _ => Err(TypeCodecError::UnknownNbtTag(tag_id))
    }
}