Skip to main content

yog_world/
lib.rs

1//! World access for Yog mods.
2//!
3//! A thin, ergonomic wrapper over the block primitives on [`yog_core::Server`]:
4//! bind a [`World`] to a dimension once, then `get_block` / `set_block` by
5//! position. World-domain events will live here too as they land.
6
7use yog_core::{BlockPos, Server};
8
9/// Well-known vanilla dimension ids.
10pub mod dimension {
11    pub use yog_ids::game::{OVERWORLD, THE_END, THE_NETHER};
12}
13
14/// An ergonomic handle to one dimension's blocks, bound to a [`Server`].
15pub struct World<'a> {
16    server: &'a dyn Server,
17    dimension: String,
18}
19
20impl<'a> World<'a> {
21    /// Bind to `dimension` (e.g. [`dimension::OVERWORLD`]) on `server`.
22    pub fn new(server: &'a dyn Server, dimension: impl Into<String>) -> Self {
23        Self {
24            server,
25            dimension: dimension.into(),
26        }
27    }
28
29    /// Registry id of the block at `pos`, or `None` if unavailable.
30    pub fn get_block(&self, pos: BlockPos) -> Option<String> {
31        self.server.get_block(&self.dimension, pos)
32    }
33
34    /// Set the block at `pos` to `block_id`; returns whether it was applied.
35    pub fn set_block(&self, pos: BlockPos, block_id: &str) -> bool {
36        self.server.set_block(&self.dimension, pos, block_id)
37    }
38
39    /// Game time in ticks since world creation.
40    pub fn time(&self) -> Option<i64> {
41        self.server.world_time(&self.dimension)
42    }
43
44    /// Set the time-of-day (0 = dawn, 6000 = noon, 12000 = dusk, 18000 = midnight).
45    pub fn set_time(&self, time: i64) -> bool {
46        self.server.world_set_time(&self.dimension, time)
47    }
48
49    /// Whether it is currently raining in this dimension.
50    pub fn is_raining(&self) -> bool {
51        self.server.world_is_raining(&self.dimension)
52    }
53
54    /// Start or stop rain. `duration_ticks = 0` picks a server-default duration.
55    pub fn set_weather(&self, raining: bool, duration_ticks: i32) -> bool {
56        self.server
57            .world_set_weather(&self.dimension, raining, duration_ticks)
58    }
59
60    /// Number of loaded entities of `entity_type` (e.g. `"minecraft:zombie"`)
61    /// in this dimension. Returns `-1` if the dimension or type is unknown.
62    pub fn entity_count(&self, entity_type: &str) -> i32 {
63        self.server.world_entity_count(&self.dimension, entity_type)
64    }
65}
66
67/// Convenience: the overworld of `server`.
68pub fn overworld(server: &dyn Server) -> World<'_> {
69    World::new(server, dimension::OVERWORLD)
70}