Skip to main content

yog_core/
server.rs

1//! The handle Rust mods use to act on the running server — the Rust → Minecraft
2//! path.
3
4use crate::{BlockHit, BlockInfo, BlockPos, EntityInfo, Face, PlayerInfo};
5
6/// Low-level capabilities the Yog runtime exposes to mods (the engine contract).
7///
8/// The runtime provides the concrete implementation, backed by JNI calls into
9/// the Java host; this crate stays JVM-free. Higher-level, ergonomic wrappers
10/// live in the domain crates (e.g. `yog-world`'s `World`).
11///
12/// Dimensions are identified by their registry id string, e.g.
13/// `"minecraft:overworld"`.
14pub trait Server {
15    /// Broadcast a chat message to all players on the server.
16    fn broadcast(&self, message: &str);
17
18    /// Registry id of the block at `pos` in `dimension`
19    /// (e.g. `"minecraft:stone"`), or `None` if the dimension/position is
20    /// unavailable. Call from the server thread (e.g. an event handler).
21    fn get_block(&self, dimension: &str, pos: BlockPos) -> Option<String>;
22
23    /// Set the block at `pos` in `dimension` to `block_id` — a registry id
24    /// or, like `/setblock`, a state (`"minecraft:water_cauldron[level=3]"`).
25    /// Returns whether the change was applied. Call from the server thread.
26    fn set_block(&self, dimension: &str, pos: BlockPos, block_id: &str) -> bool;
27
28    /// Give `count` of `item_id` to the named player. Returns whether it worked
29    /// (player online, item valid). Call from the server thread.
30    fn give_item(&self, player: &str, item_id: &str, count: u32) -> bool;
31
32    /// Teleport the named player to `(x, y, z)` in their current world. Returns
33    /// whether it worked. Call from the server thread.
34    fn teleport(&self, player: &str, x: f64, y: f64, z: f64) -> bool;
35
36    /// Send a raw-byte packet to the named player on `channel` (server → client).
37    /// Returns whether the player was online. Payload is opaque bytes — no NBT.
38    fn send_to_player(&self, player: &str, channel: &str, payload: &[u8]) -> bool;
39
40    /// Send a raw-byte packet to the server on `channel` (client → server).
41    /// Only works in a client context; returns whether it was sent.
42    fn send_to_server(&self, channel: &str, payload: &[u8]) -> bool;
43
44    // ── entity (universal, by UUID) ─────────────────────────────────────────
45
46    /// Teleport any entity (by UUID) within its current world.
47    fn entity_teleport(&self, uuid: &str, x: f64, y: f64, z: f64) -> bool;
48
49    /// Position of an entity, or `None` if not loaded.
50    fn entity_position(&self, uuid: &str) -> Option<(f64, f64, f64)>;
51
52    /// Health of a living entity, or `None`.
53    fn entity_health(&self, uuid: &str) -> Option<f32>;
54
55    /// Set a living entity's health; returns whether it applied.
56    fn entity_set_health(&self, uuid: &str, health: f32) -> bool;
57
58    /// Remove/kill an entity.
59    fn entity_kill(&self, uuid: &str) -> bool;
60
61    /// Spawn an entity of `entity_type` (e.g. `minecraft:pig`) at a position;
62    /// returns its UUID, or `None` on failure.
63    fn spawn_entity(
64        &self,
65        entity_type: &str,
66        dimension: &str,
67        x: f64,
68        y: f64,
69        z: f64,
70    ) -> Option<String>;
71
72    // ── status effects ──────────────────────────────────────────────────────
73
74    /// Apply a status effect to a living entity. `effect_id` is a registry id
75    /// such as `"minecraft:speed"` or `"minecraft:regeneration"`. `amplifier` is
76    /// 0-based (0 = level I). Returns `false` if the entity or effect is unknown.
77    fn entity_add_effect(
78        &self,
79        uuid: &str,
80        effect_id: &str,
81        duration_ticks: i32,
82        amplifier: u8,
83        show_particles: bool,
84    ) -> bool;
85
86    /// Remove a single status effect from a living entity.
87    fn entity_remove_effect(&self, uuid: &str, effect_id: &str) -> bool;
88
89    /// Clear all active status effects from a living entity.
90    fn entity_clear_effects(&self, uuid: &str) -> bool;
91
92    // ── loot tables ─────────────────────────────────────────────────────────
93
94    /// Roll a loot table and spawn the resulting item entities in the world.
95    /// `table_id` is the namespaced id, e.g. `"minecraft:entities/zombie"`.
96    fn drop_loot(&self, table_id: &str, dimension: &str, x: f64, y: f64, z: f64) -> bool;
97
98    // ── tag queries ─────────────────────────────────────────────────────────
99
100    /// Returns whether `item_id` (e.g. `"minecraft:stone"`) belongs to `tag_id`
101    /// (e.g. `"minecraft:planks"`).
102    fn has_item_tag(&self, item_id: &str, tag_id: &str) -> bool;
103
104    /// Returns whether `block_id` belongs to `tag_id`.
105    fn has_block_tag(&self, block_id: &str, tag_id: &str) -> bool;
106
107    // ── world state ─────────────────────────────────────────────────────────
108
109    /// Game time in ticks since world creation (never wraps, keeps counting).
110    fn world_time(&self, dimension: &str) -> Option<i64>;
111
112    /// Set the time-of-day (0 = dawn, 6000 = noon, 12000 = dusk, 18000 = midnight).
113    /// Only changes the visual time, not the absolute world age.
114    fn world_set_time(&self, dimension: &str, time: i64) -> bool;
115
116    /// Whether it is currently raining in the given dimension.
117    fn world_is_raining(&self, dimension: &str) -> bool;
118
119    /// Start or stop rain. `duration_ticks` controls how long the weather lasts
120    /// (use 0 for a server-chosen default duration).
121    fn world_set_weather(&self, dimension: &str, raining: bool, duration_ticks: i32) -> bool;
122
123    // ── entity velocity ─────────────────────────────────────────────────────
124
125    /// Current velocity `(vx, vy, vz)` of any entity, or `None` if not loaded.
126    fn entity_velocity(&self, uuid: &str) -> Option<(f64, f64, f64)>;
127
128    /// Set the velocity of any entity. Returns `false` if not loaded.
129    fn entity_set_velocity(&self, uuid: &str, vx: f64, vy: f64, vz: f64) -> bool;
130
131    /// Add a velocity impulse (cumulative with existing velocity).
132    fn entity_add_velocity(&self, uuid: &str, vx: f64, vy: f64, vz: f64) -> bool;
133
134    // ── scoreboard ──────────────────────────────────────────────────────────
135
136    /// Score of `player` on `objective`, or `None` if the objective doesn't exist.
137    fn scoreboard_get(&self, objective: &str, player: &str) -> Option<i32>;
138
139    /// Set the score; returns `false` if the objective doesn't exist.
140    fn scoreboard_set(&self, objective: &str, player: &str, score: i32) -> bool;
141
142    /// Add `delta` to the score (negative = subtract). Returns the new score,
143    /// or `None` if the objective doesn't exist.
144    fn scoreboard_add(&self, objective: &str, player: &str, delta: i32) -> Option<i32>;
145
146    // ── sound ───────────────────────────────────────────────────────────────
147
148    /// Play a sound at `(x, y, z)` in `dimension`. `sound_id` is a registry id
149    /// (e.g. `"minecraft:entity.player.levelup"`). All players within range hear
150    /// it. Returns `false` if the dimension is unknown.
151    fn play_sound(
152        &self,
153        dimension: &str,
154        x: f64,
155        y: f64,
156        z: f64,
157        sound_id: &str,
158        volume: f32,
159        pitch: f32,
160    ) -> bool;
161
162    /// Play a sound at the named player's current position. All players nearby
163    /// (including the target) hear it. Returns `false` if the player is offline.
164    fn play_sound_to_player(&self, player: &str, sound_id: &str, volume: f32, pitch: f32) -> bool;
165
166    // ── title / actionbar ───────────────────────────────────────────────────
167
168    /// Send a title+subtitle screen to a player. Pass empty strings to omit
169    /// either line. Timings are in ticks (20 ticks = 1 second).
170    fn send_title(
171        &self,
172        player: &str,
173        title: &str,
174        subtitle: &str,
175        fadein: i32,
176        stay: i32,
177        fadeout: i32,
178    ) -> bool;
179
180    /// Send a short message to the action-bar (the line just above the hotbar).
181    fn send_actionbar(&self, player: &str, message: &str) -> bool;
182
183    /// Yaw and pitch (degrees) of an entity by UUID, or `None` if it does not
184    /// exist. Minecraft convention: yaw 0 = south, positive pitch looks down.
185    fn entity_rotation(&self, uuid: &str) -> Option<(f32, f32)>;
186
187    // ── player management ───────────────────────────────────────────────────
188
189    /// Disconnect `player` with the given `reason` message.
190    fn kick_player(&self, player: &str, reason: &str) -> bool;
191
192    /// Change a player's game mode. `gamemode` is one of `"survival"`,
193    /// `"creative"`, `"adventure"`, `"spectator"` (or the abbreviations
194    /// `"s"`, `"c"`, `"a"`, `"sp"`). Returns `false` if the player is offline
195    /// or `gamemode` is unrecognised.
196    fn set_gamemode(&self, player: &str, gamemode: &str) -> bool;
197
198    // ── boss bar ────────────────────────────────────────────────────────────
199
200    /// Create a new boss bar identified by `id` (a namespaced id such as
201    /// `"mymod:progress"`). `color`: `"pink"` / `"blue"` / `"red"` / `"green"` /
202    /// `"yellow"` / `"purple"` / `"white"`. `style`: `"progress"` /
203    /// `"notched_6"` / `"notched_10"` / `"notched_12"` / `"notched_20"`.
204    /// Returns `false` if a bar with that id already exists.
205    fn bossbar_create(&self, id: &str, title: &str, color: &str, style: &str) -> bool;
206
207    /// Remove a boss bar (also removes it from all players). Returns `false` if
208    /// the bar doesn't exist.
209    fn bossbar_remove(&self, id: &str) -> bool;
210
211    /// Update the displayed title of a boss bar.
212    fn bossbar_set_title(&self, id: &str, title: &str) -> bool;
213
214    /// Set the fill level of a boss bar (0.0 = empty, 1.0 = full).
215    fn bossbar_set_progress(&self, id: &str, progress: f32) -> bool;
216
217    /// Change the color of a boss bar (same color names as [`bossbar_create`]).
218    fn bossbar_set_color(&self, id: &str, color: &str) -> bool;
219
220    /// Add an online player to the boss bar's audience.
221    fn bossbar_add_player(&self, id: &str, player: &str) -> bool;
222
223    /// Remove a player from the boss bar's audience.
224    fn bossbar_remove_player(&self, id: &str, player: &str) -> bool;
225
226    /// Show or hide a boss bar for all its current audience members.
227    fn bossbar_set_visible(&self, id: &str, visible: bool) -> bool;
228
229    // ── misc ────────────────────────────────────────────────────────────────
230
231    /// Absolute path of the game / server root directory.
232    fn game_dir(&self) -> String;
233
234    /// Names of all currently connected players.
235    fn online_players(&self) -> Vec<String>;
236
237    // ── block entity ─────────────────────────────────────────────────────────
238
239    /// SNBT string of the block entity at `pos` (e.g. chest contents, furnace
240    /// state, sign text). Returns `None` if there is no block entity there.
241    fn get_block_nbt(&self, dimension: &str, pos: BlockPos) -> Option<String>;
242
243    /// Write `snbt` data into the block entity at `pos` and mark it dirty.
244    /// Returns `false` if there is no block entity at that position.
245    fn set_block_nbt(&self, dimension: &str, pos: BlockPos, snbt: &str) -> bool;
246
247    // ── inventory-backed block slots (yog-inventory) ─────────────────────────
248
249    /// `(item_id, count, snbt)` of one slot of the block container at `pos`
250    /// — a Yog inventory block (see `yog_inventory::InventoryDef`), a chest,
251    /// any container. `snbt` is `"{}"` when the item carries no data.
252    /// `None` if the slot is empty or there's no container there.
253    fn get_inventory_slot(
254        &self,
255        dimension: &str,
256        pos: BlockPos,
257        slot: u32,
258    ) -> Option<(String, u32, String)>;
259
260    /// Puts `count` of `item_id`, carrying `snbt` (`""` for none), into one
261    /// slot of the block container at `pos`; `count == 0` empties it.
262    /// `false` if there's no container there (or the item id is unknown).
263    fn set_inventory_slot(
264        &self,
265        dimension: &str,
266        pos: BlockPos,
267        slot: u32,
268        item_id: &str,
269        count: u32,
270        snbt: &str,
271    ) -> bool;
272
273    // ── inventory ────────────────────────────────────────────────────────────
274
275    /// All occupied inventory slots of an online player.
276    /// Returns one entry per occupied slot: `(slot_index, item_id, count)`.
277    fn player_inventory(&self, player: &str) -> Vec<(u32, String, u32)>;
278
279    /// Set (or clear when `count == 0`) one inventory slot of an online player.
280    fn player_set_slot(&self, player: &str, slot: u32, item_id: &str, count: u32) -> bool;
281
282    // ── cross-dimension teleport ─────────────────────────────────────────────
283
284    /// Teleport a player to `(x, y, z)` in a different (or same) dimension.
285    fn teleport_to_dim(&self, player: &str, dimension: &str, x: f64, y: f64, z: f64) -> bool;
286
287    /// Teleport any entity (by UUID) to `(x, y, z)` in `dimension`.
288    fn entity_teleport_to_dim(&self, uuid: &str, dimension: &str, x: f64, y: f64, z: f64) -> bool;
289
290    // ── entity query ─────────────────────────────────────────────────────────
291
292    /// Number of loaded entities of `entity_type` (e.g. `"minecraft:zombie"`)
293    /// in `dimension`. Returns `-1` if the dimension or entity type is unknown.
294    fn world_entity_count(&self, dimension: &str, entity_type: &str) -> i32;
295
296    // ── entity NBT ───────────────────────────────────────────────────────────
297
298    /// SNBT string of the entity's persistent NBT, or `None` if not found.
299    fn entity_get_nbt(&self, uuid: &str) -> Option<String>;
300
301    /// Merge SNBT data into the entity's persistent NBT. Returns `false` if not found.
302    fn entity_set_nbt(&self, uuid: &str, snbt: &str) -> bool;
303
304    // ── particles ────────────────────────────────────────────────────────────
305
306    /// Spawn `count` particles at `(x, y, z)` in `dimension`.
307    /// `particle_type` is a particle id or, like `/particle`, an id with its
308    /// options (`"minecraft:dust 1 0.5 0 1"`).
309    /// `dx/dy/dz` control the spatial spread; `speed` controls particle velocity.
310    fn spawn_particles(
311        &self,
312        dimension: &str,
313        x: f64,
314        y: f64,
315        z: f64,
316        particle_type: &str,
317        count: i32,
318        dx: f64,
319        dy: f64,
320        dz: f64,
321        speed: f64,
322    ) -> bool;
323
324    // ── attributes ───────────────────────────────────────────────────────────
325
326    /// Get the base value of an attribute on a living entity.
327    /// `attribute_id` e.g. `"minecraft:generic.max_health"`.
328    /// Returns `None` if the entity or attribute is not found.
329    fn entity_attribute_get(&self, uuid: &str, attribute_id: &str) -> Option<f64>;
330
331    /// Set the base value of an attribute. Returns false if not found.
332    fn entity_attribute_set(&self, uuid: &str, attribute_id: &str, value: f64) -> bool;
333
334    // ── held item NBT (ABI minor 11) ─────────────────────────────────────────
335
336    /// SNBT string of the item currently held in the player's main hand.
337    /// Returns `None` if the player is offline or holding air.
338    fn get_held_item_nbt(&self, player: &str) -> Option<String>;
339
340    /// Merge `snbt` data into the NBT of the item in the player's main hand.
341    /// Returns `false` if the player is offline or holding air.
342    fn set_held_item_nbt(&self, player: &str, snbt: &str) -> bool;
343
344    // ── item stack query (ABI minor 12) ──────────────────────────────────────
345
346    /// SNBT of the item in the player's off hand.
347    /// Returns `None` if offline or holding air.
348    fn get_offhand_item_nbt(&self, player: &str) -> Option<String>;
349
350    /// Merge `snbt` into the NBT of the player's off-hand item.
351    /// Returns `false` if offline or holding air.
352    fn set_offhand_item_nbt(&self, player: &str, snbt: &str) -> bool;
353
354    /// Full item stack at inventory `slot`: `(item_id, count, nbt_snbt)`.
355    /// `nbt_snbt` is `"{}"` when the item has no NBT.
356    /// Returns `None` if the player is offline or the slot is empty.
357    fn get_slot_item(&self, player: &str, slot: u32) -> Option<(String, u32, String)>;
358
359    /// Replace inventory `slot`. Pass `count == 0` to clear the slot.
360    /// `snbt` is merged into the new item's NBT (pass `""` for no NBT).
361    fn set_slot_item(&self, player: &str, slot: u32, item_id: &str, count: u32, snbt: &str)
362    -> bool;
363
364    // ── world queries & effects (ABI minor 1) ────────────────────────────────
365
366    /// Type, position, size and kind of a loaded entity, or `None` if no
367    /// entity has that uuid.
368    fn entity_info(&self, uuid: &str) -> Option<EntityInfo>;
369
370    /// Uuids of the entities whose bounding boxes touch the box `min`–`max`
371    /// in `dimension`.
372    fn entities_in_box(
373        &self,
374        dimension: &str,
375        min: (f64, f64, f64),
376        max: (f64, f64, f64),
377    ) -> Vec<String>;
378
379    /// The first block with a collision shape on the segment `from`–`to`
380    /// (fluids are ignored).
381    fn raycast_block(
382        &self,
383        dimension: &str,
384        from: (f64, f64, f64),
385        to: (f64, f64, f64),
386    ) -> Option<BlockHit>;
387
388    /// The nearest pickable entity whose (slightly inflated) bounding box the
389    /// segment `from`–`to` crosses, other than `exclude` (a uuid, or `""`)
390    /// and whatever it rides.
391    fn raycast_entity(
392        &self,
393        dimension: &str,
394        from: (f64, f64, f64),
395        to: (f64, f64, f64),
396        exclude: &str,
397    ) -> Option<String>;
398
399    /// State, hardness, fluid and redstone power of the block at `pos`.
400    fn block_info(&self, dimension: &str, pos: BlockPos) -> Option<BlockInfo>;
401
402    /// Break the block at `pos` as `breaker` (a player name, or `""`):
403    /// plays the break effects and, if `drop`, drops its loot as if mined
404    /// with an empty hand. Returns `false` for air and unbreakable blocks.
405    fn break_block(&self, dimension: &str, pos: BlockPos, drop: bool, breaker: &str) -> bool;
406
407    /// Place the block item in the player's inventory `slot` at `pos`, as if
408    /// the player clicked the block below it: the item's own placement
409    /// rules apply and one item is used up (unless creative).
410    fn place_block_from_slot(
411        &self,
412        player: &str,
413        slot: u32,
414        dimension: &str,
415        pos: BlockPos,
416    ) -> bool;
417
418    /// Apply bone meal to the block at `pos`. Returns whether it grew.
419    fn bonemeal(&self, dimension: &str, pos: BlockPos) -> bool;
420
421    /// An explosion of `power` (TNT is 4) at `pos`, caused by entity
422    /// `source` (a uuid, or `""`); `fire` sets fires like a ghast fireball.
423    #[allow(clippy::too_many_arguments)]
424    fn explode(
425        &self,
426        dimension: &str,
427        x: f64,
428        y: f64,
429        z: f64,
430        power: f32,
431        fire: bool,
432        source: &str,
433    ) -> bool;
434
435    /// A system chat message to one player, like `/tellraw`: `json` is a
436    /// JSON text component, so it can hold translation keys
437    /// (`{"translate":"mymod.msg","with":["x"]}`) that each client shows in
438    /// its own language.
439    fn tellraw(&self, player: &str, json: &str) -> bool;
440
441    /// Hurt an entity. `damage_type` is a damage type id
442    /// (`"minecraft:magic"`, `"minecraft:drown"`, …; `""` picks one from
443    /// the attacker: player attack, mob attack, or magic without one), and
444    /// `attacker` the uuid of the entity responsible (or `""`). An unknown
445    /// damage type falls back to magic. Returns whether it took the damage.
446    fn entity_damage(&self, uuid: &str, amount: f32, damage_type: &str, attacker: &str) -> bool;
447
448    /// Set an entity on fire for `ticks` (`0` puts it out).
449    fn entity_set_fire_ticks(&self, uuid: &str, ticks: i32) -> bool;
450
451    /// Give (or, negative, take) experience points.
452    fn player_add_xp(&self, player: &str, amount: i32) -> bool;
453
454    /// Let a player fly (creative-style) or take it away (also stops them
455    /// flying). Game mode changes reset it.
456    fn player_set_may_fly(&self, player: &str, may_fly: bool) -> bool;
457
458    /// Game mode, selected slot, experience, air and flight of a player.
459    fn player_info(&self, player: &str) -> Option<PlayerInfo>;
460
461    /// Drop an item stack into the world with velocity `vel`; `snbt` is the
462    /// stack's NBT (`""` for none). Returns the item entity's uuid.
463    #[allow(clippy::too_many_arguments)]
464    fn spawn_item(
465        &self,
466        dimension: &str,
467        pos: (f64, f64, f64),
468        vel: (f64, f64, f64),
469        item_id: &str,
470        count: u32,
471        snbt: &str,
472    ) -> Option<String>;
473
474    /// The stack inside a dropped item entity: `(item_id, count, snbt)`.
475    fn item_entity_stack(&self, uuid: &str) -> Option<(String, u32, String)>;
476
477    /// Replace the stack inside a dropped item entity (`count == 0` removes
478    /// the entity).
479    fn item_entity_set_stack(&self, uuid: &str, item_id: &str, count: u32, snbt: &str) -> bool;
480
481    /// An action-bar message (the line above the hotbar) from a JSON text
482    /// component, like [`tellraw`](Self::tellraw): translation keys show
483    /// in each client's own language.
484    fn actionbar_json(&self, player: &str, json: &str) -> bool;
485
486    /// Use one `item_id` (a fresh single item, nothing is taken from anyone)
487    /// on the `face` of the block at `pos`, as `player` (a name, or `""` for
488    /// nobody): flint and steel lights it, a shovel puts out a campfire, a
489    /// hoe tills it… Returns whether the item did something.
490    fn use_item_on_block(
491        &self,
492        dimension: &str,
493        pos: BlockPos,
494        face: Face,
495        item_id: &str,
496        player: &str,
497    ) -> bool;
498
499    /// Empty the fluid of a bucket item (`"minecraft:water_bucket"`) at `pos`
500    /// the way a player would: it waterlogs a waterloggable block, replaces
501    /// a replaceable one, and evaporates where water can't exist. Returns
502    /// whether any fluid was placed.
503    fn pour_fluid(&self, dimension: &str, pos: BlockPos, bucket_item_id: &str) -> bool;
504
505    /// Whether an entity type (`"minecraft:zombie"`) is in `tag_id`
506    /// (`"minecraft:skeletons"`).
507    fn has_entity_tag(&self, entity_type: &str, tag_id: &str) -> bool;
508
509    /// The block an item places (`"minecraft:torch"` for the torch item), or
510    /// `None` if it isn't a block item.
511    fn item_block(&self, item_id: &str) -> Option<String>;
512
513    /// Place a configured feature (`"minecraft:oak"`, or a datapack's own
514    /// `data/<ns>/worldgen/configured_feature/*.json`) at `pos`, the way
515    /// a growing sapling places its tree. Returns whether it was placed.
516    fn place_feature(&self, dimension: &str, pos: BlockPos, feature_id: &str) -> bool;
517
518    // ── ABI minor 3 ──────────────────────────────────────────────────────────
519
520    /// Puts `item_id` on cooldown for an online player for `ticks` (the
521    /// grey sweep over it in the hotbar; using it waits). `false` if the
522    /// player is offline or the item unknown.
523    fn set_item_cooldown(&self, player: &str, item_id: &str, ticks: u32) -> bool;
524
525    /// What an entity rides — a boat, a horse, a minecart — if anything.
526    fn entity_vehicle(&self, uuid: &str) -> Option<String>;
527
528    /// What rides an entity directly.
529    fn entity_passengers(&self, uuid: &str) -> Vec<String>;
530
531    /// Seats `rider` on `vehicle`; `force`: even on one that wouldn't take
532    /// it (a full boat, a mob that doesn't carry riders).
533    fn entity_mount(&self, rider: &str, vehicle: &str, force: bool) -> bool;
534
535    /// Gets an entity off what it rides; `false` if it rode nothing.
536    fn entity_dismount(&self, uuid: &str) -> bool;
537
538    /// Gives the entity its own hitbox — `Some((width, height, eye
539    /// height))` in blocks — or vanilla's again (`None`); it stays till
540    /// changed, through pose changes. The server side: the clients are told
541    /// with `yog_api::body::set_dimensions` (the mod's own packet). `false`
542    /// if there's no such entity.
543    fn entity_set_dimensions(&self, uuid: &str, size: Option<(f32, f32, f32)>) -> bool;
544
545    /// The value the entity keeps under `key` (`set_entity_data`).
546    fn entity_data(&self, uuid: &str, key: &str) -> Option<Vec<u8>>;
547
548    /// Sets (`Some`) or removes the value the entity keeps under `key`
549    /// (`mymod:morph`) — the loader sends it to every player who sees the
550    /// entity, now and when they start seeing it; their clients read it in
551    /// body frames (`Frame::data`) and with `yog_api::body::entity_data`.
552    /// `persist` saves it with the entity. Only the server writes these: a
553    /// client asks with its mod's packet. `false` for an empty uuid or key.
554    fn set_entity_data(&self, uuid: &str, key: &str, value: Option<&[u8]>, persist: bool) -> bool;
555
556    /// Sets how far the entity has fallen — fall damage counts it when it
557    /// lands (0: none). `false` if there's no such entity.
558    fn entity_set_fall_distance(&self, uuid: &str, distance: f32) -> bool;
559
560    /// The strongest signal the block at `pos` gives off (a lever, a torch,
561    /// dust: its power); `None` if it isn't a redstone source.
562    fn redstone_signal(&self, dimension: &str, pos: BlockPos) -> Option<u8>;
563
564    /// The redstone power the block at `pos` receives from its neighbours
565    /// (0–15); `None` if the dimension doesn't exist.
566    fn redstone_power(&self, dimension: &str, pos: BlockPos) -> Option<u8>;
567
568    /// Sets the power (0–15) the block at `pos` gives off — a block
569    /// declared a redstone source (`BlockDef::redstone_source`,
570    /// `"redstone": true` in its `.yblock`). Neighbours update.
571    fn set_redstone_output(&self, dimension: &str, pos: BlockPos, level: u8) -> bool;
572
573    /// The full state of the block at `pos`: its id and properties,
574    /// `"minecraft:repeater[delay=2,facing=east,locked=false,powered=false]"`.
575    fn get_block_state(&self, dimension: &str, pos: BlockPos) -> Option<String>;
576
577    /// Places a block in a full state, as `/setblock` writes it
578    /// (`"minecraft:repeater[delay=2,facing=east]"`; unset properties take
579    /// their defaults). `false` if it doesn't parse.
580    fn set_block_state(&self, dimension: &str, pos: BlockPos, state: &str) -> bool;
581
582    /// Property `name` of the block at `pos` (`"energized"` → `"true"`).
583    fn block_property(&self, dimension: &str, pos: BlockPos, name: &str) -> Option<String> {
584        let state = self.get_block_state(dimension, pos)?;
585        split_state(&state).1.into_iter().find(|(k, _)| *k == name).map(|(_, v)| v.to_string())
586    }
587
588    /// Sets some properties of the block at `pos` (`"energized=true,powered=false"`),
589    /// keeping the rest. `false` if there's no block or the state doesn't take.
590    fn set_block_properties(&self, dimension: &str, pos: BlockPos, properties: &str) -> bool {
591        let Some(state) = self.get_block_state(dimension, pos) else { return false };
592        let (id, mut props) = split_state(&state);
593        for pair in properties.split(',').filter(|p| !p.is_empty()) {
594            let Some((k, v)) = pair.split_once('=') else { return false };
595            match props.iter_mut().find(|(n, _)| *n == k) {
596                Some(p) => p.1 = v,
597                None => props.push((k, v)),
598            }
599        }
600        let props: Vec<String> = props.iter().map(|(k, v)| format!("{k}={v}")).collect();
601        self.set_block_state(dimension, pos, &format!("{id}[{}]", props.join(",")))
602    }
603
604    /// Whether a thunderstorm is going on in `dimension`.
605    fn world_is_thundering(&self, dimension: &str) -> bool;
606
607    /// Starts (`true`) or ends a thunderstorm; `duration_ticks` 0 = a
608    /// server-chosen length.
609    fn world_set_thunder(&self, dimension: &str, thundering: bool, duration_ticks: i32) -> bool;
610
611    /// Sets every block of the box between `from` and `to` (inclusive, any
612    /// corners) to `state` (`"minecraft:barrier"`, `"minecraft:air"`, a full
613    /// state) in one call. How many blocks changed; `None` if the dimension
614    /// or state is unknown.
615    fn fill_region(&self, dimension: &str, from: BlockPos, to: BlockPos, state: &str) -> Option<u32>;
616
617    /// Every non-air block of the box between `from` and `to` with its full
618    /// state (see [`Server::get_block_state`]) — one call for a whole area.
619    fn scan_region(&self, dimension: &str, from: BlockPos, to: BlockPos) -> Vec<(BlockPos, String)>;
620}
621
622/// A block state (`"ns:id[k=v,...]"`) as its id and its properties.
623pub fn split_state(state: &str) -> (&str, Vec<(&str, &str)>) {
624    let (id, rest) = state.split_once('[').unwrap_or((state, ""));
625    let props = rest
626        .trim_end_matches(']')
627        .split(',')
628        .filter_map(|p| p.split_once('='))
629        .collect();
630    (id, props)
631}
632
633#[cfg(test)]
634mod state_tests {
635    use super::split_state;
636
637    #[test]
638    fn a_state_splits_into_its_id_and_properties() {
639        assert_eq!(split_state("minecraft:stone"), ("minecraft:stone", vec![]));
640        assert_eq!(
641            split_state("hexcasting:impetus/look[energized=false,facing=up]"),
642            ("hexcasting:impetus/look", vec![("energized", "false"), ("facing", "up")])
643        );
644    }
645}