kojacoord-protocol 0.1.0

Minecraft protocol types, packet (de)serialization and version registry for the Kojacoord proxy.
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
use bytes::{Buf, BufMut, Bytes, BytesMut};

use crate::codec::{Decode, Encode, PacketId};
use crate::error::ProtocolError;

fn decode_legacy_string(src: &mut Bytes) -> Result<String, ProtocolError> {
    if src.len() < 2 {
        return Err(ProtocolError::UnexpectedEof);
    }
    let len = u16::from_be_bytes([src[0], src[1]]) as usize;
    src.advance(2);
    let byte_len = len * 2;
    if src.len() < byte_len {
        return Err(ProtocolError::UnexpectedEof);
    }
    let raw = src.copy_to_bytes(byte_len);
    let chars: Vec<u16> = raw
        .chunks(2)
        .map(|c| u16::from_be_bytes([c[0], c[1]]))
        .collect();
    String::from_utf16(&chars).map_err(|_| ProtocolError::UnexpectedEof)
}

fn encode_legacy_string(s: &str, dst: &mut BytesMut) {
    let utf16: Vec<u16> = s.encode_utf16().collect();
    dst.extend_from_slice(&(utf16.len() as u16).to_be_bytes());
    for ch in &utf16 {
        dst.extend_from_slice(&ch.to_be_bytes());
    }
}

fn need(src: &Bytes, n: usize) -> Result<(), ProtocolError> {
    if src.remaining() < n {
        Err(ProtocolError::UnexpectedEof)
    } else {
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundKeepAlive {
    pub keep_alive_id: i32,
}

impl PacketId for ClientboundKeepAlive {
    fn packet_id(_ver: u32) -> u8 {
        0x00
    }
}

impl Encode for ClientboundKeepAlive {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.extend_from_slice(&self.keep_alive_id.to_be_bytes());
        Ok(())
    }
}

impl Decode for ClientboundKeepAlive {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4)?;
        Ok(Self {
            keep_alive_id: src.get_i32(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundChatMessage {
    pub message: String,
}

impl PacketId for ClientboundChatMessage {
    fn packet_id(_ver: u32) -> u8 {
        0x03
    }
}

impl Encode for ClientboundChatMessage {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_legacy_string(&self.message, dst);
        Ok(())
    }
}

impl Decode for ClientboundChatMessage {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        Ok(Self {
            message: decode_legacy_string(src)?,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundPlayerPosition {
    pub x: f64,
    pub y: f64,
    pub stance: f64,
    pub z: f64,
    pub yaw: f32,
    pub pitch: f32,
    pub on_ground: bool,
}

impl PacketId for ClientboundPlayerPosition {
    fn packet_id(_ver: u32) -> u8 {
        0x13
    }
}

impl Encode for ClientboundPlayerPosition {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_f64(self.x);
        dst.put_f64(self.y);
        dst.put_f64(self.stance);
        dst.put_f64(self.z);
        dst.put_f32(self.yaw);
        dst.put_f32(self.pitch);
        dst.put_u8(self.on_ground as u8);
        Ok(())
    }
}

impl Decode for ClientboundPlayerPosition {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 8 + 8 + 8 + 8 + 4 + 4 + 1)?;
        Ok(Self {
            x: src.get_f64(),
            y: src.get_f64(),
            stance: src.get_f64(),
            z: src.get_f64(),
            yaw: src.get_f32(),
            pitch: src.get_f32(),
            on_ground: src.get_u8() != 0,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundHeldItemChange {
    pub slot: i8,
}

impl PacketId for ClientboundHeldItemChange {
    fn packet_id(_ver: u32) -> u8 {
        0x09
    }
}

impl Encode for ClientboundHeldItemChange {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i8(self.slot);
        Ok(())
    }
}

impl Decode for ClientboundHeldItemChange {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 1)?;
        Ok(Self { slot: src.get_i8() })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundPlayerAbilities {
    pub flags: u8,
    pub flying_speed: f32,
    pub walking_speed: f32,
}

impl PacketId for ClientboundPlayerAbilities {
    fn packet_id(_ver: u32) -> u8 {
        0x43
    }
}

impl Encode for ClientboundPlayerAbilities {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_u8(self.flags);
        dst.put_f32(self.flying_speed);
        dst.put_f32(self.walking_speed);
        Ok(())
    }
}

impl Decode for ClientboundPlayerAbilities {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 1 + 4 + 4)?;
        Ok(Self {
            flags: src.get_u8(),
            flying_speed: src.get_f32(),
            walking_speed: src.get_f32(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundDisconnect {
    pub reason: String,
}

impl PacketId for ClientboundDisconnect {
    fn packet_id(_ver: u32) -> u8 {
        0xFF
    }
}

impl Encode for ClientboundDisconnect {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_legacy_string(&self.reason, dst);
        Ok(())
    }
}

impl Decode for ClientboundDisconnect {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        Ok(Self {
            reason: decode_legacy_string(src)?,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundSpawnPosition {
    pub x: i32,
    pub y: i32,
    pub z: i32,
}

impl PacketId for ClientboundSpawnPosition {
    fn packet_id(_ver: u32) -> u8 {
        0x05
    }
}

impl Encode for ClientboundSpawnPosition {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.x);
        dst.put_i32(self.y);
        dst.put_i32(self.z);
        Ok(())
    }
}

impl Decode for ClientboundSpawnPosition {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 4 + 4)?;
        Ok(Self {
            x: src.get_i32(),
            y: src.get_i32(),
            z: src.get_i32(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundUpdateHealth {
    pub health: f32,
    pub food: i16,
    pub food_saturation: f32,
}

impl PacketId for ClientboundUpdateHealth {
    fn packet_id(_ver: u32) -> u8 {
        0x08
    }
}

impl Encode for ClientboundUpdateHealth {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_f32(self.health);
        dst.put_i16(self.food);
        dst.put_f32(self.food_saturation);
        Ok(())
    }
}

impl Decode for ClientboundUpdateHealth {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 2 + 4)?;
        Ok(Self {
            health: src.get_f32(),
            food: src.get_i16(),
            food_saturation: src.get_f32(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundRespawn {
    pub dimension: i8,
    pub difficulty: u8,
    pub gamemode: u8,
    pub world_height: i16,
}

impl PacketId for ClientboundRespawn {
    fn packet_id(_ver: u32) -> u8 {
        0x09
    }
}

impl Encode for ClientboundRespawn {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i8(self.dimension);
        dst.put_u8(self.difficulty);
        dst.put_u8(self.gamemode);
        dst.put_i16(self.world_height);
        Ok(())
    }
}

impl Decode for ClientboundRespawn {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 1 + 1 + 1 + 2)?;
        Ok(Self {
            dimension: src.get_i8(),
            difficulty: src.get_u8(),
            gamemode: src.get_u8(),
            world_height: src.get_i16(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntityEquipment {
    pub entity_id: i32,
    pub slot: i16,
    pub item_id: i16,
}

impl PacketId for ClientboundEntityEquipment {
    fn packet_id(_ver: u32) -> u8 {
        0x1C
    }
}

impl Encode for ClientboundEntityEquipment {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        dst.put_i16(self.slot);
        dst.put_i16(self.item_id);
        Ok(())
    }
}

impl Decode for ClientboundEntityEquipment {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 2 + 2)?;
        Ok(Self {
            entity_id: src.get_i32(),
            slot: src.get_i16(),
            item_id: src.get_i16(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundSpawnPlayer {
    pub entity_id: i32,
    pub username: String,
    pub x: i32,
    pub y: i32,
    pub z: i32,
    pub yaw: i8,
    pub pitch: i8,
    pub current_item: i16,
}

impl PacketId for ClientboundSpawnPlayer {
    fn packet_id(_ver: u32) -> u8 {
        0x14
    }
}

impl Encode for ClientboundSpawnPlayer {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        encode_legacy_string(&self.username, dst);
        dst.put_i32(self.x);
        dst.put_i32(self.y);
        dst.put_i32(self.z);
        dst.put_i8(self.yaw);
        dst.put_i8(self.pitch);
        dst.put_i16(self.current_item);
        Ok(())
    }
}

impl Decode for ClientboundSpawnPlayer {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let entity_id = src.get_i32();
        let username = decode_legacy_string(src)?;
        need(src, 4 + 4 + 4 + 1 + 1 + 2)?;
        Ok(Self {
            entity_id,
            username,
            x: src.get_i32(),
            y: src.get_i32(),
            z: src.get_i32(),
            yaw: src.get_i8(),
            pitch: src.get_i8(),
            current_item: src.get_i16(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundChunkData {
    pub chunk_x: i32,
    pub chunk_z: i32,
    pub continuous: bool,
    pub primary_bitmap: i16,
    pub add_bitmap: i16,
    pub compressed_data: Vec<u8>,
}

impl PacketId for ClientboundChunkData {
    fn packet_id(_ver: u32) -> u8 {
        0x33
    }
}

impl Encode for ClientboundChunkData {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.chunk_x);
        dst.put_i32(self.chunk_z);
        dst.put_u8(self.continuous as u8);
        dst.put_i16(self.primary_bitmap);
        dst.put_i16(self.add_bitmap);
        dst.extend_from_slice(&(self.compressed_data.len() as i32).to_be_bytes());
        dst.extend_from_slice(&self.compressed_data);
        Ok(())
    }
}

impl Decode for ClientboundChunkData {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 4 + 1 + 2 + 2 + 4)?;
        let chunk_x = src.get_i32();
        let chunk_z = src.get_i32();
        let continuous = src.get_u8() != 0;
        let primary_bitmap = src.get_i16();
        let add_bitmap = src.get_i16();
        let data_len = src.get_i32() as usize;
        if src.remaining() < data_len {
            return Err(ProtocolError::UnexpectedEof);
        }
        let compressed_data = src.copy_to_bytes(data_len).to_vec();
        Ok(Self {
            chunk_x,
            chunk_z,
            continuous,
            primary_bitmap,
            add_bitmap,
            compressed_data,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundMultiBlockChange {
    pub chunk_x: i32,
    pub chunk_z: i32,
    pub record_count: i32,
    pub data: Vec<u8>,
}

impl PacketId for ClientboundMultiBlockChange {
    fn packet_id(_ver: u32) -> u8 {
        0x34
    }
}

impl Encode for ClientboundMultiBlockChange {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.chunk_x);
        dst.put_i32(self.chunk_z);
        dst.put_i32(self.record_count);
        dst.extend_from_slice(&self.data);
        Ok(())
    }
}

impl Decode for ClientboundMultiBlockChange {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 4 + 4)?;
        let chunk_x = src.get_i32();
        let chunk_z = src.get_i32();
        let record_count = src.get_i32();
        let data = src.to_vec();
        Ok(Self {
            chunk_x,
            chunk_z,
            record_count,
            data,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundBlockChange {
    pub x: i32,
    pub y: i8,
    pub z: i32,
    pub block_id: i16,
    pub block_metadata: i8,
}

impl PacketId for ClientboundBlockChange {
    fn packet_id(_ver: u32) -> u8 {
        0x35
    }
}

impl Encode for ClientboundBlockChange {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.x);
        dst.put_i8(self.y);
        dst.put_i32(self.z);
        dst.put_i16(self.block_id);
        dst.put_i8(self.block_metadata);
        Ok(())
    }
}

impl Decode for ClientboundBlockChange {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 1 + 4 + 2 + 1)?;
        Ok(Self {
            x: src.get_i32(),
            y: src.get_i8(),
            z: src.get_i32(),
            block_id: src.get_i16(),
            block_metadata: src.get_i8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundNamedSoundEffect {
    pub sound_name: String,
    pub x: i32,
    pub y: i32,
    pub z: i32,
    pub volume: f32,
    pub pitch: u8,
}

impl PacketId for ClientboundNamedSoundEffect {
    fn packet_id(_ver: u32) -> u8 {
        0x3E
    }
}

impl Encode for ClientboundNamedSoundEffect {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_legacy_string(&self.sound_name, dst);
        dst.put_i32(self.x);
        dst.put_i32(self.y);
        dst.put_i32(self.z);
        dst.put_f32(self.volume);
        dst.put_u8(self.pitch);
        Ok(())
    }
}

impl Decode for ClientboundNamedSoundEffect {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let sound_name = decode_legacy_string(src)?;
        need(src, 4 + 4 + 4 + 4 + 1)?;
        Ok(Self {
            sound_name,
            x: src.get_i32(),
            y: src.get_i32(),
            z: src.get_i32(),
            volume: src.get_f32(),
            pitch: src.get_u8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundSetSlot {
    pub window_id: i8,
    pub slot: i16,
    pub item_id: i16,
    pub item_count: i8,
    pub item_damage: i16,
    pub nbt_data: Vec<u8>,
}

impl PacketId for ClientboundSetSlot {
    fn packet_id(_ver: u32) -> u8 {
        0x67
    }
}

impl Encode for ClientboundSetSlot {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i8(self.window_id);
        dst.put_i16(self.slot);
        dst.put_i16(self.item_id);
        dst.put_i8(self.item_count);
        dst.put_i16(self.item_damage);
        dst.put_i16(self.nbt_data.len() as i16);
        dst.extend_from_slice(&self.nbt_data);
        Ok(())
    }
}

impl Decode for ClientboundSetSlot {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 1 + 2 + 2 + 1 + 2 + 2)?;
        let window_id = src.get_i8();
        let slot = src.get_i16();
        let item_id = src.get_i16();
        let item_count = src.get_i8();
        let item_damage = src.get_i16();
        let nbt_len = src.get_i16() as usize;
        if src.remaining() < nbt_len {
            return Err(ProtocolError::UnexpectedEof);
        }
        let nbt_data = src.copy_to_bytes(nbt_len).to_vec();
        Ok(Self {
            window_id,
            slot,
            item_id,
            item_count,
            item_damage,
            nbt_data,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundWindowItems {
    pub window_id: i8,
    pub items: Vec<(i16, i8, i16, Vec<u8>)>,
}

impl PacketId for ClientboundWindowItems {
    fn packet_id(_ver: u32) -> u8 {
        0x68
    }
}

impl Encode for ClientboundWindowItems {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i8(self.window_id);
        dst.put_i16(self.items.len() as i16);
        for (item_id, item_count, item_damage, nbt_data) in &self.items {
            dst.put_i16(*item_id);
            dst.put_i8(*item_count);
            dst.put_i16(*item_damage);
            dst.put_i16(nbt_data.len() as i16);
            dst.extend_from_slice(nbt_data);
        }
        Ok(())
    }
}

impl Decode for ClientboundWindowItems {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 1 + 2)?;
        let window_id = src.get_i8();
        let count = src.get_i16() as usize;
        let mut items = Vec::with_capacity(count);
        for _ in 0..count {
            need(src, 2 + 1 + 2 + 2)?;
            let item_id = src.get_i16();
            let item_count = src.get_i8();
            let item_damage = src.get_i16();
            let nbt_len = src.get_i16() as usize;
            if src.remaining() < nbt_len {
                return Err(ProtocolError::UnexpectedEof);
            }
            let nbt_data = src.copy_to_bytes(nbt_len).to_vec();
            items.push((item_id, item_count, item_damage, nbt_data));
        }
        Ok(Self { window_id, items })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundUpdateTime {
    pub age: i64,
    pub time: i64,
}

impl PacketId for ClientboundUpdateTime {
    fn packet_id(_ver: u32) -> u8 {
        0x04
    }
}

impl Encode for ClientboundUpdateTime {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i64(self.age);
        dst.put_i64(self.time);
        Ok(())
    }
}

impl Decode for ClientboundUpdateTime {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 8 + 8)?;
        Ok(Self {
            age: src.get_i64(),
            time: src.get_i64(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntity {
    pub entity_id: i32,
}

impl PacketId for ClientboundEntity {
    fn packet_id(_ver: u32) -> u8 {
        0x1E
    }
}

impl Encode for ClientboundEntity {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        Ok(())
    }
}

impl Decode for ClientboundEntity {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4)?;
        Ok(Self {
            entity_id: src.get_i32(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntityRelativeMove {
    pub entity_id: i32,
    pub dx: i8,
    pub dy: i8,
    pub dz: i8,
}

impl PacketId for ClientboundEntityRelativeMove {
    fn packet_id(_ver: u32) -> u8 {
        0x15
    }
}

impl Encode for ClientboundEntityRelativeMove {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        dst.put_i8(self.dx);
        dst.put_i8(self.dy);
        dst.put_i8(self.dz);
        Ok(())
    }
}

impl Decode for ClientboundEntityRelativeMove {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 1 + 1 + 1)?;
        Ok(Self {
            entity_id: src.get_i32(),
            dx: src.get_i8(),
            dy: src.get_i8(),
            dz: src.get_i8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntityLook {
    pub entity_id: i32,
    pub yaw: i8,
    pub pitch: i8,
}

impl PacketId for ClientboundEntityLook {
    fn packet_id(_ver: u32) -> u8 {
        0x16
    }
}

impl Encode for ClientboundEntityLook {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        dst.put_i8(self.yaw);
        dst.put_i8(self.pitch);
        Ok(())
    }
}

impl Decode for ClientboundEntityLook {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 1 + 1)?;
        Ok(Self {
            entity_id: src.get_i32(),
            yaw: src.get_i8(),
            pitch: src.get_i8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntityMoveLook {
    pub entity_id: i32,
    pub dx: i8,
    pub dy: i8,
    pub dz: i8,
    pub yaw: i8,
    pub pitch: i8,
}

impl PacketId for ClientboundEntityMoveLook {
    fn packet_id(_ver: u32) -> u8 {
        0x17
    }
}

impl Encode for ClientboundEntityMoveLook {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        dst.put_i8(self.dx);
        dst.put_i8(self.dy);
        dst.put_i8(self.dz);
        dst.put_i8(self.yaw);
        dst.put_i8(self.pitch);
        Ok(())
    }
}

impl Decode for ClientboundEntityMoveLook {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 1 + 1 + 1 + 1 + 1)?;
        Ok(Self {
            entity_id: src.get_i32(),
            dx: src.get_i8(),
            dy: src.get_i8(),
            dz: src.get_i8(),
            yaw: src.get_i8(),
            pitch: src.get_i8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntityTeleport {
    pub entity_id: i32,
    pub x: i32,
    pub y: i32,
    pub z: i32,
    pub yaw: i8,
    pub pitch: i8,
}

impl PacketId for ClientboundEntityTeleport {
    fn packet_id(_ver: u32) -> u8 {
        0x18
    }
}

impl Encode for ClientboundEntityTeleport {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        dst.put_i32(self.x);
        dst.put_i32(self.y);
        dst.put_i32(self.z);
        dst.put_i8(self.yaw);
        dst.put_i8(self.pitch);
        Ok(())
    }
}

impl Decode for ClientboundEntityTeleport {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 4 + 4 + 4 + 1 + 1)?;
        Ok(Self {
            entity_id: src.get_i32(),
            x: src.get_i32(),
            y: src.get_i32(),
            z: src.get_i32(),
            yaw: src.get_i8(),
            pitch: src.get_i8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEntityHeadRotation {
    pub entity_id: i32,
    pub head_yaw: i8,
}

impl PacketId for ClientboundEntityHeadRotation {
    fn packet_id(_ver: u32) -> u8 {
        0x19
    }
}

impl Encode for ClientboundEntityHeadRotation {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i32(self.entity_id);
        dst.put_i8(self.head_yaw);
        Ok(())
    }
}

impl Decode for ClientboundEntityHeadRotation {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 1)?;
        Ok(Self {
            entity_id: src.get_i32(),
            head_yaw: src.get_i8(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundSetExperience {
    pub experience_bar: f32,
    pub level: i16,
    pub total_experience: i16,
}

impl PacketId for ClientboundSetExperience {
    fn packet_id(_ver: u32) -> u8 {
        0x2B
    }
}

impl Encode for ClientboundSetExperience {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_f32(self.experience_bar);
        dst.put_i16(self.level);
        dst.put_i16(self.total_experience);
        Ok(())
    }
}

impl Decode for ClientboundSetExperience {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 4 + 2 + 2)?;
        Ok(Self {
            experience_bar: src.get_f32(),
            level: src.get_i16(),
            total_experience: src.get_i16(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundMapData {
    pub item_damage: i16,
    pub scale: i8,
    pub data: Vec<u8>,
}

impl PacketId for ClientboundMapData {
    fn packet_id(_ver: u32) -> u8 {
        0x36
    }
}

impl Encode for ClientboundMapData {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i16(self.item_damage);
        dst.put_i8(self.scale);
        dst.extend_from_slice(&self.data);
        Ok(())
    }
}

impl Decode for ClientboundMapData {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 2 + 1)?;
        let item_damage = src.get_i16();
        let scale = src.get_i8();
        let data = src.to_vec();
        Ok(Self {
            item_damage,
            scale,
            data,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundOpenWindow {
    pub window_id: i8,
    pub inventory_type: i8,
    pub window_title: String,
    pub slots_count: i8,
    pub use_provided_title: bool,
}

impl PacketId for ClientboundOpenWindow {
    fn packet_id(_ver: u32) -> u8 {
        0x64
    }
}

impl Encode for ClientboundOpenWindow {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i8(self.window_id);
        dst.put_i8(self.inventory_type);
        encode_legacy_string(&self.window_title, dst);
        dst.put_i8(self.slots_count);
        dst.put_u8(self.use_provided_title as u8);
        Ok(())
    }
}

impl Decode for ClientboundOpenWindow {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let window_id = src.get_i8();
        let inventory_type = src.get_i8();
        let window_title = decode_legacy_string(src)?;
        need(src, 1 + 1)?;
        let slots_count = src.get_i8();
        let use_provided_title = src.get_u8() != 0;
        Ok(Self {
            window_id,
            inventory_type,
            window_title,
            slots_count,
            use_provided_title,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundCloseWindow {
    pub window_id: i8,
}

impl PacketId for ClientboundCloseWindow {
    fn packet_id(_ver: u32) -> u8 {
        0x65
    }
}

impl Encode for ClientboundCloseWindow {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        dst.put_i8(self.window_id);
        Ok(())
    }
}

impl Decode for ClientboundCloseWindow {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        need(src, 1)?;
        Ok(Self {
            window_id: src.get_i8(),
        })
    }
}