Skip to main content

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; the runtime loads a mod
10//! only when its `ABI_VERSION` is exactly the runtime's.
11
12use std::os::raw::c_void;
13
14// ── Version ──────────────────────────────────────────────────────────────────
15
16pub const ABI_MAJOR: u32 = 1;
17pub const ABI_MINOR: u32 = 0;
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
658    // ── appended in ABI minor 21 ──────────────────────────────────────────────
659    /// Render an item stack (3D block models included) via MC's item renderer,
660    /// like Patchouli's `renderItemStack`. `id` is a registry item id
661    /// ("minecraft:crafting_table"); `size` is the on-screen size in GUI px
662    /// (16 = inventory size). Only valid during `on_hud_render`.
663    pub draw2d_item: unsafe extern "C" fn(id: YogStr, x: f32, y: f32, size: f32),
664
665    // ── appended in ABI minor 33 ──────────────────────────────────────────────
666    /// `draw2d_text` in the font `font` — a font id such as
667    /// `"minecraft:default"`, `"minecraft:uniform"`, `"minecraft:alt"` or a
668    /// resource-pack font (`"mymod:fancy"`); empty = the default font. Only
669    /// valid during a render callback.
670    pub draw2d_text_font: unsafe extern "C" fn(
671        text: YogStr,
672        font: YogStr,
673        x: f32,
674        y: f32,
675        color: u32,
676        shadow: bool,
677    ),
678    /// Width in GUI pixels of `text` drawn in `font` (empty = default), as the
679    /// host's text renderer measures it. Callable from the render thread at
680    /// any time (render callbacks and UI event handlers); the pointer stays
681    /// valid for the life of the process.
682    pub text_width: unsafe extern "C" fn(text: YogStr, font: YogStr) -> f32,
683
684    // ── appended in ABI minor 34 ──────────────────────────────────────────────
685    /// Draw the image `source` — a Yog image source (`item:apple`,
686    /// `block:stone`, `sprite:button`, `mymod:gui/logo`, ...; see
687    /// `yog_gfx::image`), resolved by the host for its Minecraft version —
688    /// scaled into `(x, y, w, h)` (GUI pixels, nearest-neighbour). `(u, v,
689    /// uw, vh)` is the part of the image to draw, in the image's own pixels;
690    /// `uw <= 0` or `vh <= 0` = the whole image (items and sprites are
691    /// always whole). `tint` multiplies the colors (`0xAARRGGBB`;
692    /// `0xFFFFFFFF` = none; items ignore it). Only valid during a render
693    /// callback.
694    pub draw2d_image: unsafe extern "C" fn(
695        source: YogStr,
696        x: f32,
697        y: f32,
698        w: f32,
699        h: f32,
700        u: f32,
701        v: f32,
702        uw: f32,
703        vh: f32,
704        tint: u32,
705    ),
706    /// Natural size in GUI pixels of the image `source` (16×16 for items, a
707    /// texture's pixel size, ...) into `*w`/`*h`. `false` (outputs untouched)
708    /// when the source can't be resolved. Callable from the render thread at
709    /// any time; the pointer stays valid for the life of the process.
710    pub image_size: unsafe extern "C" fn(source: YogStr, w: *mut f32, h: *mut f32) -> bool,
711}
712
713unsafe impl Send for YogGfxApi {}
714unsafe impl Sync for YogGfxApi {}
715
716// ── Server action table (runtime → mod direction is wrong; it's mod → runtime) ─
717
718/// All Minecraft-mutating calls available inside a handler.
719///
720/// `ctx` is an opaque pointer to the runtime's JNI state.  Every function takes
721/// it as its first argument.  The pointer is valid for the lifetime of the process.
722///
723/// Strings **returned** by functions in this table are heap-allocated by the
724/// runtime and must be freed with `free_str` after the caller has read them.
725#[repr(C)]
726pub struct YogServer {
727    pub ctx: *mut c_void,
728    pub abi_version: u32,
729    /// `sizeof(YogServer)` at build time — allows mods compiled against an older
730    /// table to detect and skip fields they don't know about.
731    pub size: u32,
732
733    /// Free a string returned by any function in this table.
734    pub free_str: unsafe extern "C" fn(ptr: *mut u8, len: u32),
735
736    // ── chat ─────────────────────────────────────────────────────────────────
737    pub broadcast: unsafe extern "C" fn(ctx: *mut c_void, msg: YogStr),
738
739    // ── world ────────────────────────────────────────────────────────────────
740    pub get_block:
741        unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr,
742    pub set_block: unsafe extern "C" fn(
743        ctx: *mut c_void,
744        dim: YogStr,
745        pos: YogBlockPos,
746        block: YogStr,
747    ) -> bool,
748    pub world_time: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, out: *mut i64) -> bool,
749    pub set_time: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, time: i64) -> bool,
750    pub is_raining: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr) -> bool,
751    pub set_weather:
752        unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, raining: bool, dur: i32) -> bool,
753
754    // ── player ───────────────────────────────────────────────────────────────
755    pub give_item:
756        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, item: YogStr, count: u32) -> bool,
757    pub player_teleport:
758        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, pos: YogVec3) -> bool,
759    pub send_to_player: unsafe extern "C" fn(
760        ctx: *mut c_void,
761        player: YogStr,
762        channel: YogStr,
763        data: *const u8,
764        len: u32,
765    ) -> bool,
766    pub send_to_server:
767        unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, data: *const u8, len: u32) -> bool,
768    pub kick_player: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, reason: YogStr) -> bool,
769    pub set_gamemode: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, mode: YogStr) -> bool,
770    pub send_title: unsafe extern "C" fn(
771        ctx: *mut c_void,
772        player: YogStr,
773        title: YogStr,
774        sub: YogStr,
775        fi: i32,
776        stay: i32,
777        fo: i32,
778    ) -> bool,
779    pub send_actionbar: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, msg: YogStr) -> bool,
780    pub play_sound: unsafe extern "C" fn(
781        ctx: *mut c_void,
782        dim: YogStr,
783        pos: YogVec3,
784        sound: YogStr,
785        vol: f32,
786        pitch: f32,
787    ) -> bool,
788    pub play_sound_player: unsafe extern "C" fn(
789        ctx: *mut c_void,
790        player: YogStr,
791        sound: YogStr,
792        vol: f32,
793        pitch: f32,
794    ) -> bool,
795
796    // ── entity ───────────────────────────────────────────────────────────────
797    pub entity_teleport: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, pos: YogVec3) -> bool,
798    pub entity_position:
799        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
800    pub entity_health: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut f32) -> bool,
801    pub entity_set_health: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, hp: f32) -> bool,
802    pub entity_kill: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> bool,
803    pub spawn_entity: unsafe extern "C" fn(
804        ctx: *mut c_void,
805        type_id: YogStr,
806        dim: YogStr,
807        pos: YogVec3,
808    ) -> YogOwnedStr,
809    pub entity_add_effect: unsafe extern "C" fn(
810        ctx: *mut c_void,
811        uuid: YogStr,
812        fx: YogStr,
813        dur: i32,
814        amp: u8,
815        particles: bool,
816    ) -> bool,
817    pub entity_remove_effect:
818        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, fx: YogStr) -> bool,
819    pub entity_clear_effects: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> bool,
820    pub entity_velocity:
821        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
822    pub entity_set_velocity:
823        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, vel: YogVec3) -> bool,
824    pub entity_add_velocity:
825        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, vel: YogVec3) -> bool,
826
827    // ── tags & loot ──────────────────────────────────────────────────────────
828    pub has_item_tag: unsafe extern "C" fn(ctx: *mut c_void, item: YogStr, tag: YogStr) -> bool,
829    pub has_block_tag: unsafe extern "C" fn(ctx: *mut c_void, block: YogStr, tag: YogStr) -> bool,
830    pub drop_loot:
831        unsafe extern "C" fn(ctx: *mut c_void, table: YogStr, dim: YogStr, pos: YogVec3) -> bool,
832
833    // ── scoreboard ───────────────────────────────────────────────────────────
834    pub scoreboard_get:
835        unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, out: *mut i32) -> bool,
836    pub scoreboard_set:
837        unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, score: i32) -> bool,
838    pub scoreboard_add: unsafe extern "C" fn(
839        ctx: *mut c_void,
840        obj: YogStr,
841        player: YogStr,
842        delta: i32,
843        out: *mut i32,
844    ) -> bool,
845
846    // ── boss bar ─────────────────────────────────────────────────────────────
847    pub bossbar_create: unsafe extern "C" fn(
848        ctx: *mut c_void,
849        id: YogStr,
850        title: YogStr,
851        color: YogStr,
852        style: YogStr,
853    ) -> bool,
854    pub bossbar_remove: unsafe extern "C" fn(ctx: *mut c_void, id: YogStr) -> bool,
855    pub bossbar_set_title:
856        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, title: YogStr) -> bool,
857    pub bossbar_set_progress:
858        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, progress: f32) -> bool,
859    pub bossbar_set_color:
860        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, color: YogStr) -> bool,
861    pub bossbar_add_player:
862        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, player: YogStr) -> bool,
863    pub bossbar_remove_player:
864        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, player: YogStr) -> bool,
865    pub bossbar_set_visible:
866        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, visible: bool) -> bool,
867
868    // ── misc ─────────────────────────────────────────────────────────────────
869    pub game_dir: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
870
871    // ── player query (ABI minor 4) ────────────────────────────────────────────
872    /// Newline-separated list of online player names, or NONE if server not up.
873    pub online_players: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
874
875    // ── block entity (NBT, ABI minor 3) ──────────────────────────────────────
876    /// SNBT string of the block entity at `pos`, or NONE if there is none.
877    pub get_block_nbt:
878        unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr,
879    /// Write SNBT into the block entity at `pos`. Returns false if no block entity exists.
880    pub set_block_nbt:
881        unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos, snbt: YogStr) -> bool,
882
883    // ── inventory (ABI minor 3) ───────────────────────────────────────────────
884    /// Tab/newline-encoded inventory: one line per occupied slot, `slot\titem_id\tcount`.
885    pub player_inventory: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
886    /// Set (or clear when count==0) a specific inventory slot.
887    pub player_set_slot: unsafe extern "C" fn(
888        ctx: *mut c_void,
889        player: YogStr,
890        slot: u32,
891        item_id: YogStr,
892        count: u32,
893    ) -> bool,
894
895    // ── inventory-backed block slots (ABI minor 25, yog-inventory) ──────────
896    /// `"item_id\tcount"` for the given slot of the inventory-backed block at
897    /// `pos`, or NONE if the slot is empty / there is no such inventory there.
898    pub get_inventory_slot: unsafe extern "C" fn(
899        ctx: *mut c_void,
900        dim: YogStr,
901        pos: YogBlockPos,
902        slot: u32,
903    ) -> YogOwnedStr,
904    /// Set (or clear when count==0) a specific slot of the inventory-backed
905    /// block at `pos`. Returns false if there is no such inventory there.
906    pub set_inventory_slot: unsafe extern "C" fn(
907        ctx: *mut c_void,
908        dim: YogStr,
909        pos: YogBlockPos,
910        slot: u32,
911        item_id: YogStr,
912        count: u32,
913    ) -> bool,
914
915    // ── cross-dimension teleport (ABI minor 3) ────────────────────────────────
916    pub player_teleport_dim:
917        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, dim: YogStr, pos: YogVec3) -> bool,
918    pub entity_teleport_dim:
919        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, dim: YogStr, pos: YogVec3) -> bool,
920
921    // ── entity counting ───────────────────────────────────────────────────────
922    /// Count loaded instances of `entity_type` in `dimension`. Returns -1 on error.
923    pub world_entity_count:
924        unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, entity_type: YogStr) -> i32,
925
926    // ── entity NBT (ABI minor 6) ──────────────────────────────────────────────
927    /// SNBT of the entity's persistent data, or NONE if entity not found.
928    pub entity_get_nbt: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr,
929    /// Merge SNBT data into the entity. Returns false if entity not found.
930    pub entity_set_nbt: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, snbt: YogStr) -> bool,
931
932    // ── particles (ABI minor 6) ───────────────────────────────────────────────
933    /// Spawn `count` particles at `pos` in `dim`.
934    /// `dx/dy/dz` control spread, `speed` controls particle speed.
935    /// Returns false if the dimension or particle type is unknown.
936    pub spawn_particles: unsafe extern "C" fn(
937        ctx: *mut c_void,
938        dim: YogStr,
939        pos: YogVec3,
940        particle_type: YogStr,
941        count: i32,
942        dx: f64,
943        dy: f64,
944        dz: f64,
945        speed: f64,
946    ) -> bool,
947
948    // ── attributes (ABI minor 7) ──────────────────────────────────────────────
949    /// Get the base value of an attribute on a living entity.
950    /// `attribute_id` is a registry id, e.g. `"minecraft:generic.max_health"`.
951    /// Returns `f64::NAN` if entity or attribute is not found.
952    pub entity_attribute_get:
953        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, attribute_id: YogStr) -> f64,
954    /// Set the base value of an attribute. Returns false if entity or attribute is not found.
955    pub entity_attribute_set: unsafe extern "C" fn(
956        ctx: *mut c_void,
957        uuid: YogStr,
958        attribute_id: YogStr,
959        value: f64,
960    ) -> bool,
961
962    // ── held item NBT (ABI minor 11) ─────────────────────────────────────────
963    /// SNBT of the item currently held in the player's main hand.
964    /// Returns NONE if the player is offline or holding air.
965    pub get_held_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
966    /// Merge `snbt` data into the NBT of the player's held main-hand item in-place.
967    /// Returns false if the player is offline or holding air.
968    pub set_held_item_nbt:
969        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, snbt: YogStr) -> bool,
970
971    // ── item stack query (ABI minor 12) ──────────────────────────────────────
972    /// SNBT of the item in the player's off hand, or NONE if offline / holding air.
973    pub get_offhand_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
974    /// Merge `snbt` into the NBT of the player's off-hand item.
975    /// Returns false if offline or holding air.
976    pub set_offhand_item_nbt:
977        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, snbt: YogStr) -> bool,
978    /// Full item stack at inventory `slot`: tab-separated `item_id\tcount\tsnbt`.
979    /// `snbt` is `{}` when the item has no NBT. Returns NONE if offline or slot empty.
980    pub get_slot_item:
981        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, slot: u32) -> YogOwnedStr,
982    /// Replace inventory `slot` with an item stack. `snbt` may be empty to clear NBT.
983    /// Pass `count == 0` to clear the slot (ignores `item_id` and `snbt`).
984    pub set_slot_item: unsafe extern "C" fn(
985        ctx: *mut c_void,
986        player: YogStr,
987        slot: u32,
988        item_id: YogStr,
989        count: u32,
990        snbt: YogStr,
991    ) -> bool,
992
993    // ── entity rotation (ABI minor 25) ───────────────────────────────────────
994    /// Yaw and pitch (degrees) of an entity by UUID. Returns false if the
995    /// entity does not exist. `out` receives x=yaw, y=pitch, z=0.
996    pub entity_rotation:
997        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
998}
999
1000// ctx = *mut JavaVM which is global/stable. All fn ptrs are pure C-ABI.
1001unsafe impl Send for YogServer {}
1002unsafe impl Sync for YogServer {}
1003
1004// ── Chunk generator callback (ABI minor 29) ───────────────────────────────────
1005
1006/// Per-callback context handed to a registered chunk generator (see
1007/// `YogApi::register_chunk_generator`) — mirrors `YogGfxApi`'s pattern of
1008/// bundling per-call context with the function pointer(s) needed to act on
1009/// it. Valid only for the duration of the generator callback.
1010#[repr(C)]
1011#[derive(Copy, Clone)]
1012pub struct YogChunkWriterApi {
1013    /// Chunk X coordinate (in chunks, not blocks).
1014    pub chunk_x: i32,
1015    /// Chunk Z coordinate (in chunks, not blocks).
1016    pub chunk_z: i32,
1017    /// Minimum build height (world Y) for this dimension.
1018    pub min_y: i32,
1019    /// Total world height for this dimension.
1020    pub height: i32,
1021    /// Opaque host-side handle for the chunk currently being generated.
1022    pub ctx: *mut c_void,
1023    /// Set a block within this chunk column. `x`/`z` are local (0..16)
1024    /// chunk-relative coordinates; `y` is world-absolute.
1025    pub set_block:
1026        unsafe extern "C" fn(ctx: *mut c_void, x: i32, y: i32, z: i32, block_id: YogStr) -> bool,
1027}
1028
1029unsafe impl Send for YogChunkWriterApi {}
1030unsafe impl Sync for YogChunkWriterApi {}
1031
1032/// Called by the host once per chunk column that needs generating in the
1033/// registered dimension. `writer` is valid only for the duration of the call.
1034pub type YogChunkGeneratorFn =
1035    unsafe extern "C" fn(ud: *mut c_void, writer: *const YogChunkWriterApi);
1036
1037// ── Registration table (passed to yog_mod_register) ──────────────────────────
1038
1039/// Passed to `yog_mod_register`. Call the function pointers here to register
1040/// handlers, commands, content, and schedulers.
1041///
1042/// When mods compiled against ABI `N` load on a runtime with ABI `M > N`:
1043/// fields beyond `size` are not present in the mod's view — check `size` before
1044/// accessing fields added in later minor versions.
1045#[repr(C)]
1046pub struct YogApi {
1047    pub abi_version: u32,
1048    /// `sizeof(YogApi)` at the runtime's build time.
1049    pub size: u32,
1050    /// Opaque pointer to runtime handler storage.
1051    pub ctx: *mut c_void,
1052    /// Stable server action table — pass to handlers.
1053    pub server: *const YogServer,
1054
1055    // ── events — all handlers receive (ud, srv, event, phase: u8) → bool ────────
1056    // phase 0 = Pre (return false to cancel), phase 1 = Post (return ignored).
1057    pub on_block_break: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogBlockBreakFn),
1058    pub on_chat: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogChatFn),
1059    pub on_player_join: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerFn),
1060    pub on_player_leave: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerFn),
1061    pub on_use_item: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogUseItemFn),
1062    pub on_use_block: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogUseBlockFn),
1063    pub on_attack_entity:
1064        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogAttackEntityFn),
1065    pub on_entity_damage:
1066        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityDamageFn),
1067    pub on_entity_death:
1068        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityDeathFn),
1069    pub on_entity_spawn:
1070        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntitySpawnFn),
1071    pub on_player_place_block:
1072        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlaceBlockFn),
1073    pub on_player_death:
1074        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerDeathFn),
1075    pub on_player_respawn:
1076        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerRespawnFn),
1077    pub on_advancement:
1078        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogAdvancementFn),
1079    // ── ABI minor 8 ──────────────────────────────────────────────────────────
1080    pub on_entity_interact:
1081        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityInteractFn),
1082    pub on_item_craft: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogCraftFn),
1083    pub on_explosion: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogExplosionFn),
1084    // ── ABI minor 9 ──────────────────────────────────────────────────────────
1085    pub on_item_pickup: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogItemPickupFn),
1086    pub on_player_move: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerMoveFn),
1087    pub on_container_open:
1088        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogContainerOpenFn),
1089    pub on_container_close:
1090        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogContainerCloseFn),
1091    pub on_projectile_hit:
1092        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogProjectileHitFn),
1093    pub on_server_tick: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
1094    pub on_server_started: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
1095    pub on_server_stopping: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
1096
1097    // ── networking ───────────────────────────────────────────────────────────
1098    pub on_packet:
1099        unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, ud: *mut c_void, h: YogPacketFn),
1100    pub on_client_packet:
1101        unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, ud: *mut c_void, h: YogPacketFn),
1102
1103    // ── commands ─────────────────────────────────────────────────────────────
1104    pub register_command:
1105        unsafe extern "C" fn(ctx: *mut c_void, name: YogStr, ud: *mut c_void, h: YogCommandFn),
1106    pub register_typed_command: unsafe extern "C" fn(
1107        ctx: *mut c_void,
1108        name: YogStr,
1109        schema: YogStr,
1110        ud: *mut c_void,
1111        h: YogCommandFn,
1112    ),
1113
1114    // ── recipes ──────────────────────────────────────────────────────────────
1115    /// Register a recipe by supplying Minecraft JSON (`data/` format).
1116    /// `namespace` + `name` form the file path: `data/{ns}/recipes/{name}.json`.
1117    pub register_recipe_json:
1118        unsafe extern "C" fn(ctx: *mut c_void, namespace: YogStr, name: YogStr, json: YogStr),
1119
1120    // ── content ──────────────────────────────────────────────────────────────
1121    pub register_item: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogItemDef),
1122    pub register_block: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogBlockDef),
1123
1124    // ── scheduler ────────────────────────────────────────────────────────────
1125    pub schedule_once: unsafe extern "C" fn(
1126        ctx: *mut c_void,
1127        delay_ticks: u64,
1128        ud: *mut c_void,
1129        h: YogScheduledFn,
1130    ),
1131    pub schedule_repeating: unsafe extern "C" fn(
1132        ctx: *mut c_void,
1133        period_ticks: u64,
1134        ud: *mut c_void,
1135        h: YogScheduledFn,
1136    ),
1137
1138    // ── ABI minor 10 — client-side events ────────────────────────────────────
1139    pub on_client_tick: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogClientFn),
1140    pub on_hud_render: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogHudRenderFn),
1141    pub on_key_press: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogKeyPressFn),
1142    pub on_screen_open: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogScreenFn),
1143    pub on_screen_close: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogScreenFn),
1144
1145    // ── ABI minor 14 — world render ──────────────────────────────────────────
1146    /// Register a handler that fires after world geometry is rendered.
1147    /// `gfx.view_proj` and `gfx.camera_pos` are filled; use them to project
1148    /// custom 3D geometry into clip space.
1149    pub on_world_render:
1150        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogWorldRenderFn),
1151
1152    // ── ABI minor 16 — startup grants ────────────────────────────────────────
1153    pub register_startup_grant:
1154        unsafe extern "C" fn(ctx: *mut c_void, grant: *const YogStartupGrantDef),
1155
1156    // ── ABI minor 18 — books ─────────────────────────────────────────────────
1157    pub register_book: unsafe extern "C" fn(ctx: *mut c_void, book_id: YogStr, book_json: YogStr),
1158
1159    // ── ABI minor 19 — UI system ──────────────────────────────────────────────
1160    /// Register a UI tree. `ui_id` is the unique identifier (e.g. "mymod:menu").
1161    /// `layout_json` is the serialized layout tree.
1162    /// `handler` is called when an interactive element is clicked/keyed.
1163    pub register_ui: unsafe extern "C" fn(
1164        ctx: *mut c_void,
1165        ui_id: YogStr,
1166        layout_json: YogStr,
1167        ud: *mut c_void,
1168        h: YogUIEventFn,
1169    ),
1170
1171    // ── ABI minor 20 — per-UI screen renderer ────────────────────────────────
1172    /// Register a render callback that fires during `YogUIScreen.render()` for a
1173    /// specific UI id — i.e. AFTER the screen darkening, unlike `on_hud_render`.
1174    /// Reuses `YogHudRenderFn` (`fn(ud, gfx)`); `gfx.screen_w/h` are set.
1175    /// Clicks are forwarded as `"click:X:Y"` events to the `register_ui` handler
1176    /// so mods can do their own hit-testing with their stored layout.
1177    pub on_ui_render:
1178        unsafe extern "C" fn(ctx: *mut c_void, ui_id: YogStr, ud: *mut c_void, h: YogHudRenderFn),
1179
1180    // ── ABI minor 22 — menu entry registration ──────────────────────────────
1181    /// Register a button/link that the host renders on vanilla screens
1182    /// (TitleScreen on Fabric, ModListScreen on Forge/NeoForge).
1183    /// `label` is the human-readable button text (e.g. "Yog Mods").
1184    /// `ui_id` is the Yog UI to open when clicked (e.g. "yog:modlist").
1185    pub register_menu_entry: unsafe extern "C" fn(ctx: *mut c_void, label: YogStr, ui_id: YogStr),
1186
1187    // ── ABI minor 23 — installed mods listing ───────────────────────────────
1188    /// All installed mods as TSV, one per line:
1189    /// `source \t id \t name \t version \t authors \t description`
1190    /// where `source` is `yog` (a .yog mod) or `platform` (a loader mod, e.g.
1191    /// a Fabric/Forge jar). Tabs and newlines inside fields become spaces.
1192    /// Callable at any time after registration (also client-side).
1193    pub mods_list: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
1194    /// Free a `YogOwnedStr` returned by an api-table call (same allocator as
1195    /// `YogServer::free_str`).
1196    pub free_str: unsafe extern "C" fn(ptr: *mut u8, len: u32),
1197
1198    // ── ABI minor 24 — open a Yog UI screen ─────────────────────────────────
1199    /// Open the Yog UI registered as `ui_id` on the client (schedules onto the
1200    /// render thread). Client-side only; a no-op on dedicated servers.
1201    /// `modal` blocks game input; `pause` pauses a singleplayer game.
1202    pub ui_open: unsafe extern "C" fn(ctx: *mut c_void, ui_id: YogStr, modal: bool, pause: bool),
1203
1204    // ── ABI minor 25 — inventory framework (yog-inventory) ──────────────────
1205    /// Register a real Container/Menu inventory screen — see `yog_inventory`.
1206    pub register_inventory: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogInventoryDef),
1207
1208    // ── ABI minor 26 — inter-mod communication ─────────────────────────────
1209    /// Export a function pointer under `symbol` for the calling mod (`mod_id`).
1210    /// Other mods can import it via `interop_import`.
1211    pub interop_export:
1212        unsafe extern "C" fn(ctx: *mut c_void, mod_id: YogStr, symbol: YogStr, ptr: *const c_void),
1213    /// Import a function pointer exported by `mod_id` under `symbol`.
1214    /// Returns null if the symbol is not (yet) registered.
1215    pub interop_import:
1216        unsafe extern "C" fn(ctx: *mut c_void, mod_id: YogStr, symbol: YogStr) -> *const c_void,
1217
1218    // ── ABI minor 29 — custom dimensions ────────────────────────────────────
1219    /// Register a dimension-type definition (id + `YogDimensionTypeDef` JSON).
1220    /// Must be called at mod-init time (same window as blocks/items) — the
1221    /// host registers a `LevelStem`/`DimensionType` entry before the
1222    /// registry freezes. See `yog-dimensions` crate.
1223    pub register_dimension_json: unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, json: YogStr),
1224    /// Register a chunk generator for a previously-declared dimension `id`.
1225    /// `handler` is called once per chunk column that needs generating —
1226    /// see `YogChunkWriterApi`/`YogChunkGeneratorFn`.
1227    pub register_chunk_generator:
1228        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, ud: *mut c_void, h: YogChunkGeneratorFn),
1229
1230    // ── ABI minor 33 — closing Yog UI screens, consuming UI key events ──────
1231    /// Close the Yog UI screen showing `ui_id` (empty = whichever Yog UI
1232    /// screen is open). Scheduled onto the render thread, so it's safe to
1233    /// call from that screen's own event handler. Client-side only.
1234    pub ui_close: unsafe extern "C" fn(ctx: *mut c_void, ui_id: YogStr),
1235    /// Called from inside a `register_ui` handler while it handles a `key:`
1236    /// or `char:` event: the mod used the key, so the host skips its default
1237    /// handling (Esc closing the screen, Tab focus navigation, ...).
1238    pub ui_consume_event: unsafe extern "C" fn(ctx: *mut c_void),
1239
1240    // ── ABI minor 33 — translations ─────────────────────────────────────────
1241    /// The text of translation key `key` (e.g. `mymod.screen.title`) in the
1242    /// game's current language, from the loaded lang files — resource packs
1243    /// and `.yog` mods' `assets/<ns>/lang/*.json` (compiled from `.yres`).
1244    /// `YogOwnedStr::NONE` when no language defines the key. Format
1245    /// specifiers (`%s`, `%1$s`) come back as-is. Free with `free_str`.
1246    /// Client-side; a dedicated server only knows `en_us`.
1247    pub translate: unsafe extern "C" fn(ctx: *mut c_void, key: YogStr) -> YogOwnedStr,
1248}
1249
1250unsafe impl Send for YogApi {}
1251unsafe impl Sync for YogApi {}