graphite_mc_protocol 0.1.0

Implementation of the Minecraft protocol for the graphite_minecraft project
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
use graphite_binary::nbt::CachedNBT;
use graphite_binary::slice_serialization::*;
use std::borrow::Cow;

use crate::identify_packets;
use crate::types::{
    BlockPosition, ByteRotation, CommandNode, EquipmentList, EquipmentSlot, GameProfile,
    ProtocolItemStack, QuantizedShort, SignatureData,
};
use crate::IdentifiedPacket;
use num_enum::{IntoPrimitive, TryFromPrimitive};

identify_packets! {
    PacketId,
    AddEntity = 0x00,
    // AddExperienceOrb = 0x01,
    AddPlayer = 0x02,
    AnimateEntity = 0x03,
    // AwardStats = 0x04,
    BlockChangedAck = 0x05,
    BlockDestruction = 0x06,
    // BlockEntityData = 0x07,
    // BlockEvent = 0x08,
    BlockUpdate = 0x09,
    // BossEvent = 0x0a,
    // ChangeDifficulty = 0x0b,
    // ChatPreview = 0x0c,
    // ClearTitles = 0x0d,
    // CommandSuggestions = 0x0e,
    Commands = 0x0f,
    // ContainerClose = 0x10,
    // ContainerSetContent = 0x11,
    // ContainerSetData = 0x12,
    ContainerSetSlot<'_> = 0x13,
    // Cooldown = 0x14,
    // CustomChatCompletions = 0x15,
    CustomPayload<'_> = 0x16,
    // CustomSound = 0x17,
    // DeleteChat = 0x18,
    // Disconnect = 0x19,
    // EntityEvent = 0x1a,
    // Explode = 0x1b,
    // ForgetLevelChunk = 0x1c,
    GameEvent = 0x1d,
    // HorseScreenOpen = 0x1e,
    // InitializeBorder = 0x1f,
    KeepAlive = 0x20,
    LevelChunkWithLight<'_> = 0x21,
    LevelEvent = 0x22,
    // LevelParticles = 0x23,
    // LightUpdate = 0x24,
    Login<'_> = 0x25,
    // MapItemData = 0x26,
    // MerchantOffers = 0x27,
    MoveEntityPos = 0x28,
    MoveEntityPosRot = 0x29,
    MoveEntityRot = 0x2a,
    // MoveVehicle = 0x2b,
    // OpenBook = 0x2c,
    // OpenScreen = 0x2d,
    // OpenSignEditor = 0x2e,
    // Ping = 0x2f,
    // PlaceGhostRecipe = 0x30,
    PlayerAbilities = 0x31,
    // PlayerChatHeader = 0x32,
    // PlayerChat = 0x33,
    // PlayerCombatEnd = 0x34,
    // PlayerCombatEnter = 0x35,
    // PlayerCombatKill = 0x36,
    PlayerInfo<'_> = 0x37,
    // PlayerLookAt = 0x38,
    PlayerPosition = 0x39,
    // UnlockRecipe = 0x3a,
    RemoveEntities = 0x3b,
    // RemoveMobEffect = 0x3c,
    // ResourcePack = 0x3d,
    // Respawn = 0x3e,
    RotateHead = 0x3f,
    // SectionBlocksUpdate = 0x40,
    // SelectAdvancementTab = 0x41,
    // ServerData = 0x42,
    // SetActionBarText = 0x43,
    // SetBorderCenter = 0x44,
    // SetBorderLerpSize = 0x45,
    // SetBorderSize = 0x46,
    // SetBorderWarningDelay = 0x47,
    // SetBorderWarningDistance = 0x48,
    // SetCamera = 0x49,
    // SetCarriedItem = 0x4a,
    SetChunkCacheCenter = 0x4b,
    // SetChunkCacheRadius = 0x4c,
    // SetDefaultSpawnPosition = 0x4d,
    // SetDisplayChatPreview = 0x4e,
    // SetDisplayObjective = 0x4f,
    SetEntityData<'_> = 0x50,
    // SetEntityLink = 0x51,
    // SetEntityMotion = 0x52,
    SetEquipment<'_> = 0x53,
    // SetExperience = 0x54,
    // SetHealth = 0x55,
    // SetObjective = 0x56,
    // SetPassengers = 0x57,
    // SetPlayerTeam = 0x58,
    // SetScore = 0x59,
    // SetSimulationDistance = 0x5a,
    // SetSubtitleText = 0x5b,
    // SetTime = 0x5c,
    // SetTitleText = 0x5d,
    // SetTitleAnimation = 0x5e,
    // SoundEntity = 0x5f,
    // Sound = 0x60,
    // StopSound = 0x61,
    SystemChat<'_> = 0x62,
    // TabList = 0x63,
    // TagQuery = 0x64,
    // TakeItemEntity = 0x65,
    TeleportEntity = 0x66,
    // UpdateAdvancements = 0x67,
    // UpdateAttributes = 0x68,
    // UpdateMobEffect = 0x69,
    // UpdateRecipes = 0x6f,
    UpdateTags<'_> = 0x6b
}

// Add Entity

slice_serializable! {
    #[derive(Debug)]
    pub struct AddEntity {
        pub id: i32 as VarInt,
        pub uuid: u128 as BigEndian,
        pub entity_type: i32 as VarInt,
        pub x: f64 as BigEndian,
        pub y: f64 as BigEndian,
        pub z: f64 as BigEndian,
        pub yaw: f32 as ByteRotation,
        pub pitch: f32 as ByteRotation,
        pub head_yaw: f32 as ByteRotation,
        pub data: i32 as VarInt, // nice naming mojang
        pub x_vel: f32 as QuantizedShort,
        pub y_vel: f32 as QuantizedShort,
        pub z_vel: f32 as QuantizedShort,
    }
}

// Add Player
slice_serializable! {
    #[derive(Debug)]
    pub struct AddPlayer {
        pub id: i32 as VarInt,
        pub uuid: u128 as BigEndian,
        pub x: f64 as BigEndian,
        pub y: f64 as BigEndian,
        pub z: f64 as BigEndian,
        pub yaw: f32 as ByteRotation,
        pub pitch: f32 as ByteRotation
    }
}

// Animate Entity
#[derive(Default, Debug, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum EntityAnimation {
    #[default]
    SwingMainHand,
    Hurt,
    WakeUp,
    SwingOffHand,
    CriticalHit,
    MagicCriticalHit,
}

// Animate Entity
slice_serializable! {
    #[derive(Debug)]
    pub struct AnimateEntity {
        pub id: i32 as VarInt,
        pub animation: EntityAnimation as AttemptFrom<Single, u8>
    }
}

// Block Changed Ack
slice_serializable! {
    #[derive(Debug)]
    pub struct BlockChangedAck {
        pub sequence: i32 as VarInt
    }
}

// Block Destruction
slice_serializable! {
    #[derive(Debug)]
    pub struct BlockDestruction {
        pub entity_id: i32 as VarInt,
        pub location: BlockPosition,
        pub destroy_stage: i8 as Single
    }
}

// Block Update
slice_serializable! {
    #[derive(Debug)]
    pub struct BlockUpdate {
        pub pos: BlockPosition,
        pub block_state: i32 as VarInt
    }
}

// Commands
slice_serializable! {
    #[derive(Debug)]
    pub struct Commands {
        pub nodes: Vec<CommandNode> as SizedArray<CommandNode>,
        pub root_index: i32 as VarInt
    }
}

// Container Set Slot
slice_serializable! {
    #[derive(Debug)]
    pub struct ContainerSetSlot<'a> {
        pub window_id: i8 as Single,
        pub state_id: i32 as VarInt,
        pub slot: i16 as BigEndian,
        pub item: Option<ProtocolItemStack<'a>>
    }
}

// Custom Payload
slice_serializable! {
    #[derive(Debug)]
    pub struct CustomPayload<'a> {
        pub channel: &'a str as SizedString,
        pub data: &'a [u8] as GreedyBlob
    }
}

// Game Event

#[derive(Debug, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum GameEventType {
    NoRespawnBlockAvailable,
    StartRaining,
    StopRaining,
    ChangeGameMode,
    WinGame,
    DemoEvent,
    ArrowHitPlayer,
    RainLevelChange,
    ThunderLevelChange,
    PufferFishSting,
    GuardianElderEffect,
    ImmediateRespawn,
}

slice_serializable! {
    #[derive(Debug)]
    pub struct GameEvent {
        pub event_type: GameEventType as AttemptFrom<Single, u8>,
        pub param: f32 as BigEndian
    }
}

// Keep Alive
slice_serializable! {
    #[derive(Debug)]
    pub struct KeepAlive {
        pub id: u64 as BigEndian
    }
}

// LevelChunkWithLight
slice_serializable! {
    #[derive(Debug)]
    pub struct ChunkBlockData<'a> {
        pub heightmaps: Cow<'a, CachedNBT> as NBTBlob,
        pub data: &'a [u8] as SizedBlob,
        pub block_entity_count: i32 as VarInt,
        pub block_entity_data: &'a [u8] as WriteOnlyBlob,
        pub trust_edges: bool as Single
    }
}

#[derive(Debug, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[repr(i32)]
pub enum LevelEventType {
    SoundDispenserDispense = 1000,
    SoundDispenserFail = 1001,
    SoundDispenserProjectileLaunch = 1002,
    SoundEnderEyeLaunch = 1003,
    SoundFireworkShoot = 1004,
    SoundOpenIronDoor = 1005,
    SoundOpenWoodenDoor = 1006,
    SoundOpenWoodenTrapDoor = 1007,
    SoundOpenFenceGate = 1008,
    SoundExtinguishFire = 1009,
    SoundPlayRecording = 1010,
    SoundCloseIronDoor = 1011,
    SoundCloseWoodenDoor = 1012,
    SoundCloseWoodenTrapDoor = 1013,
    SoundCloseFenceGate = 1014,
    SoundGhastWarning = 1015,
    SoundGhastFireball = 1016,
    SoundDragonFireball = 1017,
    SoundBlazeFireball = 1018,
    SoundZombieWoodenDoor = 1019,
    SoundZombieIronDoor = 1020,
    SoundZombieDoorCrash = 1021,
    SoundWitherBlockBreak = 1022,
    SoundWitherBossSpawn = 1023,
    SoundWitherBossShoot = 1024,
    SoundBatLiftoff = 1025,
    SoundZombieInfected = 1026,
    SoundZombieConverted = 1027,
    SoundDragonDeath = 1028,
    SoundAnvilBroken = 1029,
    SoundAnvilUsed = 1030,
    SoundAnvilLand = 1031,
    SoundPortalTravel = 1032,
    SoundChorusGrow = 1033,
    SoundChorusDeath = 1034,
    SoundBrewingStandBrew = 1035,
    SoundCloseIronTrapDoor = 1036,
    SoundOpenIronTrapDoor = 1037,
    SoundEndPortalSpawn = 1038,
    SoundPhantomBite = 1039,
    SoundZombieToDrowned = 1040,
    SoundHuskToZombie = 1041,
    SoundGrindstoneUsed = 1042,
    SoundPageTurn = 1043,
    SoundSmithingTableUsed = 1044,
    SoundPointedDripstoneLand = 1045,
    SoundDripLavaIntoCauldron = 1046,
    SoundDripWaterIntoCauldron = 1047,
    SoundSkeletonToStray = 1048,
    ComposterFill = 1500,
    LavaFizz = 1501,
    RedstoneTorchBurnout = 1502,
    EndPortalFrameFill = 1503,
    DripstoneDrip = 1504,
    ParticlesAndSoundPlantGrowth = 1505,
    ParticlesShoot = 2000,
    ParticlesDestroyBlock = 2001,
    ParticlesSpellPotionSplash = 2002,
    ParticlesEyeOfEnderDeath = 2003,
    ParticlesMobblockSpawn = 2004,
    ParticlesPlantGrowth = 2005,
    ParticlesDragonFireballSplash = 2006,
    ParticlesInstantPotionSplash = 2007,
    ParticlesDragonBlockBreak = 2008,
    ParticlesWaterEvaporating = 2009,
    AnimationEndGatewaySpawn = 3000,
    AnimationDragonSummonRoar = 3001,
    ParticlesElectricSpark = 3002,
    ParticlesAndSoundWaxOn = 3003,
    ParticlesWaxOff = 3004,
    ParticlesScrape = 3005,
    ParticlesSculkCharge = 3006,
    ParticlesSculkShriek = 3007,
}

// Level Event
slice_serializable! {
    #[derive(Debug)]
    pub struct LevelEvent {
        pub event_type: LevelEventType as AttemptFrom<BigEndian, i32>,
        pub pos: BlockPosition,
        pub data: i32 as BigEndian,
        pub global: bool as Single // global used by vanilla for dragon death and portal opening
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct ChunkLightData<'a> {
        pub sky_light_mask: Vec<u64> as SizedArray<BigEndian>,
        pub block_light_mask: Vec<u64> as SizedArray<BigEndian>,
        pub empty_sky_light_mask: Vec<u64> as SizedArray<BigEndian>,
        pub empty_block_light_mask: Vec<u64> as SizedArray<BigEndian>,
        pub sky_light_entries: Vec<&'a [u8]> as SizedArray<SizedBlob>,
        pub block_light_entries: Vec<&'a [u8]> as SizedArray<SizedBlob>
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct LevelChunkWithLight<'a> {
        pub chunk_x: i32 as BigEndian,
        pub chunk_z: i32 as BigEndian,
        pub chunk_block_data: ChunkBlockData<'a>,
        pub chunk_light_data: ChunkLightData<'a>
    }
}

// Login
slice_serializable! {
    #[derive(Debug)]
    pub struct Login<'a> {
        pub entity_id: i32 as BigEndian,
        pub is_hardcore: bool as Single,
        pub gamemode: u8 as Single,
        pub previous_gamemode: i8 as Single,
        pub dimension_names: Vec<&'a str> as SizedArray<SizedString>,
        pub registry_codec: Cow<'a, CachedNBT> as NBTBlob,
        pub dimension_type: &'a str as SizedString,
        pub dimension_name: &'a str as SizedString,
        pub hashed_seed: u64 as BigEndian,
        pub max_players: i32 as VarInt,
        pub view_distance: i32 as VarInt,
        pub simulation_distance: i32 as VarInt,
        pub reduced_debug_info: bool as Single,
        pub enable_respawn_screen: bool as Single,
        pub is_debug: bool as Single,
        pub is_flat: bool as Single,
        pub death_location: Option<BlockPosition>
    }
}

// Move Entity

slice_serializable! {
    #[derive(Debug)]
    pub struct MoveEntityPos {
        pub entity_id: i32 as VarInt,
        pub delta_x: i16 as BigEndian,
        pub delta_y: i16 as BigEndian,
        pub delta_z: i16 as BigEndian,
        pub on_ground: bool as Single,
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct MoveEntityPosRot {
        pub entity_id: i32 as VarInt,
        pub delta_x: i16 as BigEndian,
        pub delta_y: i16 as BigEndian,
        pub delta_z: i16 as BigEndian,
        pub yaw: f32 as ByteRotation,
        pub pitch: f32 as ByteRotation,
        pub on_ground: bool as Single,
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct MoveEntityRot {
        pub entity_id: i32 as VarInt,
        pub yaw: f32 as ByteRotation,
        pub pitch: f32 as ByteRotation,
        pub on_ground: bool as Single,
    }
}

// Player Abilities
slice_serializable! {
    #[derive(Debug)]
    pub struct PlayerAbilities {
        pub invulnerable: bool as packed!(),
        pub is_flying: bool as packed!(),
        pub allow_flying: bool as packed!(),
        pub instant_breaking: bool as packed!(),
        pub flying_speed: f32 as BigEndian,
        pub walking_speed: f32 as BigEndian,
    }
}

// PlayerInfo
slice_serializable! {
    #[derive(Debug)]
    pub struct PlayerInfoAddPlayer<'a> {
        pub profile: GameProfile,
        pub gamemode: u8 as Single,
        pub ping: i32 as VarInt,
        pub display_name: Option<&'a str> as Option<SizedString>,
        pub signature_data: Option<SignatureData<'a>>
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct PlayerInfoUpdateGamemode {
        pub uuid: u128 as BigEndian,
        pub gamemode: u8 as Single
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct PlayerInfoUpdateLatency {
        pub uuid: u128 as BigEndian,
        pub ping: i32 as VarInt
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct PlayerInfoDisplayName<'a> {
        pub uuid: u128 as BigEndian,
        pub display_name: Option<&'a str> as Option<SizedString>,
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub enum PlayerInfo<'a> {
        AddPlayer {
            values: Vec<PlayerInfoAddPlayer<'a>> as SizedArray<PlayerInfoAddPlayer>
        },
        UpdateGameMode {
            values: Vec<PlayerInfoUpdateGamemode> as SizedArray<PlayerInfoUpdateGamemode>
        },
        UpdateLatency {
            values: Vec<PlayerInfoUpdateLatency> as SizedArray<PlayerInfoUpdateLatency>
        },
        UpdateDisplayName {
            values: Vec<PlayerInfoDisplayName<'a>> as SizedArray<PlayerInfoDisplayName>
        },
        RemovePlayer {
            uuids: Vec<u128> as SizedArray<BigEndian>,
        }
    }
}

// Remove Entities
slice_serializable! {
    #[derive(Debug)]
    pub struct RemoveEntities {
        pub entities: Vec<i32> as SizedArray<VarInt>
    }
}

// Player Position
slice_serializable! {
    #[derive(Debug)]
    pub struct PlayerPosition {
        pub x: f64 as BigEndian,
        pub y: f64 as BigEndian,
        pub z: f64 as BigEndian,
        pub yaw: f32 as BigEndian,
        pub pitch: f32 as BigEndian,
        pub relative_arguments: u8 as Single,
        pub id: i32 as VarInt,
        pub dismount_vehicle: bool as Single
    }
}

// Rotate Head
slice_serializable! {
    #[derive(Debug)]
    pub struct RotateHead {
        pub entity_id: i32 as VarInt,
        pub head_yaw: f32 as ByteRotation
    }
}

// Set Chunk Cache Center
slice_serializable! {
    #[derive(Debug)]
    pub struct SetChunkCacheCenter {
        pub chunk_x: i32 as VarInt,
        pub chunk_z: i32 as VarInt
    }
}

// Set Equipment
slice_serializable! {
    #[derive(Debug)]
    pub struct SetEquipment<'a> {
        pub entity_id: i32 as VarInt,
        pub equipment: Vec<(EquipmentSlot, Option<ProtocolItemStack<'a>>)> as EquipmentList
    }
}

// Set Entity Data
slice_serializable! {
    #[derive(Debug)]
    pub struct SetEntityData<'a> {
        pub entity_id: i32 as VarInt,
        pub data: &'a [u8] as GreedyBlob
    }
}

// System Chat
slice_serializable! {
    #[derive(Debug)]
    pub struct SystemChat<'a> {
        pub message: &'a str as SizedString,
        pub overlay: bool as Single
    }
}

// Teleport Entity
slice_serializable! {
    #[derive(Debug)]
    pub struct TeleportEntity {
        pub entity_id: i32 as VarInt,
        pub x: f64 as BigEndian,
        pub y: f64 as BigEndian,
        pub z: f64 as BigEndian,
        pub yaw: f32 as ByteRotation,
        pub pitch: f32 as ByteRotation,
        pub on_ground: bool as Single
    }
}

// Update Tags
slice_serializable! {
    #[derive(Debug)]
    pub struct Tag<'a> {
        // Tag identifier (Vanilla required tags are minecraft:block, minecraft:item, minecraft:fluid, minecraft:entity_type, and minecraft:game_event)
        pub name: &'a str as SizedString,
        pub entries: Vec<u16> as SizedArray<VarInt>
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct TagRegistry<'a> {
        // Tag identifier (Vanilla required tags are minecraft:block, minecraft:item, minecraft:fluid, minecraft:entity_type, and minecraft:game_event)
        pub tag_type: &'a str as SizedString,
        pub values: Vec<Tag<'a>> as SizedArray<Tag>
    }
}

slice_serializable! {
    #[derive(Debug)]
    pub struct UpdateTags<'a> {
        pub registries: Vec<TagRegistry<'a>> as SizedArray<TagRegistry>
    }
}