aboutsummaryrefslogtreecommitdiff
path: root/src/v1_19_3.rs
blob: 131f02e89c256b37f46892f9d838008db72b8e1f (plain)
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
use alloc::{
    borrow::ToOwned,
    string::String,
};
use alloc::fmt;
use fmt::Debug;

use crate::{*, types::*, uuid::*};
#[cfg(all(test, feature = "std"))]
use crate::protocol::TestRandom;

define_protocol!(761, Packet761, RawPacket761, RawPacket761Body, Packet761Kind => {
    // Handshaking
    Handshake, 0x00, Handshaking, ServerBound => HandshakeSpec {
        version: VarInt,
        server_address: String,
        server_port: u16,
        next_state: HandshakeNextState
    },

    // Status - ClientBound
    StatusRespone, 0x00, Status, ClientBound => StatusResponseSpec {
        response: super::status::StatusSpec
    },
    PingResponse, 0x01, Status, ClientBound => PingResponseSpec {
        payload: i64
    },

    // Status - ServerBound
    StatusRequest, 0x00, Status, ServerBound => StatusRequestSpec {
    },
    PingRequest, 0x01, Status, ServerBound => PingRequestSpec {
        payload: i64
    },

    // Login - ClientBound
    DisconnectLogin, 0x00, Login, ClientBound => DisconnectLoginSpec {
        payloadreason: Chat
    },
    EncryptionRequest, 0x01, Login, ClientBound => EncryptionRequestSpec {
        server_id: String,
        public_key: CountedArray<u8, VarInt>,
        verify_token: CountedArray<u8, VarInt>
    },
    LoginSuccess, 0x02, Login, ClientBound => LoginSuccessSpec {
        uuid: UUID4,
        username: String,
        properties: CountedArray<LoginProperty, VarInt>
    },
    SetCompression, 0x03, Login, ClientBound => SetCompressionSpec {
        threshold: VarInt
    },
    LoginPluginRequest, 0x04, Login, ClientBound => LoginPluginRequestSpec {
        message_id: VarInt,
        channel: String,
        data: RemainingBytes
    },

    // Login - ServerBound
    LoginStart, 0x00, Login, ServerBound => LoginStartSpec {
        name: String,
        player_uuid: Option<UUID4>
    },
    EncryptionResponse, 0x01, Login, ServerBound => EncryptionResponseSpec {
        shared_secret: CountedArray<u8, VarInt>,
        verfiy_token: CountedArray<u8, VarInt>
    },
    LoginPluginResponse, 0x02, Login, ServerBound => LoginPluginResponseSpec {
        message_id: VarInt,
        successful: bool,
        data: RemainingBytes
    },

    // Play - ClientBound
    SpawnEntity, 0x00, Play, ClientBound => SpawnEntitySpec {
        entity_id: VarInt,
        entity_uuid: UUID4,
        entity_type: VarInt, // TODO enumerate mob types
        position: Vec3<f64>,
        pitch: Angle,
        yaw: Angle,
        data: i32,
        velocity: Vec3<i16>
    },
    SpawnExperienceOrb, 0x01, Play, ClientBound => SpawnExperienceOrbSpec {
        entity_id: VarInt,
        position: Vec3<f64>,
        count: i16
    },
    SpawnPlayer, 0x02, Play, ClientBound => SpawnPlayerSpec {
        entity_id: VarInt,
        player_uuid: UUID4,
        position: Vec3<f64>,
        yaw: Angle,
        pitch: Angle
    },
    EntityAnimation, 0x03, Play, ClientBound => EntityAnimationSpec {
        entity_id: VarInt,
        animation: u8 // TODO: enumerate animation id
    },
    AwardStatistics, 0x04, Play, ClientBound => AwardStatisticsSpec {
        statistics: CountedArray<StatisticValue, VarInt>
    },
    AcknowledgeBlockChange, 0x05, Play, ClientBound => AcknowledgeBlockChangeSpec {
        sequence_id: VarInt
    },
    SetBlockDestroyStage, 0x06, Play, ClientBound => SetBlockDestroyStageSpec {
        entity_id: VarInt,
        location: IntPosition,
        destroy_stage: u8
    },
    BlockEntityData, 0x07, Play, ClientBound => BlockEntityDataSpec {
        location: IntPosition,
        block_entity_type: VarInt,
        nbt_data: NamedNbtTag
    },
    BlockAction, 0x08, Play, ClientBound => BlockActionSpec {
        location: IntPosition,
        action_id: u8,
        action_parameter: u8,
        block_type: VarInt
    },
    BlockUpdate, 0x09, Play, ClientBound => BlockUpdateSpec {
        location: IntPosition,
        block_id: VarInt
    },
    BossBar, 0x0A, Play, ClientBound => BossBarSpec {
        uuid: UUID4,
        action: BossBarAction
    },
    ChangeDifficulty, 0x0B, Play, ClientBound => ChangeDifficultySpec {
        difficulty: Difficulty,
        difficulty_locked: bool
    },
    ClearTitles, 0x0C, Play, ClientBound => ClearTitlesSpec {
        reset: bool
    },
    /*
    CommandSuggestionResponse, 0x0D, Play, ClientBound => CommandSuggestionResponseSpec {
        transaction_id: VarInt,
        start: VarInt,
        length: VarInt,
        suggestions: CountedArray<CommandSuggestion, VarInt>
    },
    */
    Commands, 0x0E, Play, ClientBound => CommandsSpec {
        nodes: CountedArray<CommandNodeSpec, VarInt>,
        root_index: VarInt
    },
    CloseContainer, 0x0F, Play, ClientBound => CloseContainerSpec {
        window_id: u8
    },
    SetContainerContent, 0x10, Play, ClientBound => SetContainerContentSpec {
        window_id: u8,
        state_id: VarInt,
        slot_data: CountedArray<Slot, VarInt>,
        carried_item: Slot
    },
    SetContainerProperty, 0x11, Play, ClientBound => SetContainerPropertySpec {
        window_id: u8,
        property: i16,
        value: i16
    },
    SetContainerSlot, 0x12, Play, ClientBound => SetContainerSlotSpec {
        window_id: u8,
        state_id: VarInt,
        slot_index: i16,
        slot_data: Slot
    },
    SetCooldown, 0x13, Play, ClientBound => SetCooldownSpec {
        item_id: VarInt,
        cooldown_ticks: VarInt
    },
    ChatSuggestions, 0x14, Play, ClientBound => ChatSuggestionsSpec {
        action: VarInt,
        entries: CountedArray<String, VarInt>
    },
    PluginMessage, 0x15, Play, ClientBound => PluginMessageSpec {
        channel: String,
        data: RemainingBytes
    },
    DeleteMessage, 0x16, Play, ClientBound => DeleteMessageSpec {
        signature: CountedArray<u8, VarInt>
    },
    DisconnectPlay, 0x17, Play, ClientBound => DisconnectPlaySpec {
        reason: Chat
    },
    DisguisedChatMessage, 0x18, Play, ClientBound => DisguisedChatMessageSpec {
        message: Chat,
        chat_type: VarInt,
        chat_type_name: Chat,
        target_name: Option<Chat>
    },
    EntityEvent, 0x19, Play, ClientBound => EntityEventSpec {
        entity_id: i32,
        entity_status: u8
    },
    Explosion, 0x1A, Play, ClientBound => ExplosionSpec {
        position: Vec3<f64>,
        strength: f32,
        affected_blocks: CountedArray<Vec3<i8>, VarInt>,
        player_motion: Vec3<f32>
    },
    UnloadChunk, 0x1B, Play, ClientBound => UnloadChunkSpec {
        chunk_x: i32,
        chunk_z: i32
    },
    GameEvent, 0x1C, Play, ClientBound => GameEventSpec {
        event: GameEventType,
        data: f32
    },
    OpenHorseScreen, 0x1D, Play, ClientBound => OpenHorseScreenSpec {
        window_id: u8,
        slot_count: VarInt,
        entity_id: i32
    },
    InitializeWorldBorder, 0x1E, Play, ClientBound => InitializeWorldBorderSpec {
        x: f64,
        z: f64,
        old_diameter: f64,
        new_diameter: f64,
        speed: VarLong,
        portal_teleport_boundary: VarInt,
        warning_blocks: VarInt,
        warning_time: VarInt
    },
    KeepAliveClientBound, 0x1f, Play, ClientBound => KeepAliveClientBoundSpec {
        keep_alive_id: i64
    },
    ChunkDataAndUpdateLight, 0x20, Play, ClientBound => ChunkDataAndUpdateLightSpec {
        chunk_x: i32,
        chunk_z: i32,
        chunk_data: ChunkDataSpec,
        light_data: LightDataSpec,
    },
    // TODO 0x21 - 0x23
    LoginPlay, 0x24, Play, ClientBound => LoginPlaySpec {
        entity_id: i32,
        is_hardcore: bool,
        gamemode: GameMode,
        previous_gamemode: PreviousGameMode,
        dimension_names: CountedArray<String, VarInt>,
        registry_codex: NamedNbtTag,
        dimension_type: String,
        dimension_name: String,
        hashed_seed: u64,
        max_players: VarInt,
        view_distance: VarInt,
        simulation_distance: VarInt,
        reduced_debug_info: bool,
        enable_respawn_screen: bool,
        is_debug: bool,
        is_flat: bool,
        death_location: Option<DeathLocation>,
    },
    // TODO 0x25 - 0x48
    SynchronizePlayerPosition, 0x38, Play, ClientBound => SynchronizePlayerPositionSpec {
        position: Vec3<f64>,
        yaw: f32,
        pitch: f32,
        flags: SynchronizePlayerPositionFlags,
        teleport_id: VarInt,
        dismount_vehicle: bool
    },
    UpdateRecipeBook, 0x39, Play, ClientBound => UpdateRecipeBookSpec {
        action: RecipeBookAction,
    },
    // TODO missing recipe


    SetCenterChunk, 0x4A, Play, ClientBound => SetCenterChunkSpec {
        chunk_x: VarInt,
        chunk_z: VarInt,
    },
    SetDefaultSpawnPosition, 0x4C, Play, ClientBound => SetDefaultSpawnPositionSpec {
        location: IntPosition,
        angle: f32,
    },

    // TODO missing recipe

    SetHeldItemClient, 0x49, Play, ClientBound => SetHeldItemClientSpec {
        selected_slot: u8,
    },
    // TODO missing recipe
    UpdateRecipes, 0x69, Play, ClientBound => UpdateRecipesSpec {
        recipes: CountedArray<Recipe, VarInt>
    },

    UpdateTags, 0x6A, Play, ClientBound => UpdateTagsSpec {
        tags: CountedArray<TypedTagList, VarInt>
    },

    // Play - ServerBound
    
    // TODO 0x00 - 0x06
    
    ClientInformation, 0x07, Play, ServerBound => ClientInformationSpec {
        locale: String,
        view_distance: u8,
        chat_mode: ChatMode,
        chat_colors: bool,
        displayed_skin_parts: DisplayedSkinParts,
        main_hand: MainHand,
        enable_text_filtering: bool,
        allow_server_listings: bool,
    },
    // TODO Missing packets
    SetHeldItemServer, 0x28, Play, ServerBound => SetHeldItemServerSpec {
        selected_slot: u8,
    },
});


// Helper types
proto_struct!(ChunkSection {
    block_count: u16,
    block_state: PalettedContainer,
    biomes: PalettedContainer,
});



#[derive(Debug, Clone, PartialEq)]
pub enum PalettedContainer {
    SingularValue(VarInt),
    GlobalPalette(BlobArray),
}

impl Serialize for PalettedContainer {
    fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
        match self {
            PalettedContainer::SingularValue(value) => {
                to.serialize_byte(0)?;
                to.serialize_other(value)?;
                // Serialize 0 bit bitstorage
                to.serialize_other(&BitSet::empty())?;
            }
            PalettedContainer::GlobalPalette(data) => {
                to.serialize_byte(9)?;
                to.serialize_other(data)?;
            }
        }
        Ok(())
    }
}

impl Deserialize for PalettedContainer {
    fn mc_deserialize(_data: &[u8]) -> DeserializeResult<Self> {
        Err(DeserializeErr::CannotUnderstandValue("Not implemented".into()))
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct BlobArray {
    // TODO make this work same as bit storage with a const generic entry size
    pub data: Vec<u64>,
    entry_size: usize,
}

impl BlobArray {
    pub fn new(entry_size: u8) -> Self {
        BlobArray { entry_size: entry_size as usize, data: vec![] }
    }

    pub fn ensure_capacity(&mut self, index: usize) {
        let entries_per_long = 64usize / self.entry_size;
        let needed_length = index / entries_per_long + 1;
        if self.data.len() < needed_length {
            self.data.extend(vec![0u64; needed_length - self.data.len()]);
        }
    }

    pub fn set_entry(&mut self, index: usize, value: u64) {
        let entries_per_long = 64usize / self.entry_size;
        let array_index = index / entries_per_long;
        let position = index % entries_per_long;
        let array_shift = position * self.entry_size;
        let mask = (1u64 << self.entry_size) - 1;
        self.ensure_capacity(index);
        self.data[array_index] |= (value & mask) << array_shift;
    }

    pub fn get_entry(&mut self, index: usize) -> u64 {
        let entries_per_long = 64 / self.entry_size;
        let array_index = index / entries_per_long;
        let position = index % entries_per_long;
        let array_shift = position * self.entry_size;
        let mask = (1u64 << self.entry_size) - 1;
        self.ensure_capacity(index);
        (self.data[array_index] >> array_shift) & mask
    }
}

impl Serialize for BlobArray {
    fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
        to.serialize_other(&VarInt::from(self.data.len() as i32))?;
        for elem in &self.data {
            to.serialize_other(elem)?;
        }
        Ok(())
    }
}

impl Deserialize for BlobArray {
    fn mc_deserialize(_data: &[u8]) -> DeserializeResult<Self> {
        Err(DeserializeErr::CannotUnderstandValue("Not implemented".into()))
    }
}


proto_struct!(BitSet {
    data: CountedArray<u64, VarInt>,
});

impl BitSet {
    pub fn empty() -> BitSet {
        BitSet { data: vec![].into() }
    }

    pub fn is_bit_set(&self, index: usize) -> bool {
        if index * 64 >= self.data.len() {
            return false;
        }
        self.is_bit_set_unsafe(index)
    }
    pub fn ensure_capacity(&mut self, index: usize) {
        let needed_length = index / 64 + 1;
        let length_increase = needed_length - self.data.len();
        if length_increase > 0 {
            self.data.extend(vec![0u64; length_increase]);
        }
    }

    pub fn is_bit_set_unsafe(&self, index: usize) -> bool {
        self.data[index / 64] & (1u64 << (index % 64)) != 0
    }

    pub fn set_bit(&mut self, index: usize, value: bool) {
        self.ensure_capacity(index);
        self.set_bit_unsafe(index, value)
    }

    pub fn set_bit_unsafe(&mut self, index: usize, value: bool) {
        if value {
            self.data[index / 64] |= 1u64 << (index % 64);
        } else {
            self.data[index / 64] &= !(1u64 << (index % 64));
        }
    }
}


proto_struct!(LightDataSpec {
    trust_edges: bool,
    sky_y_mask: BitSet,
    block_y_mask: BitSet,
    empty_sky_y_mask: BitSet,
    empty_block_y_mask: BitSet,
    sky_updates: CountedArray<CountedArray<u8, VarInt>, VarInt>,
    block_updates: CountedArray<CountedArray<u8, VarInt>, VarInt>,
});
proto_struct!(ChunkDataSpec {
    heightmaps: NamedNbtTag,
    buffer: CountedArray<u8, VarInt>,
    block_entities: CountedArray<BlockEntity, VarInt>,
});

proto_byte_flag!(SynchronizePlayerPositionFlags,
    0x01 :: is_x_relative set_x_relative,
    0x02 :: is_y_relative set_y_relative,
    0x04 :: is_z_relative set_z_relative,
    0x08 :: is_y_rot_relative set_y_rot_relative,
    0x10 :: is_x_rot_relative set_x_rot_relative,
);

proto_varint_enum!(RecipeBookAction,
    0 :: Init(RecipeBookInitSpec),
    1 :: Add(RecipeBookDiffSpec),
    2 :: Remove(RecipeBookDiffSpec),
);

proto_struct!(RecipeBookInitSpec {
    book_settings: BookSettings,
    known_recipes: CountedArray<String, VarInt>,
    highlighted_recipes: CountedArray<String, VarInt>,
});

proto_struct!(RecipeBookDiffSpec {
    book_settings: BookSettings,
    to_add_or_remove: CountedArray<String, VarInt>,
});
proto_struct!(BookSettings {
    crafting: BookSetting,
    smelting: BookSetting,
    blast_furnace: BookSetting,
    smoker: BookSetting,
});
impl Default for BookSettings {
    fn default() -> Self {
        return BookSettings {
            crafting: BookSetting::default(),
            smelting: BookSetting::default(),
            blast_furnace: BookSetting::default(),
            smoker: BookSetting::default(),
        };
    }
}

proto_struct!(BookSetting {
    open: bool,
    filter_active: bool,
});

impl Default for BookSetting {
    fn default() -> Self {
        return BookSetting { open: false, filter_active: false };
    }
}

proto_str_enum!(TagType,
    "minecraft:block" :: Block,
    "minecraft:item" :: Item,
    "minecraft:fluid" :: Fluid,
    "minecraft:entity_type" :: EntityType,
    "minecraft:game_event" :: GameEvent,
);

#[derive(Clone, Debug, PartialEq)]
pub struct CommandNodeSpec {
    pub children_indices: CountedArray<VarInt, VarInt>,
    pub redirect_node: Option<VarInt>,
    pub is_executable: bool,
    pub node: CommandNode,
}

#[derive(Clone, Debug, PartialEq)]
pub enum CommandNode {
    Root,
    Argument(CommandArgumentNodeSpec),
    Literal(CommandLiteralNodeSpec),
}

proto_struct!(CommandLiteralNodeSpec {
    name: String
});

#[derive(Clone, Debug, PartialEq)]
pub struct CommandArgumentNodeSpec {
    name: String,
    parser_id: VarInt,
    properties: CommandNodeProperties,
    suggestions_types: Option<SuggestionsTypeSpec>,
}

impl Serialize for CommandArgumentNodeSpec {
    fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
        to.serialize_other(&self.name)?;
        to.serialize_other(&self.parser_id)?;
        to.serialize_other(&self.properties)?;
        if let Some(body) = &self.suggestions_types {
            to.serialize_other(body)?;
        }

        Ok(())
    }
}

impl CommandArgumentNodeSpec {
    fn mc_deserialize(has_suggestions: bool, data: &[u8]) -> DeserializeResult<Self> {
        let Deserialized { value: name, data } = String::mc_deserialize(data)?;
        let Deserialized { value: parser_id, data } = VarInt::mc_deserialize(data)?;
        let Deserialized { value: properties, data } = CommandNodeProperties::mc_deserialize(data)?;
        let (suggestions_types, data) = if has_suggestions {
            let Deserialized { value: suggestions_types, data } = SuggestionsTypeSpec::mc_deserialize(data)?;
            (Some(suggestions_types), data)
        } else {
            (None, data)
        };
        Deserialized::ok(
            Self {
                name,
                parser_id,
                properties,
                suggestions_types,
            },
            data,
        )
    }
}
proto_str_enum!(SuggestionsTypeSpec,
    "minecraft:ask_server" :: AskServer,
    "minecraft:all_recipes" :: AllRecipes,
    "minecraft:available_sounds" :: AvailableSounds,
    "minecraft:available_bioms" :: AvailableBiomes,
    "minecraft:summonable_entities" :: SummonableEntities,
);

proto_varint_enum!(CommandNodeProperties,
    0 :: Bool,
);


impl Serialize for CommandNodeSpec {
    fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
        let mut flags = 0u8;
        use CommandNode::*;
        flags |= match &self.node {
            Root => 0x00,
            Argument(_) => 0x1,
            Literal(_) => 0x2,
        };

        if self.is_executable {
            flags |= 0x04;
        }
        if self.redirect_node.is_some() {
            flags |= 0x08;
        }

        if let Argument(argument_data) = &self.node {
            if argument_data.suggestions_types.is_some() {
                flags |= 0x10;
            }
        }

        to.serialize_byte(flags)?;
        to.serialize_other(&self.children_indices)?;
        if let Some(redirect_node) = &self.redirect_node {
            to.serialize_other(redirect_node)?;
        }
        match &self.node {
            Root => Ok(()),
            Argument(body) => to.serialize_other(body),
            Literal(body) => to.serialize_other(body)
        }
    }
}

impl Deserialize for CommandNodeSpec {
    fn mc_deserialize(data: &[u8]) -> DeserializeResult<Self> {
        let Deserialized { value: flags, data } = u8::mc_deserialize(data)?;
        let Deserialized {
            value: children_indices,
            data,
        } = <CountedArray<VarInt, VarInt>>::mc_deserialize(data)?;
        let is_executable = flags & 0x04 != 0;
        let has_redirect = flags & 0x08 != 0;
        let (redirect_node, data) = if has_redirect {
            let Deserialized {
                value: redirect_node,
                data,
            } = VarInt::mc_deserialize(data)?;
            (Some(redirect_node), data)
        } else {
            (None, data)
        };
        use CommandNode::*;

        let Deserialized { value: node, data } = match flags & 0x03 {
            0x00 => Deserialized::ok(Root, data),
            0x01 => {
                Ok(CommandLiteralNodeSpec::mc_deserialize(data)?.map(move |body| Literal(body)))
            }
            0x02 => Ok(
                CommandArgumentNodeSpec::mc_deserialize(flags & 0x10 != 0, data)?
                    .map(move |body| Argument(body)),
            ),
            _ => Err(DeserializeErr::CannotUnderstandValue(
                format!("Cannot understand {} as command node type", flags))),
        }?;

        Deserialized::ok(
            Self {
                children_indices,
                redirect_node,
                is_executable,
                node,
            },
            data,
        )
    }
}


proto_struct!(TypedTagList {
    tag_type: TagType,
    tags: CountedArray<TagSpec, VarInt>
});

proto_struct!(TagSpec {
    name: String,
    entries: CountedArray<VarInt, VarInt>
});

proto_str_enum!(Recipe,
    "TODO" :: Todo
);

proto_varint_enum!(MainHand,
    0 :: Left,
    1 :: Right,
);

proto_byte_flag!(DisplayedSkinParts,
    0x01 :: is_cape_enabled set_cape_enabled,
    0x02 :: is_jacket_enabled set_jacket_enabled,
    0x04 :: is_left_sleeve_enabled set_left_sleeve_enabled,
    0x08 :: is_right_sleeve_enabled set_right_sleeve_enabled,
    0x10 :: is_left_pants_leg_enabled set_left_pants_leg_enabled,
    0x20 :: is_right_pants_leg_enabled set_right_pants_leg_enabled,
    0x40 :: is_hat_enabled set_hat_enabled,
);

proto_varint_enum!(ChatMode,
    0 :: Enabled,
    1 :: CommandsOnly,
    2 :: Hidden,
);
proto_struct!(DeathLocation {
    death_dimension_name: String,
    death_location: IntPosition
});

proto_byte_enum!(GameMode,
    0 :: Survival,
    1 :: Creative,
    2 :: Adventure,
    3 :: Spectator,
);

#[derive(Clone, Debug, PartialEq)]
pub enum PreviousGameMode {
    NoPrevious,
    Previous(GameMode),
}

impl PreviousGameMode {
    pub fn id(&self) -> i8 {
        use PreviousGameMode::*;
        match self {
            NoPrevious => -1,
            Previous(mode) => mode.id() as i8,
        }
    }
}

impl Serialize for PreviousGameMode {
    fn mc_serialize<S: Serializer>(&self, to: &mut S) -> SerializeResult {
        to.serialize_byte(self.id() as u8)
    }
}

impl Deserialize for PreviousGameMode {
    fn mc_deserialize(data: &[u8]) -> DeserializeResult<'_, Self> {
        let Deserialized { value: id, data } = i8::mc_deserialize(data)?;

        use PreviousGameMode::*;
        match id {
            -1 => Deserialized::ok(NoPrevious, data),
            other => {
                Ok(GameMode::deserialize_with_id(other as u8, data)?.map(move |gm| Previous(gm)))
            }
        }
    }
}
proto_struct!(BlockEntity {
    packed_xz: u8,
    y: i16,
    block_entity_type: VarInt,
    data: NamedNbtTag
});
proto_byte_enum!(GameEventType,
    0 :: NoRespawnBlockAvailable,
    1 :: EndRaining,
    2 :: BeginRaining,
    3 :: ChangeGamemode,
    4 :: WinGame,
    5 :: DemoEvent,
    6 :: ArrowHitPlayer,
    7 :: RainLevelChange,
    8 :: ThunderLevelChange,
    9 :: PlayPufferfishStingSound,
    10 :: PlayElderGuardianMobAppearance,
    11 :: SetImmediateRespawn
);

proto_struct!(CommandSuggestion {
    match_text: String,
    tooltip: Option<Chat>
});

proto_byte_enum!(Difficulty,
    0 :: Peaceful,
    1 :: Easy,
    2 :: Normal,
    3 :: Hard
);

proto_byte_enum!(HandshakeNextState,
    0x01 :: Status,
    0x02 :: Login
);

proto_struct!(LoginProperty {
    name: String,
    value: String,
    signature: Option<String>
});

proto_struct!(StatisticValue {
    category_id: StatisticCategory,
    statistic_id: VarInt,
    value: VarInt
});

proto_varint_enum!(StatisticCategory,
    0 :: Mined,
    1 :: Crafted,
    2 :: Used,
    3 :: Broken,
    4 :: PickedUp,
    5 :: Dropped,
    6 :: Killed,
    7 :: KilledBy,
    8 :: Custom
);

proto_varint_enum!(BossBarAction,
    0 :: Add(BossBarAddSpec),
    1 :: Remove,
    2 :: UpdateHealth(f32),
    3 :: UpdateTitle(Chat),
    4 :: UpdateStyle(BossBarUpdateStyleSpec),
    5 :: UpdateFlags(BossBarFlags)
);

proto_struct!(BossBarAddSpec {
    title: Chat,
    health: f32,
    color: BossBarColor,
    division: BossBarDivision,
    flags: BossBarFlags
});

proto_struct!(BossBarUpdateStyleSpec {
    color: BossBarColor,
    dividers: BossBarDivision
});

proto_varint_enum!(BossBarColor,
    0 :: Pink,
    1 :: Blue,
    2 :: Red,
    3 :: Green,
    4 :: Yellow,
    5 :: Purple,
    6 :: White
);
proto_varint_enum!(BossBarDivision,
    0 :: NoDivision,
    1 :: SixNotches,
    2 :: TenNotches,
    3 :: TwelveNotches,
    4 :: TwentyNotches
);

proto_byte_flag!(BossBarFlags,
    0x01 :: is_darken_sky set_darken_sky,
    0x02 :: is_dragon_bar set_dragon_bar,
    0x03 :: is_create_fog set_create_fog
);