yog_abi/lib.rs
1//! Yog stable C ABI — the ONLY types that cross the mod/runtime boundary.
2//!
3//! Rules this file must never break:
4//! - Every type is `#[repr(C)]`.
5//! - No Rust trait objects, no generics, no std types in public structs.
6//! - New fields are appended only at the END of structs; increment `ABI_MINOR`.
7//! - ABI_MAJOR bumps only when an existing field is removed or reordered.
8//!
9//! Mods and the runtime are compiled independently. They are compatible when
10//! `ABI_MAJOR` matches and `mod_ABI_MINOR <= runtime_ABI_MINOR`.
11
12use std::os::raw::c_void;
13
14// ── Version ──────────────────────────────────────────────────────────────────
15
16pub const ABI_MAJOR: u32 = 0;
17pub const ABI_MINOR: u32 = 34;
18/// `ABI_MAJOR * 10_000 + ABI_MINOR`. Checked at mod load time.
19pub const ABI_VERSION: u32 = ABI_MAJOR * 10_000 + ABI_MINOR;
20
21// ── Primitive types ───────────────────────────────────────────────────────────
22
23/// Borrowed UTF-8 byte slice passed to a function. NOT null-terminated.
24/// Valid only for the duration of the call that provides it — never store.
25#[repr(C)]
26#[derive(Copy, Clone)]
27pub struct YogStr {
28 pub ptr: *const u8,
29 pub len: u32,
30}
31
32impl YogStr {
33 pub const EMPTY: Self = Self {
34 ptr: std::ptr::null(),
35 len: 0,
36 };
37
38 /// Borrow `s` — not `FromStr`, which can't return a borrowing value.
39 #[allow(clippy::should_implement_trait)]
40 #[inline]
41 pub fn from_str(s: &str) -> Self {
42 Self {
43 ptr: s.as_ptr(),
44 len: s.len() as u32,
45 }
46 }
47
48 #[inline]
49 pub fn is_empty(self) -> bool {
50 self.len == 0 || self.ptr.is_null()
51 }
52
53 /// # Safety
54 ///
55 /// `ptr` must be valid UTF-8 of `len` bytes for at least the duration of
56 /// the call that provided this `YogStr`.
57 #[inline]
58 pub unsafe fn as_str<'a>(self) -> &'a str {
59 if self.ptr.is_null() || self.len == 0 {
60 return "";
61 }
62 std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.ptr, self.len as usize))
63 }
64}
65
66/// Heap-allocated UTF-8 string owned by the RUNTIME.
67/// `ptr == null` encodes `None` / "not found".
68/// When `ptr` is non-null the caller MUST free it via `YogServer::free_str`.
69#[repr(C)]
70#[derive(Copy, Clone)]
71pub struct YogOwnedStr {
72 pub ptr: *mut u8,
73 pub len: u32,
74}
75
76impl YogOwnedStr {
77 pub const NONE: Self = Self {
78 ptr: std::ptr::null_mut(),
79 len: 0,
80 };
81
82 /// Allocate a new `YogOwnedStr` from a Rust `String`.
83 pub fn from_string(s: String) -> Self {
84 let len = s.len() as u32;
85 let ptr = Box::into_raw(s.into_bytes().into_boxed_slice()) as *mut u8;
86 Self { ptr, len }
87 }
88
89 #[inline]
90 pub fn is_none(self) -> bool {
91 self.ptr.is_null()
92 }
93}
94
95/// Integer 3-D block position.
96#[repr(C)]
97#[derive(Copy, Clone)]
98pub struct YogBlockPos {
99 pub x: i32,
100 pub y: i32,
101 pub z: i32,
102}
103
104/// Float 3-D vector (position, velocity, …).
105#[repr(C)]
106#[derive(Copy, Clone)]
107pub struct YogVec3 {
108 pub x: f64,
109 pub y: f64,
110 pub z: f64,
111}
112
113// ── Event structs (Java → Rust) ───────────────────────────────────────────────
114
115#[repr(C)]
116pub struct YogBlockBreakEvent {
117 pub player: YogStr,
118 pub block: YogStr,
119 pub pos: YogBlockPos,
120 /// Dimension the block was broken in, e.g. `minecraft:overworld`.
121 pub dimension: YogStr,
122}
123
124#[repr(C)]
125pub struct YogChatEvent {
126 pub player: YogStr,
127 pub message: YogStr,
128 pub dimension: YogStr,
129}
130
131/// Shared by player_join and player_leave.
132#[repr(C)]
133pub struct YogPlayerEvent {
134 pub player: YogStr,
135 pub uuid: YogStr,
136 pub dimension: YogStr,
137}
138
139#[repr(C)]
140pub struct YogUseItemEvent {
141 pub player: YogStr,
142 pub item: YogStr,
143 /// ABI minor 25: whether the player was sneaking (shift) during use.
144 pub sneaking: bool,
145 pub dimension: YogStr,
146}
147
148#[repr(C)]
149pub struct YogUseBlockEvent {
150 pub player: YogStr,
151 pub block: YogStr,
152 pub pos: YogBlockPos,
153 pub dimension: YogStr,
154}
155
156#[repr(C)]
157pub struct YogAttackEntityEvent {
158 pub player: YogStr,
159 pub target_type: YogStr,
160 pub target_uuid: YogStr,
161 pub dimension: YogStr,
162}
163
164#[repr(C)]
165pub struct YogEntityDamageEvent {
166 pub entity_type: YogStr,
167 pub uuid: YogStr,
168 pub amount: f32,
169 pub source: YogStr,
170 pub dimension: YogStr,
171}
172
173#[repr(C)]
174pub struct YogEntityDeathEvent {
175 pub entity_type: YogStr,
176 pub uuid: YogStr,
177 pub source: YogStr,
178 pub dimension: YogStr,
179}
180
181#[repr(C)]
182pub struct YogEntitySpawnEvent {
183 pub entity_type: YogStr,
184 pub uuid: YogStr,
185 pub dimension: YogStr,
186}
187
188/// Fired when a player dies (Pre: before death is processed; Post: after death).
189/// Pre phase — return false to prevent death (keep entity alive at 0.5 HP).
190#[repr(C)]
191pub struct YogPlayerDeathEvent {
192 pub player: YogStr,
193 pub uuid: YogStr,
194 /// Damage source identifier, e.g. `"player"`, `"fall"`.
195 pub source: YogStr,
196 pub dimension: YogStr,
197}
198
199/// Fired when a player respawns after death.
200#[repr(C)]
201pub struct YogPlayerRespawnEvent {
202 pub player: YogStr,
203 pub uuid: YogStr,
204 /// True if respawning at a bed or anchor, false at world spawn.
205 pub at_anchor: bool,
206 /// Dimension respawned into (may differ from where the player died).
207 pub dimension: YogStr,
208}
209
210/// Fired when a player earns an advancement (Post only).
211#[repr(C)]
212pub struct YogAdvancementEvent {
213 pub player: YogStr,
214 pub uuid: YogStr,
215 /// Namespaced id, e.g. `"minecraft:story/mine_stone"`.
216 pub advancement: YogStr,
217 pub dimension: YogStr,
218}
219
220/// Fired when a player right-clicks (interacts with) an entity (Pre: before; Post: after).
221/// Pre phase — return false to cancel the interaction.
222#[repr(C)]
223pub struct YogEntityInteractEvent {
224 pub player: YogStr,
225 pub player_uuid: YogStr,
226 pub entity_type: YogStr,
227 pub entity_uuid: YogStr,
228 /// `"main_hand"` or `"off_hand"`.
229 pub hand: YogStr,
230 pub dimension: YogStr,
231}
232
233/// Fired when a player takes a crafted item from a crafting output slot (Post only).
234#[repr(C)]
235pub struct YogCraftEvent {
236 pub player: YogStr,
237 pub player_uuid: YogStr,
238 pub result_item: YogStr,
239 pub result_count: u32,
240 pub dimension: YogStr,
241}
242
243/// Fired when an explosion occurs in a world (Pre: before block destruction; Post: after).
244/// Pre phase — return false to cancel the explosion (block and entity damage suppressed).
245#[repr(C)]
246pub struct YogExplosionEvent {
247 pub dimension: YogStr,
248 pub x: f64,
249 pub y: f64,
250 pub z: f64,
251 pub power: f32,
252 /// UUID of the entity that caused the explosion, or empty if world/tnt.
253 pub cause_uuid: YogStr,
254}
255
256// ── ABI minor 9–10 event structs ──────────────────────────────────────────────
257
258/// Fired when a player picks up an item entity (Pre: cancellable; Post: informational).
259#[repr(C)]
260pub struct YogItemPickupEvent {
261 pub player: YogStr,
262 pub player_uuid: YogStr,
263 /// Registry id of the item, e.g. `"minecraft:diamond"`.
264 pub item_id: YogStr,
265 pub item_count: u32,
266 /// UUID of the item entity that was picked up.
267 pub entity_uuid: YogStr,
268 pub dimension: YogStr,
269}
270
271/// Fired when a player sends a movement packet (Post only; very high frequency).
272#[repr(C)]
273pub struct YogPlayerMoveEvent {
274 pub player: YogStr,
275 pub player_uuid: YogStr,
276 pub x: f64,
277 pub y: f64,
278 pub z: f64,
279 pub yaw: f32,
280 pub pitch: f32,
281 pub dimension: YogStr,
282}
283
284/// Fired when a player opens a container screen (Pre: cancellable; Post: informational).
285/// `container_type` is the screen handler registry id, e.g. `"minecraft:chest"`.
286/// Empty string if the type is not in the registry (e.g. the player inventory).
287#[repr(C)]
288pub struct YogContainerOpenEvent {
289 pub player: YogStr,
290 pub player_uuid: YogStr,
291 pub container_type: YogStr,
292 pub dimension: YogStr,
293}
294
295/// Fired when a player closes a container screen (Post only).
296#[repr(C)]
297pub struct YogContainerCloseEvent {
298 pub player: YogStr,
299 pub player_uuid: YogStr,
300 pub dimension: YogStr,
301}
302
303/// Fired when a persistent projectile (arrow, trident) hits a target (Pre: cancellable).
304/// Pre phase — return false to cancel the hit (projectile passes through).
305#[repr(C)]
306pub struct YogProjectileHitEvent {
307 /// Registry id of the projectile, e.g. `"minecraft:arrow"`.
308 pub projectile_type: YogStr,
309 pub projectile_uuid: YogStr,
310 /// UUID of the entity that shot/threw this projectile, or empty.
311 pub shooter_uuid: YogStr,
312 /// `"block"` or `"entity"`.
313 pub hit_type: YogStr,
314 /// UUID of the entity that was hit (empty for block hits).
315 pub hit_entity_uuid: YogStr,
316 pub x: f64,
317 pub y: f64,
318 pub z: f64,
319 pub dimension: YogStr,
320}
321
322/// Fired when a player places a block (Pre: before placement; Post: after).
323#[repr(C)]
324pub struct YogPlaceBlockEvent {
325 pub player: YogStr,
326 pub block: YogStr,
327 pub pos: YogBlockPos,
328 pub dimension: YogStr,
329}
330
331#[repr(C)]
332pub struct YogPacketEvent {
333 pub channel: YogStr,
334 pub player: YogStr, // empty string on client-received packets
335 pub payload: *const u8,
336 pub payload_len: u32,
337}
338
339#[repr(C)]
340pub struct YogCommandEvent {
341 pub name: YogStr,
342 pub args: YogStr,
343 pub source: YogStr,
344 pub uuid: YogStr,
345 /// Empty for console-run commands (no world).
346 pub dimension: YogStr,
347}
348
349/// A startup grant definition — items/books to give on first join.
350#[repr(C)]
351pub struct YogStartupGrantDef {
352 pub id: YogStr,
353 pub items: YogStr, // '|'-separated item ids
354 pub book: YogStr, // empty = none
355 pub command: YogStr, // empty = none
356}
357
358// ── Content definition structs (mod → runtime) ────────────────────────────────
359
360#[repr(C)]
361pub struct YogItemDef {
362 pub id: YogStr,
363 pub max_stack: u32,
364 pub name: YogStr, // empty = no override
365 pub tooltip: YogStr, // empty = none
366 pub max_damage: u32,
367 pub fire_resistant: bool,
368 pub fuel_ticks: u32,
369 pub food_nutrition: u32, // 0 = not a food item
370 pub food_saturation: f32,
371 pub food_always_eat: bool,
372}
373
374#[repr(C)]
375pub struct YogBlockDef {
376 pub id: YogStr,
377 pub hardness: f32,
378 pub resistance: f32,
379 pub name: YogStr,
380 pub light_level: u8,
381 pub sound: YogStr, // empty = default stone sound
382 pub requires_tool: bool,
383 pub no_collision: bool,
384 pub slipperiness: f32,
385 /// Bounding box in pixels: `[x1, y1, z1, x2, y2, z2]`. All zeros = full cube.
386 pub shape: [f32; 6],
387 /// Fence/pipe-style dynamic arm growth toward compatible neighbors — see
388 /// `yog_registry::BlockDef::connects_to_neighbors`.
389 pub connects: bool,
390 /// Comma-joined connection compatibility tags — see
391 /// `yog_registry::BlockDef::connect_groups`. Empty = no tags.
392 pub connect_groups: YogStr,
393 /// Id of a `YogInventoryDef` (registered via `register_inventory`) this
394 /// block is backed by — empty = no inventory (plain block, as before).
395 /// See `yog_inventory`'s DESIGN.md.
396 pub inventory_id: YogStr,
397}
398
399/// An inventory-backed screen definition — see `yog_inventory::InventoryDef`.
400#[repr(C)]
401pub struct YogInventoryDef {
402 pub id: YogStr,
403 pub slot_count: u32,
404 /// `"x:y,x:y,..."` — one pair per slot, in slot-index order. Empty =
405 /// use the default vanilla-style grid (see `yog_inventory::InventoryDef::default_grid`).
406 pub layout: YogStr,
407 pub include_player_inventory: bool,
408 pub player_inv_x: f32,
409 pub player_inv_y: f32,
410 /// Empty = default vanilla-style panel texture.
411 pub background_texture: YogStr,
412 pub title: YogStr,
413 /// Screen size to center the container on — `0.0` (either field) means
414 /// "use the vanilla default (176×166)". Set this to whatever a custom
415 /// `on_ui_render` overlay actually occupies so the real vanilla `Slot`s
416 /// (positioned by `layout`/`player_inv_x/y` relative to `guiLeft`/`guiTop`)
417 /// land under the matching decoration the overlay draws, instead of the
418 /// two disagreeing about where the container's top-left corner is.
419 pub background_w: f32,
420 pub background_h: f32,
421}
422
423// ── ABI minor 10 client event structs ─────────────────────────────────────────
424
425/// Key press / release / repeat from the keyboard (client-side only).
426#[repr(C)]
427#[derive(Copy, Clone)]
428pub struct YogKeyPressEvent {
429 /// GLFW key code (e.g. `GLFW_KEY_E = 69`).
430 pub key_code: i32,
431 pub scan_code: i32,
432 /// 0 = release, 1 = press, 2 = repeat.
433 pub action: i32,
434 /// Modifier bitmask: 1=shift, 2=ctrl, 4=alt, 8=super.
435 pub modifiers: i32,
436}
437
438// ── Handler function-pointer types ────────────────────────────────────────────
439//
440// All event handlers receive a `phase: u8` argument:
441// 0 = Pre — fires before the action; return value matters (false = cancel).
442// 1 = Post — fires after the action; return value is ignored.
443//
444// This unified signature lets one registered closure handle both phases.
445//
446// Client-side handlers (minor 10) do NOT receive a `YogServer*` — they run on
447// the render thread and have no server context.
448
449pub type YogBlockBreakFn =
450 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogBlockBreakEvent, u8) -> bool;
451pub type YogChatFn =
452 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogChatEvent, u8) -> bool;
453pub type YogPlayerFn =
454 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerEvent, u8) -> bool;
455pub type YogUseItemFn =
456 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogUseItemEvent, u8) -> bool;
457pub type YogUseBlockFn =
458 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogUseBlockEvent, u8) -> bool;
459pub type YogAttackEntityFn =
460 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogAttackEntityEvent, u8) -> bool;
461pub type YogEntityDamageFn =
462 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntityDamageEvent, u8) -> bool;
463pub type YogEntityDeathFn =
464 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntityDeathEvent, u8) -> bool;
465pub type YogEntitySpawnFn =
466 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntitySpawnEvent, u8) -> bool;
467pub type YogPlaceBlockFn =
468 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlaceBlockEvent, u8) -> bool;
469pub type YogPlayerDeathFn =
470 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerDeathEvent, u8) -> bool;
471pub type YogPlayerRespawnFn =
472 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerRespawnEvent, u8) -> bool;
473pub type YogAdvancementFn =
474 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogAdvancementEvent, u8) -> bool;
475pub type YogEntityInteractFn =
476 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntityInteractEvent, u8) -> bool;
477pub type YogCraftFn =
478 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogCraftEvent, u8) -> bool;
479pub type YogExplosionFn =
480 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogExplosionEvent, u8) -> bool;
481pub type YogItemPickupFn =
482 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogItemPickupEvent, u8) -> bool;
483pub type YogPlayerMoveFn =
484 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerMoveEvent, u8) -> bool;
485pub type YogContainerOpenFn =
486 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogContainerOpenEvent, u8) -> bool;
487pub type YogContainerCloseFn =
488 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogContainerCloseEvent, u8) -> bool;
489pub type YogProjectileHitFn =
490 unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogProjectileHitEvent, u8) -> bool;
491
492/// Packet events — always Post, no phase.
493pub type YogPacketFn = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPacketEvent);
494/// Server lifecycle / tick — no event struct, always fires.
495pub type YogServerFn = unsafe extern "C" fn(*mut c_void, *const YogServer);
496/// Command handler.
497pub type YogCommandFn = unsafe extern "C" fn(
498 ud: *mut c_void,
499 srv: *const YogServer,
500 ev: *const YogCommandEvent,
501 reply_buf: *mut u8,
502 reply_cap: u32,
503 reply_len: *mut u32,
504);
505/// Scheduler handler (once or repeating).
506pub type YogScheduledFn = unsafe extern "C" fn(*mut c_void, *const YogServer);
507
508// ── ABI minor 10 — client-side function pointer types ────────────────────────
509
510/// Client tick — no event, no server context.
511pub type YogClientFn = unsafe extern "C" fn(ud: *mut c_void);
512/// UI event for a `register_ui` screen. `event_id` is one of
513/// `click:X:Y`, `drag:X:Y`, `release:X:Y`, `scroll:DY` and (ABI minor 33)
514/// `key:KEY:MODS` — a GLFW key code pressed or repeating, with GLFW modifier
515/// bits — and `char:CODEPOINT` — a typed character (Unicode scalar value).
516/// Call `YogApi::ui_consume_event` while handling `key:`/`char:` to stop the
517/// host's default handling of that key (Esc closing the screen, ...).
518pub type YogUIEventFn = unsafe extern "C" fn(ud: *mut c_void, ui_id: YogStr, event_id: YogStr);
519/// HUD render — `gfx` is the graphics context for this frame; only valid for
520/// the call duration. `draw2d_*` functions in `gfx` work here.
521pub type YogHudRenderFn = unsafe extern "C" fn(ud: *mut c_void, gfx: *const YogGfxApi);
522/// World render — `gfx` contains `view_proj` and `camera_pos` for 3D rendering.
523/// Valid only for the call duration.
524pub type YogWorldRenderFn = unsafe extern "C" fn(ud: *mut c_void, gfx: *const YogGfxApi);
525/// Key press — return `false` to cancel (prevent Minecraft from processing the key).
526pub type YogKeyPressFn = unsafe extern "C" fn(ud: *mut c_void, ev: *const YogKeyPressEvent) -> bool;
527/// Screen event — `screen_class` is the simple class name (e.g. `"InventoryScreen"`).
528/// For `on_screen_open` return `false` to prevent the screen from opening.
529pub type YogScreenFn = unsafe extern "C" fn(ud: *mut c_void, screen_class: YogStr) -> bool;
530
531// ── ABI minor 14 — low-level GPU pipeline ────────────────────────────────────
532
533/// Low-level GPU context passed to render handlers.
534///
535/// Provides direct access to the OpenGL pipeline: buffer objects, vertex arrays,
536/// shader programs, textures, draw calls, and render state.
537///
538/// The per-frame fields (`screen_w/h`, `delta_tick`, `view_proj`, `camera_pos`)
539/// are filled by the runtime before calling the handler; the function pointers
540/// point to statically-allocated implementations.
541///
542/// Valid only for the duration of the render callback — never store the pointer.
543/// GPU resource **handles** (`u32`) may be stored between frames.
544///
545/// Colors are `0xAARRGGBB` (Minecraft convention).
546#[repr(C)]
547#[derive(Copy, Clone)]
548pub struct YogGfxApi {
549 // ── Per-frame context ─────────────────────────────────────────────────────
550 /// GUI-pixel screen width.
551 pub screen_w: i32,
552 /// GUI-pixel screen height.
553 pub screen_h: i32,
554 /// Partial-tick interpolation factor (0.0–1.0).
555 pub delta_tick: f32,
556 /// GUI scale factor: physical pixels per GUI pixel (e.g. 2.0 for 2× GUI scale).
557 /// Useful for converting GUI-pixel coordinates to physical pixels for OpenGL calls.
558 pub scale_factor: f32,
559 /// View-projection matrix in camera-relative space (column-major, 16 × f32).
560 /// All zeros during `on_hud_render`; filled during `on_world_render`.
561 pub view_proj: [f32; 16],
562 /// Camera world-space position. All zeros during `on_hud_render`.
563 pub camera_pos: [f32; 3],
564 /// Local player world-space position (eye height). All zeros during `on_hud_render`.
565 /// Differs from `camera_pos` in third-person view.
566 pub player_pos: [f32; 3],
567 pub _pad1: f32,
568
569 // ── GPU buffers ───────────────────────────────────────────────────────────
570 /// Allocate a new GPU buffer (VBO / EBO). Returns 0 on failure.
571 pub buf_create: unsafe extern "C" fn() -> u32,
572 /// Delete a buffer created by `buf_create`.
573 pub buf_delete: unsafe extern "C" fn(handle: u32),
574 /// Upload `len` bytes from `bytes` into `handle`.
575 /// `dynamic`: hints frequent updates (`GL_DYNAMIC_DRAW` vs `GL_STATIC_DRAW`).
576 pub buf_data: unsafe extern "C" fn(handle: u32, bytes: *const u8, len: u32, dynamic: bool),
577 /// Overwrite `len` bytes at `offset` in `handle`.
578 pub buf_subdata: unsafe extern "C" fn(handle: u32, offset: u32, bytes: *const u8, len: u32),
579
580 // ── Vertex arrays ─────────────────────────────────────────────────────────
581 /// Create a vertex array object. Returns 0 on failure.
582 pub vao_create: unsafe extern "C" fn() -> u32,
583 /// Delete a VAO created by `vao_create`.
584 pub vao_delete: unsafe extern "C" fn(handle: u32),
585 /// Declare one vertex attribute in `vao`, sourced from `vbo`.
586 /// `dtype`: 0=f32, 1=u8, 2=i32, 3=u32.
587 pub vao_attrib: unsafe extern "C" fn(
588 vao: u32,
589 vbo: u32,
590 index: u32,
591 components: u8,
592 dtype: u8,
593 normalized: bool,
594 stride: u32,
595 offset: u32,
596 ),
597 /// Bind an index buffer (EBO) to `vao`.
598 pub vao_set_ebo: unsafe extern "C" fn(vao: u32, ebo: u32),
599
600 // ── Shader programs ───────────────────────────────────────────────────────
601 /// Compile + link `vert_src` / `frag_src` (GLSL 150 core).
602 /// Writes the program handle to `*out`. Returns false and logs on error.
603 pub prog_create: unsafe extern "C" fn(vert: YogStr, frag: YogStr, out: *mut u32) -> bool,
604 /// Delete a shader program.
605 pub prog_delete: unsafe extern "C" fn(handle: u32),
606 pub prog_uniform_1i: unsafe extern "C" fn(prog: u32, name: YogStr, v: i32),
607 pub prog_uniform_1f: unsafe extern "C" fn(prog: u32, name: YogStr, v: f32),
608 pub prog_uniform_2f: unsafe extern "C" fn(prog: u32, name: YogStr, x: f32, y: f32),
609 pub prog_uniform_3f: unsafe extern "C" fn(prog: u32, name: YogStr, x: f32, y: f32, z: f32),
610 pub prog_uniform_4f:
611 unsafe extern "C" fn(prog: u32, name: YogStr, x: f32, y: f32, z: f32, w: f32),
612 /// Set a mat4 uniform from 16 column-major floats.
613 pub prog_uniform_mat4: unsafe extern "C" fn(prog: u32, name: YogStr, col_major: *const f32),
614
615 // ── Textures ──────────────────────────────────────────────────────────────
616 /// Upload RGBA8 pixel data as a new texture.
617 /// `linear`: `GL_LINEAR` if true, `GL_NEAREST` if false.
618 pub tex_create: unsafe extern "C" fn(w: u32, h: u32, rgba: *const u8, linear: bool) -> u32,
619 /// Delete a texture created by `tex_create`.
620 pub tex_delete: unsafe extern "C" fn(handle: u32),
621 /// Bind `handle` to texture unit `unit` (0–7).
622 pub tex_bind: unsafe extern "C" fn(unit: u32, handle: u32),
623 /// Return the GL texture handle Minecraft uses for a namespaced resource
624 /// (e.g. `"minecraft:textures/gui/icons.png"`). Returns 0 if not found.
625 /// Do **not** delete handles obtained this way — Minecraft owns them.
626 pub tex_from_mc: unsafe extern "C" fn(id: YogStr) -> u32,
627
628 // ── Draw calls ────────────────────────────────────────────────────────────
629 /// Draw `count` primitives from `vao`, using shader `prog`.
630 /// `mode`: 0=Triangles, 1=Lines, 2=LineStrip, 3=TriangleStrip, 4=TriangleFan.
631 pub draw_arrays: unsafe extern "C" fn(vao: u32, prog: u32, mode: u8, first: u32, count: u32),
632 /// Draw indexed primitives. `ebo` must be bound to `vao` via `vao_set_ebo`.
633 /// `u32_idx`: `true` for `u32` indices, `false` for `u16` indices.
634 pub draw_elements:
635 unsafe extern "C" fn(vao: u32, ebo: u32, prog: u32, mode: u8, count: u32, u32_idx: bool),
636
637 // ── Render state ──────────────────────────────────────────────────────────
638 /// Enable/disable blending. `src`/`dst` are raw GL blend factor enum values.
639 pub set_blend: unsafe extern "C" fn(enabled: bool, src: u32, dst: u32),
640 /// Enable/disable depth testing and depth writes.
641 pub set_depth: unsafe extern "C" fn(test: bool, write: bool),
642 /// Enable scissor clipping (GUI-pixel rectangle).
643 pub set_scissor: unsafe extern "C" fn(x: i32, y: i32, w: i32, h: i32),
644 /// Disable scissor clipping.
645 pub clear_scissor: unsafe extern "C" fn(),
646 /// Set the GL viewport (physical pixel coordinates).
647 pub set_viewport: unsafe extern "C" fn(x: i32, y: i32, w: i32, h: i32),
648
649 // ── 2D convenience (HUD-render only — uses MC's DrawContext) ─────────────
650 /// Filled rectangle. Only valid during `on_hud_render`.
651 pub draw2d_rect: unsafe extern "C" fn(x1: f32, y1: f32, x2: f32, y2: f32, color: u32),
652 /// Vertical-gradient rectangle. Only valid during `on_hud_render`.
653 pub draw2d_gradient:
654 unsafe extern "C" fn(x1: f32, y1: f32, x2: f32, y2: f32, top: u32, bottom: u32),
655 /// MC text renderer string. Only valid during `on_hud_render`.
656 pub draw2d_text: unsafe extern "C" fn(text: YogStr, x: f32, y: f32, color: u32, shadow: bool),
657 /// Blit from a Minecraft-managed texture. Only valid during `on_hud_render`.
658 /// `(u0, v0)` in texels; `(w, h)` in pixels; `(tw, th)` full texture size.
659 pub draw2d_mc_tex: unsafe extern "C" fn(
660 id: YogStr,
661 x: f32,
662 y: f32,
663 u0: f32,
664 v0: f32,
665 w: f32,
666 h: f32,
667 tw: f32,
668 th: f32,
669 ),
670
671 // ── appended in ABI minor 21 ──────────────────────────────────────────────
672 /// Render an item stack (3D block models included) via MC's item renderer,
673 /// like Patchouli's `renderItemStack`. `id` is a registry item id
674 /// ("minecraft:crafting_table"); `size` is the on-screen size in GUI px
675 /// (16 = inventory size). Only valid during `on_hud_render`.
676 pub draw2d_item: unsafe extern "C" fn(id: YogStr, x: f32, y: f32, size: f32),
677
678 // ── appended in ABI minor 33 ──────────────────────────────────────────────
679 /// `draw2d_text` in the font `font` — a font id such as
680 /// `"minecraft:default"`, `"minecraft:uniform"`, `"minecraft:alt"` or a
681 /// resource-pack font (`"mymod:fancy"`); empty = the default font. Only
682 /// valid during a render callback.
683 pub draw2d_text_font: unsafe extern "C" fn(
684 text: YogStr,
685 font: YogStr,
686 x: f32,
687 y: f32,
688 color: u32,
689 shadow: bool,
690 ),
691 /// Width in GUI pixels of `text` drawn in `font` (empty = default), as the
692 /// host's text renderer measures it. Callable from the render thread at
693 /// any time (render callbacks and UI event handlers); the pointer stays
694 /// valid for the life of the process.
695 pub text_width: unsafe extern "C" fn(text: YogStr, font: YogStr) -> f32,
696
697 // ── appended in ABI minor 34 ──────────────────────────────────────────────
698 /// Draw the image `source` — a Yog image source (`item:apple`,
699 /// `block:stone`, `sprite:button`, `mymod:gui/logo`, ...; see
700 /// `yog_gfx::image`), resolved by the host for its Minecraft version —
701 /// scaled into `(x, y, w, h)` (GUI pixels, nearest-neighbour). `(u, v,
702 /// uw, vh)` is the part of the image to draw, in the image's own pixels;
703 /// `uw <= 0` or `vh <= 0` = the whole image (items and sprites are
704 /// always whole). `tint` multiplies the colors (`0xAARRGGBB`;
705 /// `0xFFFFFFFF` = none; items ignore it). Only valid during a render
706 /// callback.
707 pub draw2d_image: unsafe extern "C" fn(
708 source: YogStr,
709 x: f32,
710 y: f32,
711 w: f32,
712 h: f32,
713 u: f32,
714 v: f32,
715 uw: f32,
716 vh: f32,
717 tint: u32,
718 ),
719 /// Natural size in GUI pixels of the image `source` (16×16 for items, a
720 /// texture's pixel size, ...) into `*w`/`*h`. `false` (outputs untouched)
721 /// when the source can't be resolved. Callable from the render thread at
722 /// any time; the pointer stays valid for the life of the process.
723 pub image_size: unsafe extern "C" fn(source: YogStr, w: *mut f32, h: *mut f32) -> bool,
724}
725
726unsafe impl Send for YogGfxApi {}
727unsafe impl Sync for YogGfxApi {}
728
729// ── Server action table (runtime → mod direction is wrong; it's mod → runtime) ─
730
731/// All Minecraft-mutating calls available inside a handler.
732///
733/// `ctx` is an opaque pointer to the runtime's JNI state. Every function takes
734/// it as its first argument. The pointer is valid for the lifetime of the process.
735///
736/// Strings **returned** by functions in this table are heap-allocated by the
737/// runtime and must be freed with `free_str` after the caller has read them.
738#[repr(C)]
739pub struct YogServer {
740 pub ctx: *mut c_void,
741 pub abi_version: u32,
742 /// `sizeof(YogServer)` at build time — allows mods compiled against an older
743 /// table to detect and skip fields they don't know about.
744 pub size: u32,
745
746 /// Free a string returned by any function in this table.
747 pub free_str: unsafe extern "C" fn(ptr: *mut u8, len: u32),
748
749 // ── chat ─────────────────────────────────────────────────────────────────
750 pub broadcast: unsafe extern "C" fn(ctx: *mut c_void, msg: YogStr),
751
752 // ── world ────────────────────────────────────────────────────────────────
753 pub get_block:
754 unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr,
755 pub set_block: unsafe extern "C" fn(
756 ctx: *mut c_void,
757 dim: YogStr,
758 pos: YogBlockPos,
759 block: YogStr,
760 ) -> bool,
761 pub world_time: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, out: *mut i64) -> bool,
762 pub set_time: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, time: i64) -> bool,
763 pub is_raining: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr) -> bool,
764 pub set_weather:
765 unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, raining: bool, dur: i32) -> bool,
766
767 // ── player ───────────────────────────────────────────────────────────────
768 pub give_item:
769 unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, item: YogStr, count: u32) -> bool,
770 pub player_teleport:
771 unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, pos: YogVec3) -> bool,
772 pub send_to_player: unsafe extern "C" fn(
773 ctx: *mut c_void,
774 player: YogStr,
775 channel: YogStr,
776 data: *const u8,
777 len: u32,
778 ) -> bool,
779 pub send_to_server:
780 unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, data: *const u8, len: u32) -> bool,
781 pub kick_player: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, reason: YogStr) -> bool,
782 pub set_gamemode: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, mode: YogStr) -> bool,
783 pub send_title: unsafe extern "C" fn(
784 ctx: *mut c_void,
785 player: YogStr,
786 title: YogStr,
787 sub: YogStr,
788 fi: i32,
789 stay: i32,
790 fo: i32,
791 ) -> bool,
792 pub send_actionbar: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, msg: YogStr) -> bool,
793 pub play_sound: unsafe extern "C" fn(
794 ctx: *mut c_void,
795 dim: YogStr,
796 pos: YogVec3,
797 sound: YogStr,
798 vol: f32,
799 pitch: f32,
800 ) -> bool,
801 pub play_sound_player: unsafe extern "C" fn(
802 ctx: *mut c_void,
803 player: YogStr,
804 sound: YogStr,
805 vol: f32,
806 pitch: f32,
807 ) -> bool,
808
809 // ── entity ───────────────────────────────────────────────────────────────
810 pub entity_teleport: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, pos: YogVec3) -> bool,
811 pub entity_position:
812 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
813 pub entity_health: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut f32) -> bool,
814 pub entity_set_health: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, hp: f32) -> bool,
815 pub entity_kill: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> bool,
816 pub spawn_entity: unsafe extern "C" fn(
817 ctx: *mut c_void,
818 type_id: YogStr,
819 dim: YogStr,
820 pos: YogVec3,
821 ) -> YogOwnedStr,
822 pub entity_add_effect: unsafe extern "C" fn(
823 ctx: *mut c_void,
824 uuid: YogStr,
825 fx: YogStr,
826 dur: i32,
827 amp: u8,
828 particles: bool,
829 ) -> bool,
830 pub entity_remove_effect:
831 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, fx: YogStr) -> bool,
832 pub entity_clear_effects: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> bool,
833 pub entity_velocity:
834 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
835 pub entity_set_velocity:
836 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, vel: YogVec3) -> bool,
837 pub entity_add_velocity:
838 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, vel: YogVec3) -> bool,
839
840 // ── tags & loot ──────────────────────────────────────────────────────────
841 pub has_item_tag: unsafe extern "C" fn(ctx: *mut c_void, item: YogStr, tag: YogStr) -> bool,
842 pub has_block_tag: unsafe extern "C" fn(ctx: *mut c_void, block: YogStr, tag: YogStr) -> bool,
843 pub drop_loot:
844 unsafe extern "C" fn(ctx: *mut c_void, table: YogStr, dim: YogStr, pos: YogVec3) -> bool,
845
846 // ── scoreboard ───────────────────────────────────────────────────────────
847 pub scoreboard_get:
848 unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, out: *mut i32) -> bool,
849 pub scoreboard_set:
850 unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, score: i32) -> bool,
851 pub scoreboard_add: unsafe extern "C" fn(
852 ctx: *mut c_void,
853 obj: YogStr,
854 player: YogStr,
855 delta: i32,
856 out: *mut i32,
857 ) -> bool,
858
859 // ── boss bar ─────────────────────────────────────────────────────────────
860 pub bossbar_create: unsafe extern "C" fn(
861 ctx: *mut c_void,
862 id: YogStr,
863 title: YogStr,
864 color: YogStr,
865 style: YogStr,
866 ) -> bool,
867 pub bossbar_remove: unsafe extern "C" fn(ctx: *mut c_void, id: YogStr) -> bool,
868 pub bossbar_set_title:
869 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, title: YogStr) -> bool,
870 pub bossbar_set_progress:
871 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, progress: f32) -> bool,
872 pub bossbar_set_color:
873 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, color: YogStr) -> bool,
874 pub bossbar_add_player:
875 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, player: YogStr) -> bool,
876 pub bossbar_remove_player:
877 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, player: YogStr) -> bool,
878 pub bossbar_set_visible:
879 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, visible: bool) -> bool,
880
881 // ── misc ─────────────────────────────────────────────────────────────────
882 pub game_dir: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
883
884 // ── player query (ABI minor 4) ────────────────────────────────────────────
885 /// Newline-separated list of online player names, or NONE if server not up.
886 pub online_players: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
887
888 // ── block entity (NBT, ABI minor 3) ──────────────────────────────────────
889 /// SNBT string of the block entity at `pos`, or NONE if there is none.
890 pub get_block_nbt:
891 unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr,
892 /// Write SNBT into the block entity at `pos`. Returns false if no block entity exists.
893 pub set_block_nbt:
894 unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos, snbt: YogStr) -> bool,
895
896 // ── inventory (ABI minor 3) ───────────────────────────────────────────────
897 /// Tab/newline-encoded inventory: one line per occupied slot, `slot\titem_id\tcount`.
898 pub player_inventory: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
899 /// Set (or clear when count==0) a specific inventory slot.
900 pub player_set_slot: unsafe extern "C" fn(
901 ctx: *mut c_void,
902 player: YogStr,
903 slot: u32,
904 item_id: YogStr,
905 count: u32,
906 ) -> bool,
907
908 // ── inventory-backed block slots (ABI minor 25, yog-inventory) ──────────
909 /// `"item_id\tcount"` for the given slot of the inventory-backed block at
910 /// `pos`, or NONE if the slot is empty / there is no such inventory there.
911 pub get_inventory_slot: unsafe extern "C" fn(
912 ctx: *mut c_void,
913 dim: YogStr,
914 pos: YogBlockPos,
915 slot: u32,
916 ) -> YogOwnedStr,
917 /// Set (or clear when count==0) a specific slot of the inventory-backed
918 /// block at `pos`. Returns false if there is no such inventory there.
919 pub set_inventory_slot: unsafe extern "C" fn(
920 ctx: *mut c_void,
921 dim: YogStr,
922 pos: YogBlockPos,
923 slot: u32,
924 item_id: YogStr,
925 count: u32,
926 ) -> bool,
927
928 // ── cross-dimension teleport (ABI minor 3) ────────────────────────────────
929 pub player_teleport_dim:
930 unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, dim: YogStr, pos: YogVec3) -> bool,
931 pub entity_teleport_dim:
932 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, dim: YogStr, pos: YogVec3) -> bool,
933
934 // ── entity counting ───────────────────────────────────────────────────────
935 /// Count loaded instances of `entity_type` in `dimension`. Returns -1 on error.
936 pub world_entity_count:
937 unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, entity_type: YogStr) -> i32,
938
939 // ── entity NBT (ABI minor 6) ──────────────────────────────────────────────
940 /// SNBT of the entity's persistent data, or NONE if entity not found.
941 pub entity_get_nbt: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr,
942 /// Merge SNBT data into the entity. Returns false if entity not found.
943 pub entity_set_nbt: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, snbt: YogStr) -> bool,
944
945 // ── particles (ABI minor 6) ───────────────────────────────────────────────
946 /// Spawn `count` particles at `pos` in `dim`.
947 /// `dx/dy/dz` control spread, `speed` controls particle speed.
948 /// Returns false if the dimension or particle type is unknown.
949 pub spawn_particles: unsafe extern "C" fn(
950 ctx: *mut c_void,
951 dim: YogStr,
952 pos: YogVec3,
953 particle_type: YogStr,
954 count: i32,
955 dx: f64,
956 dy: f64,
957 dz: f64,
958 speed: f64,
959 ) -> bool,
960
961 // ── attributes (ABI minor 7) ──────────────────────────────────────────────
962 /// Get the base value of an attribute on a living entity.
963 /// `attribute_id` is a registry id, e.g. `"minecraft:generic.max_health"`.
964 /// Returns `f64::NAN` if entity or attribute is not found.
965 pub entity_attribute_get:
966 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, attribute_id: YogStr) -> f64,
967 /// Set the base value of an attribute. Returns false if entity or attribute is not found.
968 pub entity_attribute_set: unsafe extern "C" fn(
969 ctx: *mut c_void,
970 uuid: YogStr,
971 attribute_id: YogStr,
972 value: f64,
973 ) -> bool,
974
975 // ── held item NBT (ABI minor 11) ─────────────────────────────────────────
976 /// SNBT of the item currently held in the player's main hand.
977 /// Returns NONE if the player is offline or holding air.
978 pub get_held_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
979 /// Merge `snbt` data into the NBT of the player's held main-hand item in-place.
980 /// Returns false if the player is offline or holding air.
981 pub set_held_item_nbt:
982 unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, snbt: YogStr) -> bool,
983
984 // ── item stack query (ABI minor 12) ──────────────────────────────────────
985 /// SNBT of the item in the player's off hand, or NONE if offline / holding air.
986 pub get_offhand_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
987 /// Merge `snbt` into the NBT of the player's off-hand item.
988 /// Returns false if offline or holding air.
989 pub set_offhand_item_nbt:
990 unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, snbt: YogStr) -> bool,
991 /// Full item stack at inventory `slot`: tab-separated `item_id\tcount\tsnbt`.
992 /// `snbt` is `{}` when the item has no NBT. Returns NONE if offline or slot empty.
993 pub get_slot_item:
994 unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, slot: u32) -> YogOwnedStr,
995 /// Replace inventory `slot` with an item stack. `snbt` may be empty to clear NBT.
996 /// Pass `count == 0` to clear the slot (ignores `item_id` and `snbt`).
997 pub set_slot_item: unsafe extern "C" fn(
998 ctx: *mut c_void,
999 player: YogStr,
1000 slot: u32,
1001 item_id: YogStr,
1002 count: u32,
1003 snbt: YogStr,
1004 ) -> bool,
1005
1006 // ── entity rotation (ABI minor 25) ───────────────────────────────────────
1007 /// Yaw and pitch (degrees) of an entity by UUID. Returns false if the
1008 /// entity does not exist. `out` receives x=yaw, y=pitch, z=0.
1009 pub entity_rotation:
1010 unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
1011}
1012
1013// ctx = *mut JavaVM which is global/stable. All fn ptrs are pure C-ABI.
1014unsafe impl Send for YogServer {}
1015unsafe impl Sync for YogServer {}
1016
1017// ── Chunk generator callback (ABI minor 29) ───────────────────────────────────
1018
1019/// Per-callback context handed to a registered chunk generator (see
1020/// `YogApi::register_chunk_generator`) — mirrors `YogGfxApi`'s pattern of
1021/// bundling per-call context with the function pointer(s) needed to act on
1022/// it. Valid only for the duration of the generator callback.
1023#[repr(C)]
1024#[derive(Copy, Clone)]
1025pub struct YogChunkWriterApi {
1026 /// Chunk X coordinate (in chunks, not blocks).
1027 pub chunk_x: i32,
1028 /// Chunk Z coordinate (in chunks, not blocks).
1029 pub chunk_z: i32,
1030 /// Minimum build height (world Y) for this dimension.
1031 pub min_y: i32,
1032 /// Total world height for this dimension.
1033 pub height: i32,
1034 /// Opaque host-side handle for the chunk currently being generated.
1035 pub ctx: *mut c_void,
1036 /// Set a block within this chunk column. `x`/`z` are local (0..16)
1037 /// chunk-relative coordinates; `y` is world-absolute.
1038 pub set_block:
1039 unsafe extern "C" fn(ctx: *mut c_void, x: i32, y: i32, z: i32, block_id: YogStr) -> bool,
1040}
1041
1042unsafe impl Send for YogChunkWriterApi {}
1043unsafe impl Sync for YogChunkWriterApi {}
1044
1045/// Called by the host once per chunk column that needs generating in the
1046/// registered dimension. `writer` is valid only for the duration of the call.
1047pub type YogChunkGeneratorFn =
1048 unsafe extern "C" fn(ud: *mut c_void, writer: *const YogChunkWriterApi);
1049
1050// ── Registration table (passed to yog_mod_register) ──────────────────────────
1051
1052/// Passed to `yog_mod_register`. Call the function pointers here to register
1053/// handlers, commands, content, and schedulers.
1054///
1055/// When mods compiled against ABI `N` load on a runtime with ABI `M > N`:
1056/// fields beyond `size` are not present in the mod's view — check `size` before
1057/// accessing fields added in later minor versions.
1058#[repr(C)]
1059pub struct YogApi {
1060 pub abi_version: u32,
1061 /// `sizeof(YogApi)` at the runtime's build time.
1062 pub size: u32,
1063 /// Opaque pointer to runtime handler storage.
1064 pub ctx: *mut c_void,
1065 /// Stable server action table — pass to handlers.
1066 pub server: *const YogServer,
1067
1068 // ── events — all handlers receive (ud, srv, event, phase: u8) → bool ────────
1069 // phase 0 = Pre (return false to cancel), phase 1 = Post (return ignored).
1070 pub on_block_break: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogBlockBreakFn),
1071 pub on_chat: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogChatFn),
1072 pub on_player_join: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerFn),
1073 pub on_player_leave: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerFn),
1074 pub on_use_item: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogUseItemFn),
1075 pub on_use_block: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogUseBlockFn),
1076 pub on_attack_entity:
1077 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogAttackEntityFn),
1078 pub on_entity_damage:
1079 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityDamageFn),
1080 pub on_entity_death:
1081 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityDeathFn),
1082 pub on_entity_spawn:
1083 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntitySpawnFn),
1084 pub on_player_place_block:
1085 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlaceBlockFn),
1086 pub on_player_death:
1087 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerDeathFn),
1088 pub on_player_respawn:
1089 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerRespawnFn),
1090 pub on_advancement:
1091 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogAdvancementFn),
1092 // ── ABI minor 8 ──────────────────────────────────────────────────────────
1093 pub on_entity_interact:
1094 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityInteractFn),
1095 pub on_item_craft: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogCraftFn),
1096 pub on_explosion: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogExplosionFn),
1097 // ── ABI minor 9 ──────────────────────────────────────────────────────────
1098 pub on_item_pickup: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogItemPickupFn),
1099 pub on_player_move: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerMoveFn),
1100 pub on_container_open:
1101 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogContainerOpenFn),
1102 pub on_container_close:
1103 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogContainerCloseFn),
1104 pub on_projectile_hit:
1105 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogProjectileHitFn),
1106 pub on_server_tick: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
1107 pub on_server_started: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
1108 pub on_server_stopping: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
1109
1110 // ── networking ───────────────────────────────────────────────────────────
1111 pub on_packet:
1112 unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, ud: *mut c_void, h: YogPacketFn),
1113 pub on_client_packet:
1114 unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, ud: *mut c_void, h: YogPacketFn),
1115
1116 // ── commands ─────────────────────────────────────────────────────────────
1117 pub register_command:
1118 unsafe extern "C" fn(ctx: *mut c_void, name: YogStr, ud: *mut c_void, h: YogCommandFn),
1119 pub register_typed_command: unsafe extern "C" fn(
1120 ctx: *mut c_void,
1121 name: YogStr,
1122 schema: YogStr,
1123 ud: *mut c_void,
1124 h: YogCommandFn,
1125 ),
1126
1127 // ── recipes ──────────────────────────────────────────────────────────────
1128 /// Register a recipe by supplying Minecraft JSON (`data/` format).
1129 /// `namespace` + `name` form the file path: `data/{ns}/recipes/{name}.json`.
1130 pub register_recipe_json:
1131 unsafe extern "C" fn(ctx: *mut c_void, namespace: YogStr, name: YogStr, json: YogStr),
1132
1133 // ── content ──────────────────────────────────────────────────────────────
1134 pub register_item: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogItemDef),
1135 pub register_block: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogBlockDef),
1136
1137 // ── scheduler ────────────────────────────────────────────────────────────
1138 pub schedule_once: unsafe extern "C" fn(
1139 ctx: *mut c_void,
1140 delay_ticks: u64,
1141 ud: *mut c_void,
1142 h: YogScheduledFn,
1143 ),
1144 pub schedule_repeating: unsafe extern "C" fn(
1145 ctx: *mut c_void,
1146 period_ticks: u64,
1147 ud: *mut c_void,
1148 h: YogScheduledFn,
1149 ),
1150
1151 // ── ABI minor 10 — client-side events ────────────────────────────────────
1152 pub on_client_tick: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogClientFn),
1153 pub on_hud_render: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogHudRenderFn),
1154 pub on_key_press: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogKeyPressFn),
1155 pub on_screen_open: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogScreenFn),
1156 pub on_screen_close: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogScreenFn),
1157
1158 // ── ABI minor 14 — world render ──────────────────────────────────────────
1159 /// Register a handler that fires after world geometry is rendered.
1160 /// `gfx.view_proj` and `gfx.camera_pos` are filled; use them to project
1161 /// custom 3D geometry into clip space.
1162 pub on_world_render:
1163 unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogWorldRenderFn),
1164
1165 // ── ABI minor 16 — startup grants ────────────────────────────────────────
1166 pub register_startup_grant:
1167 unsafe extern "C" fn(ctx: *mut c_void, grant: *const YogStartupGrantDef),
1168
1169 // ── ABI minor 18 — books ─────────────────────────────────────────────────
1170 pub register_book: unsafe extern "C" fn(ctx: *mut c_void, book_id: YogStr, book_json: YogStr),
1171
1172 // ── ABI minor 19 — UI system ──────────────────────────────────────────────
1173 /// Register a UI tree. `ui_id` is the unique identifier (e.g. "mymod:menu").
1174 /// `layout_json` is the serialized layout tree.
1175 /// `handler` is called when an interactive element is clicked/keyed.
1176 pub register_ui: unsafe extern "C" fn(
1177 ctx: *mut c_void,
1178 ui_id: YogStr,
1179 layout_json: YogStr,
1180 ud: *mut c_void,
1181 h: YogUIEventFn,
1182 ),
1183
1184 // ── ABI minor 20 — per-UI screen renderer ────────────────────────────────
1185 /// Register a render callback that fires during `YogUIScreen.render()` for a
1186 /// specific UI id — i.e. AFTER the screen darkening, unlike `on_hud_render`.
1187 /// Reuses `YogHudRenderFn` (`fn(ud, gfx)`); `gfx.screen_w/h` are set.
1188 /// Clicks are forwarded as `"click:X:Y"` events to the `register_ui` handler
1189 /// so mods can do their own hit-testing with their stored layout.
1190 pub on_ui_render:
1191 unsafe extern "C" fn(ctx: *mut c_void, ui_id: YogStr, ud: *mut c_void, h: YogHudRenderFn),
1192
1193 // ── ABI minor 22 — menu entry registration ──────────────────────────────
1194 /// Register a button/link that the host renders on vanilla screens
1195 /// (TitleScreen on Fabric, ModListScreen on Forge/NeoForge).
1196 /// `label` is the human-readable button text (e.g. "Yog Mods").
1197 /// `ui_id` is the Yog UI to open when clicked (e.g. "yog:modlist").
1198 pub register_menu_entry: unsafe extern "C" fn(ctx: *mut c_void, label: YogStr, ui_id: YogStr),
1199
1200 // ── ABI minor 23 — installed mods listing ───────────────────────────────
1201 /// All installed mods as TSV, one per line:
1202 /// `source \t id \t name \t version \t authors \t description`
1203 /// where `source` is `yog` (a .yog mod) or `platform` (a loader mod, e.g.
1204 /// a Fabric/Forge jar). Tabs and newlines inside fields become spaces.
1205 /// Callable at any time after registration (also client-side).
1206 pub mods_list: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
1207 /// Free a `YogOwnedStr` returned by an api-table call (same allocator as
1208 /// `YogServer::free_str`).
1209 pub free_str: unsafe extern "C" fn(ptr: *mut u8, len: u32),
1210
1211 // ── ABI minor 24 — open a Yog UI screen ─────────────────────────────────
1212 /// Open the Yog UI registered as `ui_id` on the client (schedules onto the
1213 /// render thread). Client-side only; a no-op on dedicated servers.
1214 /// `modal` blocks game input; `pause` pauses a singleplayer game.
1215 pub ui_open: unsafe extern "C" fn(ctx: *mut c_void, ui_id: YogStr, modal: bool, pause: bool),
1216
1217 // ── ABI minor 25 — inventory framework (yog-inventory) ──────────────────
1218 /// Register a real Container/Menu inventory screen — see `yog_inventory`.
1219 pub register_inventory: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogInventoryDef),
1220
1221 // ── ABI minor 26 — inter-mod communication ─────────────────────────────
1222 /// Export a function pointer under `symbol` for the calling mod (`mod_id`).
1223 /// Other mods can import it via `interop_import`.
1224 pub interop_export:
1225 unsafe extern "C" fn(ctx: *mut c_void, mod_id: YogStr, symbol: YogStr, ptr: *const c_void),
1226 /// Import a function pointer exported by `mod_id` under `symbol`.
1227 /// Returns null if the symbol is not (yet) registered.
1228 pub interop_import:
1229 unsafe extern "C" fn(ctx: *mut c_void, mod_id: YogStr, symbol: YogStr) -> *const c_void,
1230
1231 // ── ABI minor 29 — custom dimensions ────────────────────────────────────
1232 /// Register a dimension-type definition (id + `YogDimensionTypeDef` JSON).
1233 /// Must be called at mod-init time (same window as blocks/items) — the
1234 /// host registers a `LevelStem`/`DimensionType` entry before the
1235 /// registry freezes. See `yog-dimensions` crate.
1236 pub register_dimension_json: unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, json: YogStr),
1237 /// Register a chunk generator for a previously-declared dimension `id`.
1238 /// `handler` is called once per chunk column that needs generating —
1239 /// see `YogChunkWriterApi`/`YogChunkGeneratorFn`.
1240 pub register_chunk_generator:
1241 unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, ud: *mut c_void, h: YogChunkGeneratorFn),
1242
1243 // ── ABI minor 33 — closing Yog UI screens, consuming UI key events ──────
1244 /// Close the Yog UI screen showing `ui_id` (empty = whichever Yog UI
1245 /// screen is open). Scheduled onto the render thread, so it's safe to
1246 /// call from that screen's own event handler. Client-side only.
1247 pub ui_close: unsafe extern "C" fn(ctx: *mut c_void, ui_id: YogStr),
1248 /// Called from inside a `register_ui` handler while it handles a `key:`
1249 /// or `char:` event: the mod used the key, so the host skips its default
1250 /// handling (Esc closing the screen, Tab focus navigation, ...).
1251 pub ui_consume_event: unsafe extern "C" fn(ctx: *mut c_void),
1252
1253 // ── ABI minor 33 — translations ─────────────────────────────────────────
1254 /// The text of translation key `key` (e.g. `mymod.screen.title`) in the
1255 /// game's current language, from the loaded lang files — resource packs
1256 /// and `.yog` mods' `assets/<ns>/lang/*.json` (compiled from `.ylang`).
1257 /// `YogOwnedStr::NONE` when no language defines the key. Format
1258 /// specifiers (`%s`, `%1$s`) come back as-is. Free with `free_str`.
1259 /// Client-side; a dedicated server only knows `en_us`.
1260 pub translate: unsafe extern "C" fn(ctx: *mut c_void, key: YogStr) -> YogOwnedStr,
1261}
1262
1263unsafe impl Send for YogApi {}
1264unsafe impl Sync for YogApi {}