ghast_client/common.rs
1//! The version-agnostic layer: what the client core receives from and sends to the server,
2//! independent of the protocol version.
3//!
4//! Each supported version (see [`crate::version`]) maps its generated packets to [`Inbound`]
5//! and [`Outbound`] values. Only the packets the core or the behaviour needs are mapped; the
6//! rest is dropped by the frame decoder's filter before it is decoded.
7
8use bytes::Bytes;
9use ghast_protocol::types::ByteString;
10use uuid::Uuid;
11
12/// A position or direction in world space.
13#[derive(Debug, Clone, Copy, Default, PartialEq)]
14pub struct Vec3 {
15 /// East (+) / west (−).
16 pub x: f64,
17 /// Up (+) / down (−).
18 pub y: f64,
19 /// South (+) / north (−).
20 pub z: f64,
21}
22
23impl Vec3 {
24 /// A vector from its components.
25 #[must_use]
26 pub const fn new(x: f64, y: f64, z: f64) -> Self {
27 Vec3 { x, y, z }
28 }
29}
30
31/// The player's pose: feet position, look, motion and movement flags.
32///
33/// `yaw` / `pitch` are degrees in vanilla's convention (yaw is an unbounded `f32` like the
34/// client's `Entity.yaw`; 0 = south, 90 = west; pitch 90 = straight down).
35#[derive(Debug, Clone, Copy, Default, PartialEq)]
36pub struct Pose {
37 /// Feet position (bounding box minimum Y).
38 pub pos: Vec3,
39 /// Yaw in degrees.
40 pub yaw: f32,
41 /// Pitch in degrees.
42 pub pitch: f32,
43 /// Motion per tick.
44 pub motion: Vec3,
45 /// On ground as the client computes it (and sends it).
46 pub on_ground: bool,
47 /// Creative / ability flight.
48 pub flying: bool,
49 /// Sneaking (as sent with `entity_action`).
50 pub sneaking: bool,
51 /// Sprinting (as sent with `entity_action`).
52 pub sprinting: bool,
53}
54
55impl Pose {
56 /// Eye position: feet + 1.62 (1.54 while sneaking in 1.8; 1.16.5 uses a crouching pose
57 /// with eyes at 1.27, see [`crate::GameVersion::sneak_eye_height`]).
58 #[must_use]
59 pub fn eyes(&self, eye_height: f64) -> Vec3 {
60 Vec3::new(self.pos.x, self.pos.y + eye_height, self.pos.z)
61 }
62}
63
64/// Movement keys held for the next movement step (the keyboard state in vanilla).
65#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
66pub struct Controls {
67 /// Walk forward (W).
68 pub forward: bool,
69 /// Walk backward (S).
70 pub back: bool,
71 /// Strafe left (A).
72 pub left: bool,
73 /// Strafe right (D).
74 pub right: bool,
75 /// Jump (space).
76 pub jump: bool,
77 /// Sneak (shift).
78 pub sneak: bool,
79 /// Sprint key (ctrl).
80 pub sprint: bool,
81}
82
83impl Controls {
84 /// No key held.
85 pub const NONE: Controls = Controls {
86 forward: false,
87 back: false,
88 left: false,
89 right: false,
90 jump: false,
91 sneak: false,
92 sprint: false,
93 };
94}
95
96/// Where a chat message is shown.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
98#[non_exhaustive]
99pub enum ChatPosition {
100 /// A player chat message.
101 Chat,
102 /// A system message (command feedback, server announcements).
103 System,
104 /// The action bar above the hotbar.
105 ActionBar,
106 /// A value this client does not know.
107 Other(i8),
108}
109
110impl ChatPosition {
111 /// From the wire byte (same values in every supported version).
112 #[must_use]
113 pub fn from_wire(v: i8) -> Self {
114 match v {
115 0 => Self::Chat,
116 1 => Self::System,
117 2 => Self::ActionBar,
118 v => Self::Other(v),
119 }
120 }
121}
122
123/// A chat message from the server. The text stays raw JSON until asked for.
124#[derive(Debug, Clone, PartialEq, Eq)]
125#[non_exhaustive]
126pub struct ChatMessage {
127 /// The chat component as the server sent it (JSON).
128 pub json: ByteString,
129 /// Where it is shown.
130 pub position: ChatPosition,
131 /// Sender UUID (1.16+; `None` on 1.8 and for system messages with the nil UUID).
132 pub sender: Option<Uuid>,
133 /// 1.20.3+: the text already rendered from the NBT component (`json` is empty then).
134 pub(crate) rendered: Option<String>,
135}
136
137impl ChatMessage {
138 /// A message from its JSON chat component.
139 #[must_use]
140 pub fn from_json(json: ByteString, position: ChatPosition, sender: Option<Uuid>) -> Self {
141 ChatMessage {
142 json,
143 position,
144 sender,
145 rendered: None,
146 }
147 }
148
149 /// A message already rendered to plain text (1.20.3+ NBT components).
150 #[must_use]
151 pub fn from_text(text: String, position: ChatPosition, sender: Option<Uuid>) -> Self {
152 ChatMessage {
153 json: ByteString::from_static(""),
154 position,
155 sender,
156 rendered: Some(text),
157 }
158 }
159
160 /// The message as plain text, with legacy `§` formatting codes kept.
161 #[must_use]
162 pub fn text(&self) -> String {
163 match &self.rendered {
164 Some(t) => t.clone(),
165 None => ghast_protocol::chat::to_plain_text(&self.json),
166 }
167 }
168
169 /// The message as plain text without `§` formatting codes.
170 #[must_use]
171 pub fn plain(&self) -> String {
172 ghast_protocol::chat::strip_formatting(&self.text())
173 }
174}
175
176/// Converts a chat component (JSON) to plain text without formatting codes.
177#[must_use]
178pub fn chat_to_plain(json: &str) -> String {
179 ghast_protocol::chat::strip_formatting(&ghast_protocol::chat::to_plain_text(json))
180}
181
182/// Game mode.
183#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
184pub enum GameMode {
185 /// Survival.
186 #[default]
187 Survival,
188 /// Creative.
189 Creative,
190 /// Adventure.
191 Adventure,
192 /// Spectator.
193 Spectator,
194 /// A value this client does not know.
195 Other(u8),
196}
197
198impl GameMode {
199 /// From the wire value (the hardcore bit 0x08 of 1.8 is ignored).
200 #[must_use]
201 pub fn from_wire(v: u8) -> Self {
202 match v & 0x07 {
203 0 => Self::Survival,
204 1 => Self::Creative,
205 2 => Self::Adventure,
206 3 => Self::Spectator,
207 _ => Self::Other(v),
208 }
209 }
210}
211
212/// Player abilities (`abilities` packet).
213#[derive(Debug, Clone, Copy, PartialEq)]
214#[non_exhaustive]
215pub struct Abilities {
216 /// Raw flags: 1 invulnerable, 2 flying, 4 may fly, 8 creative (instant break).
217 pub flags: i8,
218 /// Flying speed (0.05 by default).
219 pub flying_speed: f32,
220 /// Walking speed (0.1 by default).
221 pub walking_speed: f32,
222}
223
224impl Default for Abilities {
225 fn default() -> Self {
226 Abilities {
227 flags: 0,
228 flying_speed: 0.05,
229 walking_speed: 0.1,
230 }
231 }
232}
233
234impl Abilities {
235 /// The server allows flight.
236 #[must_use]
237 pub fn may_fly(&self) -> bool {
238 self.flags & 4 != 0
239 }
240
241 /// The server says the player is flying.
242 #[must_use]
243 pub fn flying(&self) -> bool {
244 self.flags & 2 != 0
245 }
246}
247
248/// Health, food and saturation (`update_health`).
249#[derive(Debug, Clone, Copy, PartialEq)]
250#[non_exhaustive]
251pub struct Health {
252 /// Health points (20 = full; ≤ 0 = dead).
253 pub health: f32,
254 /// Food level 0 … 20.
255 pub food: i32,
256 /// Food saturation.
257 pub saturation: f32,
258}
259
260impl Default for Health {
261 fn default() -> Self {
262 Health {
263 health: 20.0,
264 food: 20,
265 saturation: 5.0,
266 }
267 }
268}
269
270/// Relative-teleport flag bits of the server's position packet.
271pub mod teleport_flags {
272 /// X is relative.
273 pub const X: i8 = 0x01;
274 /// Y is relative.
275 pub const Y: i8 = 0x02;
276 /// Z is relative.
277 pub const Z: i8 = 0x04;
278 /// Yaw is relative.
279 pub const YAW: i8 = 0x08;
280 /// Pitch is relative.
281 pub const PITCH: i8 = 0x10;
282}
283
284/// A server teleport (position packet), before it is applied to the pose.
285#[derive(Debug, Clone, Copy, PartialEq, Default)]
286#[non_exhaustive]
287pub struct Teleport {
288 /// X (absolute or relative, see `flags`).
289 pub x: f64,
290 /// Y.
291 pub y: f64,
292 /// Z.
293 pub z: f64,
294 /// Yaw.
295 pub yaw: f32,
296 /// Pitch.
297 pub pitch: f32,
298 /// [`teleport_flags`] bits.
299 pub flags: i8,
300 /// Teleport id to confirm (1.9+).
301 pub teleport_id: Option<i32>,
302 /// Every relative bit as on the wire: `flags` (bits 0 – 4) plus, from 1.21.2 on, 5 – 7
303 /// delta X / Y / Z relative and 8 "rotate the delta".
304 pub relative: u32,
305 /// The motion the teleport sets (1.21.2+; zero before).
306 pub delta: Vec3,
307}
308
309/// Login-state packets from the server, common to every version.
310#[derive(Debug, Clone, PartialEq)]
311#[non_exhaustive]
312pub enum LoginInbound {
313 /// The server refused the login (chat JSON).
314 Disconnect(ByteString),
315 /// Online mode: the server asks for encryption.
316 EncryptionRequest {
317 /// Server id (usually empty) for the session hash.
318 server_id: ByteString,
319 /// The server's RSA public key (DER).
320 public_key: Bytes,
321 /// Token to encrypt and send back.
322 verify_token: Bytes,
323 },
324 /// The server asks for encryption without a session-server check (1.20.5+
325 /// `shouldAuthenticate = false`): the client encrypts and skips the `join` call, so an
326 /// offline account can answer it too.
327 EncryptionRequestUnauthenticated {
328 /// Server id (usually empty).
329 server_id: ByteString,
330 /// The server's RSA public key (DER).
331 public_key: Bytes,
332 /// Token to encrypt and send back.
333 verify_token: Bytes,
334 },
335 /// Compression starts with the next packet.
336 Compress(i32),
337 /// Login finished; the connection is in the play state.
338 Success {
339 /// The player's UUID as the server assigned it.
340 uuid: Option<Uuid>,
341 /// The player's name.
342 username: ByteString,
343 },
344 /// A login plugin request (1.13+): answered with "not understood".
345 PluginRequest {
346 /// Message id to answer.
347 message_id: i32,
348 /// Channel name.
349 channel: ByteString,
350 },
351 /// The server asks for a cookie (1.20.5+): answered with none.
352 CookieRequest(ByteString),
353}
354
355/// A data pack the server offers as known (configuration `select_known_packs`).
356#[derive(Debug, Clone, PartialEq, Eq)]
357#[non_exhaustive]
358pub struct KnownPack {
359 /// Namespace (`minecraft`).
360 pub namespace: ByteString,
361 /// Pack id (`core`).
362 pub id: ByteString,
363 /// Pack version (the game version, e.g. `26.1.2`).
364 pub version: ByteString,
365}
366
367/// Configuration-state packets from the server the client reacts to (1.20.2+).
368#[derive(Debug, Clone, PartialEq)]
369#[non_exhaustive]
370pub enum ConfigInbound {
371 /// Keep-alive to echo.
372 KeepAlive(i64),
373 /// Ping to answer with a pong.
374 Ping(i32),
375 /// Configuration is done: answer and enter play.
376 Finish,
377 /// The server ends the connection (plain text).
378 Disconnect(String),
379 /// The packs the server can skip sending data for.
380 SelectKnownPacks(Vec<KnownPack>),
381 /// A registry (`registry_data`), kept raw for the world and the wire layer.
382 RegistryData(ghast_protocol::RawPacket),
383 /// A server resource pack.
384 ResourcePackPush {
385 /// Pack id.
386 id: Uuid,
387 /// The server requires it.
388 required: bool,
389 },
390 /// The server asks for a cookie.
391 CookieRequest(ByteString),
392 /// The server's code of conduct (26.x): the player accepts it to continue.
393 CodeOfConduct(String),
394 /// The server sends the client to another server (`host:port`): the session ends with
395 /// [`crate::SessionEnd::Transferred`] (followed with [`crate::Client::follow_transfers`]).
396 Transfer(String),
397 /// The server shows a dialog (26.1+, see [`crate::dialog`]).
398 ShowDialog(Box<crate::dialog::Dialog>),
399 /// The server closes the open dialog.
400 ClearDialog,
401}
402
403/// Configuration-state packets the client writes (1.20.2+).
404#[derive(Debug, Clone, PartialEq)]
405#[non_exhaustive]
406pub enum ConfigOutbound {
407 /// Keep-alive answer.
408 KeepAlive(i64),
409 /// Pong.
410 Pong(i32),
411 /// Acknowledge the end of configuration.
412 Finish,
413 /// The packs the client knows too.
414 SelectKnownPacks(Vec<KnownPack>),
415 /// Resource pack status (see [`Outbound::ResourcePackStatus`]).
416 ResourcePackStatus {
417 /// Pack id.
418 id: Uuid,
419 /// Status.
420 result: i32,
421 },
422 /// Cookie answer.
423 CookieResponse {
424 /// Key.
425 key: ByteString,
426 },
427 /// Accept the code of conduct.
428 AcceptCodeOfConduct,
429 /// The client brand.
430 Brand(String),
431 /// Client settings.
432 Settings(ClientSettings),
433 /// A dialog button's custom action (26.1+, see [`Outbound::CustomClickAction`]).
434 CustomClickAction {
435 /// Action id.
436 id: String,
437 /// Payload tag.
438 payload: Option<ghast_protocol::nbt::AnonNbt>,
439 },
440}
441
442/// Static information from the join game packet.
443#[derive(Debug, Clone, PartialEq)]
444#[non_exhaustive]
445pub struct JoinGame {
446 /// The player's entity id.
447 pub entity_id: i32,
448 /// Game mode.
449 pub game_mode: GameMode,
450 /// Dimension: the 1.8 id (−1 nether, 0 overworld, 1 end) as a string, or the 1.16 world
451 /// name (e.g. `minecraft:overworld`).
452 pub dimension: String,
453 /// Hardcore world.
454 pub hardcore: bool,
455 /// 1.16: the dimension codec and dimension type NBT, kept raw (not parsed).
456 pub dimension_nbt: Option<(ghast_protocol::nbt::RawNbt, ghast_protocol::nbt::RawNbt)>,
457 /// 1.16: the respawn screen is shown (false = immediate respawn gamerule).
458 pub respawn_screen: bool,
459}
460
461/// Clientbound play packets the core understands, common to every version.
462#[derive(Debug, Clone, PartialEq)]
463#[non_exhaustive]
464pub enum Inbound {
465 /// Keep-alive to echo.
466 KeepAlive(i64),
467 /// Join game (also sent again on a proxy server switch).
468 JoinGame(JoinGame),
469 /// Respawn / dimension change.
470 Respawn {
471 /// Dimension key (see [`JoinGame::dimension`]).
472 dimension: String,
473 /// Game mode after the respawn.
474 game_mode: GameMode,
475 /// 1.20.2+ data kept from the old player: bit 1 attributes, bit 2 entity data (the
476 /// last sent input and sprinting); 0 before.
477 keep: u8,
478 },
479 /// Server teleport / position correction.
480 Teleport(Teleport),
481 /// Chat message.
482 Chat(ChatMessage),
483 /// The server ends the session (chat JSON).
484 Disconnect(ByteString),
485 /// Health, food and saturation.
486 Health(Health),
487 /// Abilities.
488 Abilities(Abilities),
489 /// The server selects a hotbar slot.
490 HeldSlot(i8),
491 /// Game state change (`reason`, `value`), e.g. reason 3 = game mode.
492 GameState {
493 /// Reason id (same numbering in all supported versions).
494 reason: u8,
495 /// Value.
496 value: f32,
497 },
498 /// Window confirm transaction.
499 Transaction {
500 /// Window id.
501 window_id: i8,
502 /// Action number.
503 action: i16,
504 /// Accepted.
505 accepted: bool,
506 },
507 /// A window opened.
508 OpenWindow(i32),
509 /// The open window was closed by the server.
510 CloseWindow(i32),
511 /// Compression threshold change in the play state (1.8).
512 SetCompression(i32),
513 /// Entity velocity (blocks per tick; the wire's 1/8000 units converted).
514 Velocity {
515 /// The entity.
516 entity_id: i32,
517 /// Motion per tick.
518 motion: Vec3,
519 },
520 /// The player motion an explosion adds.
521 ExplosionPush(Vec3),
522 /// Movement speed attribute of an entity (entity properties packet).
523 MovementSpeed {
524 /// The entity.
525 entity_id: i32,
526 /// Base value.
527 base: f64,
528 /// Modifiers `(uuid, amount, operation)` in packet order.
529 modifiers: Vec<(Uuid, f64, i8)>,
530 },
531 /// A status effect was applied to an entity.
532 Effect {
533 /// The entity.
534 entity_id: i32,
535 /// Effect id.
536 id: i8,
537 /// Amplifier (level − 1).
538 amplifier: i8,
539 },
540 /// A status effect ended.
541 EffectRemoved {
542 /// The entity.
543 entity_id: i32,
544 /// Effect id.
545 id: i8,
546 },
547 /// Entity status (e.g. 9 = our item use finished).
548 EntityStatus {
549 /// The entity.
550 entity_id: i32,
551 /// Status id.
552 status: i8,
553 },
554 /// An entity (the player, if it is our id) died (1.16 `combat_event` "entity dead").
555 Died {
556 /// The dead player's entity id.
557 entity_id: i32,
558 },
559 /// Movement attributes of an entity (1.20.5+ registry attributes; only the ones movement
560 /// reads are kept).
561 Attributes {
562 /// The entity.
563 entity_id: i32,
564 /// The attributes in packet order.
565 attributes: Vec<AttributeUpdate>,
566 },
567 /// The server's ping (1.17+): answered with a pong.
568 Ping(i32),
569 /// A chunk batch starts (1.20.2+).
570 ChunkBatchStart,
571 /// A chunk batch ended with this many chunks (1.20.2+): answered with the chunks per tick.
572 ChunkBatchFinished(i32),
573 /// The server moves the connection back to the configuration state (1.20.2+).
574 StartConfiguration,
575 /// A server resource pack (1.20.3+ ids).
576 ResourcePackPush {
577 /// Pack id.
578 id: Uuid,
579 /// The server requires it.
580 required: bool,
581 },
582 /// The server asks for a cookie (1.20.5+).
583 CookieRequest(ByteString),
584 /// The server sends the client to another server (1.20.5+ `transfer`): the session ends
585 /// with [`crate::SessionEnd::Transferred`] (followed with [`crate::Client::follow_transfers`]).
586 Transfer {
587 /// Target host.
588 host: String,
589 /// Target port.
590 port: u16,
591 },
592 /// The server stores a cookie (1.20.5+).
593 StoreCookie {
594 /// Key.
595 key: ByteString,
596 /// Value.
597 value: Bytes,
598 },
599 /// A rotation-only server turn (1.21.2+ `player_rotation`).
600 Rotate {
601 /// Yaw (added when `relative_yaw`).
602 yaw: f32,
603 /// Pitch (added when `relative_pitch`).
604 pitch: f32,
605 /// Yaw is relative.
606 relative_yaw: bool,
607 /// Pitch is relative.
608 relative_pitch: bool,
609 },
610 /// The server shows a dialog (26.1+ `show_dialog`, only with
611 /// [`crate::Interests::DIALOGS`]).
612 ShowDialog(std::sync::Arc<crate::dialog::Dialog>),
613 /// The server closes the open dialog (`clear_dialog`).
614 ClearDialog,
615}
616
617/// One entry of a 1.20.5+ attributes packet.
618#[derive(Debug, Clone, PartialEq)]
619#[non_exhaustive]
620pub struct AttributeUpdate {
621 /// Registry name without namespace (`movement_speed`, `step_height`, …).
622 pub name: &'static str,
623 /// Base value.
624 pub base: f64,
625 /// Modifiers `(id, amount, operation)` in packet order.
626 pub modifiers: Vec<(ByteString, f64, i8)>,
627}
628
629/// Sneak / sprint `entity_action` values.
630#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
631#[non_exhaustive]
632pub enum EntityAction {
633 /// Start sneaking.
634 StartSneaking,
635 /// Stop sneaking.
636 StopSneaking,
637 /// Start sprinting.
638 StartSprinting,
639 /// Stop sprinting.
640 StopSprinting,
641 /// Start gliding with an elytra (1.9+ `START_FALL_FLYING`; ghast sends it on 26.1).
642 StartFallFlying,
643}
644
645/// The one movement packet of a tick.
646///
647/// `horizontal_collision` is sent from 1.21.2 on (always `false` for older versions).
648#[derive(Debug, Clone, Copy, PartialEq)]
649#[non_exhaustive]
650pub enum MovePacket {
651 /// `flying` (C03): only on-ground.
652 Flying {
653 /// On ground.
654 on_ground: bool,
655 /// Horizontal collision (1.21.2+).
656 horizontal_collision: bool,
657 },
658 /// `position` (C04).
659 Position {
660 /// Feet position.
661 pos: Vec3,
662 /// On ground.
663 on_ground: bool,
664 /// Horizontal collision (1.21.2+).
665 horizontal_collision: bool,
666 },
667 /// `look` (C05).
668 Look {
669 /// Yaw.
670 yaw: f32,
671 /// Pitch.
672 pitch: f32,
673 /// On ground.
674 on_ground: bool,
675 /// Horizontal collision (1.21.2+).
676 horizontal_collision: bool,
677 },
678 /// `position_look` (C06).
679 PositionLook {
680 /// Feet position.
681 pos: Vec3,
682 /// Yaw.
683 yaw: f32,
684 /// Pitch.
685 pitch: f32,
686 /// On ground.
687 on_ground: bool,
688 /// Horizontal collision (1.21.2+).
689 horizontal_collision: bool,
690 },
691}
692
693impl MovePacket {
694 /// The 1.8 trace name (`C03` … `C06`).
695 #[must_use]
696 pub fn trace_id(&self) -> &'static str {
697 match self {
698 Self::Flying { .. } => "C03",
699 Self::Position { .. } => "C04",
700 Self::Look { .. } => "C05",
701 Self::PositionLook { .. } => "C06",
702 }
703 }
704}
705
706/// Client settings (`settings` packet), sent right after join game like vanilla.
707#[derive(Debug, Clone, PartialEq)]
708#[non_exhaustive]
709pub struct ClientSettings {
710 /// Locale, e.g. `en_US` (1.8) / `en_us` (1.16).
711 pub locale: String,
712 /// View distance in chunks.
713 pub view_distance: i8,
714 /// Chat visibility: 0 full, 1 commands only, 2 hidden.
715 pub chat_mode: i8,
716 /// Chat colours.
717 pub chat_colors: bool,
718 /// Displayed skin parts (bit mask; 0x7f = all).
719 pub skin_parts: u8,
720 /// Main hand (1.9+): 0 left, 1 right.
721 pub main_hand: i32,
722}
723
724impl ClientSettings {
725 /// The locale, e.g. `en_US` (1.8) / `en_us` (1.16).
726 #[must_use]
727 pub fn with_locale(mut self, locale: impl Into<String>) -> Self {
728 self.locale = locale.into();
729 self
730 }
731
732 /// The view distance in chunks.
733 #[must_use]
734 pub const fn with_view_distance(mut self, chunks: i8) -> Self {
735 self.view_distance = chunks;
736 self
737 }
738
739 /// Chat visibility: 0 full, 1 commands only, 2 hidden.
740 #[must_use]
741 pub const fn with_chat_mode(mut self, mode: i8) -> Self {
742 self.chat_mode = mode;
743 self
744 }
745
746 /// Chat colours.
747 #[must_use]
748 pub const fn with_chat_colors(mut self, on: bool) -> Self {
749 self.chat_colors = on;
750 self
751 }
752
753 /// Displayed skin parts (bit mask; 0x7f = all).
754 #[must_use]
755 pub const fn with_skin_parts(mut self, mask: u8) -> Self {
756 self.skin_parts = mask;
757 self
758 }
759
760 /// Main hand (1.9+): 0 left, 1 right.
761 #[must_use]
762 pub const fn with_main_hand(mut self, hand: i32) -> Self {
763 self.main_hand = hand;
764 self
765 }
766}
767
768impl Default for ClientSettings {
769 fn default() -> Self {
770 ClientSettings {
771 locale: "en_US".to_owned(),
772 view_distance: 8,
773 chat_mode: 0,
774 chat_colors: true,
775 skin_parts: 0x7f,
776 main_hand: 1,
777 }
778 }
779}
780
781/// A block face, as in the dig / place packets.
782#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
783pub enum Face {
784 /// −Y (0).
785 Down,
786 /// +Y (1).
787 Up,
788 /// −Z (2).
789 North,
790 /// +Z (3).
791 South,
792 /// −X (4).
793 West,
794 /// +X (5).
795 East,
796}
797
798impl Face {
799 /// The wire value 0 … 5.
800 #[must_use]
801 pub const fn id(self) -> i8 {
802 match self {
803 Face::Down => 0,
804 Face::Up => 1,
805 Face::North => 2,
806 Face::South => 3,
807 Face::West => 4,
808 Face::East => 5,
809 }
810 }
811
812 /// From the wire value.
813 #[must_use]
814 pub const fn from_id(id: i8) -> Option<Face> {
815 Some(match id {
816 0 => Face::Down,
817 1 => Face::Up,
818 2 => Face::North,
819 3 => Face::South,
820 4 => Face::West,
821 5 => Face::East,
822 _ => return None,
823 })
824 }
825
826 /// The neighbouring block in this direction.
827 #[must_use]
828 pub const fn offset(self, p: ghast_protocol::BlockPos) -> ghast_protocol::BlockPos {
829 let (x, y, z) = match self {
830 Face::Down => (0, -1, 0),
831 Face::Up => (0, 1, 0),
832 Face::North => (0, 0, -1),
833 Face::South => (0, 0, 1),
834 Face::West => (-1, 0, 0),
835 Face::East => (1, 0, 0),
836 };
837 ghast_protocol::BlockPos::new(p.x + x, p.y + y, p.z + z)
838 }
839}
840
841/// The `use_entity` action.
842#[derive(Debug, Clone, Copy, PartialEq)]
843#[non_exhaustive]
844pub enum EntityUse {
845 /// Right click (0).
846 Interact,
847 /// Left click (1).
848 Attack,
849 /// Right click at a point relative to the entity position (2).
850 InteractAt(Vec3),
851}
852
853/// `block_dig` status values used by the client.
854#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
855#[non_exhaustive]
856pub enum DigStatus {
857 /// Start destroying a block (0).
858 Start,
859 /// Abort destroying (1).
860 Abort,
861 /// Finish destroying (2).
862 Finish,
863 /// Release the used item: stop eating / blocking / drawing (5).
864 ReleaseUseItem,
865}
866
867impl DigStatus {
868 /// The wire value.
869 #[must_use]
870 pub const fn id(self) -> i32 {
871 match self {
872 DigStatus::Start => 0,
873 DigStatus::Abort => 1,
874 DigStatus::Finish => 2,
875 DigStatus::ReleaseUseItem => 5,
876 }
877 }
878}
879
880/// The vehicle the player rides ([`crate::TickContext::vehicle`]).
881#[derive(Debug, Clone, Copy, PartialEq)]
882#[non_exhaustive]
883pub struct Vehicle {
884 /// Its entity id.
885 pub entity_id: i32,
886 /// Position after this tick (the client's, for a vehicle it controls).
887 pub pos: Vec3,
888 /// Yaw.
889 pub yaw: f32,
890 /// Pitch.
891 pub pitch: f32,
892 /// On the ground.
893 pub on_ground: bool,
894 /// The client moves it (a boat) rather than the server (a minecart).
895 pub controlled: bool,
896}
897
898/// Serverbound play packets the core writes, common to every version.
899#[derive(Debug, Clone, PartialEq)]
900#[non_exhaustive]
901pub enum Outbound {
902 /// Keep-alive answer.
903 KeepAlive(i64),
904 /// Teleport confirm (1.9+; never produced for 1.8).
905 TeleportConfirm(i32),
906 /// The movement packet.
907 Move(MovePacket),
908 /// Sneak / sprint change.
909 EntityAction {
910 /// Our entity id.
911 entity_id: i32,
912 /// The action.
913 action: EntityAction,
914 },
915 /// Chat line or command (already truncated to the version's limit).
916 Chat(String),
917 /// Respawn request (client command "perform respawn").
918 Respawn,
919 /// Window confirm transaction answer.
920 Transaction {
921 /// Window id.
922 window_id: i8,
923 /// Action number.
924 action: i16,
925 /// Accepted.
926 accepted: bool,
927 },
928 /// Abilities (sent when the client toggles flight).
929 Abilities {
930 /// Flags (1 invulnerable, 2 flying, 4 may fly, 8 creative).
931 flags: i8,
932 /// Flying speed.
933 flying_speed: f32,
934 /// Walking speed.
935 walking_speed: f32,
936 },
937 /// Select a hotbar slot (0 … 8).
938 HeldSlot(i16),
939 /// Arm swing (main hand).
940 Swing,
941 /// `block_dig`.
942 Dig {
943 /// Status.
944 status: DigStatus,
945 /// Block.
946 pos: ghast_protocol::BlockPos,
947 /// Face.
948 face: Face,
949 },
950 /// `block_place` on a block face (1.8 with the held stack; 1.16 main hand).
951 Place {
952 /// The block clicked.
953 pos: ghast_protocol::BlockPos,
954 /// The face clicked.
955 face: Face,
956 /// Hit point relative to the block (floats, 0 … 1).
957 cursor: Vec3,
958 /// The held stack (1.8 sends it; vanilla servers / Grim compare it with the hand).
959 held: Option<ghast_inventory::Stack>,
960 /// 1.9+: the off hand.
961 off_hand: bool,
962 },
963 /// Use the held item (1.8 `block_place` at −1/−1/−1 face 255 with the held stack; 1.16
964 /// `use_item`).
965 UseItem {
966 /// The held stack.
967 held: Option<ghast_inventory::Stack>,
968 /// 1.9+: the off hand.
969 off_hand: bool,
970 },
971 /// 1.9+: off-hand arm swing.
972 SwingOffHand,
973 /// `use_entity`: attack or interact with an entity.
974 UseEntity {
975 /// The entity.
976 target: i32,
977 /// What to do.
978 action: EntityUse,
979 /// 1.9+: the off hand (interactions).
980 off_hand: bool,
981 /// 1.16: the sneak key is held.
982 sneaking: bool,
983 },
984 /// Client settings.
985 Settings(ClientSettings),
986 /// A packet of the inventory (window click, confirm answer, close, open inventory).
987 Inventory(ghast_inventory::Outgoing),
988 /// The client brand plugin message (`MC|Brand` on 1.8, `minecraft:brand` on 1.13+).
989 Brand(String),
990 /// Held movement keys (1.21.2+ `player_input`; sneaking is sent this way).
991 PlayerInput(Controls),
992 /// End of the client tick (1.21.2+ `tick_end`, every tick).
993 TickEnd,
994 /// The client finished loading the level (1.21.4+ `player_loaded`).
995 PlayerLoaded,
996 /// Announce the chat session the wire layer holds (1.19.3+ `chat_session_update`; the
997 /// keys never pass through the core).
998 ChatSessionUpdate,
999 /// Chunk batch acknowledgement with the desired chunks per tick (1.20.2+).
1000 ChunkBatchReceived(f32),
1001 /// Pong for a server ping (1.17+).
1002 Pong(i32),
1003 /// Resource pack status (1.20.3+): 0 loaded, 1 declined, 3 accepted, 4 downloaded, …
1004 ResourcePackStatus {
1005 /// Pack id.
1006 id: Uuid,
1007 /// Status.
1008 result: i32,
1009 },
1010 /// Cookie answer (1.20.5+): the stored value, if any.
1011 CookieResponse {
1012 /// Key.
1013 key: ByteString,
1014 /// Value.
1015 value: Option<Bytes>,
1016 },
1017 /// Rotation-only movement packet, as the answer to a server turn (1.21.2+).
1018 LookAnswer {
1019 /// Yaw.
1020 yaw: f32,
1021 /// Pitch.
1022 pitch: f32,
1023 },
1024 /// `move_vehicle` (serverbound): where the vehicle the player controls is (26.1 boats).
1025 VehicleMove {
1026 /// Position.
1027 pos: Vec3,
1028 /// Yaw.
1029 yaw: f32,
1030 /// Pitch.
1031 pitch: f32,
1032 /// The vehicle's on-ground flag.
1033 on_ground: bool,
1034 },
1035 /// `paddle_boat` / `steer_boat`: the boat's paddle states (26.1, every boat tick).
1036 SteerBoat {
1037 /// Left paddle.
1038 left: bool,
1039 /// Right paddle.
1040 right: bool,
1041 },
1042 /// `custom_click_action` (26.1+): a dialog button's custom action, with its payload tag
1043 /// (written as vanilla does: the tag length-prefixed, `TAG_End` for none).
1044 CustomClickAction {
1045 /// Action id.
1046 id: String,
1047 /// Payload tag.
1048 payload: Option<ghast_protocol::nbt::AnonNbt>,
1049 },
1050 /// A dialog's command as vanilla sends it (`sendUnattendedCommand` after its checks): an
1051 /// unsigned `chat_command` (without the `/`).
1052 DialogCommand(String),
1053 /// A packet the behaviour built itself ([`crate::TickContext::send_raw`]), written as given.
1054 Raw(ghast_protocol::RawPacket),
1055}