ghast-client 0.5.6

Headless Minecraft Java Edition client for the ghast library: connection, login, fixed 50 ms tick loop and a version-agnostic bot API (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3)
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
//! The cross-layer version row: what a protocol version can do, as facts the version-agnostic
//! core asks for instead of comparing version numbers.
//!
//! One constant row per [`GameVersion`]. Adding a version is adding a row (plus whatever is
//! really new); the core never names a version. Every field is a plain fact of the vanilla
//! client of that version.

use super::GameVersion;

/// The physics rule set a version runs on (`ghast-physics` module).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PhysicsRules {
    /// `ghast_physics::v1_8` (1.8 – 1.8.9).
    V1_8,
    /// `ghast_physics::v1_12` (1.12.1, 1.12.2).
    V1_12,
    /// `ghast_physics::v1_16_5` (1.16.2 – 1.16.5).
    V1_16_5,
    /// `ghast_physics::modern` with the `V26_1` rules (26.1 – 26.1.2).
    Modern26_1,
    /// `ghast_physics::modern` with the `V26_2` rules (26.2: collision restitution).
    Modern26_2,
    /// `ghast_physics::modern` with the `V26_3` rules (26.3: movement packets after the
    /// level's entities and block entities ticked, the teleport answer with the pose).
    Modern26_3,
}

/// When a change of the stack in the used hand ends an item use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum UseStop {
    /// At once when the bot selects another slot (or the server does).
    AtSelect,
    /// In the player tick after the key handling, when the hand's stack object was replaced:
    /// another hotbar slot, or any server write of that slot (`set_slot`, `window_items`), even
    /// of an equal stack (1.8 `PlayerEntity.tick`, 1.12 `updateActiveHand`: identity).
    HeldReplaced,
    /// In the player tick after the key handling, when the hand holds another item (1.16.5
    /// `tickActiveItemStack`: `ItemStack.areItemsEqual`); a slot change to the same item or a
    /// resync keeps the use.
    HeldItemChanged,
}

/// When a change of the held item resets the 1.9+ attack cooldown.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CooldownReset {
    /// At once when the bot selects another slot.
    AtSelect,
    /// In the player tick after the key handling (`PlayerEntity.tick`, after the cooldown
    /// count), when the held stack is another item, or the same item without durability with
    /// another damage value (1.12 `ItemStack.equals`); an attack in the tick of a slot change
    /// still has the old strength.
    ItemAndDamage,
    /// As [`CooldownReset::ItemAndDamage`] comparing the item only.
    Item,
}

/// Item cooldowns of a version (`ItemCooldownManager`, 1.9+; see [`crate::cooldown`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ItemCooldownRules {
    /// None (1.8).
    None,
    /// Per item (1.9 – 1.21.1): the server's `set_cooldown` names an item; the client starts
    /// the ender pearl's own 20 ticks.
    PerItem,
    /// Per cooldown group (1.21.2+): the server's `set_cooldown` names a group; the client
    /// starts the `use_cooldown` component's cooldown after a use.
    Groups,
}

/// What an invulnerable player (creative, spectator) may eat: `PlayerEntity.canConsume`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum InvulnerableEating {
    /// Nothing (1.8, 1.12: `!abilities.invulnerable` is part of the test).
    Never,
    /// Anything, full or not (1.14+ and 26.x: `abilities.invulnerable || …`).
    Always,
    /// As any player (the hunger rule only; no version uses it any more).
    Hunger,
}

/// The data tables of a version (`ghast-data` module).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DataSet {
    /// `ghast_data::v1_8`: numeric block / item ids with metadata.
    V1_8,
    /// `ghast_data::v1_12`: numeric block / item ids with metadata.
    V1_12,
    /// `ghast_data::v1_16_5`: flattened block states and item ids.
    V1_16_5,
    /// `ghast_data::v26_1`: flattened block states and item ids.
    V26_1,
    /// `ghast_data::v26_2`: flattened block states and item ids.
    V26_2,
    /// `ghast_data::v26_3`: flattened block states and item ids.
    V26_3,
}

/// How item stacks carry their extra data on the wire.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ItemModel {
    /// Numeric id, damage value and an optional NBT compound (1.8 – 1.12).
    LegacyNbt,
    /// Flattened id and an optional NBT compound (1.13 – 1.20.4).
    FlatNbt,
    /// Flattened id and data components (1.20.5+).
    Components,
}

/// What a protocol version can do: the facts the client core asks for.
///
/// Read one with [`GameVersion::caps`].
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Capabilities {
    /// The version this row describes.
    pub version: GameVersion,
    /// Protocol number.
    pub protocol: i32,
    /// Physics rule set.
    pub physics: PhysicsRules,
    /// Data tables.
    pub data: DataSet,
    /// How item stacks carry extra data.
    pub item_model: ItemModel,
    /// 1.9 combat: attack cooldown, swing sent after the click packet, the off hand.
    pub combat_1_9: bool,
    /// The crosshair pick skips entities with the player's root vehicle (1.9+), not only the
    /// vehicle the player sits on (1.8).
    pub entity_pick_root_vehicle: bool,
    /// Eye height above the feet while standing.
    pub eye_height: f64,
    /// Eye height while sneaking (1.54 in 1.8 – 1.12, the crouching pose's 1.27 since 1.14).
    pub sneak_eye_height: f64,
    /// Longest chat line the server accepts, in UTF-16 units.
    pub chat_limit: usize,
    /// Game event 11 ("immediate respawn" game rule) exists (1.15+).
    pub immediate_respawn_rule: bool,
    /// The client uses a seed item at once when planting on its soil (the 1.8 `SeedItem.use`
    /// prediction; later versions wait for the server's slot update).
    pub seed_planting_prediction: bool,
    /// Server teleports carry an id the client confirms (1.9+).
    pub teleport_confirm: bool,
    /// Window clicks are confirmed with transactions (1.8 – 1.16).
    pub window_confirm: bool,
    /// A configuration state sits between login and play (1.20.2+).
    pub configuration_state: bool,
    /// Sneaking is sent as `player_input` key state, not as an entity action (1.21.2+).
    pub sneak_via_player_input: bool,
    /// The client sends `tick_end` after every tick (1.21.2+).
    pub sends_tick_end: bool,
    /// The player does not tick until the client sends `player_loaded` (1.21.4+).
    pub player_loaded_gate: bool,
    /// Chunk batches are acknowledged with the desired chunks per tick (1.20.2+).
    pub chunk_batch_ack: bool,
    /// Dig / place / use packets carry a sequence number the server acknowledges (1.19+).
    pub block_sequence: bool,
    /// Chat packets carry a timestamp, salt and message acknowledgements (1.19.3+).
    pub chat_acknowledgements: bool,
    /// The drop key takes the item off the held stack on the client at once and then swings
    /// the main arm when it took one (1.16.5 `ClientPlayerEntity.dropSelectedItem` and the
    /// key handler; 26.1 `LocalPlayer.drop`); 1.8.9 / 1.12.2 only send the packet and wait for
    /// the server's slot update.
    pub drop_prediction: bool,
    /// The client itself pushes the player away from other entities in its entity tick
    /// (26.1+; 1.12.2: `LivingEntity.tickCramming`, other players and boats push the local
    /// player on the client too; 1.16.5: `LivingEntity.tickCramming`,
    /// `OtherClientPlayerEntity.tickMovement` and `BoatEntity.tick`, in the client world's entity
    /// map order; 1.8.9 leaves it to the server). Judging those pushes needs
    /// each player entity's tab-list name (team collision rules) and game mode (spectators do
    /// not push), so entity tracking then also keeps a names-only tab list.
    pub client_entity_push: bool,
    /// A spectator's right click on a block (the block packet, then the click ends) swings the
    /// arm: 1.8 – 1.16 `interactBlock` returns true / SUCCESS for spectators; 26.1 / 26.2
    /// `MultiPlayerGameMode.performUseItemOn` returns CONSUME, which swings nothing.
    pub spectator_block_click_swings: bool,
    /// Switching to spectator ends an item use at once: 26.1 / 26.2
    /// `ServerPlayer.setGameMode(SPECTATOR)` calls `stopUsingItem` and the client's use ends
    /// with the living flags; 1.8 – 1.16.5 stop nothing (the use runs until release or finish).
    pub spectator_switch_stops_use: bool,
    /// The client's attack reads the held item's knockback enchantment level
    /// (`EnchantmentHelper.getKnockback`, 1.8 – 1.16.5): with knockback the attacker slows down
    /// on the client too. 26.1 / 26.2 `LivingEntity.getKnockback` runs the data-driven
    /// enchantment effects only on a `ServerLevel`: the client's knockback is the attribute
    /// alone (a sprint attack at full strength still slows).
    pub attack_knockback_enchantment: bool,
    /// Putting armor on with a right click swings the arm: 1.16.5 `ArmorItem.use` answers
    /// `success(…, isClient)` (SUCCESS swings) and 26.x `Equippable.swapWithEquipmentSlot`
    /// SUCCESS (a client swing); 1.12.2 `ArmorItem.use` SUCCESS swings nothing, 1.8
    /// `interactItem` neither.
    pub use_equip_swings: bool,
    /// The player's own inventory screen passes key presses through to the game while it is
    /// open (1.16.5 `InventoryScreen` / `CreativeInventoryScreen` set `passEvents`: GLFW key
    /// events the screen does not use reach the key bindings, and `handleInputEvents` runs):
    /// opening it releases every key (`KeyBinding.unpressAll`), a movement key pressed while
    /// it is open applies, closing it re-reads the held keys. 1.8.9 / 1.12.2 screens take
    /// every keyboard event (`handleInput` before `tickKeyboard`), 26.x not verified (off).
    pub own_inventory_passes_keys: bool,
    /// When a change of the used hand's stack ends an item use.
    pub use_stop: UseStop,
    /// Creative digging as `attackBlock` / `updateBlockBreakingProgress` (26.x
    /// `startDestroyBlock` / `continueDestroyBlock`) do it: a start packet and the block broken
    /// at once (not by a sword, 26.x: a tool that cannot destroy blocks in creative), then
    /// every 5 ticks while the button is held; nothing is aborted (oracle: 26.x
    /// `held_dig.jsonl.gz`). Off: the survival path.
    pub creative_instant_dig: bool,
    /// What an invulnerable (creative) player may eat (`canConsume`).
    pub invulnerable_eating: InvulnerableEating,
    /// Our living flags from the server drive the client's item use (1.9+
    /// `ClientPlayerEntity.onTrackedDataSet(LIVING_FLAGS)`): bit 1 set while not using starts a
    /// use of the stack in the hand of bit 2, bit 1 clear while using ends it (no packet).
    pub living_flags_drive_use: bool,
    /// When a held item change resets the 1.9+ attack cooldown.
    pub cooldown_reset: CooldownReset,
    /// Item cooldowns: none, per item or per cooldown group.
    pub item_cooldowns: ItemCooldownRules,
    /// The key handling's crosshair pick turns with the player's head yaw (1.8
    /// `LivingEntity.getRotationVector(1.0F)`: the head yaw the last tick's `tickNewAi` set, so
    /// the yaw before this tick's turn) and this tick's pitch; the movement packet carries the
    /// new yaw. 1.9+ picks with this tick's yaw.
    pub pick_head_yaw: bool,
    /// The pick block key as 1.8.9 / 1.12.2 / 1.16.5 `MinecraftClient.doPick` ends (1.8
    /// `PlayerInventory.addPickBlock` of an item and damage, 1.12 `method_13250` / 1.16.5
    /// `addPickBlock` of a stack) and, in creative mode, a `creative_inventory_action` for the
    /// selected slot (oracle `clicks_creative`); 1.12.2 / 1.16.5 also load saved toolbars.
    /// Off: not modelled (refused).
    pub legacy_pick_block: bool,
    /// The client sends its arm swings (`swing` / `arm_animation`); 26.3 removed the
    /// serverbound swing: an attack sends the payload-less `punch` instead and a used item
    /// only animates the hand (`LocalPlayer.itemUsed`).
    pub sends_swing: bool,
    /// 26.3 `ClientPacketListener.handleMovePlayer`: the teleport answer is one
    /// `accept_teleportation` carrying the resulting position and rotation (no
    /// position-rotation packet after it), and the teleport aborts a dig in progress
    /// (`MultiPlayerGameMode.stopDestroyBlock`).
    pub teleport_confirm_pose: bool,
    /// 26.3 `MultiPlayerGameMode.continueDestroyBlock`: while the same block is dug, a hit on
    /// another face sends `CHANGE_DESTROY_DIRECTION` (player action 1) with the new face; the
    /// later player-action ids are shifted by one.
    pub dig_change_direction: bool,
    /// 26.3 `MultiPlayerGameMode.attack`: an attack in creative sets the 5-tick dig delay, so a
    /// held button that reaches a block right after sends only swings until it ran out
    /// (oracle: 26.3 `held_dig.jsonl.gz`, `attack_mob_creative`).
    pub creative_attack_dig_delay: bool,
    /// The serverbound swing-free `punch` packet goes out where 1.9+ sends the swing after an
    /// attack or a missed click (26.3 `Minecraft.startAttack` / `LocalPlayer.swing` path).
    pub sends_punch: bool,
    /// The server can show boss bars (1.9+ `boss_bar`; [`crate::hud::Hud::boss_bars`]).
    pub boss_bars: bool,
    /// How the sign editor opens, closes and sends its lines ([`crate::sign`]).
    pub sign_editor: crate::sign::SignRules,
    /// What the book screen sends and changes in the held stack ([`crate::book`]).
    pub book: crate::book::BookRules,
    /// 1.13+ `LivingEntity.tick`: a sleeping player's pitch is set to 0 every tick.
    pub sleep_levels_pitch: bool,
}

const fn f(v: f32) -> f64 {
    v as f64
}

/// 1.12.1 (protocol 338): the 1.12.2 row with its own number (the same jar code apart from
/// the keep-alive width and GUI details; oracle records identical).
pub(crate) const V1_12_1: Capabilities = Capabilities {
    version: GameVersion::V1_12_1,
    protocol: 338,
    ..V1_12_2
};

/// 1.16.2 (protocol 751): the 1.16.5 row with its own number (client movement, block and
/// network code identical in the jars; oracle records identical).
pub(crate) const V1_16_2: Capabilities = Capabilities {
    version: GameVersion::V1_16_2,
    protocol: 751,
    ..V1_16_5
};

/// 1.16.3 (protocol 753): the 1.16.5 row with its own number, like [`V1_16_2`].
pub(crate) const V1_16_3: Capabilities = Capabilities {
    version: GameVersion::V1_16_3,
    protocol: 753,
    ..V1_16_5
};

/// 1.8 – 1.8.9 (protocol 47).
pub(crate) const V1_8_9: Capabilities = Capabilities {
    version: GameVersion::V1_8_9,
    protocol: 47,
    physics: PhysicsRules::V1_8,
    data: DataSet::V1_8,
    item_model: ItemModel::LegacyNbt,
    combat_1_9: false,
    entity_pick_root_vehicle: false,
    eye_height: f(1.62),
    // `1.62F - 0.08F` in float.
    sneak_eye_height: f(1.62 - 0.08),
    chat_limit: 100,
    own_inventory_passes_keys: false,
    immediate_respawn_rule: false,
    // the 1.8.9 right-click rules (`Sim::place_rules` / `place_sent`) predict planting now
    seed_planting_prediction: false,
    teleport_confirm: false,
    window_confirm: true,
    configuration_state: false,
    sneak_via_player_input: false,
    sends_tick_end: false,
    player_loaded_gate: false,
    chunk_batch_ack: false,
    block_sequence: false,
    chat_acknowledgements: false,
    client_entity_push: false,
    drop_prediction: false,
    spectator_block_click_swings: true,
    spectator_switch_stops_use: false,
    attack_knockback_enchantment: true,
    use_equip_swings: false,
    use_stop: UseStop::HeldReplaced,
    creative_instant_dig: true,
    invulnerable_eating: InvulnerableEating::Never,
    living_flags_drive_use: false,
    cooldown_reset: CooldownReset::AtSelect,
    item_cooldowns: ItemCooldownRules::None,
    pick_head_yaw: true,
    legacy_pick_block: true,
    sends_swing: true,
    teleport_confirm_pose: false,
    dig_change_direction: false,
    creative_attack_dig_delay: false,
    sends_punch: false,
    boss_bars: false,
    sign_editor: crate::sign::SignRules::Legacy { json: true },
    book: crate::book::BookRules::Legacy18,
    sleep_levels_pitch: false,
};

/// 1.12.2 (protocol 340).
pub(crate) const V1_12_2: Capabilities = Capabilities {
    version: GameVersion::V1_12_2,
    protocol: 340,
    physics: PhysicsRules::V1_12,
    data: DataSet::V1_12,
    combat_1_9: true,
    entity_pick_root_vehicle: true,
    chat_limit: 256,
    seed_planting_prediction: false,
    teleport_confirm: true,
    client_entity_push: true,
    living_flags_drive_use: true,
    cooldown_reset: CooldownReset::ItemAndDamage,
    item_cooldowns: ItemCooldownRules::PerItem,
    pick_head_yaw: false,
    boss_bars: true,
    sign_editor: crate::sign::SignRules::Legacy { json: false },
    book: crate::book::BookRules::Legacy112,
    ..V1_8_9
};

/// 1.16.4 – 1.16.5 (protocol 754).
pub(crate) const V1_16_5: Capabilities = Capabilities {
    version: GameVersion::V1_16_5,
    protocol: 754,
    own_inventory_passes_keys: true,
    physics: PhysicsRules::V1_16_5,
    data: DataSet::V1_16_5,
    item_model: ItemModel::FlatNbt,
    sneak_eye_height: f(1.27),
    immediate_respawn_rule: true,
    client_entity_push: true,
    drop_prediction: true,
    use_equip_swings: true,
    invulnerable_eating: InvulnerableEating::Always,
    use_stop: UseStop::HeldItemChanged,
    cooldown_reset: CooldownReset::Item,
    pick_head_yaw: false,
    sign_editor: crate::sign::SignRules::FirstTickCheck,
    book: crate::book::BookRules::Stack116,
    sleep_levels_pitch: true,
    ..V1_12_2
};

/// 26.1 – 26.1.2 (protocol 775).
pub(crate) const V26_1: Capabilities = Capabilities {
    version: GameVersion::V26_1,
    protocol: 775,
    own_inventory_passes_keys: false,
    physics: PhysicsRules::Modern26_1,
    data: DataSet::V26_1,
    item_model: ItemModel::Components,
    window_confirm: false,
    configuration_state: true,
    sneak_via_player_input: true,
    sends_tick_end: true,
    player_loaded_gate: true,
    chunk_batch_ack: true,
    block_sequence: true,
    chat_acknowledgements: true,
    client_entity_push: true,
    spectator_block_click_swings: false,
    spectator_switch_stops_use: true,
    attack_knockback_enchantment: false,
    creative_instant_dig: true,
    // Player.canEat: abilities.invulnerable || canAlwaysEat || needsFood (26.1 – 26.3).
    invulnerable_eating: InvulnerableEating::Always,
    living_flags_drive_use: false,
    cooldown_reset: CooldownReset::AtSelect,
    item_cooldowns: ItemCooldownRules::Groups,
    pick_head_yaw: false,
    use_stop: UseStop::AtSelect,
    // 26.x picks with `pick_item_from_block` / `_entity` (not modelled)
    legacy_pick_block: false,
    sign_editor: crate::sign::SignRules::Sided,
    book: crate::book::BookRules::Pages,
    ..V1_16_5
};

/// 26.2 (protocol 776): the 26.1 row with its own physics rules and data.
pub(crate) const V26_2: Capabilities = Capabilities {
    version: GameVersion::V26_2,
    protocol: 776,
    physics: PhysicsRules::Modern26_2,
    data: DataSet::V26_2,
    ..V26_1
};

/// 26.3 (protocol 777): the 26.2 row with its own physics rules and data, no serverbound
/// swing, the teleport answer with the pose and the dig direction changes.
pub(crate) const V26_3: Capabilities = Capabilities {
    version: GameVersion::V26_3,
    protocol: 777,
    physics: PhysicsRules::Modern26_3,
    data: DataSet::V26_3,
    sends_swing: false,
    teleport_confirm_pose: true,
    dig_change_direction: true,
    creative_attack_dig_delay: true,
    sends_punch: true,
    ..V26_2
};