ghast_world/lib.rs
1//! Chunk storage for the ghast Minecraft Java Edition client library.
2//!
3//! One world type per protocol version ([`v1_8::World`], [`v1_12::World`], [`v1_16_5::World`],
4//! [`v26_1::World`], [`v26_2::World`], `v26_3::World`, each behind the feature of its
5//! module's name) decodes the version's chunk and block-change packets into a version-independent
6//! [`ColumnMap`] of [`Column`]s made of compact [`Section`]s.
7//!
8//! - **Loaded vs unloaded is explicit.** [`v1_8::World::block_state`] returns `None` for a
9//! column the server has not sent (or has unloaded); it never reports air there. Behaviour code
10//! must treat `None` as "unknown". Only the `ghast_physics::BlockView` implementation (feature
11//! `physics`) maps unloaded columns to air, because that is exactly what the vanilla client's
12//! empty chunk does and what the physics must reproduce.
13//! - **Vanilla decoding.** States are stored as vanilla reads them: unknown 1.8 `id << 4 | meta`
14//! values and unregistered metas, unknown 1.16.5 palette ids and out-of-range palette indices
15//! all read as air. Packet handling follows the vanilla client (ground-up chunks replace the
16//! column, partial chunks for unloaded columns are ignored, block changes in unloaded columns
17//! are ignored, respawn into the same dimension keeps the world, ...). See each method.
18//! - **Cheap lookups.** A block lookup is one hash of the column position plus one or two array
19//! reads; all-air sections are not stored.
20//! - **Never panics on server data.** Malformed chunk data returns a [`WorldError`]. A 1.8 chunk
21//! shorter than its mask is applied as far as the vanilla client gets before its copy fails
22//! (a ground-up chunk still loads its column, as vanilla does); a malformed 1.16 section
23//! unloads the column.
24//!
25//! ```
26//! # #[cfg(feature = "v1_8")] {
27//! use ghast_world::{WorldConfig, v1_8::World};
28//! let mut world = World::new(WorldConfig::default());
29//! assert_eq!(world.block_state(0, 64, 0), None); // nothing loaded yet: unknown, not air
30//! # }
31//! ```
32
33#![forbid(unsafe_code)]
34#![warn(missing_docs, unnameable_types)]
35
36mod border;
37#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
38pub mod climate;
39mod column;
40mod error;
41mod helpers;
42pub mod packed;
43#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
44pub mod paletted;
45mod section;
46mod shared;
47
48#[cfg(feature = "v1_12")]
49pub mod v1_12;
50#[cfg(feature = "v1_16_5")]
51pub mod v1_16_5;
52#[cfg(feature = "v1_8")]
53pub mod v1_8;
54/// The modern world source (`src/v26_1/`) compiled for one protocol: its `data` and `proto`
55/// name that protocol's `ghast-data` and `ghast-protocol` modules.
56#[cfg(feature = "v26_1")]
57#[path = "."]
58mod modern_775 {
59 pub(crate) use ghast_data::v26_1 as data;
60 pub(crate) use ghast_protocol::v26_1 as proto;
61 /// Protocol number (keeps the sections of different block-id tables apart in a store).
62 pub(crate) const PROTOCOL: u16 = 775;
63 #[path = "v26_1/mod.rs"]
64 pub mod world;
65}
66#[cfg(feature = "v26_1")]
67#[doc(inline)]
68pub use modern_775::world as v26_1;
69
70/// The same world source for protocol 776 (26.2): nothing in the world changed between 26.1
71/// and 26.2 except the block-state ids, which come from `ghast_data::v26_2`.
72#[cfg(feature = "v26_2")]
73#[path = "."]
74mod modern_776 {
75 pub(crate) use ghast_data::v26_2 as data;
76 pub(crate) use ghast_protocol::v26_2 as proto;
77 /// Protocol number (keeps the sections of different block-id tables apart in a store).
78 pub(crate) const PROTOCOL: u16 = 776;
79 #[allow(clippy::duplicate_mod)]
80 #[path = "v26_1/mod.rs"]
81 pub mod world;
82}
83#[cfg(feature = "v26_2")]
84#[doc(inline)]
85pub use modern_776::world as v26_2;
86
87/// The same world source for protocol 777 (26.3). Chunk data, block updates and block
88/// entities are byte-for-byte the 26.2 layout; what changed on the wire that the world sees is
89/// read by the generated `ghast_protocol::v26_3` types (light masks as byte arrays, which the
90/// world skips; the spawn info's game modes as VarInts, which the world does not read). The
91/// block-state ids come from `ghast_data::v26_3`.
92#[cfg(feature = "v26_3")]
93#[path = "."]
94mod modern_777 {
95 pub(crate) use ghast_data::v26_3 as data;
96 pub(crate) use ghast_protocol::v26_3 as proto;
97 /// Protocol number (keeps the sections of different block-id tables apart in a store).
98 pub(crate) const PROTOCOL: u16 = 777;
99 #[allow(clippy::duplicate_mod)]
100 #[path = "v26_1/mod.rs"]
101 pub mod world;
102}
103#[cfg(feature = "v26_3")]
104#[doc(inline)]
105pub use modern_777::world as v26_3;
106
107pub use border::Border;
108pub use column::{
109 Biomes, BlockEntity, ChunkPos, Column, ColumnMap, MAX_Y, MIN_Y, SECTIONS, SectionLight,
110};
111pub use error::WorldError;
112pub use section::{Direct, Indexed, SECTION_VOLUME, Section, block_index};
113pub use shared::SharedChunks;
114
115/// What a world keeps besides block states.
116///
117/// `#[non_exhaustive]`: build it from [`WorldConfig::default`] with the `with_*` methods, so new
118/// options can be added without breaking callers:
119///
120/// ```
121/// let c = ghast_world::WorldConfig::default().with_biomes(false).with_block_entities(true);
122/// assert!(!c.biomes && c.block_entities && !c.light);
123/// ```
124#[derive(Clone, Copy, Debug, PartialEq, Eq)]
125#[non_exhaustive]
126pub struct WorldConfig {
127 /// Keep biome ids (1.8: 256 bytes, 1.16.5: 2 KiB per column). Default `true`.
128 pub biomes: bool,
129 /// Keep the 1.8 block / sky light arrays sent with chunks (4 KiB per section, never
130 /// recomputed locally). Default `false`: the physics does not need light.
131 pub light: bool,
132 /// Keep block entities (1.8 `update_sign` / `tile_entity_data`, 1.16.5 chunk block entity
133 /// lists and `tile_entity_data`), see [`Column::block_entities`]. Default `false`: off costs
134 /// nothing (the packets are not in the world's packet ids, nothing is parsed).
135 pub block_entities: bool,
136}
137
138impl WorldConfig {
139 /// Sets [`WorldConfig::biomes`].
140 #[must_use]
141 pub const fn with_biomes(mut self, on: bool) -> Self {
142 self.biomes = on;
143 self
144 }
145
146 /// Sets [`WorldConfig::light`].
147 #[must_use]
148 pub const fn with_light(mut self, on: bool) -> Self {
149 self.light = on;
150 self
151 }
152
153 /// Sets [`WorldConfig::block_entities`].
154 #[must_use]
155 pub const fn with_block_entities(mut self, on: bool) -> Self {
156 self.block_entities = on;
157 self
158 }
159}
160
161impl Default for WorldConfig {
162 fn default() -> Self {
163 WorldConfig {
164 biomes: true,
165 light: false,
166 block_entities: false,
167 }
168 }
169}
170
171/// What applying a packet did.
172#[derive(Clone, Copy, Debug, PartialEq, Eq)]
173#[non_exhaustive]
174pub enum Applied {
175 /// The world changed.
176 Changed,
177 /// The packet was valid but vanilla ignores it in this situation (e.g. a partial chunk or a
178 /// block change for a column that is not loaded).
179 Ignored,
180 /// The packet is not one the world handles.
181 NotWorldPacket,
182}