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. 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 = 18;
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 { ptr: std::ptr::null(), len: 0 };
34
35    #[inline]
36    pub fn from_str(s: &str) -> Self {
37        Self { ptr: s.as_ptr(), len: s.len() as u32 }
38    }
39
40    #[inline]
41    pub fn is_empty(self) -> bool { self.len == 0 || self.ptr.is_null() }
42
43    /// SAFETY: `ptr` must be valid UTF-8 of `len` bytes for at least the
44    /// duration of the call that provided this `YogStr`.
45    #[inline]
46    pub unsafe fn as_str<'a>(self) -> &'a str {
47        if self.ptr.is_null() || self.len == 0 {
48            return "";
49        }
50        std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.ptr, self.len as usize))
51    }
52}
53
54/// Heap-allocated UTF-8 string owned by the RUNTIME.
55/// `ptr == null` encodes `None` / "not found".
56/// When `ptr` is non-null the caller MUST free it via `YogServer::free_str`.
57#[repr(C)]
58#[derive(Copy, Clone)]
59pub struct YogOwnedStr {
60    pub ptr: *mut u8,
61    pub len: u32,
62}
63
64impl YogOwnedStr {
65    pub const NONE: Self = Self { ptr: std::ptr::null_mut(), len: 0 };
66
67    /// Allocate a new `YogOwnedStr` from a Rust `String`.
68    pub fn from_string(s: String) -> Self {
69        let len = s.len() as u32;
70        let ptr = Box::into_raw(s.into_bytes().into_boxed_slice()) as *mut u8;
71        Self { ptr, len }
72    }
73
74    #[inline]
75    pub fn is_none(self) -> bool { self.ptr.is_null() }
76}
77
78/// Integer 3-D block position.
79#[repr(C)]
80#[derive(Copy, Clone)]
81pub struct YogBlockPos {
82    pub x: i32,
83    pub y: i32,
84    pub z: i32,
85}
86
87/// Float 3-D vector (position, velocity, …).
88#[repr(C)]
89#[derive(Copy, Clone)]
90pub struct YogVec3 {
91    pub x: f64,
92    pub y: f64,
93    pub z: f64,
94}
95
96// ── Event structs (Java → Rust) ───────────────────────────────────────────────
97
98#[repr(C)]
99pub struct YogBlockBreakEvent {
100    pub player: YogStr,
101    pub block:  YogStr,
102    pub pos:    YogBlockPos,
103}
104
105#[repr(C)]
106pub struct YogChatEvent {
107    pub player:  YogStr,
108    pub message: YogStr,
109}
110
111/// Shared by player_join and player_leave.
112#[repr(C)]
113pub struct YogPlayerEvent {
114    pub player: YogStr,
115    pub uuid:   YogStr,
116}
117
118#[repr(C)]
119pub struct YogUseItemEvent {
120    pub player: YogStr,
121    pub item:   YogStr,
122}
123
124#[repr(C)]
125pub struct YogUseBlockEvent {
126    pub player: YogStr,
127    pub block:  YogStr,
128    pub pos:    YogBlockPos,
129}
130
131#[repr(C)]
132pub struct YogAttackEntityEvent {
133    pub player:      YogStr,
134    pub target_type: YogStr,
135    pub target_uuid: YogStr,
136}
137
138#[repr(C)]
139pub struct YogEntityDamageEvent {
140    pub entity_type: YogStr,
141    pub uuid:        YogStr,
142    pub amount:      f32,
143    pub source:      YogStr,
144}
145
146#[repr(C)]
147pub struct YogEntityDeathEvent {
148    pub entity_type: YogStr,
149    pub uuid:        YogStr,
150    pub source:      YogStr,
151}
152
153#[repr(C)]
154pub struct YogEntitySpawnEvent {
155    pub entity_type: YogStr,
156    pub uuid:        YogStr,
157    pub dimension:   YogStr,
158}
159
160/// Fired when a player dies (Pre: before death is processed; Post: after death).
161/// Pre phase — return false to prevent death (keep entity alive at 0.5 HP).
162#[repr(C)]
163pub struct YogPlayerDeathEvent {
164    pub player: YogStr,
165    pub uuid:   YogStr,
166    /// Damage source identifier, e.g. `"player"`, `"fall"`.
167    pub source: YogStr,
168}
169
170/// Fired when a player respawns after death.
171#[repr(C)]
172pub struct YogPlayerRespawnEvent {
173    pub player:    YogStr,
174    pub uuid:      YogStr,
175    /// True if respawning at a bed or anchor, false at world spawn.
176    pub at_anchor: bool,
177}
178
179/// Fired when a player earns an advancement (Post only).
180#[repr(C)]
181pub struct YogAdvancementEvent {
182    pub player:      YogStr,
183    pub uuid:        YogStr,
184    /// Namespaced id, e.g. `"minecraft:story/mine_stone"`.
185    pub advancement: YogStr,
186}
187
188/// Fired when a player right-clicks (interacts with) an entity (Pre: before; Post: after).
189/// Pre phase — return false to cancel the interaction.
190#[repr(C)]
191pub struct YogEntityInteractEvent {
192    pub player:      YogStr,
193    pub player_uuid: YogStr,
194    pub entity_type: YogStr,
195    pub entity_uuid: YogStr,
196    /// `"main_hand"` or `"off_hand"`.
197    pub hand:        YogStr,
198}
199
200/// Fired when a player takes a crafted item from a crafting output slot (Post only).
201#[repr(C)]
202pub struct YogCraftEvent {
203    pub player:       YogStr,
204    pub player_uuid:  YogStr,
205    pub result_item:  YogStr,
206    pub result_count: u32,
207}
208
209/// Fired when an explosion occurs in a world (Pre: before block destruction; Post: after).
210/// Pre phase — return false to cancel the explosion (block and entity damage suppressed).
211#[repr(C)]
212pub struct YogExplosionEvent {
213    pub dimension:    YogStr,
214    pub x:            f64,
215    pub y:            f64,
216    pub z:            f64,
217    pub power:        f32,
218    /// UUID of the entity that caused the explosion, or empty if world/tnt.
219    pub cause_uuid:   YogStr,
220}
221
222// ── ABI minor 9–10 event structs ──────────────────────────────────────────────
223
224/// Fired when a player picks up an item entity (Pre: cancellable; Post: informational).
225#[repr(C)]
226pub struct YogItemPickupEvent {
227    pub player:      YogStr,
228    pub player_uuid: YogStr,
229    /// Registry id of the item, e.g. `"minecraft:diamond"`.
230    pub item_id:     YogStr,
231    pub item_count:  u32,
232    /// UUID of the item entity that was picked up.
233    pub entity_uuid: YogStr,
234}
235
236/// Fired when a player sends a movement packet (Post only; very high frequency).
237#[repr(C)]
238pub struct YogPlayerMoveEvent {
239    pub player:      YogStr,
240    pub player_uuid: YogStr,
241    pub x: f64,
242    pub y: f64,
243    pub z: f64,
244    pub yaw:   f32,
245    pub pitch: f32,
246}
247
248/// Fired when a player opens a container screen (Pre: cancellable; Post: informational).
249/// `container_type` is the screen handler registry id, e.g. `"minecraft:chest"`.
250/// Empty string if the type is not in the registry (e.g. the player inventory).
251#[repr(C)]
252pub struct YogContainerOpenEvent {
253    pub player:         YogStr,
254    pub player_uuid:    YogStr,
255    pub container_type: YogStr,
256}
257
258/// Fired when a player closes a container screen (Post only).
259#[repr(C)]
260pub struct YogContainerCloseEvent {
261    pub player:      YogStr,
262    pub player_uuid: YogStr,
263}
264
265/// Fired when a persistent projectile (arrow, trident) hits a target (Pre: cancellable).
266/// Pre phase — return false to cancel the hit (projectile passes through).
267#[repr(C)]
268pub struct YogProjectileHitEvent {
269    /// Registry id of the projectile, e.g. `"minecraft:arrow"`.
270    pub projectile_type: YogStr,
271    pub projectile_uuid: YogStr,
272    /// UUID of the entity that shot/threw this projectile, or empty.
273    pub shooter_uuid:    YogStr,
274    /// `"block"` or `"entity"`.
275    pub hit_type:        YogStr,
276    /// UUID of the entity that was hit (empty for block hits).
277    pub hit_entity_uuid: YogStr,
278    pub x: f64,
279    pub y: f64,
280    pub z: f64,
281    pub dimension: YogStr,
282}
283
284/// Fired when a player places a block (Pre: before placement; Post: after).
285#[repr(C)]
286pub struct YogPlaceBlockEvent {
287    pub player: YogStr,
288    pub block:  YogStr,
289    pub pos:    YogBlockPos,
290}
291
292#[repr(C)]
293pub struct YogPacketEvent {
294    pub channel:     YogStr,
295    pub player:      YogStr, // empty string on client-received packets
296    pub payload:     *const u8,
297    pub payload_len: u32,
298}
299
300#[repr(C)]
301pub struct YogCommandEvent {
302    pub name:   YogStr,
303    pub args:   YogStr,
304    pub source: YogStr,
305    pub uuid:   YogStr,
306}
307
308/// A startup grant definition — items/books to give on first join.
309#[repr(C)]
310pub struct YogStartupGrantDef {
311    pub id:    YogStr,
312    pub items: YogStr, // '|'-separated item ids
313    pub book:  YogStr, // empty = none
314    pub command: YogStr, // empty = none
315}
316
317// ── Content definition structs (mod → runtime) ────────────────────────────────
318
319#[repr(C)]
320pub struct YogItemDef {
321    pub id:              YogStr,
322    pub max_stack:       u32,
323    pub name:            YogStr, // empty = no override
324    pub tooltip:         YogStr, // empty = none
325    pub max_damage:      u32,
326    pub fire_resistant:  bool,
327    pub fuel_ticks:      u32,
328    pub food_nutrition:  u32,    // 0 = not a food item
329    pub food_saturation: f32,
330    pub food_always_eat: bool,
331}
332
333#[repr(C)]
334pub struct YogBlockDef {
335    pub id:            YogStr,
336    pub hardness:      f32,
337    pub resistance:    f32,
338    pub name:          YogStr,
339    pub light_level:   u8,
340    pub sound:         YogStr,   // empty = default stone sound
341    pub requires_tool: bool,
342    pub no_collision:  bool,
343    pub slipperiness:  f32,
344    /// Bounding box in pixels: `[x1, y1, z1, x2, y2, z2]`. All zeros = full cube.
345    pub shape:         [f32; 6],
346}
347
348// ── ABI minor 10 client event structs ─────────────────────────────────────────
349
350/// Key press / release / repeat from the keyboard (client-side only).
351#[repr(C)]
352#[derive(Copy, Clone)]
353pub struct YogKeyPressEvent {
354    /// GLFW key code (e.g. `GLFW_KEY_E = 69`).
355    pub key_code:  i32,
356    pub scan_code: i32,
357    /// 0 = release, 1 = press, 2 = repeat.
358    pub action:    i32,
359    /// Modifier bitmask: 1=shift, 2=ctrl, 4=alt, 8=super.
360    pub modifiers: i32,
361}
362
363// ── Handler function-pointer types ────────────────────────────────────────────
364//
365// All event handlers receive a `phase: u8` argument:
366//   0 = Pre  — fires before the action; return value matters (false = cancel).
367//   1 = Post — fires after the action; return value is ignored.
368//
369// This unified signature lets one registered closure handle both phases.
370//
371// Client-side handlers (minor 10) do NOT receive a `YogServer*` — they run on
372// the render thread and have no server context.
373
374pub type YogBlockBreakFn   = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogBlockBreakEvent,   u8) -> bool;
375pub type YogChatFn         = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogChatEvent,         u8) -> bool;
376pub type YogPlayerFn       = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerEvent,       u8) -> bool;
377pub type YogUseItemFn      = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogUseItemEvent,      u8) -> bool;
378pub type YogUseBlockFn     = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogUseBlockEvent,     u8) -> bool;
379pub type YogAttackEntityFn = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogAttackEntityEvent, u8) -> bool;
380pub type YogEntityDamageFn = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntityDamageEvent, u8) -> bool;
381pub type YogEntityDeathFn  = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntityDeathEvent,  u8) -> bool;
382pub type YogEntitySpawnFn   = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntitySpawnEvent,   u8) -> bool;
383pub type YogPlaceBlockFn    = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlaceBlockEvent,    u8) -> bool;
384pub type YogPlayerDeathFn   = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerDeathEvent,   u8) -> bool;
385pub type YogPlayerRespawnFn = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerRespawnEvent, u8) -> bool;
386pub type YogAdvancementFn      = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogAdvancementEvent,      u8) -> bool;
387pub type YogEntityInteractFn   = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogEntityInteractEvent,   u8) -> bool;
388pub type YogCraftFn            = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogCraftEvent,            u8) -> bool;
389pub type YogExplosionFn        = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogExplosionEvent,        u8) -> bool;
390pub type YogItemPickupFn       = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogItemPickupEvent,       u8) -> bool;
391pub type YogPlayerMoveFn       = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPlayerMoveEvent,       u8) -> bool;
392pub type YogContainerOpenFn    = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogContainerOpenEvent,    u8) -> bool;
393pub type YogContainerCloseFn   = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogContainerCloseEvent,   u8) -> bool;
394pub type YogProjectileHitFn    = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogProjectileHitEvent,    u8) -> bool;
395
396/// Packet events — always Post, no phase.
397pub type YogPacketFn  = unsafe extern "C" fn(*mut c_void, *const YogServer, *const YogPacketEvent);
398/// Server lifecycle / tick — no event struct, always fires.
399pub type YogServerFn  = unsafe extern "C" fn(*mut c_void, *const YogServer);
400/// Command handler.
401pub type YogCommandFn = unsafe extern "C" fn(
402    ud: *mut c_void,
403    srv: *const YogServer,
404    ev: *const YogCommandEvent,
405    reply_buf: *mut u8,
406    reply_cap: u32,
407    reply_len: *mut u32,
408);
409/// Scheduler handler (once or repeating).
410pub type YogScheduledFn = unsafe extern "C" fn(*mut c_void, *const YogServer);
411
412// ── ABI minor 10 — client-side function pointer types ────────────────────────
413
414/// Client tick — no event, no server context.
415pub type YogClientFn = unsafe extern "C" fn(ud: *mut c_void);
416/// HUD render — `gfx` is the graphics context for this frame; only valid for
417/// the call duration.  `draw2d_*` functions in `gfx` work here.
418pub type YogHudRenderFn = unsafe extern "C" fn(ud: *mut c_void, gfx: *const YogGfxApi);
419/// World render — `gfx` contains `view_proj` and `camera_pos` for 3D rendering.
420/// Valid only for the call duration.
421pub type YogWorldRenderFn = unsafe extern "C" fn(ud: *mut c_void, gfx: *const YogGfxApi);
422/// Key press — return `false` to cancel (prevent Minecraft from processing the key).
423pub type YogKeyPressFn  = unsafe extern "C" fn(ud: *mut c_void, ev: *const YogKeyPressEvent) -> bool;
424/// Screen event — `screen_class` is the simple class name (e.g. `"InventoryScreen"`).
425/// For `on_screen_open` return `false` to prevent the screen from opening.
426pub type YogScreenFn    = unsafe extern "C" fn(ud: *mut c_void, screen_class: YogStr) -> bool;
427
428// ── ABI minor 14 — low-level GPU pipeline ────────────────────────────────────
429
430/// Low-level GPU context passed to render handlers.
431///
432/// Provides direct access to the OpenGL pipeline: buffer objects, vertex arrays,
433/// shader programs, textures, draw calls, and render state.
434///
435/// The per-frame fields (`screen_w/h`, `delta_tick`, `view_proj`, `camera_pos`)
436/// are filled by the runtime before calling the handler; the function pointers
437/// point to statically-allocated implementations.
438///
439/// Valid only for the duration of the render callback — never store the pointer.
440/// GPU resource **handles** (`u32`) may be stored between frames.
441///
442/// Colors are `0xAARRGGBB` (Minecraft convention).
443#[repr(C)]
444#[derive(Copy, Clone)]
445pub struct YogGfxApi {
446    // ── Per-frame context ─────────────────────────────────────────────────────
447    /// GUI-pixel screen width.
448    pub screen_w:   i32,
449    /// GUI-pixel screen height.
450    pub screen_h:   i32,
451    /// Partial-tick interpolation factor (0.0–1.0).
452    pub delta_tick:   f32,
453    /// GUI scale factor: physical pixels per GUI pixel (e.g. 2.0 for 2× GUI scale).
454    /// Useful for converting GUI-pixel coordinates to physical pixels for OpenGL calls.
455    pub scale_factor: f32,
456    /// View-projection matrix in camera-relative space (column-major, 16 × f32).
457    /// All zeros during `on_hud_render`; filled during `on_world_render`.
458    pub view_proj:  [f32; 16],
459    /// Camera world-space position.  All zeros during `on_hud_render`.
460    pub camera_pos:  [f32; 3],
461    /// Local player world-space position (eye height).  All zeros during `on_hud_render`.
462    /// Differs from `camera_pos` in third-person view.
463    pub player_pos:  [f32; 3],
464    pub _pad1:       f32,
465
466    // ── GPU buffers ───────────────────────────────────────────────────────────
467    /// Allocate a new GPU buffer (VBO / EBO). Returns 0 on failure.
468    pub buf_create:  unsafe extern "C" fn() -> u32,
469    /// Delete a buffer created by `buf_create`.
470    pub buf_delete:  unsafe extern "C" fn(handle: u32),
471    /// Upload `len` bytes from `bytes` into `handle`.
472    /// `dynamic`: hints frequent updates (`GL_DYNAMIC_DRAW` vs `GL_STATIC_DRAW`).
473    pub buf_data:    unsafe extern "C" fn(handle: u32, bytes: *const u8, len: u32, dynamic: bool),
474    /// Overwrite `len` bytes at `offset` in `handle`.
475    pub buf_subdata: unsafe extern "C" fn(handle: u32, offset: u32, bytes: *const u8, len: u32),
476
477    // ── Vertex arrays ─────────────────────────────────────────────────────────
478    /// Create a vertex array object. Returns 0 on failure.
479    pub vao_create: unsafe extern "C" fn() -> u32,
480    /// Delete a VAO created by `vao_create`.
481    pub vao_delete: unsafe extern "C" fn(handle: u32),
482    /// Declare one vertex attribute in `vao`, sourced from `vbo`.
483    /// `dtype`: 0=f32, 1=u8, 2=i32, 3=u32.
484    pub vao_attrib: unsafe extern "C" fn(
485        vao: u32, vbo: u32, index: u32, components: u8,
486        dtype: u8, normalized: bool, stride: u32, offset: u32,
487    ),
488    /// Bind an index buffer (EBO) to `vao`.
489    pub vao_set_ebo: unsafe extern "C" fn(vao: u32, ebo: u32),
490
491    // ── Shader programs ───────────────────────────────────────────────────────
492    /// Compile + link `vert_src` / `frag_src` (GLSL 150 core).
493    /// Writes the program handle to `*out`. Returns false and logs on error.
494    pub prog_create:       unsafe extern "C" fn(vert: YogStr, frag: YogStr, out: *mut u32) -> bool,
495    /// Delete a shader program.
496    pub prog_delete:       unsafe extern "C" fn(handle: u32),
497    pub prog_uniform_1i:   unsafe extern "C" fn(prog: u32, name: YogStr, v: i32),
498    pub prog_uniform_1f:   unsafe extern "C" fn(prog: u32, name: YogStr, v: f32),
499    pub prog_uniform_2f:   unsafe extern "C" fn(prog: u32, name: YogStr, x: f32, y: f32),
500    pub prog_uniform_3f:   unsafe extern "C" fn(prog: u32, name: YogStr, x: f32, y: f32, z: f32),
501    pub prog_uniform_4f:   unsafe extern "C" fn(prog: u32, name: YogStr, x: f32, y: f32, z: f32, w: f32),
502    /// Set a mat4 uniform from 16 column-major floats.
503    pub prog_uniform_mat4: unsafe extern "C" fn(prog: u32, name: YogStr, col_major: *const f32),
504
505    // ── Textures ──────────────────────────────────────────────────────────────
506    /// Upload RGBA8 pixel data as a new texture.
507    /// `linear`: `GL_LINEAR` if true, `GL_NEAREST` if false.
508    pub tex_create:  unsafe extern "C" fn(w: u32, h: u32, rgba: *const u8, linear: bool) -> u32,
509    /// Delete a texture created by `tex_create`.
510    pub tex_delete:  unsafe extern "C" fn(handle: u32),
511    /// Bind `handle` to texture unit `unit` (0–7).
512    pub tex_bind:    unsafe extern "C" fn(unit: u32, handle: u32),
513    /// Return the GL texture handle Minecraft uses for a namespaced resource
514    /// (e.g. `"minecraft:textures/gui/icons.png"`). Returns 0 if not found.
515    /// Do **not** delete handles obtained this way — Minecraft owns them.
516    pub tex_from_mc: unsafe extern "C" fn(id: YogStr) -> u32,
517
518    // ── Draw calls ────────────────────────────────────────────────────────────
519    /// Draw `count` primitives from `vao`, using shader `prog`.
520    /// `mode`: 0=Triangles, 1=Lines, 2=LineStrip, 3=TriangleStrip, 4=TriangleFan.
521    pub draw_arrays:   unsafe extern "C" fn(vao: u32, prog: u32, mode: u8, first: u32, count: u32),
522    /// Draw indexed primitives.  `ebo` must be bound to `vao` via `vao_set_ebo`.
523    /// `u32_idx`: `true` for `u32` indices, `false` for `u16` indices.
524    pub draw_elements: unsafe extern "C" fn(vao: u32, ebo: u32, prog: u32, mode: u8, count: u32, u32_idx: bool),
525
526    // ── Render state ──────────────────────────────────────────────────────────
527    /// Enable/disable blending. `src`/`dst` are raw GL blend factor enum values.
528    pub set_blend:    unsafe extern "C" fn(enabled: bool, src: u32, dst: u32),
529    /// Enable/disable depth testing and depth writes.
530    pub set_depth:    unsafe extern "C" fn(test: bool, write: bool),
531    /// Enable scissor clipping (GUI-pixel rectangle).
532    pub set_scissor:  unsafe extern "C" fn(x: i32, y: i32, w: i32, h: i32),
533    /// Disable scissor clipping.
534    pub clear_scissor: unsafe extern "C" fn(),
535    /// Set the GL viewport (physical pixel coordinates).
536    pub set_viewport:  unsafe extern "C" fn(x: i32, y: i32, w: i32, h: i32),
537
538    // ── 2D convenience (HUD-render only — uses MC's DrawContext) ─────────────
539    /// Filled rectangle. Only valid during `on_hud_render`.
540    pub draw2d_rect:     unsafe extern "C" fn(x1: f32, y1: f32, x2: f32, y2: f32, color: u32),
541    /// Vertical-gradient rectangle. Only valid during `on_hud_render`.
542    pub draw2d_gradient: unsafe extern "C" fn(x1: f32, y1: f32, x2: f32, y2: f32, top: u32, bottom: u32),
543    /// MC text renderer string. Only valid during `on_hud_render`.
544    pub draw2d_text:     unsafe extern "C" fn(text: YogStr, x: f32, y: f32, color: u32, shadow: bool),
545    /// Blit from a Minecraft-managed texture. Only valid during `on_hud_render`.
546    /// `(u0, v0)` in texels; `(w, h)` in pixels; `(tw, th)` full texture size.
547    pub draw2d_mc_tex:   unsafe extern "C" fn(id: YogStr, x: f32, y: f32, u0: f32, v0: f32, w: f32, h: f32, tw: f32, th: f32),
548}
549
550unsafe impl Send for YogGfxApi {}
551unsafe impl Sync for YogGfxApi {}
552
553// ── Server action table (runtime → mod direction is wrong; it's mod → runtime) ─
554
555/// All Minecraft-mutating calls available inside a handler.
556///
557/// `ctx` is an opaque pointer to the runtime's JNI state.  Every function takes
558/// it as its first argument.  The pointer is valid for the lifetime of the process.
559///
560/// Strings **returned** by functions in this table are heap-allocated by the
561/// runtime and must be freed with `free_str` after the caller has read them.
562#[repr(C)]
563pub struct YogServer {
564    pub ctx:         *mut c_void,
565    pub abi_version: u32,
566    /// `sizeof(YogServer)` at build time — allows mods compiled against an older
567    /// table to detect and skip fields they don't know about.
568    pub size:        u32,
569
570    /// Free a string returned by any function in this table.
571    pub free_str: unsafe extern "C" fn(ptr: *mut u8, len: u32),
572
573    // ── chat ─────────────────────────────────────────────────────────────────
574    pub broadcast: unsafe extern "C" fn(ctx: *mut c_void, msg: YogStr),
575
576    // ── world ────────────────────────────────────────────────────────────────
577    pub get_block:   unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr,
578    pub set_block:   unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos, block: YogStr) -> bool,
579    pub world_time:  unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, out: *mut i64) -> bool,
580    pub set_time:    unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, time: i64) -> bool,
581    pub is_raining:  unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr) -> bool,
582    pub set_weather: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, raining: bool, dur: i32) -> bool,
583
584    // ── player ───────────────────────────────────────────────────────────────
585    pub give_item:         unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, item: YogStr, count: u32) -> bool,
586    pub player_teleport:   unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, pos: YogVec3) -> bool,
587    pub send_to_player:    unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, channel: YogStr, data: *const u8, len: u32) -> bool,
588    pub send_to_server:    unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, data: *const u8, len: u32) -> bool,
589    pub kick_player:       unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, reason: YogStr) -> bool,
590    pub set_gamemode:      unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, mode: YogStr) -> bool,
591    pub send_title:        unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, title: YogStr, sub: YogStr, fi: i32, stay: i32, fo: i32) -> bool,
592    pub send_actionbar:    unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, msg: YogStr) -> bool,
593    pub play_sound:        unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogVec3, sound: YogStr, vol: f32, pitch: f32) -> bool,
594    pub play_sound_player: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, sound: YogStr, vol: f32, pitch: f32) -> bool,
595
596    // ── entity ───────────────────────────────────────────────────────────────
597    pub entity_teleport:      unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, pos: YogVec3) -> bool,
598    pub entity_position:      unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
599    pub entity_health:        unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut f32) -> bool,
600    pub entity_set_health:    unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, hp: f32) -> bool,
601    pub entity_kill:          unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> bool,
602    pub spawn_entity:         unsafe extern "C" fn(ctx: *mut c_void, type_id: YogStr, dim: YogStr, pos: YogVec3) -> YogOwnedStr,
603    pub entity_add_effect:    unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, fx: YogStr, dur: i32, amp: u8, particles: bool) -> bool,
604    pub entity_remove_effect: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, fx: YogStr) -> bool,
605    pub entity_clear_effects: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> bool,
606    pub entity_velocity:      unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, out: *mut YogVec3) -> bool,
607    pub entity_set_velocity:  unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, vel: YogVec3) -> bool,
608    pub entity_add_velocity:  unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, vel: YogVec3) -> bool,
609
610    // ── tags & loot ──────────────────────────────────────────────────────────
611    pub has_item_tag:  unsafe extern "C" fn(ctx: *mut c_void, item: YogStr, tag: YogStr) -> bool,
612    pub has_block_tag: unsafe extern "C" fn(ctx: *mut c_void, block: YogStr, tag: YogStr) -> bool,
613    pub drop_loot:     unsafe extern "C" fn(ctx: *mut c_void, table: YogStr, dim: YogStr, pos: YogVec3) -> bool,
614
615    // ── scoreboard ───────────────────────────────────────────────────────────
616    pub scoreboard_get: unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, out: *mut i32) -> bool,
617    pub scoreboard_set: unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, score: i32) -> bool,
618    pub scoreboard_add: unsafe extern "C" fn(ctx: *mut c_void, obj: YogStr, player: YogStr, delta: i32, out: *mut i32) -> bool,
619
620    // ── boss bar ─────────────────────────────────────────────────────────────
621    pub bossbar_create:        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, title: YogStr, color: YogStr, style: YogStr) -> bool,
622    pub bossbar_remove:        unsafe extern "C" fn(ctx: *mut c_void, id: YogStr) -> bool,
623    pub bossbar_set_title:     unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, title: YogStr) -> bool,
624    pub bossbar_set_progress:  unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, progress: f32) -> bool,
625    pub bossbar_set_color:     unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, color: YogStr) -> bool,
626    pub bossbar_add_player:    unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, player: YogStr) -> bool,
627    pub bossbar_remove_player: unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, player: YogStr) -> bool,
628    pub bossbar_set_visible:   unsafe extern "C" fn(ctx: *mut c_void, id: YogStr, visible: bool) -> bool,
629
630    // ── misc ─────────────────────────────────────────────────────────────────
631    pub game_dir: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
632
633    // ── player query (ABI minor 4) ────────────────────────────────────────────
634    /// Newline-separated list of online player names, or NONE if server not up.
635    pub online_players: unsafe extern "C" fn(ctx: *mut c_void) -> YogOwnedStr,
636
637    // ── block entity (NBT, ABI minor 3) ──────────────────────────────────────
638    /// SNBT string of the block entity at `pos`, or NONE if there is none.
639    pub get_block_nbt: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr,
640    /// Write SNBT into the block entity at `pos`. Returns false if no block entity exists.
641    pub set_block_nbt: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, pos: YogBlockPos, snbt: YogStr) -> bool,
642
643    // ── inventory (ABI minor 3) ───────────────────────────────────────────────
644    /// Tab/newline-encoded inventory: one line per occupied slot, `slot\titem_id\tcount`.
645    pub player_inventory: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
646    /// Set (or clear when count==0) a specific inventory slot.
647    pub player_set_slot:  unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, slot: u32, item_id: YogStr, count: u32) -> bool,
648
649    // ── cross-dimension teleport (ABI minor 3) ────────────────────────────────
650    pub player_teleport_dim: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, dim: YogStr, pos: YogVec3) -> bool,
651    pub entity_teleport_dim: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, dim: YogStr, pos: YogVec3) -> bool,
652
653    // ── entity counting ───────────────────────────────────────────────────────
654    /// Count loaded instances of `entity_type` in `dimension`. Returns -1 on error.
655    pub world_entity_count: unsafe extern "C" fn(ctx: *mut c_void, dim: YogStr, entity_type: YogStr) -> i32,
656
657    // ── entity NBT (ABI minor 6) ──────────────────────────────────────────────
658    /// SNBT of the entity's persistent data, or NONE if entity not found.
659    pub entity_get_nbt: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr,
660    /// Merge SNBT data into the entity. Returns false if entity not found.
661    pub entity_set_nbt: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, snbt: YogStr) -> bool,
662
663    // ── particles (ABI minor 6) ───────────────────────────────────────────────
664    /// Spawn `count` particles at `pos` in `dim`.
665    /// `dx/dy/dz` control spread, `speed` controls particle speed.
666    /// Returns false if the dimension or particle type is unknown.
667    pub spawn_particles: unsafe extern "C" fn(
668        ctx: *mut c_void,
669        dim: YogStr,
670        pos: YogVec3,
671        particle_type: YogStr,
672        count: i32,
673        dx: f64, dy: f64, dz: f64,
674        speed: f64,
675    ) -> bool,
676
677    // ── attributes (ABI minor 7) ──────────────────────────────────────────────
678    /// Get the base value of an attribute on a living entity.
679    /// `attribute_id` is a registry id, e.g. `"minecraft:generic.max_health"`.
680    /// Returns `f64::NAN` if entity or attribute is not found.
681    pub entity_attribute_get: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, attribute_id: YogStr) -> f64,
682    /// Set the base value of an attribute. Returns false if entity or attribute is not found.
683    pub entity_attribute_set: unsafe extern "C" fn(ctx: *mut c_void, uuid: YogStr, attribute_id: YogStr, value: f64) -> bool,
684
685    // ── held item NBT (ABI minor 11) ─────────────────────────────────────────
686    /// SNBT of the item currently held in the player's main hand.
687    /// Returns NONE if the player is offline or holding air.
688    pub get_held_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
689    /// Merge `snbt` data into the NBT of the player's held main-hand item in-place.
690    /// Returns false if the player is offline or holding air.
691    pub set_held_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, snbt: YogStr) -> bool,
692
693    // ── item stack query (ABI minor 12) ──────────────────────────────────────
694    /// SNBT of the item in the player's off hand, or NONE if offline / holding air.
695    pub get_offhand_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr) -> YogOwnedStr,
696    /// Merge `snbt` into the NBT of the player's off-hand item.
697    /// Returns false if offline or holding air.
698    pub set_offhand_item_nbt: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, snbt: YogStr) -> bool,
699    /// Full item stack at inventory `slot`: tab-separated `item_id\tcount\tsnbt`.
700    /// `snbt` is `{}` when the item has no NBT. Returns NONE if offline or slot empty.
701    pub get_slot_item: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, slot: u32) -> YogOwnedStr,
702    /// Replace inventory `slot` with an item stack. `snbt` may be empty to clear NBT.
703    /// Pass `count == 0` to clear the slot (ignores `item_id` and `snbt`).
704    pub set_slot_item: unsafe extern "C" fn(ctx: *mut c_void, player: YogStr, slot: u32, item_id: YogStr, count: u32, snbt: YogStr) -> bool,
705}
706
707// ctx = *mut JavaVM which is global/stable. All fn ptrs are pure C-ABI.
708unsafe impl Send for YogServer {}
709unsafe impl Sync for YogServer {}
710
711// ── Registration table (passed to yog_mod_register) ──────────────────────────
712
713/// Passed to `yog_mod_register`. Call the function pointers here to register
714/// handlers, commands, content, and schedulers.
715///
716/// When mods compiled against ABI `N` load on a runtime with ABI `M > N`:
717/// fields beyond `size` are not present in the mod's view — check `size` before
718/// accessing fields added in later minor versions.
719#[repr(C)]
720pub struct YogApi {
721    pub abi_version: u32,
722    /// `sizeof(YogApi)` at the runtime's build time.
723    pub size:        u32,
724    /// Opaque pointer to runtime handler storage.
725    pub ctx:         *mut c_void,
726    /// Stable server action table — pass to handlers.
727    pub server:      *const YogServer,
728
729    // ── events — all handlers receive (ud, srv, event, phase: u8) → bool ────────
730    // phase 0 = Pre (return false to cancel), phase 1 = Post (return ignored).
731    pub on_block_break:       unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogBlockBreakFn),
732    pub on_chat:              unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogChatFn),
733    pub on_player_join:       unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerFn),
734    pub on_player_leave:      unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerFn),
735    pub on_use_item:          unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogUseItemFn),
736    pub on_use_block:         unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogUseBlockFn),
737    pub on_attack_entity:     unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogAttackEntityFn),
738    pub on_entity_damage:     unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityDamageFn),
739    pub on_entity_death:      unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityDeathFn),
740    pub on_entity_spawn:       unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntitySpawnFn),
741    pub on_player_place_block: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlaceBlockFn),
742    pub on_player_death:       unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerDeathFn),
743    pub on_player_respawn:     unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerRespawnFn),
744    pub on_advancement:        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogAdvancementFn),
745    // ── ABI minor 8 ──────────────────────────────────────────────────────────
746    pub on_entity_interact:    unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogEntityInteractFn),
747    pub on_item_craft:         unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogCraftFn),
748    pub on_explosion:          unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogExplosionFn),
749    // ── ABI minor 9 ──────────────────────────────────────────────────────────
750    pub on_item_pickup:        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogItemPickupFn),
751    pub on_player_move:        unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogPlayerMoveFn),
752    pub on_container_open:     unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogContainerOpenFn),
753    pub on_container_close:    unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogContainerCloseFn),
754    pub on_projectile_hit:     unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogProjectileHitFn),
755    pub on_server_tick:       unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
756    pub on_server_started:    unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
757    pub on_server_stopping:   unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogServerFn),
758
759    // ── networking ───────────────────────────────────────────────────────────
760    pub on_packet:        unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, ud: *mut c_void, h: YogPacketFn),
761    pub on_client_packet: unsafe extern "C" fn(ctx: *mut c_void, channel: YogStr, ud: *mut c_void, h: YogPacketFn),
762
763    // ── commands ─────────────────────────────────────────────────────────────
764    pub register_command: unsafe extern "C" fn(ctx: *mut c_void, name: YogStr, ud: *mut c_void, h: YogCommandFn),
765    pub register_typed_command: unsafe extern "C" fn(ctx: *mut c_void, name: YogStr, schema: YogStr, ud: *mut c_void, h: YogCommandFn),
766
767    // ── recipes ──────────────────────────────────────────────────────────────
768    /// Register a recipe by supplying Minecraft JSON (`data/` format).
769    /// `namespace` + `name` form the file path: `data/{ns}/recipes/{name}.json`.
770    pub register_recipe_json: unsafe extern "C" fn(ctx: *mut c_void, namespace: YogStr, name: YogStr, json: YogStr),
771
772    // ── content ──────────────────────────────────────────────────────────────
773    pub register_item:  unsafe extern "C" fn(ctx: *mut c_void, def: *const YogItemDef),
774    pub register_block: unsafe extern "C" fn(ctx: *mut c_void, def: *const YogBlockDef),
775
776    // ── scheduler ────────────────────────────────────────────────────────────
777    pub schedule_once:      unsafe extern "C" fn(ctx: *mut c_void, delay_ticks: u64, ud: *mut c_void, h: YogScheduledFn),
778    pub schedule_repeating: unsafe extern "C" fn(ctx: *mut c_void, period_ticks: u64, ud: *mut c_void, h: YogScheduledFn),
779
780    // ── ABI minor 10 — client-side events ────────────────────────────────────
781    pub on_client_tick:  unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogClientFn),
782    pub on_hud_render:   unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogHudRenderFn),
783    pub on_key_press:    unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogKeyPressFn),
784    pub on_screen_open:  unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogScreenFn),
785    pub on_screen_close: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogScreenFn),
786
787    // ── ABI minor 14 — world render ──────────────────────────────────────────
788    /// Register a handler that fires after world geometry is rendered.
789    /// `gfx.view_proj` and `gfx.camera_pos` are filled; use them to project
790    /// custom 3D geometry into clip space.
791    pub on_world_render: unsafe extern "C" fn(ctx: *mut c_void, ud: *mut c_void, h: YogWorldRenderFn),
792
793    // ── ABI minor 16 — startup grants ────────────────────────────────────────
794    pub register_startup_grant: unsafe extern "C" fn(ctx: *mut c_void, grant: *const YogStartupGrantDef),
795
796    // ── ABI minor 18 — books ─────────────────────────────────────────────────
797    pub register_book: unsafe extern "C" fn(ctx: *mut c_void, book_id: YogStr, book_json: YogStr),
798}
799
800unsafe impl Send for YogApi {}
801unsafe impl Sync for YogApi {}