Skip to main content

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;
41pub mod packed;
42#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
43pub mod paletted;
44mod section;
45mod shared;
46
47#[cfg(feature = "v1_12")]
48pub mod v1_12;
49#[cfg(feature = "v1_16_5")]
50pub mod v1_16_5;
51#[cfg(feature = "v1_8")]
52pub mod v1_8;
53/// The modern world source (`src/v26_1.rs`) compiled for one protocol: its `data` and `proto`
54/// name that protocol's `ghast-data` and `ghast-protocol` modules.
55#[cfg(feature = "v26_1")]
56#[path = "."]
57mod modern_775 {
58    pub(crate) use ghast_data::v26_1 as data;
59    pub(crate) use ghast_protocol::v26_1 as proto;
60    /// Protocol number (keeps the sections of different block-id tables apart in a store).
61    pub(crate) const PROTOCOL: u16 = 775;
62    #[path = "v26_1.rs"]
63    pub mod world;
64}
65#[cfg(feature = "v26_1")]
66#[doc(inline)]
67pub use modern_775::world as v26_1;
68
69/// The same world source for protocol 776 (26.2): nothing in the world changed between 26.1
70/// and 26.2 except the block-state ids, which come from `ghast_data::v26_2`.
71#[cfg(feature = "v26_2")]
72#[path = "."]
73mod modern_776 {
74    pub(crate) use ghast_data::v26_2 as data;
75    pub(crate) use ghast_protocol::v26_2 as proto;
76    /// Protocol number (keeps the sections of different block-id tables apart in a store).
77    pub(crate) const PROTOCOL: u16 = 776;
78    #[allow(clippy::duplicate_mod)]
79    #[path = "v26_1.rs"]
80    pub mod world;
81}
82#[cfg(feature = "v26_2")]
83#[doc(inline)]
84pub use modern_776::world as v26_2;
85
86/// The same world source for protocol 777 (26.3). Chunk data, block updates and block
87/// entities are byte-for-byte the 26.2 layout; what changed on the wire that the world sees is
88/// read by the generated `ghast_protocol::v26_3` types (light masks as byte arrays, which the
89/// world skips; the spawn info's game modes as VarInts, which the world does not read). The
90/// block-state ids come from `ghast_data::v26_3`.
91#[cfg(feature = "v26_3")]
92#[path = "."]
93mod modern_777 {
94    pub(crate) use ghast_data::v26_3 as data;
95    pub(crate) use ghast_protocol::v26_3 as proto;
96    /// Protocol number (keeps the sections of different block-id tables apart in a store).
97    pub(crate) const PROTOCOL: u16 = 777;
98    #[allow(clippy::duplicate_mod)]
99    #[path = "v26_1.rs"]
100    pub mod world;
101}
102#[cfg(feature = "v26_3")]
103#[doc(inline)]
104pub use modern_777::world as v26_3;
105
106pub use border::Border;
107pub use column::{
108    Biomes, BlockEntity, ChunkPos, Column, ColumnMap, MAX_Y, MIN_Y, SECTIONS, SectionLight,
109};
110pub use error::WorldError;
111pub use section::{Direct, Indexed, SECTION_VOLUME, Section, block_index};
112pub use shared::SharedChunks;
113
114/// What a world keeps besides block states.
115///
116/// `#[non_exhaustive]`: build it from [`WorldConfig::default`] with the `with_*` methods, so new
117/// options can be added without breaking callers:
118///
119/// ```
120/// let c = ghast_world::WorldConfig::default().with_biomes(false).with_block_entities(true);
121/// assert!(!c.biomes && c.block_entities && !c.light);
122/// ```
123#[derive(Clone, Copy, Debug, PartialEq, Eq)]
124#[non_exhaustive]
125pub struct WorldConfig {
126    /// Keep biome ids (1.8: 256 bytes, 1.16.5: 2 KiB per column). Default `true`.
127    pub biomes: bool,
128    /// Keep the 1.8 block / sky light arrays sent with chunks (4 KiB per section, never
129    /// recomputed locally). Default `false`: the physics does not need light.
130    pub light: bool,
131    /// Keep block entities (1.8 `update_sign` / `tile_entity_data`, 1.16.5 chunk block entity
132    /// lists and `tile_entity_data`), see [`Column::block_entities`]. Default `false`: off costs
133    /// nothing (the packets are not in the world's packet ids, nothing is parsed).
134    pub block_entities: bool,
135}
136
137impl WorldConfig {
138    /// Sets [`WorldConfig::biomes`].
139    #[must_use]
140    pub const fn with_biomes(mut self, on: bool) -> Self {
141        self.biomes = on;
142        self
143    }
144
145    /// Sets [`WorldConfig::light`].
146    #[must_use]
147    pub const fn with_light(mut self, on: bool) -> Self {
148        self.light = on;
149        self
150    }
151
152    /// Sets [`WorldConfig::block_entities`].
153    #[must_use]
154    pub const fn with_block_entities(mut self, on: bool) -> Self {
155        self.block_entities = on;
156        self
157    }
158}
159
160impl Default for WorldConfig {
161    fn default() -> Self {
162        WorldConfig {
163            biomes: true,
164            light: false,
165            block_entities: false,
166        }
167    }
168}
169
170/// What applying a packet did.
171#[derive(Clone, Copy, Debug, PartialEq, Eq)]
172#[non_exhaustive]
173pub enum Applied {
174    /// The world changed.
175    Changed,
176    /// The packet was valid but vanilla ignores it in this situation (e.g. a partial chunk or a
177    /// block change for a column that is not loaded).
178    Ignored,
179    /// The packet is not one the world handles.
180    NotWorldPacket,
181}