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: what belongs to
232 /// the install (configs), not to a world.
233 fn game_dir(&self) -> String;
234
235 /// Absolute path of the directory of the world being played (its save) —
236 /// where what belongs to this world is kept ([`crate`]'s users open
237 /// their world storage here).
238 fn world_dir(&self) -> String;
239
240 /// Names of all currently connected players.
241 fn online_players(&self) -> Vec<String>;
242
243 // ── block entity ─────────────────────────────────────────────────────────
244
245 /// SNBT string of the block entity at `pos` (e.g. chest contents, furnace
246 /// state, sign text). Returns `None` if there is no block entity there.
247 fn get_block_nbt(&self, dimension: &str, pos: BlockPos) -> Option<String>;
248
249 /// Write `snbt` data into the block entity at `pos` and mark it dirty.
250 /// Returns `false` if there is no block entity at that position.
251 fn set_block_nbt(&self, dimension: &str, pos: BlockPos, snbt: &str) -> bool;
252
253 // ── inventory-backed block slots (yog-inventory) ─────────────────────────
254
255 /// `(item_id, count, snbt)` of one slot of the block container at `pos`
256 /// — a Yog inventory block (see `yog_inventory::InventoryDef`), a chest,
257 /// any container. `snbt` is `"{}"` when the item carries no data.
258 /// `None` if the slot is empty or there's no container there.
259 fn get_inventory_slot(
260 &self,
261 dimension: &str,
262 pos: BlockPos,
263 slot: u32,
264 ) -> Option<(String, u32, String)>;
265
266 /// Puts `count` of `item_id`, carrying `snbt` (`""` for none), into one
267 /// slot of the block container at `pos`; `count == 0` empties it.
268 /// `false` if there's no container there (or the item id is unknown).
269 fn set_inventory_slot(
270 &self,
271 dimension: &str,
272 pos: BlockPos,
273 slot: u32,
274 item_id: &str,
275 count: u32,
276 snbt: &str,
277 ) -> bool;
278
279 // ── inventory ────────────────────────────────────────────────────────────
280
281 /// All occupied inventory slots of an online player.
282 /// Returns one entry per occupied slot: `(slot_index, item_id, count)`.
283 fn player_inventory(&self, player: &str) -> Vec<(u32, String, u32)>;
284
285 /// Set (or clear when `count == 0`) one inventory slot of an online player.
286 fn player_set_slot(&self, player: &str, slot: u32, item_id: &str, count: u32) -> bool;
287
288 // ── cross-dimension teleport ─────────────────────────────────────────────
289
290 /// Teleport a player to `(x, y, z)` in a different (or same) dimension.
291 fn teleport_to_dim(&self, player: &str, dimension: &str, x: f64, y: f64, z: f64) -> bool;
292
293 /// Teleport any entity (by UUID) to `(x, y, z)` in `dimension`.
294 fn entity_teleport_to_dim(&self, uuid: &str, dimension: &str, x: f64, y: f64, z: f64) -> bool;
295
296 // ── entity query ─────────────────────────────────────────────────────────
297
298 /// Number of loaded entities of `entity_type` (e.g. `"minecraft:zombie"`)
299 /// in `dimension`. Returns `-1` if the dimension or entity type is unknown.
300 fn world_entity_count(&self, dimension: &str, entity_type: &str) -> i32;
301
302 // ── entity NBT ───────────────────────────────────────────────────────────
303
304 /// SNBT string of the entity's persistent NBT, or `None` if not found.
305 fn entity_get_nbt(&self, uuid: &str) -> Option<String>;
306
307 /// Merge SNBT data into the entity's persistent NBT. Returns `false` if not found.
308 fn entity_set_nbt(&self, uuid: &str, snbt: &str) -> bool;
309
310 // ── particles ────────────────────────────────────────────────────────────
311
312 /// Spawn `count` particles at `(x, y, z)` in `dimension`.
313 /// `particle_type` is a particle id or, like `/particle`, an id with its
314 /// options (`"minecraft:dust 1 0.5 0 1"`).
315 /// `dx/dy/dz` control the spatial spread; `speed` controls particle velocity.
316 fn spawn_particles(
317 &self,
318 dimension: &str,
319 x: f64,
320 y: f64,
321 z: f64,
322 particle_type: &str,
323 count: i32,
324 dx: f64,
325 dy: f64,
326 dz: f64,
327 speed: f64,
328 ) -> bool;
329
330 // ── attributes ───────────────────────────────────────────────────────────
331
332 /// Get the base value of an attribute on a living entity.
333 /// `attribute_id` e.g. `"minecraft:generic.max_health"`.
334 /// Returns `None` if the entity or attribute is not found.
335 fn entity_attribute_get(&self, uuid: &str, attribute_id: &str) -> Option<f64>;
336
337 /// Set the base value of an attribute. Returns false if not found.
338 fn entity_attribute_set(&self, uuid: &str, attribute_id: &str, value: f64) -> bool;
339
340 // ── held item NBT (ABI minor 11) ─────────────────────────────────────────
341
342 /// SNBT string of the item currently held in the player's main hand.
343 /// Returns `None` if the player is offline or holding air.
344 fn get_held_item_nbt(&self, player: &str) -> Option<String>;
345
346 /// Merge `snbt` data into the NBT of the item in the player's main hand.
347 /// Returns `false` if the player is offline or holding air.
348 fn set_held_item_nbt(&self, player: &str, snbt: &str) -> bool;
349
350 // ── item stack query (ABI minor 12) ──────────────────────────────────────
351
352 /// SNBT of the item in the player's off hand.
353 /// Returns `None` if offline or holding air.
354 fn get_offhand_item_nbt(&self, player: &str) -> Option<String>;
355
356 /// Merge `snbt` into the NBT of the player's off-hand item.
357 /// Returns `false` if offline or holding air.
358 fn set_offhand_item_nbt(&self, player: &str, snbt: &str) -> bool;
359
360 /// Full item stack at inventory `slot`: `(item_id, count, nbt_snbt)`.
361 /// `nbt_snbt` is `"{}"` when the item has no NBT.
362 /// Returns `None` if the player is offline or the slot is empty.
363 fn get_slot_item(&self, player: &str, slot: u32) -> Option<(String, u32, String)>;
364
365 /// Replace inventory `slot`. Pass `count == 0` to clear the slot.
366 /// `snbt` is merged into the new item's NBT (pass `""` for no NBT).
367 fn set_slot_item(&self, player: &str, slot: u32, item_id: &str, count: u32, snbt: &str)
368 -> bool;
369
370 // ── world queries & effects (ABI minor 1) ────────────────────────────────
371
372 /// Type, position, size and kind of a loaded entity, or `None` if no
373 /// entity has that uuid.
374 fn entity_info(&self, uuid: &str) -> Option<EntityInfo>;
375
376 /// Uuids of the entities whose bounding boxes touch the box `min`–`max`
377 /// in `dimension`.
378 fn entities_in_box(
379 &self,
380 dimension: &str,
381 min: (f64, f64, f64),
382 max: (f64, f64, f64),
383 ) -> Vec<String>;
384
385 /// The first block with a collision shape on the segment `from`–`to`
386 /// (fluids are ignored).
387 fn raycast_block(
388 &self,
389 dimension: &str,
390 from: (f64, f64, f64),
391 to: (f64, f64, f64),
392 ) -> Option<BlockHit>;
393
394 /// The nearest pickable entity whose (slightly inflated) bounding box the
395 /// segment `from`–`to` crosses, other than `exclude` (a uuid, or `""`)
396 /// and whatever it rides.
397 fn raycast_entity(
398 &self,
399 dimension: &str,
400 from: (f64, f64, f64),
401 to: (f64, f64, f64),
402 exclude: &str,
403 ) -> Option<String>;
404
405 /// State, hardness, fluid and redstone power of the block at `pos`.
406 fn block_info(&self, dimension: &str, pos: BlockPos) -> Option<BlockInfo>;
407
408 /// Break the block at `pos` as `breaker` (a player name, or `""`):
409 /// plays the break effects and, if `drop`, drops its loot as if mined
410 /// with an empty hand. Returns `false` for air and unbreakable blocks.
411 fn break_block(&self, dimension: &str, pos: BlockPos, drop: bool, breaker: &str) -> bool;
412
413 /// Place the block item in the player's inventory `slot` at `pos`, as if
414 /// the player clicked the block below it: the item's own placement
415 /// rules apply and one item is used up (unless creative).
416 fn place_block_from_slot(
417 &self,
418 player: &str,
419 slot: u32,
420 dimension: &str,
421 pos: BlockPos,
422 ) -> bool;
423
424 /// Apply bone meal to the block at `pos`. Returns whether it grew.
425 fn bonemeal(&self, dimension: &str, pos: BlockPos) -> bool;
426
427 /// An explosion of `power` (TNT is 4) at `pos`, caused by entity
428 /// `source` (a uuid, or `""`); `fire` sets fires like a ghast fireball.
429 #[allow(clippy::too_many_arguments)]
430 fn explode(
431 &self,
432 dimension: &str,
433 x: f64,
434 y: f64,
435 z: f64,
436 power: f32,
437 fire: bool,
438 source: &str,
439 ) -> bool;
440
441 /// A system chat message to one player, like `/tellraw`: `json` is a
442 /// JSON text component, so it can hold translation keys
443 /// (`{"translate":"mymod.msg","with":["x"]}`) that each client shows in
444 /// its own language.
445 fn tellraw(&self, player: &str, json: &str) -> bool;
446
447 /// Hurt an entity. `damage_type` is a damage type id
448 /// (`"minecraft:magic"`, `"minecraft:drown"`, …; `""` picks one from
449 /// the attacker: player attack, mob attack, or magic without one), and
450 /// `attacker` the uuid of the entity responsible (or `""`). An unknown
451 /// damage type falls back to magic. Returns whether it took the damage.
452 fn entity_damage(&self, uuid: &str, amount: f32, damage_type: &str, attacker: &str) -> bool;
453
454 /// Set an entity on fire for `ticks` (`0` puts it out).
455 fn entity_set_fire_ticks(&self, uuid: &str, ticks: i32) -> bool;
456
457 /// Give (or, negative, take) experience points.
458 fn player_add_xp(&self, player: &str, amount: i32) -> bool;
459
460 /// Let a player fly (creative-style) or take it away (also stops them
461 /// flying). Game mode changes reset it.
462 fn player_set_may_fly(&self, player: &str, may_fly: bool) -> bool;
463
464 /// Game mode, selected slot, experience, air and flight of a player.
465 fn player_info(&self, player: &str) -> Option<PlayerInfo>;
466
467 /// Drop an item stack into the world with velocity `vel`; `snbt` is the
468 /// stack's NBT (`""` for none). Returns the item entity's uuid.
469 #[allow(clippy::too_many_arguments)]
470 fn spawn_item(
471 &self,
472 dimension: &str,
473 pos: (f64, f64, f64),
474 vel: (f64, f64, f64),
475 item_id: &str,
476 count: u32,
477 snbt: &str,
478 ) -> Option<String>;
479
480 /// The stack inside a dropped item entity: `(item_id, count, snbt)`.
481 fn item_entity_stack(&self, uuid: &str) -> Option<(String, u32, String)>;
482
483 /// Replace the stack inside a dropped item entity (`count == 0` removes
484 /// the entity).
485 fn item_entity_set_stack(&self, uuid: &str, item_id: &str, count: u32, snbt: &str) -> bool;
486
487 /// An action-bar message (the line above the hotbar) from a JSON text
488 /// component, like [`tellraw`](Self::tellraw): translation keys show
489 /// in each client's own language.
490 fn actionbar_json(&self, player: &str, json: &str) -> bool;
491
492 /// Use one `item_id` (a fresh single item, nothing is taken from anyone)
493 /// on the `face` of the block at `pos`, as `player` (a name, or `""` for
494 /// nobody): flint and steel lights it, a shovel puts out a campfire, a
495 /// hoe tills it… Returns whether the item did something.
496 fn use_item_on_block(
497 &self,
498 dimension: &str,
499 pos: BlockPos,
500 face: Face,
501 item_id: &str,
502 player: &str,
503 ) -> bool;
504
505 /// Empty the fluid of a bucket item (`"minecraft:water_bucket"`) at `pos`
506 /// the way a player would: it waterlogs a waterloggable block, replaces
507 /// a replaceable one, and evaporates where water can't exist. Returns
508 /// whether any fluid was placed.
509 fn pour_fluid(&self, dimension: &str, pos: BlockPos, bucket_item_id: &str) -> bool;
510
511 /// Whether an entity type (`"minecraft:zombie"`) is in `tag_id`
512 /// (`"minecraft:skeletons"`).
513 fn has_entity_tag(&self, entity_type: &str, tag_id: &str) -> bool;
514
515 /// The block an item places (`"minecraft:torch"` for the torch item), or
516 /// `None` if it isn't a block item.
517 fn item_block(&self, item_id: &str) -> Option<String>;
518
519 /// Place a configured feature (`"minecraft:oak"`, or a datapack's own
520 /// `data/<ns>/worldgen/configured_feature/*.json`) at `pos`, the way
521 /// a growing sapling places its tree. Returns whether it was placed.
522 fn place_feature(&self, dimension: &str, pos: BlockPos, feature_id: &str) -> bool;
523
524 // ── ABI minor 3 ──────────────────────────────────────────────────────────
525
526 /// Puts `item_id` on cooldown for an online player for `ticks` (the
527 /// grey sweep over it in the hotbar; using it waits). `false` if the
528 /// player is offline or the item unknown.
529 fn set_item_cooldown(&self, player: &str, item_id: &str, ticks: u32) -> bool;
530
531 /// What an entity rides — a boat, a horse, a minecart — if anything.
532 fn entity_vehicle(&self, uuid: &str) -> Option<String>;
533
534 /// What rides an entity directly.
535 fn entity_passengers(&self, uuid: &str) -> Vec<String>;
536
537 /// Seats `rider` on `vehicle`; `force`: even on one that wouldn't take
538 /// it (a full boat, a mob that doesn't carry riders).
539 fn entity_mount(&self, rider: &str, vehicle: &str, force: bool) -> bool;
540
541 /// Gets an entity off what it rides; `false` if it rode nothing.
542 fn entity_dismount(&self, uuid: &str) -> bool;
543
544 /// Gives the entity its own hitbox — `Some((width, height, eye
545 /// height))` in blocks — or vanilla's again (`None`); it stays till
546 /// changed, through pose changes. The server side: the clients are told
547 /// with `yog_api::body::set_dimensions` (the mod's own packet). `false`
548 /// if there's no such entity.
549 fn entity_set_dimensions(&self, uuid: &str, size: Option<(f32, f32, f32)>) -> bool;
550
551 /// The value the entity keeps under `key` (`set_entity_data`).
552 fn entity_data(&self, uuid: &str, key: &str) -> Option<Vec<u8>>;
553
554 /// Sets (`Some`) or removes the value the entity keeps under `key`
555 /// (`mymod:morph`) — the loader sends it to every player who sees the
556 /// entity, now and when they start seeing it; their clients read it in
557 /// body frames (`Frame::data`) and with `yog_api::body::entity_data`.
558 /// `persist` saves it with the entity. Only the server writes these: a
559 /// client asks with its mod's packet. `false` for an empty uuid or key.
560 fn set_entity_data(&self, uuid: &str, key: &str, value: Option<&[u8]>, persist: bool) -> bool;
561
562 /// Sets how far the entity has fallen — fall damage counts it when it
563 /// lands (0: none). `false` if there's no such entity.
564 fn entity_set_fall_distance(&self, uuid: &str, distance: f32) -> bool;
565
566 /// The strongest signal the block at `pos` gives off (a lever, a torch,
567 /// dust: its power); `None` if it isn't a redstone source.
568 fn redstone_signal(&self, dimension: &str, pos: BlockPos) -> Option<u8>;
569
570 /// The redstone power the block at `pos` receives from its neighbours
571 /// (0–15); `None` if the dimension doesn't exist.
572 fn redstone_power(&self, dimension: &str, pos: BlockPos) -> Option<u8>;
573
574 /// Sets the power (0–15) the block at `pos` gives off — a block
575 /// declared a redstone source (`BlockDef::redstone_source`,
576 /// `"redstone": true` in its `.yblock`). Neighbours update.
577 fn set_redstone_output(&self, dimension: &str, pos: BlockPos, level: u8) -> bool;
578
579 /// The full state of the block at `pos`: its id and properties,
580 /// `"minecraft:repeater[delay=2,facing=east,locked=false,powered=false]"`.
581 fn get_block_state(&self, dimension: &str, pos: BlockPos) -> Option<String>;
582
583 /// Places a block in a full state, as `/setblock` writes it
584 /// (`"minecraft:repeater[delay=2,facing=east]"`; unset properties take
585 /// their defaults). `false` if it doesn't parse.
586 fn set_block_state(&self, dimension: &str, pos: BlockPos, state: &str) -> bool;
587
588 /// Property `name` of the block at `pos` (`"energized"` → `"true"`).
589 fn block_property(&self, dimension: &str, pos: BlockPos, name: &str) -> Option<String> {
590 let state = self.get_block_state(dimension, pos)?;
591 split_state(&state).1.into_iter().find(|(k, _)| *k == name).map(|(_, v)| v.to_string())
592 }
593
594 /// Sets some properties of the block at `pos` (`"energized=true,powered=false"`),
595 /// keeping the rest. `false` if there's no block or the state doesn't take.
596 fn set_block_properties(&self, dimension: &str, pos: BlockPos, properties: &str) -> bool {
597 let Some(state) = self.get_block_state(dimension, pos) else { return false };
598 let (id, mut props) = split_state(&state);
599 for pair in properties.split(',').filter(|p| !p.is_empty()) {
600 let Some((k, v)) = pair.split_once('=') else { return false };
601 match props.iter_mut().find(|(n, _)| *n == k) {
602 Some(p) => p.1 = v,
603 None => props.push((k, v)),
604 }
605 }
606 let props: Vec<String> = props.iter().map(|(k, v)| format!("{k}={v}")).collect();
607 self.set_block_state(dimension, pos, &format!("{id}[{}]", props.join(",")))
608 }
609
610 /// Whether a thunderstorm is going on in `dimension`.
611 fn world_is_thundering(&self, dimension: &str) -> bool;
612
613 /// Starts (`true`) or ends a thunderstorm; `duration_ticks` 0 = a
614 /// server-chosen length.
615 fn world_set_thunder(&self, dimension: &str, thundering: bool, duration_ticks: i32) -> bool;
616
617 /// Sets every block of the box between `from` and `to` (inclusive, any
618 /// corners) to `state` (`"minecraft:barrier"`, `"minecraft:air"`, a full
619 /// state) in one call. How many blocks changed; `None` if the dimension
620 /// or state is unknown.
621 fn fill_region(&self, dimension: &str, from: BlockPos, to: BlockPos, state: &str) -> Option<u32>;
622
623 /// Every non-air block of the box between `from` and `to` with its full
624 /// state (see [`Server::get_block_state`]) — one call for a whole area.
625 fn scan_region(&self, dimension: &str, from: BlockPos, to: BlockPos) -> Vec<(BlockPos, String)>;
626}
627
628/// A block state (`"ns:id[k=v,...]"`) as its id and its properties.
629pub fn split_state(state: &str) -> (&str, Vec<(&str, &str)>) {
630 let (id, rest) = state.split_once('[').unwrap_or((state, ""));
631 let props = rest
632 .trim_end_matches(']')
633 .split(',')
634 .filter_map(|p| p.split_once('='))
635 .collect();
636 (id, props)
637}
638
639#[cfg(test)]
640mod state_tests {
641 use super::split_state;
642
643 #[test]
644 fn a_state_splits_into_its_id_and_properties() {
645 assert_eq!(split_state("minecraft:stone"), ("minecraft:stone", vec![]));
646 assert_eq!(
647 split_state("hexcasting:impetus/look[energized=false,facing=up]"),
648 ("hexcasting:impetus/look", vec![("energized", "false"), ("facing", "up")])
649 );
650 }
651}