valence 0.1.0+mc1.19.2

A framework for building Minecraft servers in Rust.
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
//! Server to client packets.

use super::*;

pub mod status {
    use super::*;

    def_struct! {
        QueryResponse {
            json_response: String
        }
    }

    def_struct! {
        QueryPong {
            /// Should be the same as the payload from ping.
            payload: u64
        }
    }

    def_packet_group! {
        S2cStatusPacket {
            QueryResponse = 0,
            QueryPong = 1,
        }
    }
}

pub mod login {
    use super::*;

    def_struct! {
        LoginDisconnect {
            reason: Text,
        }
    }

    def_struct! {
        EncryptionRequest {
            /// Currently unused
            server_id: BoundedString<0, 20>,
            /// The RSA public key
            public_key: Vec<u8>,
            verify_token: BoundedArray<u8, 16, 16>,
        }
    }

    def_struct! {
        LoginSuccess {
            uuid: Uuid,
            username: BoundedString<3, 16>,
            properties: Vec<Property>,
        }
    }

    def_struct! {
        LoginCompression {
            threshold: VarInt
        }
    }

    def_struct! {
        LoginPluginRequest {
            message_id: VarInt,
            channel: Ident,
            data: RawBytes,
        }
    }

    def_packet_group! {
        S2cLoginPacket {
            LoginDisconnect = 0,
            EncryptionRequest = 1,
            LoginSuccess = 2,
            LoginCompression = 3,
            LoginPluginRequest = 4,
        }
    }
}

pub mod play {
    use super::*;

    def_struct! {
        EntitySpawn {
            entity_id: VarInt,
            object_uuid: Uuid,
            kind: VarInt,
            position: Vec3<f64>,
            pitch: ByteAngle,
            yaw: ByteAngle,
            head_yaw: ByteAngle,
            data: VarInt,
            velocity: Vec3<i16>,
        }
    }

    def_struct! {
        ExperienceOrbSpawn {
            entity_id: VarInt,
            position: Vec3<f64>,
            count: i16,
        }
    }

    def_struct! {
        PlayerSpawn {
            entity_id: VarInt,
            player_uuid: Uuid,
            position: Vec3<f64>,
            yaw: ByteAngle,
            pitch: ByteAngle,
        }
    }

    def_struct! {
        EntityAnimation {
            entity_id: VarInt,
            animation: u8,
        }
    }

    def_struct! {
        PlayerActionResponse {
            sequence: VarInt,
        }
    }

    def_struct! {
        BlockBreakingProgress {
            entity_id: VarInt,
            location: BlockPos,
            destroy_stage: BoundedInt<u8, 0, 10>,
        }
    }

    def_struct! {
        BlockEntityUpdate {
            location: BlockPos,
            kind: VarInt, // TODO: use enum here
            data: Compound,
        }
    }

    def_struct! {
        BlockEvent {
            location: BlockPos,
            action_id: u8,
            action_param: u8,
            block_type: VarInt, // TODO: use BlockType type.
        }
    }

    def_struct! {
        BlockUpdate {
            location: BlockPos,
            block_id: VarInt,
        }
    }

    def_struct! {
        BossBar {
            uuid: Uuid,
            action: BossBarAction,
        }
    }

    def_enum! {
        BossBarAction: VarInt {
            Add: BossBarActionAdd = 0,
            // TODO
        }
    }

    def_struct! {
        BossBarActionAdd {
            title: Text,
            health: f32,
            color: BossBarColor,
            division: BossBarDivision,
            /// TODO: bitmask
            flags: u8,
        }
    }

    def_enum! {
        BossBarColor: VarInt {
            Pink = 0,
            Blue = 1,
            Red = 2,
            Green = 3,
            Yellow = 4,
            Purple = 5,
            White = 6,
        }
    }

    def_enum! {
        BossBarDivision: VarInt {
            NoDivision = 0,
            SixNotches = 1,
            TenNotches = 2,
            TwelveNotches = 3,
            TwentyNotches = 4,
        }
    }

    def_struct! {
        SetDifficulty {
            difficulty: Difficulty,
            locked: bool,
        }
    }

    def_enum! {
        Difficulty: u8 {
            Peaceful = 0,
            Easy = 1,
            Normal = 2,
            Hard = 3,
        }
    }

    def_struct! {
        ClearTitles {
            reset: bool,
        }
    }

    def_struct! {
        Disconnect {
            reason: Text,
        }
    }

    def_struct! {
        EntityStatus {
            entity_id: i32,
            entity_status: u8,
        }
    }

    def_struct! {
        UnloadChunk {
            chunk_x: i32,
            chunk_z: i32
        }
    }

    def_struct! {
        GameStateChange {
            reason: GameStateChangeReason,
            value: f32,
        }
    }

    def_enum! {
        GameStateChangeReason: u8 {
            NoRespawnBlockAvailable = 0,
            EndRaining = 1,
            BeginRaining = 2,
            ChangeGameMode = 3,
            WinGame = 4,
            DemoEvent = 5,
            ArrowHitPlayer = 6,
            RainLevelChange = 7,
            ThunderLevelChange = 8,
            PlayPufferfishStingSound = 9,
            PlayElderGuardianMobAppearance = 10,
            EnableRespawnScreen = 11,
        }
    }

    def_struct! {
        WorldBorderInitialize {
            x: f64,
            z: f64,
            old_diameter: f64,
            new_diameter: f64,
            speed: VarLong,
            portal_teleport_boundary: VarInt,
            warning_blocks: VarInt,
            warning_time: VarInt,
        }
    }

    def_struct! {
        KeepAlive {
            id: i64,
        }
    }

    def_struct! {
        ChunkData {
            chunk_x: i32,
            chunk_z: i32,
            heightmaps: NbtBridge<ChunkDataHeightmaps>,
            blocks_and_biomes: Vec<u8>,
            block_entities: Vec<ChunkDataBlockEntity>,
            trust_edges: bool,
            sky_light_mask: BitVec<u64>,
            block_light_mask: BitVec<u64>,
            empty_sky_light_mask: BitVec<u64>,
            empty_block_light_mask: BitVec<u64>,
            sky_light_arrays: Vec<[u8; 2048]>,
            block_light_arrays: Vec<[u8; 2048]>,
        }
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct ChunkDataHeightmaps {
        #[serde(rename = "MOTION_BLOCKING", with = "crate::nbt::long_array")]
        pub motion_blocking: Vec<i64>,
    }

    def_struct! {
        ChunkDataBlockEntity {
            packed_xz: i8,
            y: i16,
            kind: VarInt,
            data: Compound,
        }
    }

    def_struct! {
        GameJoin {
            /// Entity ID of the joining player
            entity_id: i32,
            is_hardcore: bool,
            gamemode: GameMode,
            previous_gamemode: GameMode,
            dimension_names: Vec<Ident>,
            registry_codec: NbtBridge<RegistryCodec>,
            /// The name of the dimension type being spawned into.
            dimension_type_name: Ident,
            /// The name of the dimension being spawned into.
            dimension_name: Ident,
            /// Hash of the world's seed used for client biome noise.
            hashed_seed: i64,
            /// No longer used by the client.
            max_players: VarInt,
            view_distance: BoundedInt<VarInt, 2, 32>,
            simulation_distance: VarInt,
            /// If reduced debug info should be shown on the F3 screen.
            reduced_debug_info: bool,
            /// If player respawns should be instant or not.
            enable_respawn_screen: bool,
            is_debug: bool,
            /// If this is a superflat world.
            /// Superflat worlds have different void fog and horizon levels.
            is_flat: bool,
            last_death_location: Option<(Ident, BlockPos)>,
        }
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct RegistryCodec {
        #[serde(rename = "minecraft:dimension_type")]
        pub dimension_type_registry: DimensionTypeRegistry,
        #[serde(rename = "minecraft:worldgen/biome")]
        pub biome_registry: BiomeRegistry,
        #[serde(rename = "minecraft:chat_type")]
        pub chat_type_registry: ChatTypeRegistry,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct DimensionTypeRegistry {
        #[serde(rename = "type")]
        pub kind: Ident,
        pub value: Vec<DimensionTypeRegistryEntry>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct DimensionTypeRegistryEntry {
        pub name: Ident,
        pub id: i32,
        pub element: DimensionType,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct DimensionType {
        pub piglin_safe: bool,
        pub has_raids: bool,
        pub monster_spawn_light_level: i32,
        pub monster_spawn_block_light_limit: i32,
        pub natural: bool,
        pub ambient_light: f32,
        pub fixed_time: Option<i64>,
        pub infiniburn: String, // TODO: tag type?
        pub respawn_anchor_works: bool,
        pub has_skylight: bool,
        pub bed_works: bool,
        pub effects: Ident,
        pub min_y: i32,
        pub height: i32,
        pub logical_height: i32,
        pub coordinate_scale: f64,
        pub ultrawarm: bool,
        pub has_ceiling: bool,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeRegistry {
        #[serde(rename = "type")]
        pub kind: Ident,
        pub value: Vec<Biome>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct Biome {
        pub name: Ident,
        pub id: i32,
        pub element: BiomeProperty,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeProperty {
        pub precipitation: String,
        pub depth: f32,
        pub temperature: f32,
        pub scale: f32,
        pub downfall: f32,
        pub category: String,
        pub temperature_modifier: Option<String>,
        pub effects: BiomeEffects,
        pub particle: Option<BiomeParticle>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeEffects {
        pub sky_color: i32,
        pub water_fog_color: i32,
        pub fog_color: i32,
        pub water_color: i32,
        pub foliage_color: Option<i32>,
        pub grass_color: Option<i32>,
        pub grass_color_modifier: Option<String>,
        pub music: Option<BiomeMusic>,
        pub ambient_sound: Option<Ident>,
        pub additions_sound: Option<BiomeAdditionsSound>,
        pub mood_sound: Option<BiomeMoodSound>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeMusic {
        pub replace_current_music: bool,
        pub sound: Ident,
        pub max_delay: i32,
        pub min_delay: i32,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeAdditionsSound {
        pub sound: Ident,
        pub tick_chance: f64,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeMoodSound {
        pub sound: Ident,
        pub tick_delay: i32,
        pub offset: f64,
        pub block_search_extent: i32,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeParticle {
        pub probability: f32,
        pub options: BiomeParticleOptions,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct BiomeParticleOptions {
        #[serde(rename = "type")]
        pub kind: Ident,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct ChatTypeRegistry {
        #[serde(rename = "type")]
        pub kind: Ident,
        pub value: Vec<ChatTypeRegistryEntry>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct ChatTypeRegistryEntry {
        pub name: Ident,
        pub id: i32,
        pub element: ChatType,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct ChatType {
        pub chat: ChatTypeChat,
        pub narration: ChatTypeNarration,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct ChatTypeChat {}

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct ChatTypeNarration {
        pub priority: String,
    }

    def_enum! {
        #[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
        GameMode: u8 {
            #[default]
            Survival = 0,
            Creative = 1,
            Adventure = 2,
            Spectator = 3,
        }
    }

    def_struct! {
        MoveRelative {
            entity_id: VarInt,
            delta: Vec3<i16>,
            on_ground: bool,
        }
    }

    def_struct! {
        RotateAndMoveRelative {
            entity_id: VarInt,
            delta: Vec3<i16>,
            yaw: ByteAngle,
            pitch: ByteAngle,
            on_ground: bool,
        }
    }

    def_struct! {
        Rotate {
            entity_id: VarInt,
            yaw: ByteAngle,
            pitch: ByteAngle,
            on_ground: bool,
        }
    }

    def_struct! {
        ChatMessage {
            // TODO: more 1.19 stuff.
            message: Text,
            /// Index into the chat type registry
            kind: VarInt,
            sender: Uuid,
        }
    }

    def_enum! {
        UpdatePlayerList: VarInt {
            AddPlayer: Vec<PlayerListAddPlayer> = 0,
            UpdateGameMode: Vec<(Uuid, GameMode)> = 1,
            UpdateLatency: Vec<(Uuid, VarInt)> = 2,
            UpdateDisplayName: Vec<(Uuid, Option<Text>)> = 3,
            RemovePlayer: Vec<Uuid> = 4,
        }
    }

    def_struct! {
        PlayerListAddPlayer {
            uuid: Uuid,
            username: BoundedString<3, 16>,
            properties: Vec<Property>,
            game_mode: GameMode,
            ping: VarInt,
            display_name: Option<Text>,
            sig_data: Option<PublicKeyData>,
        }
    }

    def_struct! {
        PlayerPositionLook {
            position: Vec3<f64>,
            yaw: f32,
            pitch: f32,
            flags: PlayerPositionLookFlags,
            teleport_id: VarInt,
            dismount_vehicle: bool,
        }
    }

    def_bitfield! {
        PlayerPositionLookFlags: u8 {
            x = 0,
            y = 1,
            z = 2,
            y_rot = 3,
            x_rot = 4,
        }
    }

    def_struct! {
        EntitiesDestroy {
            entities: Vec<VarInt>,
        }
    }

    def_struct! {
        PlayerRespawn {
            dimension_type_name: Ident,
            dimension_name: Ident,
            hashed_seed: u64,
            game_mode: GameMode,
            previous_game_mode: GameMode,
            is_debug: bool,
            is_flat: bool,
            copy_metadata: bool,
            last_death_location: Option<(Ident, BlockPos)>,
        }
    }

    def_struct! {
        EntitySetHeadYaw {
            entity_id: VarInt,
            head_yaw: ByteAngle,
        }
    }

    def_struct! {
        ChunkSectionUpdate {
            chunk_section_position: i64,
            invert_trust_edges: bool,
            blocks: Vec<VarLong>,
        }
    }

    def_struct! {
        UpdateSelectedSlot {
            slot: BoundedInt<u8, 0, 9>,
        }
    }

    def_struct! {
        ChunkRenderDistanceCenter {
            chunk_x: VarInt,
            chunk_z: VarInt,
        }
    }

    def_struct! {
        ChunkLoadDistance {
            view_distance: BoundedInt<VarInt, 2, 32>,
        }
    }

    def_struct! {
        PlayerSpawnPosition {
            location: BlockPos,
            angle: f32,
        }
    }

    def_struct! {
        EntityTrackerUpdate {
            entity_id: VarInt,
            metadata: RawBytes,
        }
    }

    def_struct! {
        EntityVelocityUpdate {
            entity_id: VarInt,
            velocity: Vec3<i16>,
        }
    }

    def_struct! {
        UpdateSubtitle {
            subtitle_text: Text,
        }
    }

    def_struct! {
        WorldTimeUpdate {
            /// The age of the world in 1/20ths of a second.
            world_age: i64,
            /// The current time of day in 1/20ths of a second.
            /// The value should be in the range \[0, 24000].
            /// 6000 is noon, 12000 is sunset, and 18000 is midnight.
            time_of_day: i64,
        }
    }

    def_struct! {
        UpdateTitle {
            text: Text,
        }
    }

    def_struct! {
        #[derive(Copy, PartialEq, Eq)]
        TitleFade {
            /// Ticks to spend fading in.
            fade_in: u32,
            /// Ticks to keep the title displayed.
            stay: u32,
            /// Ticks to spend fading out.
            fade_out: u32,
        }
    }

    def_struct! {
        GameMessage {
            chat: Text,
            /// Index into the chat type registry.
            kind: VarInt,
        }
    }

    def_struct! {
        PlayerListHeaderFooter {
            header: Text,
            footer: Text,
        }
    }

    def_struct! {
        EntityPosition {
            entity_id: VarInt,
            position: Vec3<f64>,
            yaw: ByteAngle,
            pitch: ByteAngle,
            on_ground: bool,
        }
    }

    def_struct! {
        EntityAttributes {
            entity_id: VarInt,
            properties: Vec<EntityAttributesProperty>,
        }
    }

    def_struct! {
        EntityAttributesProperty {
            key: Ident,
            value: f64,
            modifiers: Vec<EntityAttributesModifiers>
        }
    }

    def_struct! {
        EntityAttributesModifiers {
            uuid: Uuid,
            amount: f64,
            operation: u8,
        }
    }

    def_packet_group! {
        S2cPlayPacket {
            EntitySpawn = 0,
            ExperienceOrbSpawn = 1,
            PlayerSpawn = 2,
            EntityAnimation = 3,
            PlayerActionResponse = 5,
            BlockBreakingProgress = 6,
            BlockEntityUpdate = 7,
            BlockEvent = 8,
            BlockUpdate = 9,
            BossBar = 10,
            ClearTitles = 13,
            Disconnect = 25,
            EntityStatus = 26,
            UnloadChunk = 28,
            GameStateChange = 29,
            KeepAlive = 32,
            ChunkData = 33,
            GameJoin = 37,
            MoveRelative = 40,
            RotateAndMoveRelative = 41,
            Rotate = 42,
            ChatMessage = 51,
            UpdatePlayerList = 55,
            PlayerPositionLook = 57,
            EntitiesDestroy = 59,
            PlayerRespawn = 62,
            EntitySetHeadYaw = 63,
            ChunkSectionUpdate = 64,
            UpdateSelectedSlot = 74,
            ChunkRenderDistanceCenter = 75,
            ChunkLoadDistance = 76,
            PlayerSpawnPosition = 77,
            EntityTrackerUpdate = 80,
            EntityVelocityUpdate = 82,
            UpdateSubtitle = 91,
            WorldTimeUpdate = 92,
            UpdateTitle = 93,
            TitleFade = 94,
            GameMessage = 98,
            PlayerListHeaderFooter = 99,
            EntityPosition = 102,
            EntityAttributes = 104,
        }
    }
}