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 const OVERWORLD: &str = "minecraft:overworld";
12    pub const THE_NETHER: &str = "minecraft:the_nether";
13    pub const THE_END: &str = "minecraft:the_end";
14}
15
16/// An ergonomic handle to one dimension's blocks, bound to a [`Server`].
17pub struct World<'a> {
18    server: &'a dyn Server,
19    dimension: String,
20}
21
22impl<'a> World<'a> {
23    /// Bind to `dimension` (e.g. [`dimension::OVERWORLD`]) on `server`.
24    pub fn new(server: &'a dyn Server, dimension: impl Into<String>) -> Self {
25        Self {
26            server,
27            dimension: dimension.into(),
28        }
29    }
30
31    /// Registry id of the block at `pos`, or `None` if unavailable.
32    pub fn get_block(&self, pos: BlockPos) -> Option<String> {
33        self.server.get_block(&self.dimension, pos)
34    }
35
36    /// Set the block at `pos` to `block_id`; returns whether it was applied.
37    pub fn set_block(&self, pos: BlockPos, block_id: &str) -> bool {
38        self.server.set_block(&self.dimension, pos, block_id)
39    }
40
41    /// Game time in ticks since world creation.
42    pub fn time(&self) -> Option<i64> {
43        self.server.world_time(&self.dimension)
44    }
45
46    /// Set the time-of-day (0 = dawn, 6000 = noon, 12000 = dusk, 18000 = midnight).
47    pub fn set_time(&self, time: i64) -> bool {
48        self.server.world_set_time(&self.dimension, time)
49    }
50
51    /// Whether it is currently raining in this dimension.
52    pub fn is_raining(&self) -> bool {
53        self.server.world_is_raining(&self.dimension)
54    }
55
56    /// Start or stop rain. `duration_ticks = 0` picks a server-default duration.
57    pub fn set_weather(&self, raining: bool, duration_ticks: i32) -> bool {
58        self.server.world_set_weather(&self.dimension, raining, duration_ticks)
59    }
60
61    /// Number of loaded entities of `entity_type` (e.g. `"minecraft:zombie"`)
62    /// in this dimension. Returns `-1` if the dimension or type is unknown.
63    pub fn entity_count(&self, entity_type: &str) -> i32 {
64        self.server.world_entity_count(&self.dimension, entity_type)
65    }
66}
67
68/// Convenience: the overworld of `server`.
69pub fn overworld(server: &dyn Server) -> World<'_> {
70    World::new(server, dimension::OVERWORLD)
71}