Skip to main content

yog_runtime/
lib.rs

1//! Yog runtime — the native library loaded by the Fabric host.
2//!
3//! Exposes JNI entry points (`Java_dev_yog_NativeBridge_*`) that the host calls,
4//! and a stable C ABI (`YogApi` / `YogServer`) that mods program against.
5//!
6//! Architecture:
7//!   - `YogServer`  — a `#[repr(C)]` table of standalone JNI-calling functions
8//!     that mods call to mutate the world.
9//!   - `YogApi`     — a `#[repr(C)]` table of registration functions; mods call
10//!     them inside `yog_mod_register` to subscribe to events.
11//!   - `RuntimeHandlers` — the runtime's internal event/handler storage.
12//!     Filled during `nativeInit` (write), read-only after. The scheduler sub-
13//!     state uses an inner `Mutex` for safe addition during event dispatch.
14
15use std::collections::HashMap;
16use std::num::NonZeroU32;
17use std::os::raw::c_void;
18use std::path::{Path, PathBuf};
19use std::sync::{Arc, Mutex, OnceLock, RwLock};
20
21use glow::HasContext;
22use jcall::JArg;
23use jni::objects::{JByteArray, JClass, JFloatArray, JString};
24use jni::sys::{jdouble, jfloat, jint, jstring};
25use jni::{JNIEnv, JavaVM};
26use libloading::Symbol;
27use yog_hot_reload::{HotReloader, LoadedModule, ModuleGeneration, ModuleRegistry};
28
29use yog_abi::{
30    ABI_VERSION, YogAdvancementEvent, YogAdvancementFn, YogApi, YogAttackEntityFn, YogBlockBreakFn,
31    YogBlockPos, YogChatFn, YogClientFn, YogCommandFn, YogContainerCloseEvent,
32    YogContainerCloseFn, YogContainerOpenEvent, YogContainerOpenFn, YogCraftEvent, YogCraftFn,
33    YogEntityDamageFn, YogEntityDeathFn, YogEntityInteractEvent, YogEntityInteractFn,
34    YogEntitySpawnFn, YogExplosionEvent, YogExplosionFn, YogGfxApi, YogHudRenderFn, YogItemInsertEvent, YogItemInsertFn, YogItemPickupEvent, YogItemPickupFn, YogKeyPressEvent, YogMachineCraftEvent, YogMachineCraftFn, YogKeyPressFn, YogOwnedStr, YogPacketFn,
35    YogPlaceBlockEvent, YogPlaceBlockFn, YogPlayerDeathEvent, YogPlayerDeathFn, YogPlayerFn,
36    YogPlayerMoveEvent, YogPlayerMoveFn, YogPlayerRespawnEvent, YogPlayerRespawnFn,
37    YogProjectileHitEvent, YogProjectileHitFn, YogScheduledFn, YogScreenFn, YogServer, YogServerFn,
38    YogStr, YogUseBlockFn, YogUseItemFn, YogVec3, YogWorldRenderFn,
39};
40use yog_registry::{BlockDef, ItemDef};
41
42// ── Static globals ────────────────────────────────────────────────────────────
43
44/// Cached JVM handle for any-thread callbacks.
45static JAVA_VM: OnceLock<JavaVM> = OnceLock::new();
46/// Loaded mod libraries, keyed by mod id — owns the safe `dlopen`/`dlclose`
47/// swap lifecycle (see `yog-hot-reload`) instead of a flat keep-alive `Vec`,
48/// so a reload can find "the current generation of this mod" and safely
49/// retire the previous one.
50static HOT_RELOADER: OnceLock<HotReloader<RuntimeModuleRegistry>> = OnceLock::new();
51/// The mod currently inside its own `yog_mod_register` call. Every `api_*`
52/// registration function reads this to tag what it stores with a
53/// `LoadedModule` handle, instead of requiring mods to pass a mod id
54/// through the stable C ABI (an ABI break, and edits to every mod's
55/// source). Valid because a mod's entire registration always happens
56/// synchronously inside one `yog_mod_register` call — see `load_mod_lib`
57/// and `Java_dev_yog_NativeBridge_nativeHotReload`.
58static CURRENT_REGISTRATION: Mutex<Option<LoadedModule>> = Mutex::new(None);
59/// Metadata of loaded .yog mods: [id, name, version, authors, description,
60/// name_key, description_key] (the keys empty unless the mod's yog.toml
61/// gives its name/description as `.yres` texts).
62static MOD_INFOS: Mutex<Vec<[String; 7]>> = Mutex::new(Vec::new());
63/// Inter-mod symbol registry: mod_id → (symbol_name → function pointer).
64/// Filled during `yog_mod_register` (export calls), read during import calls.
65static INTEROP_SYMBOLS: OnceLock<Mutex<HashMap<String, HashMap<String, usize>>>> = OnceLock::new();
66/// Stable server table (populated once in nativeInit, then read-only).
67static SERVER: OnceLock<YogServer> = OnceLock::new();
68/// Stable api table. Mods capture this pointer at registration (yog-api's
69/// `installed_mods()` calls through it later, e.g. from UI render) — so it
70/// must outlive `nativeInit`, not live on its stack. `ctx` inside points at
71/// `HANDLERS`' own `RwLock`, published *before* any mod registers (unlike
72/// the old design, where it pointed at a temporary pre-move `Box` that
73/// went stale the instant `nativeInit` finished) — so it stays valid for a
74/// hot-reload's later re-registration too.
75static API_TABLE: OnceLock<YogApi> = OnceLock::new();
76/// All registered handlers + content. A `RwLock`, not a plain value: unlike
77/// the original one-shot-then-immutable design, a hot-reload needs to
78/// append fresh entries for a reloaded mod after this has long been
79/// published. Read sites (`handlers()`) take a read guard; registration
80/// (initial or reload) takes a write guard — both deref straight through to
81/// `RuntimeHandlers`, so existing `handlers().field` call sites are
82/// unaffected by this type change.
83static HANDLERS: OnceLock<RwLock<RuntimeHandlers>> = OnceLock::new();
84
85// ── OpenGL context (client-side, render thread only) ─────────────────────────
86
87struct GlCtx(glow::Context);
88unsafe impl Send for GlCtx {}
89unsafe impl Sync for GlCtx {}
90
91/// Initialized by `nativeGlInit` on the render thread.  `None` on dedicated server.
92static GL: OnceLock<GlCtx> = OnceLock::new();
93
94// Raw GL function pointers for GL_ARB_get_program_binary (not exposed by glow 0.13).
95// Captured during the glow loader callback in `nativeGlInit`.
96// `None` when the extension is unavailable (very old drivers).
97static GL_GET_PROGRAM_BINARY: OnceLock<Option<usize>> = OnceLock::new();
98static GL_PROGRAM_BINARY: OnceLock<Option<usize>> = OnceLock::new();
99static GL_GET_PROGRAM_IV: OnceLock<Option<usize>> = OnceLock::new();
100
101// ── Handler storage ───────────────────────────────────────────────────────────
102
103#[derive(Debug, Clone)]
104pub struct UiLayer {
105    pub id: String,
106    pub parent: Option<String>,
107    pub modal: bool,
108    pub pause_game: bool,
109    pub visible: bool,
110    pub enabled: bool,
111    pub z_index: i32,
112}
113
114/// One command registration: owning module, typed-args schema (if any),
115/// user data and callback.
116/// A tooltip picture's mod, user data, size and draw callbacks.
117type TooltipEntry = (LoadedModule, *mut c_void, yog_abi::YogTooltipSizeFn, yog_abi::YogTooltipDrawFn);
118type ItemTextEntry = (LoadedModule, *mut c_void, yog_abi::YogItemTextFn);
119type CommandEntry = (LoadedModule, Option<String>, *mut c_void, YogCommandFn);
120
121// Every callback table below is tagged with the `LoadedModule` that
122// registered it (see `current_registration()`), so a hot-reload's dispatch
123// sites can skip a retiring module's stale entries (`LoadedModule::
124// is_retiring()`) before they're physically swept by `RuntimeModuleRegistry
125// ::purge`. Content registries — `items`/`blocks`/`sound_groups`/`inventories`/`books`/
126// `recipes`/`dimensions`/`startup_grants`/`menu_entries` — are deliberately
127// NOT tagged: they're one-time definitions copied into Minecraft's own
128// registries at startup, not callbacks invoked later, and Minecraft itself
129// has no live registry-unregister mechanism to hot-reload them against
130// regardless of what this crate does.
131#[derive(Default)]
132struct RuntimeHandlers {
133    block_break: Vec<(LoadedModule, *mut c_void, YogBlockBreakFn)>,
134    chat: Vec<(LoadedModule, *mut c_void, YogChatFn)>,
135    player_join: Vec<(LoadedModule, *mut c_void, YogPlayerFn)>,
136    player_leave: Vec<(LoadedModule, *mut c_void, YogPlayerFn)>,
137    use_item: Vec<(LoadedModule, *mut c_void, YogUseItemFn)>,
138    use_block: Vec<(LoadedModule, *mut c_void, YogUseBlockFn)>,
139    attack_entity: Vec<(LoadedModule, *mut c_void, YogAttackEntityFn)>,
140    entity_damage: Vec<(LoadedModule, *mut c_void, YogEntityDamageFn)>,
141    entity_death: Vec<(LoadedModule, *mut c_void, YogEntityDeathFn)>,
142    entity_spawn: Vec<(LoadedModule, *mut c_void, YogEntitySpawnFn)>,
143    player_place_block: Vec<(LoadedModule, *mut c_void, YogPlaceBlockFn)>,
144    player_death: Vec<(LoadedModule, *mut c_void, YogPlayerDeathFn)>,
145    player_respawn: Vec<(LoadedModule, *mut c_void, YogPlayerRespawnFn)>,
146    advancement: Vec<(LoadedModule, *mut c_void, YogAdvancementFn)>,
147    entity_interact: Vec<(LoadedModule, *mut c_void, YogEntityInteractFn)>,
148    item_craft: Vec<(LoadedModule, *mut c_void, YogCraftFn)>,
149    machine_craft: Vec<(LoadedModule, *mut c_void, YogMachineCraftFn)>,
150    explosion: Vec<(LoadedModule, *mut c_void, YogExplosionFn)>,
151    item_pickup: Vec<(LoadedModule, *mut c_void, YogItemPickupFn)>,
152    item_insert: Vec<(LoadedModule, *mut c_void, YogItemInsertFn)>,
153    player_move: Vec<(LoadedModule, *mut c_void, YogPlayerMoveFn)>,
154    container_open: Vec<(LoadedModule, *mut c_void, YogContainerOpenFn)>,
155    container_close: Vec<(LoadedModule, *mut c_void, YogContainerCloseFn)>,
156    projectile_hit: Vec<(LoadedModule, *mut c_void, YogProjectileHitFn)>,
157    client_tick: Vec<(LoadedModule, *mut c_void, YogClientFn)>,
158    hud_render: Vec<(LoadedModule, *mut c_void, YogHudRenderFn)>,
159    body: Vec<(LoadedModule, *mut c_void, yog_abi::YogBodyFn)>,
160    camera: Vec<(LoadedModule, *mut c_void, yog_abi::YogCameraFn)>,
161    travel: Vec<(LoadedModule, *mut c_void, yog_abi::YogTravelFn)>,
162    world_render: Vec<(LoadedModule, *mut c_void, YogWorldRenderFn)>,
163    key_press: Vec<(LoadedModule, *mut c_void, YogKeyPressFn)>,
164    screen_open: Vec<(LoadedModule, *mut c_void, YogScreenFn)>,
165    screen_close: Vec<(LoadedModule, *mut c_void, YogScreenFn)>,
166    server_tick: Vec<(LoadedModule, *mut c_void, YogServerFn)>,
167    server_started: Vec<(LoadedModule, *mut c_void, YogServerFn)>,
168    server_stopping: Vec<(LoadedModule, *mut c_void, YogServerFn)>,
169    /// All handlers registered under one command name, in registration order.
170    /// `schema` is `None` for a plain `on_command` (bare, zero-argument)
171    /// registration and `Some(schema)` for `on_typed_command`. Kept as a
172    /// `Vec` (not a single slot) so a mod can register several subcommands —
173    /// e.g. differing arg counts, or several bare/typed registrations sharing
174    /// one top-level name — without later registrations silently clobbering
175    /// earlier ones (see `nativeOnCommand`, which tries each entry whose
176    /// schema's arg count matches the actual invocation, in order, until one
177    /// produces a reply).
178    commands: HashMap<String, Vec<CommandEntry>>,
179    /// (namespace, name, the recipe in Yog's own format).
180    recipes: Vec<(String, String, yog_data::Value)>,
181    packets: HashMap<String, (LoadedModule, *mut c_void, YogPacketFn)>,
182    client_packets: HashMap<String, (LoadedModule, *mut c_void, YogPacketFn)>,
183    items: Vec<ItemDef>,
184    blocks: Vec<BlockDef>,
185    /// The content blob the hosts read (`nativeContent`), built once —
186    /// dropped when more content registers.
187    content_blob: Mutex<Option<Arc<Vec<u8>>>>,
188    /// Mods' sound groups by id; a block's `sound` is one of these or built in.
189    sound_groups: HashMap<String, yog_registry::SoundGroup>,
190    /// Mods' tool and armor materials by id (the game's are built in).
191    tool_materials: HashMap<String, yog_registry::ToolMaterial>,
192    /// Placed features added to biomes (an ore's): for the hosts that add them in code.
193    biome_features: Vec<yog_registry::BiomeFeature>,
194    armor_materials: HashMap<String, yog_registry::ArmorMaterial>,
195    inventories: Vec<yog_inventory::InventoryDef>,
196    /// The ids of the registered books (their renderers are `book_renderers`).
197    books: std::collections::HashSet<String>,
198    book_renderers: Mutex<HashMap<String, yog_book::BookRenderer>>,
199    /// Declared dimension-type defs: id -> `YogDimensionDef` JSON. Actual
200    /// `LevelStem`/`ServerLevel` creation is a host (Java) responsibility —
201    /// see `yog-dimensions` crate docs.
202    dimensions: HashMap<String, yog_data::Value>,
203    chunk_generators: HashMap<String, (LoadedModule, *mut c_void, yog_abi::YogChunkGeneratorFn)>,
204    /// Dimensions' `terrain`s (`.ydef`), generated here: by the dimension's id,
205    /// which is its `generator_type`.
206    terrains: HashMap<String, Arc<yog_worldgen::Generator>>,
207    ui_handlers: HashMap<String, (LoadedModule, *mut c_void, yog_abi::YogUIEventFn)>,
208    ui_render_handlers: HashMap<String, Vec<(LoadedModule, *mut c_void, YogHudRenderFn)>>,
209    /// Book pages' pictures (`BookPage::Drawn`), by drawer id.
210    page_drawers: HashMap<String, (LoadedModule, *mut c_void, yog_abi::YogPageDrawFn)>,
211    /// Pictures in item tooltips, by item id: the first that answers draws.
212    item_tooltips: HashMap<String, Vec<TooltipEntry>>,
213    /// Stacks' own names and tooltip lines, by item.
214    item_texts: HashMap<String, Vec<ItemTextEntry>>,
215    menu_entries: Vec<(String, String)>, // (label, ui_id) for vanilla screen buttons
216    pub active_uis: Mutex<Vec<UiLayer>>,
217    startup_grants: Vec<yog_registry::StartupGrant>,
218    /// Players whose startup grants are pending: (name, uuid, ticks_waited).
219    /// Granting happens on a server tick after join — giving items directly in
220    /// the join callback fails for the host of a freshly created world.
221    pending_grants: Mutex<Vec<(String, String, u32)>>,
222    scheduler: Mutex<SchedulerState>,
223}
224
225// All fn ptrs are C-ABI; ud pointers are from Box::into_raw of Send+Sync closures.
226unsafe impl Send for RuntimeHandlers {}
227unsafe impl Sync for RuntimeHandlers {}
228
229#[derive(Default)]
230struct SchedulerState {
231    once_tasks: Vec<OnceTask>,
232    repeating_tasks: Vec<RepeatingTask>,
233}
234
235impl SchedulerState {
236    /// One server tick: the tasks due now — once tasks whose delay ran out
237    /// (`schedule_once(d)`: `d` ticks later, 0 and 1 meaning the next tick),
238    /// repeating ones whose period came round (`schedule_repeating(p)`:
239    /// every `p` ticks, 0 meaning every tick). Once tasks fired are gone.
240    fn due(&mut self) -> Vec<(LoadedModule, *mut c_void, YogScheduledFn)> {
241        let mut out = Vec::new();
242        self.once_tasks.retain_mut(|t| {
243            if t.delay_remaining <= 1 {
244                out.push((t.module.clone(), t.ud, t.f));
245                false
246            } else {
247                t.delay_remaining -= 1;
248                true
249            }
250        });
251        for t in &mut self.repeating_tasks {
252            t.ticks_left = t.ticks_left.saturating_sub(1);
253            if t.ticks_left == 0 {
254                out.push((t.module.clone(), t.ud, t.f));
255                t.ticks_left = t.period.max(1);
256            }
257        }
258        out
259    }
260}
261
262struct OnceTask {
263    module: LoadedModule,
264    delay_remaining: u64,
265    ud: *mut c_void,
266    f: YogScheduledFn,
267}
268struct RepeatingTask {
269    module: LoadedModule,
270    period: u64,
271    ticks_left: u64,
272    ud: *mut c_void,
273    f: YogScheduledFn,
274}
275
276unsafe impl Send for SchedulerState {}
277unsafe impl Sync for SchedulerState {}
278unsafe impl Send for OnceTask {}
279unsafe impl Send for RepeatingTask {}
280
281/// A mod callback, tagged with the module that registered it.
282type Entry<F> = (LoadedModule, *mut c_void, F);
283
284/// Calls `call(ud, f)` for every entry whose module is still live, until
285/// one returns `false`; whether none did.
286fn each_while<F: Copy>(entries: &[Entry<F>], mut call: impl FnMut(*mut c_void, F) -> bool) -> bool {
287    for (module, ud, f) in entries {
288        if module.is_retiring() {
289            continue;
290        }
291        let Some(_call) = module.enter() else {
292            continue;
293        };
294        if !call(*ud, *f) {
295            return false;
296        }
297    }
298    true
299}
300
301/// Calls `call(ud, f)` for every entry whose module is still live (a
302/// post-phase handler's result is ignored).
303fn each<F: Copy, R>(entries: &[Entry<F>], mut call: impl FnMut(*mut c_void, F) -> R) {
304    each_while(entries, |ud, f| {
305        call(ud, f);
306        true
307    });
308}
309
310/// Pre-phase dispatch: whether every live handler allows the action. The
311/// first `false` cancels it and skips the rest; a panicking mod allows it.
312fn allowed<F: Copy>(entries: &[Entry<F>], call: impl FnMut(*mut c_void, F) -> bool) -> bool {
313    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| each_while(entries, call)))
314        .unwrap_or(true)
315}
316
317fn handlers() -> std::sync::RwLockReadGuard<'static, RuntimeHandlers> {
318    HANDLERS
319        .get()
320        .expect("yog: nativeInit not called yet")
321        .read()
322        .expect("handlers lock poisoned")
323}
324
325fn current_registration() -> LoadedModule {
326    CURRENT_REGISTRATION
327        .lock()
328        .unwrap()
329        .clone()
330        .expect("a yog-runtime api_* function was called outside of yog_mod_register")
331}
332
333fn hot_reloader() -> &'static HotReloader<RuntimeModuleRegistry> {
334    HOT_RELOADER.get().expect("yog: nativeInit not called yet")
335}
336
337/// The `yog-hot-reload` host contract: `retarget` needs no work here because
338/// `LoadedModule::is_retiring()` already makes a superseded generation's
339/// dispatch-table entries inert (every `handlers().<field>` loop checks it)
340/// the instant `HotReloader::reload` calls `retire` on the old module —
341/// well before this is even invoked. `purge` is the actual safety-net
342/// sweep, dropping every entry tagged with a retired generation right
343/// before its library is hard-unloaded.
344/// Runs `$body` with `$list` bound to each per-event handler list of `$h`.
345macro_rules! for_each_handler_list {
346    ($h:expr_2021, |$list:ident| $body:expr_2021) => {{
347        {
348            let $list = &mut $h.block_break;
349            $body;
350        }
351        {
352            let $list = &mut $h.chat;
353            $body;
354        }
355        {
356            let $list = &mut $h.player_join;
357            $body;
358        }
359        {
360            let $list = &mut $h.player_leave;
361            $body;
362        }
363        {
364            let $list = &mut $h.use_item;
365            $body;
366        }
367        {
368            let $list = &mut $h.use_block;
369            $body;
370        }
371        {
372            let $list = &mut $h.attack_entity;
373            $body;
374        }
375        {
376            let $list = &mut $h.entity_damage;
377            $body;
378        }
379        {
380            let $list = &mut $h.entity_death;
381            $body;
382        }
383        {
384            let $list = &mut $h.entity_spawn;
385            $body;
386        }
387        {
388            let $list = &mut $h.player_place_block;
389            $body;
390        }
391        {
392            let $list = &mut $h.player_death;
393            $body;
394        }
395        {
396            let $list = &mut $h.player_respawn;
397            $body;
398        }
399        {
400            let $list = &mut $h.advancement;
401            $body;
402        }
403        {
404            let $list = &mut $h.entity_interact;
405            $body;
406        }
407        {
408            let $list = &mut $h.item_craft;
409            $body;
410        }
411        {
412            let $list = &mut $h.machine_craft;
413            $body;
414        }
415        {
416            let $list = &mut $h.explosion;
417            $body;
418        }
419        {
420            let $list = &mut $h.item_pickup;
421            $body;
422        }
423        {
424            let $list = &mut $h.item_insert;
425            $body;
426        }
427        {
428            let $list = &mut $h.player_move;
429            $body;
430        }
431        {
432            let $list = &mut $h.container_open;
433            $body;
434        }
435        {
436            let $list = &mut $h.container_close;
437            $body;
438        }
439        {
440            let $list = &mut $h.projectile_hit;
441            $body;
442        }
443        {
444            let $list = &mut $h.client_tick;
445            $body;
446        }
447        {
448            let $list = &mut $h.hud_render;
449            $body;
450        }
451        {
452            let $list = &mut $h.body;
453            $body;
454        }
455        {
456            let $list = &mut $h.camera;
457            $body;
458        }
459        {
460            let $list = &mut $h.travel;
461            $body;
462        }
463        {
464            let $list = &mut $h.world_render;
465            $body;
466        }
467        {
468            let $list = &mut $h.key_press;
469            $body;
470        }
471        {
472            let $list = &mut $h.screen_open;
473            $body;
474        }
475        {
476            let $list = &mut $h.screen_close;
477            $body;
478        }
479        {
480            let $list = &mut $h.server_tick;
481            $body;
482        }
483        {
484            let $list = &mut $h.server_started;
485            $body;
486        }
487        {
488            let $list = &mut $h.server_stopping;
489            $body;
490        }
491    }};
492}
493
494struct RuntimeModuleRegistry;
495
496impl ModuleRegistry for RuntimeModuleRegistry {
497    fn retarget(&self, _mod_id: &str, _from: ModuleGeneration, _to: ModuleGeneration) {}
498
499    fn purge(&self, generation: ModuleGeneration) {
500        let Some(lock) = HANDLERS.get() else { return };
501        let mut h = lock.write().expect("handlers lock poisoned");
502        let h = &mut *h;
503        for_each_handler_list!(h, |list| list
504            .retain(|(m, ..)| m.generation() != generation));
505        for entries in h.commands.values_mut() {
506            entries.retain(|(m, ..)| m.generation() != generation);
507        }
508        h.packets.retain(|_, (m, ..)| m.generation() != generation);
509        h.client_packets
510            .retain(|_, (m, ..)| m.generation() != generation);
511        h.chunk_generators
512            .retain(|_, (m, ..)| m.generation() != generation);
513        h.ui_handlers
514            .retain(|_, (m, ..)| m.generation() != generation);
515        for entries in h.ui_render_handlers.values_mut() {
516            entries.retain(|(m, ..)| m.generation() != generation);
517        }
518        h.page_drawers
519            .retain(|_, (m, ..)| m.generation() != generation);
520        for entries in h.item_texts.values_mut() {
521            entries.retain(|(m, ..)| m.generation() != generation);
522        }
523        for entries in h.item_tooltips.values_mut() {
524            entries.retain(|(m, ..)| m.generation() != generation);
525        }
526        h.scheduler
527            .lock()
528            .expect("scheduler poisoned")
529            .once_tasks
530            .retain(|t| t.module.generation() != generation);
531        h.scheduler
532            .lock()
533            .expect("scheduler poisoned")
534            .repeating_tasks
535            .retain(|t| t.module.generation() != generation);
536    }
537}
538
539// ── JNI helpers ──────────────────────────────────────────────────────────────
540
541fn guard(label: &str, f: impl FnOnce()) {
542    if std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).is_err() {
543        yog_logging::error!("a mod panicked handling `{}` (ignored)", label);
544    }
545}
546
547/// Reads a Java string, returning `$or` (default: `Default::default()`)
548/// from the enclosing function when it can't be read.
549macro_rules! jstr {
550    ($env:expr_2021, $s:expr_2021) => {
551        jstr!($env, $s, Default::default())
552    };
553    ($env:expr_2021, $s:expr_2021, $or:expr_2021) => {
554        match $env.get_string(&$s) {
555            Ok(s) => String::from(s),
556            Err(_) => return $or,
557        }
558    };
559}
560
561fn get_env() -> Option<jni::AttachGuard<'static>> {
562    JAVA_VM.get()?.attach_current_thread().ok()
563}
564
565/// `<class>.<name>(args...)` on this thread's env, `Option<R>` — see
566/// [`jcall!`]; `None` without a JVM too.
567macro_rules! host {
568    ($class:literal, $name:literal, [$($arg:expr_2021),* $(,)?]) => {
569        match $crate::get_env() {
570            Some(mut env) => $crate::jcall::jcall!(&mut env, $class, $name, [$($arg),*]),
571            None => None,
572        }
573    };
574}
575
576/// `NativeBridge.<name>(args...)` — see [`host!`].
577macro_rules! bridge {
578    ($name:literal, [$($arg:expr_2021),* $(,)?]) => {
579        host!("dev/yog/NativeBridge", $name, [$($arg),*])
580    };
581}
582
583/// A host string reply handed to a mod (`None`/`null` → [`YogOwnedStr::NONE`]).
584fn owned(s: Option<String>) -> YogOwnedStr {
585    s.map_or(YogOwnedStr::NONE, YogOwnedStr::from_string)
586}
587
588/// The first `N` tab-separated values of a host reply.
589fn parse_tsv<T: std::str::FromStr, const N: usize>(s: &str) -> Option<[T; N]> {
590    let values: Vec<T> = s
591        .split('\t')
592        .take(N)
593        .map(|v| v.parse().ok())
594        .collect::<Option<_>>()?;
595    values.try_into().ok()
596}
597
598// ── Free-str allocator used by YogOwnedStr ────────────────────────────────────
599
600unsafe extern "C" fn yog_free_str(ptr: *mut u8, len: u32) {
601    unsafe {
602        if !ptr.is_null() {
603            drop(Box::from_raw(std::ptr::slice_from_raw_parts_mut(
604                ptr,
605                len as usize,
606            )));
607        }
608    }
609}
610
611// ── YogServer standalone functions (one per action) ───────────────────────────
612//
613// ctx is unused here — all state is in the JAVA_VM static.
614
615unsafe extern "C" fn srv_broadcast(_ctx: *mut c_void, msg: YogStr) {
616    unsafe {
617        let _: Option<()> = bridge!("broadcast", [S(msg)]);
618    }
619}
620
621unsafe extern "C" fn srv_get_block(
622    _ctx: *mut c_void,
623    dim: YogStr,
624    pos: YogBlockPos,
625) -> YogOwnedStr {
626    unsafe { owned(bridge!("getBlock", [S(dim), I(pos.x), I(pos.y), I(pos.z)])) }
627}
628
629unsafe extern "C" fn srv_set_block(
630    _ctx: *mut c_void,
631    dim: YogStr,
632    pos: YogBlockPos,
633    block: YogStr,
634) -> bool {
635    unsafe {
636        bridge!("setBlock", [S(dim), I(pos.x), I(pos.y), I(pos.z), S(block)]).unwrap_or(false)
637    }
638}
639
640unsafe extern "C" fn srv_world_time(_ctx: *mut c_void, dim: YogStr, out: *mut i64) -> bool {
641    unsafe {
642        match bridge!("worldTime", [S(dim)]) {
643            Some(v) if v != i64::MIN => {
644                *out = v;
645                true
646            }
647            _ => false,
648        }
649    }
650}
651
652unsafe extern "C" fn srv_set_time(_ctx: *mut c_void, dim: YogStr, time: i64) -> bool {
653    unsafe { bridge!("worldSetTime", [S(dim), J(time)]).unwrap_or(false) }
654}
655
656unsafe extern "C" fn srv_is_raining(_ctx: *mut c_void, dim: YogStr) -> bool {
657    unsafe { bridge!("worldIsRaining", [S(dim)]).unwrap_or(false) }
658}
659
660unsafe extern "C" fn srv_set_weather(
661    _ctx: *mut c_void,
662    dim: YogStr,
663    raining: bool,
664    dur: i32,
665) -> bool {
666    unsafe { bridge!("worldSetWeather", [S(dim), Z(raining), I(dur)]).unwrap_or(false) }
667}
668
669unsafe extern "C" fn srv_give_item(
670    _ctx: *mut c_void,
671    player: YogStr,
672    item: YogStr,
673    count: u32,
674) -> bool {
675    unsafe { bridge!("giveItem", [S(player), S(item), I(count as i32)]).unwrap_or(false) }
676}
677
678unsafe extern "C" fn srv_player_teleport(_ctx: *mut c_void, player: YogStr, pos: YogVec3) -> bool {
679    unsafe { bridge!("teleport", [S(player), D(pos.x), D(pos.y), D(pos.z)]).unwrap_or(false) }
680}
681
682unsafe extern "C" fn srv_send_to_player(
683    _ctx: *mut c_void,
684    player: YogStr,
685    channel: YogStr,
686    data: *const u8,
687    len: u32,
688) -> bool {
689    unsafe {
690        bridge!(
691            "sendToPlayer",
692            [
693                S(player),
694                S(channel),
695                Bytes(std::slice::from_raw_parts(data, len as usize))
696            ]
697        )
698        .unwrap_or(false)
699    }
700}
701
702unsafe extern "C" fn srv_send_to_server(
703    _ctx: *mut c_void,
704    channel: YogStr,
705    data: *const u8,
706    len: u32,
707) -> bool {
708    unsafe {
709        host!(
710            "dev/yog/YogClient",
711            "sendToServer",
712            [
713                S(channel),
714                Bytes(std::slice::from_raw_parts(data, len as usize))
715            ]
716        )
717        .unwrap_or(false)
718    }
719}
720
721unsafe extern "C" fn srv_kick_player(_ctx: *mut c_void, player: YogStr, reason: YogStr) -> bool {
722    unsafe { bridge!("kickPlayer", [S(player), S(reason)]).unwrap_or(false) }
723}
724
725unsafe extern "C" fn srv_set_gamemode(_ctx: *mut c_void, player: YogStr, mode: YogStr) -> bool {
726    unsafe { bridge!("setGamemode", [S(player), S(mode)]).unwrap_or(false) }
727}
728
729unsafe extern "C" fn srv_send_title(
730    _ctx: *mut c_void,
731    player: YogStr,
732    title: YogStr,
733    sub: YogStr,
734    fi: i32,
735    stay: i32,
736    fo: i32,
737) -> bool {
738    unsafe {
739        bridge!(
740            "sendTitle",
741            [S(player), S(title), S(sub), I(fi), I(stay), I(fo)]
742        )
743        .unwrap_or(false)
744    }
745}
746
747unsafe extern "C" fn srv_send_actionbar(_ctx: *mut c_void, player: YogStr, msg: YogStr) -> bool {
748    unsafe { bridge!("sendActionbar", [S(player), S(msg)]).unwrap_or(false) }
749}
750
751unsafe extern "C" fn srv_play_sound(
752    _ctx: *mut c_void,
753    dim: YogStr,
754    pos: YogVec3,
755    sound: YogStr,
756    vol: f32,
757    pitch: f32,
758) -> bool {
759    unsafe {
760        bridge!(
761            "playSound",
762            [
763                S(dim),
764                D(pos.x),
765                D(pos.y),
766                D(pos.z),
767                S(sound),
768                F(vol),
769                F(pitch)
770            ]
771        )
772        .unwrap_or(false)
773    }
774}
775
776unsafe extern "C" fn srv_play_sound_player(
777    _ctx: *mut c_void,
778    player: YogStr,
779    sound: YogStr,
780    vol: f32,
781    pitch: f32,
782) -> bool {
783    unsafe {
784        bridge!("playSoundToPlayer", [S(player), S(sound), F(vol), F(pitch)]).unwrap_or(false)
785    }
786}
787
788unsafe extern "C" fn srv_entity_teleport(_ctx: *mut c_void, uuid: YogStr, pos: YogVec3) -> bool {
789    unsafe { bridge!("entityTeleport", [S(uuid), D(pos.x), D(pos.y), D(pos.z)]).unwrap_or(false) }
790}
791
792unsafe extern "C" fn srv_entity_rotation(
793    _ctx: *mut c_void,
794    uuid: YogStr,
795    out: *mut YogVec3,
796) -> bool {
797    unsafe {
798        let Some(s): Option<String> = bridge!("entityRotation", [S(uuid)]) else {
799            return false;
800        };
801        match parse_tsv::<f64, 2>(&s) {
802            Some([yaw, pitch]) => {
803                *out = YogVec3 {
804                    x: yaw,
805                    y: pitch,
806                    z: 0.0,
807                };
808                true
809            }
810            None => false,
811        }
812    }
813}
814
815unsafe extern "C" fn srv_entity_position(
816    _ctx: *mut c_void,
817    uuid: YogStr,
818    out: *mut YogVec3,
819) -> bool {
820    unsafe {
821        let Some(s): Option<String> = bridge!("entityPosition", [S(uuid)]) else {
822            return false;
823        };
824        match parse_tsv::<f64, 3>(&s) {
825            Some([x, y, z]) => {
826                *out = YogVec3 { x, y, z };
827                true
828            }
829            None => false,
830        }
831    }
832}
833
834unsafe extern "C" fn srv_entity_health(_ctx: *mut c_void, uuid: YogStr, out: *mut f32) -> bool {
835    unsafe {
836        match bridge!("entityHealth", [S(uuid)]) {
837            Some(v) if !f64::is_nan(v) => {
838                *out = v as f32;
839                true
840            }
841            _ => false,
842        }
843    }
844}
845
846unsafe extern "C" fn srv_entity_set_health(_ctx: *mut c_void, uuid: YogStr, hp: f32) -> bool {
847    unsafe { bridge!("entitySetHealth", [S(uuid), D(hp as f64)]).unwrap_or(false) }
848}
849
850unsafe extern "C" fn srv_entity_kill(_ctx: *mut c_void, uuid: YogStr) -> bool {
851    unsafe { bridge!("entityKill", [S(uuid)]).unwrap_or(false) }
852}
853
854unsafe extern "C" fn srv_spawn_entity(
855    _ctx: *mut c_void,
856    type_id: YogStr,
857    dim: YogStr,
858    pos: YogVec3,
859) -> YogOwnedStr {
860    unsafe {
861        owned(bridge!(
862            "spawnEntity",
863            [S(type_id), S(dim), D(pos.x), D(pos.y), D(pos.z)]
864        ))
865    }
866}
867
868unsafe extern "C" fn srv_entity_add_effect(
869    _ctx: *mut c_void,
870    uuid: YogStr,
871    fx: YogStr,
872    dur: i32,
873    amp: u8,
874    particles: bool,
875) -> bool {
876    unsafe {
877        bridge!(
878            "entityAddEffect",
879            [S(uuid), S(fx), I(dur), I(amp as i32), Z(particles)]
880        )
881        .unwrap_or(false)
882    }
883}
884
885unsafe extern "C" fn srv_entity_remove_effect(_ctx: *mut c_void, uuid: YogStr, fx: YogStr) -> bool {
886    unsafe { bridge!("entityRemoveEffect", [S(uuid), S(fx)]).unwrap_or(false) }
887}
888
889unsafe extern "C" fn srv_entity_clear_effects(_ctx: *mut c_void, uuid: YogStr) -> bool {
890    unsafe { bridge!("entityClearEffects", [S(uuid)]).unwrap_or(false) }
891}
892
893unsafe extern "C" fn srv_entity_velocity(
894    _ctx: *mut c_void,
895    uuid: YogStr,
896    out: *mut YogVec3,
897) -> bool {
898    unsafe {
899        let Some(s): Option<String> = bridge!("entityVelocity", [S(uuid)]) else {
900            return false;
901        };
902        match parse_tsv::<f64, 3>(&s) {
903            Some([x, y, z]) => {
904                *out = YogVec3 { x, y, z };
905                true
906            }
907            None => false,
908        }
909    }
910}
911
912unsafe extern "C" fn srv_entity_set_velocity(
913    _ctx: *mut c_void,
914    uuid: YogStr,
915    vel: YogVec3,
916) -> bool {
917    unsafe {
918        bridge!("entitySetVelocity", [S(uuid), D(vel.x), D(vel.y), D(vel.z)]).unwrap_or(false)
919    }
920}
921
922unsafe extern "C" fn srv_entity_add_velocity(
923    _ctx: *mut c_void,
924    uuid: YogStr,
925    vel: YogVec3,
926) -> bool {
927    unsafe {
928        bridge!("entityAddVelocity", [S(uuid), D(vel.x), D(vel.y), D(vel.z)]).unwrap_or(false)
929    }
930}
931
932unsafe extern "C" fn srv_has_item_tag(_ctx: *mut c_void, item: YogStr, tag: YogStr) -> bool {
933    unsafe { bridge!("hasItemTag", [S(item), S(tag)]).unwrap_or(false) }
934}
935
936unsafe extern "C" fn srv_has_block_tag(_ctx: *mut c_void, block: YogStr, tag: YogStr) -> bool {
937    unsafe { bridge!("hasBlockTag", [S(block), S(tag)]).unwrap_or(false) }
938}
939
940unsafe extern "C" fn srv_drop_loot(
941    _ctx: *mut c_void,
942    table: YogStr,
943    dim: YogStr,
944    pos: YogVec3,
945) -> bool {
946    unsafe {
947        bridge!("dropLoot", [S(table), S(dim), D(pos.x), D(pos.y), D(pos.z)]).unwrap_or(false)
948    }
949}
950
951unsafe extern "C" fn srv_scoreboard_get(
952    _ctx: *mut c_void,
953    obj: YogStr,
954    player: YogStr,
955    out: *mut i32,
956) -> bool {
957    unsafe {
958        match bridge!("scoreboardGet", [S(obj), S(player)]) {
959            Some(v) if v != i32::MIN => {
960                *out = v;
961                true
962            }
963            _ => false,
964        }
965    }
966}
967
968unsafe extern "C" fn srv_scoreboard_set(
969    _ctx: *mut c_void,
970    obj: YogStr,
971    player: YogStr,
972    score: i32,
973) -> bool {
974    unsafe { bridge!("scoreboardSet", [S(obj), S(player), I(score)]).unwrap_or(false) }
975}
976
977unsafe extern "C" fn srv_scoreboard_add(
978    _ctx: *mut c_void,
979    obj: YogStr,
980    player: YogStr,
981    delta: i32,
982    out: *mut i32,
983) -> bool {
984    unsafe {
985        match bridge!("scoreboardAdd", [S(obj), S(player), I(delta)]) {
986            Some(v) if v != i32::MIN => {
987                *out = v;
988                true
989            }
990            _ => false,
991        }
992    }
993}
994
995unsafe extern "C" fn srv_bossbar_create(
996    _ctx: *mut c_void,
997    id: YogStr,
998    title: YogStr,
999    color: YogStr,
1000    style: YogStr,
1001) -> bool {
1002    unsafe { bridge!("bossbarCreate", [S(id), S(title), S(color), S(style)]).unwrap_or(false) }
1003}
1004
1005unsafe extern "C" fn srv_bossbar_remove(_ctx: *mut c_void, id: YogStr) -> bool {
1006    unsafe { bridge!("bossbarRemove", [S(id)]).unwrap_or(false) }
1007}
1008
1009unsafe extern "C" fn srv_bossbar_set_title(_ctx: *mut c_void, id: YogStr, title: YogStr) -> bool {
1010    unsafe { bridge!("bossbarSetTitle", [S(id), S(title)]).unwrap_or(false) }
1011}
1012
1013unsafe extern "C" fn srv_bossbar_set_progress(
1014    _ctx: *mut c_void,
1015    id: YogStr,
1016    progress: f32,
1017) -> bool {
1018    unsafe { bridge!("bossbarSetProgress", [S(id), F(progress)]).unwrap_or(false) }
1019}
1020
1021unsafe extern "C" fn srv_bossbar_set_color(_ctx: *mut c_void, id: YogStr, color: YogStr) -> bool {
1022    unsafe { bridge!("bossbarSetColor", [S(id), S(color)]).unwrap_or(false) }
1023}
1024
1025unsafe extern "C" fn srv_bossbar_add_player(_ctx: *mut c_void, id: YogStr, player: YogStr) -> bool {
1026    unsafe { bridge!("bossbarAddPlayer", [S(id), S(player)]).unwrap_or(false) }
1027}
1028
1029unsafe extern "C" fn srv_bossbar_remove_player(
1030    _ctx: *mut c_void,
1031    id: YogStr,
1032    player: YogStr,
1033) -> bool {
1034    unsafe { bridge!("bossbarRemovePlayer", [S(id), S(player)]).unwrap_or(false) }
1035}
1036
1037unsafe extern "C" fn srv_bossbar_set_visible(_ctx: *mut c_void, id: YogStr, visible: bool) -> bool {
1038    unsafe { bridge!("bossbarSetVisible", [S(id), Z(visible)]).unwrap_or(false) }
1039}
1040
1041unsafe extern "C" fn srv_get_block_nbt(
1042    _ctx: *mut c_void,
1043    dim: YogStr,
1044    pos: YogBlockPos,
1045) -> YogOwnedStr {
1046    unsafe {
1047        owned(bridge!(
1048            "getBlockNbt",
1049            [S(dim), I(pos.x), I(pos.y), I(pos.z)]
1050        ))
1051    }
1052}
1053
1054unsafe extern "C" fn srv_set_block_nbt(
1055    _ctx: *mut c_void,
1056    dim: YogStr,
1057    pos: YogBlockPos,
1058    snbt: YogStr,
1059) -> bool {
1060    unsafe {
1061        bridge!(
1062            "setBlockNbt",
1063            [S(dim), I(pos.x), I(pos.y), I(pos.z), S(snbt)]
1064        )
1065        .unwrap_or(false)
1066    }
1067}
1068
1069unsafe extern "C" fn srv_get_inventory_slot(
1070    _ctx: *mut c_void,
1071    dim: YogStr,
1072    pos: yog_abi::YogBlockPos,
1073    slot: u32,
1074) -> YogOwnedStr {
1075    unsafe {
1076        owned(bridge!(
1077            "getInventorySlot",
1078            [S(dim), I(pos.x), I(pos.y), I(pos.z), I(slot as i32)]
1079        ))
1080    }
1081}
1082
1083unsafe extern "C" fn srv_set_inventory_slot(
1084    _ctx: *mut c_void,
1085    dim: YogStr,
1086    pos: yog_abi::YogBlockPos,
1087    slot: u32,
1088    item_id: YogStr,
1089    count: u32,
1090) -> bool {
1091    unsafe {
1092        bridge!(
1093            "setInventorySlot",
1094            [
1095                S(dim),
1096                I(pos.x),
1097                I(pos.y),
1098                I(pos.z),
1099                I(slot as i32),
1100                S(item_id),
1101                I(count as i32)
1102            ]
1103        )
1104        .unwrap_or(false)
1105    }
1106}
1107
1108unsafe extern "C" fn srv_player_inventory(_ctx: *mut c_void, player: YogStr) -> YogOwnedStr {
1109    unsafe { owned(bridge!("playerInventory", [S(player)])) }
1110}
1111
1112unsafe extern "C" fn srv_player_set_slot(
1113    _ctx: *mut c_void,
1114    player: YogStr,
1115    slot: u32,
1116    item_id: YogStr,
1117    count: u32,
1118) -> bool {
1119    unsafe {
1120        bridge!(
1121            "playerSetSlot",
1122            [S(player), I(slot as i32), S(item_id), I(count as i32)]
1123        )
1124        .unwrap_or(false)
1125    }
1126}
1127
1128unsafe extern "C" fn srv_player_teleport_dim(
1129    _ctx: *mut c_void,
1130    player: YogStr,
1131    dim: YogStr,
1132    pos: YogVec3,
1133) -> bool {
1134    unsafe {
1135        bridge!(
1136            "teleportToDim",
1137            [S(player), S(dim), D(pos.x), D(pos.y), D(pos.z)]
1138        )
1139        .unwrap_or(false)
1140    }
1141}
1142
1143unsafe extern "C" fn srv_entity_teleport_dim(
1144    _ctx: *mut c_void,
1145    uuid: YogStr,
1146    dim: YogStr,
1147    pos: YogVec3,
1148) -> bool {
1149    unsafe {
1150        bridge!(
1151            "entityTeleportToDim",
1152            [S(uuid), S(dim), D(pos.x), D(pos.y), D(pos.z)]
1153        )
1154        .unwrap_or(false)
1155    }
1156}
1157
1158unsafe extern "C" fn srv_online_players(_ctx: *mut c_void) -> YogOwnedStr {
1159    unsafe { owned(bridge!("onlinePlayers", [])) }
1160}
1161
1162unsafe extern "C" fn srv_world_entity_count(
1163    _ctx: *mut c_void,
1164    dim: YogStr,
1165    entity_type: YogStr,
1166) -> i32 {
1167    unsafe { bridge!("worldEntityCount", [S(dim), S(entity_type)]).unwrap_or(-1) }
1168}
1169
1170unsafe extern "C" fn srv_game_dir(_ctx: *mut c_void) -> YogOwnedStr {
1171    unsafe { owned(bridge!("gameDir", [])) }
1172}
1173
1174unsafe extern "C" fn srv_entity_get_nbt(_ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr {
1175    unsafe { owned(bridge!("entityGetNbt", [S(uuid)])) }
1176}
1177
1178unsafe extern "C" fn srv_entity_set_nbt(_ctx: *mut c_void, uuid: YogStr, snbt: YogStr) -> bool {
1179    unsafe { bridge!("entitySetNbt", [S(uuid), S(snbt)]).unwrap_or(false) }
1180}
1181
1182unsafe extern "C" fn srv_spawn_particles(
1183    _ctx: *mut c_void,
1184    dim: YogStr,
1185    pos: YogVec3,
1186    particle_type: YogStr,
1187    count: i32,
1188    dx: f64,
1189    dy: f64,
1190    dz: f64,
1191    speed: f64,
1192) -> bool {
1193    unsafe {
1194        bridge!(
1195            "spawnParticles",
1196            [
1197                S(dim),
1198                D(pos.x),
1199                D(pos.y),
1200                D(pos.z),
1201                S(particle_type),
1202                I(count),
1203                D(dx),
1204                D(dy),
1205                D(dz),
1206                D(speed)
1207            ]
1208        )
1209        .unwrap_or(false)
1210    }
1211}
1212
1213unsafe extern "C" fn srv_entity_attribute_get(
1214    _ctx: *mut c_void,
1215    uuid: YogStr,
1216    attr: YogStr,
1217) -> f64 {
1218    unsafe { bridge!("entityAttributeGet", [S(uuid), S(attr)]).unwrap_or(f64::NAN) }
1219}
1220
1221unsafe extern "C" fn srv_entity_attribute_set(
1222    _ctx: *mut c_void,
1223    uuid: YogStr,
1224    attr: YogStr,
1225    value: f64,
1226) -> bool {
1227    unsafe { bridge!("entityAttributeSet", [S(uuid), S(attr), D(value)]).unwrap_or(false) }
1228}
1229
1230unsafe extern "C" fn srv_get_held_item_nbt(_ctx: *mut c_void, player: YogStr) -> YogOwnedStr {
1231    unsafe { owned(bridge!("getHeldItemNbt", [S(player)])) }
1232}
1233
1234unsafe extern "C" fn srv_set_held_item_nbt(
1235    _ctx: *mut c_void,
1236    player: YogStr,
1237    snbt: YogStr,
1238) -> bool {
1239    unsafe { bridge!("setHeldItemNbt", [S(player), S(snbt)]).unwrap_or(false) }
1240}
1241
1242unsafe extern "C" fn srv_get_offhand_item_nbt(_ctx: *mut c_void, player: YogStr) -> YogOwnedStr {
1243    unsafe { owned(bridge!("getOffhandItemNbt", [S(player)])) }
1244}
1245
1246unsafe extern "C" fn srv_set_offhand_item_nbt(
1247    _ctx: *mut c_void,
1248    player: YogStr,
1249    snbt: YogStr,
1250) -> bool {
1251    unsafe { bridge!("setOffhandItemNbt", [S(player), S(snbt)]).unwrap_or(false) }
1252}
1253
1254unsafe extern "C" fn srv_get_slot_item(
1255    _ctx: *mut c_void,
1256    player: YogStr,
1257    slot: u32,
1258) -> YogOwnedStr {
1259    unsafe { owned(bridge!("getSlotItem", [S(player), I(slot as i32)])) }
1260}
1261
1262unsafe extern "C" fn srv_set_slot_item(
1263    _ctx: *mut c_void,
1264    player: YogStr,
1265    slot: u32,
1266    item_id: YogStr,
1267    count: u32,
1268    snbt: YogStr,
1269) -> bool {
1270    unsafe {
1271        bridge!(
1272            "setSlotItem",
1273            [
1274                S(player),
1275                I(slot as i32),
1276                S(item_id),
1277                I(count as i32),
1278                S(snbt)
1279            ]
1280        )
1281        .unwrap_or(false)
1282    }
1283}
1284
1285// ── ABI minor 14 — low-level GPU pipeline ────────────────────────────────────
1286//
1287// All raw GL calls go through `glow::Context` stored in GL.
1288// `draw2d_*` functions still call JNI (NativeDraw) for MC text/texture rendering.
1289// Everything is called on the render thread — no synchronization needed.
1290
1291// ── helpers ───────────────────────────────────────────────────────────────────
1292
1293fn gl_draw_mode(mode: u8) -> u32 {
1294    match mode {
1295        1 => glow::LINES,
1296        2 => glow::LINE_STRIP,
1297        3 => glow::TRIANGLE_STRIP,
1298        4 => glow::TRIANGLE_FAN,
1299        _ => glow::TRIANGLES,
1300    }
1301}
1302
1303fn gl_attr_type(dtype: u8) -> u32 {
1304    match dtype {
1305        1 => glow::UNSIGNED_BYTE,
1306        2 => glow::INT,
1307        3 => glow::UNSIGNED_INT,
1308        _ => glow::FLOAT,
1309    }
1310}
1311
1312unsafe fn compile_shader(gl: &glow::Context, stage: u32, src: &str) -> Option<glow::NativeShader> {
1313    unsafe {
1314        let sh = gl.create_shader(stage).ok()?;
1315        gl.shader_source(sh, src);
1316        gl.compile_shader(sh);
1317        if !gl.get_shader_compile_status(sh) {
1318            yog_logging::error!("yog-gfx shader compile: {}", gl.get_shader_info_log(sh));
1319            gl.delete_shader(sh);
1320            return None;
1321        }
1322        Some(sh)
1323    }
1324}
1325
1326// ── GPU buffers ───────────────────────────────────────────────────────────────
1327
1328unsafe extern "C" fn gfx_buf_create() -> u32 {
1329    unsafe {
1330        let Some(g) = GL.get() else { return 0 };
1331        g.0.create_buffer().map(|b| b.0.get()).unwrap_or(0)
1332    }
1333}
1334
1335unsafe extern "C" fn gfx_buf_delete(handle: u32) {
1336    unsafe {
1337        let Some(g) = GL.get() else { return };
1338        let Some(n) = NonZeroU32::new(handle) else {
1339            return;
1340        };
1341        g.0.delete_buffer(glow::NativeBuffer(n));
1342    }
1343}
1344
1345unsafe extern "C" fn gfx_buf_data(handle: u32, bytes: *const u8, len: u32, dynamic: bool) {
1346    unsafe {
1347        let Some(g) = GL.get() else { return };
1348        let Some(n) = NonZeroU32::new(handle) else {
1349            return;
1350        };
1351        let gl = &g.0;
1352        let data = std::slice::from_raw_parts(bytes, len as usize);
1353        let usage = if dynamic {
1354            glow::DYNAMIC_DRAW
1355        } else {
1356            glow::STATIC_DRAW
1357        };
1358        gl.bind_buffer(glow::ARRAY_BUFFER, Some(glow::NativeBuffer(n)));
1359        gl.buffer_data_u8_slice(glow::ARRAY_BUFFER, data, usage);
1360        gl.bind_buffer(glow::ARRAY_BUFFER, None);
1361    }
1362}
1363
1364unsafe extern "C" fn gfx_buf_subdata(handle: u32, offset: u32, bytes: *const u8, len: u32) {
1365    unsafe {
1366        let Some(g) = GL.get() else { return };
1367        let Some(n) = NonZeroU32::new(handle) else {
1368            return;
1369        };
1370        let gl = &g.0;
1371        let data = std::slice::from_raw_parts(bytes, len as usize);
1372        gl.bind_buffer(glow::ARRAY_BUFFER, Some(glow::NativeBuffer(n)));
1373        gl.buffer_sub_data_u8_slice(glow::ARRAY_BUFFER, offset as i32, data);
1374        gl.bind_buffer(glow::ARRAY_BUFFER, None);
1375    }
1376}
1377
1378// ── Vertex arrays ─────────────────────────────────────────────────────────────
1379
1380unsafe extern "C" fn gfx_vao_create() -> u32 {
1381    unsafe {
1382        let Some(g) = GL.get() else { return 0 };
1383        g.0.create_vertex_array().map(|v| v.0.get()).unwrap_or(0)
1384    }
1385}
1386
1387unsafe extern "C" fn gfx_vao_delete(handle: u32) {
1388    unsafe {
1389        let Some(g) = GL.get() else { return };
1390        let Some(n) = NonZeroU32::new(handle) else {
1391            return;
1392        };
1393        g.0.delete_vertex_array(glow::NativeVertexArray(n));
1394    }
1395}
1396
1397unsafe extern "C" fn gfx_vao_attrib(
1398    vao: u32,
1399    vbo: u32,
1400    index: u32,
1401    components: u8,
1402    dtype: u8,
1403    normalized: bool,
1404    stride: u32,
1405    offset: u32,
1406) {
1407    unsafe {
1408        let Some(g) = GL.get() else { return };
1409        let (Some(vn), Some(bn)) = (NonZeroU32::new(vao), NonZeroU32::new(vbo)) else {
1410            return;
1411        };
1412        let gl = &g.0;
1413        gl.bind_vertex_array(Some(glow::NativeVertexArray(vn)));
1414        gl.bind_buffer(glow::ARRAY_BUFFER, Some(glow::NativeBuffer(bn)));
1415        let gl_type = gl_attr_type(dtype);
1416        if dtype == 2 || dtype == 3 {
1417            gl.vertex_attrib_pointer_i32(
1418                index,
1419                components as i32,
1420                gl_type,
1421                stride as i32,
1422                offset as i32,
1423            );
1424        } else {
1425            gl.vertex_attrib_pointer_f32(
1426                index,
1427                components as i32,
1428                gl_type,
1429                normalized,
1430                stride as i32,
1431                offset as i32,
1432            );
1433        }
1434        gl.enable_vertex_attrib_array(index);
1435        gl.bind_vertex_array(None);
1436    }
1437}
1438
1439unsafe extern "C" fn gfx_vao_set_ebo(vao: u32, ebo: u32) {
1440    unsafe {
1441        let Some(g) = GL.get() else { return };
1442        let (Some(vn), Some(en)) = (NonZeroU32::new(vao), NonZeroU32::new(ebo)) else {
1443            return;
1444        };
1445        let gl = &g.0;
1446        gl.bind_vertex_array(Some(glow::NativeVertexArray(vn)));
1447        gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(glow::NativeBuffer(en)));
1448        gl.bind_vertex_array(None);
1449    }
1450}
1451
1452// ── Shader programs ───────────────────────────────────────────────────────────
1453
1454/// Returns a path under `~/.cache/yog/shaders/<hash>.ysc` for the given GLSL source pair,
1455/// creating the directory if needed.  Returns `None` if the home directory is unknown.
1456fn shader_cache_path(vert: &str, frag: &str) -> Option<PathBuf> {
1457    use std::hash::{Hash, Hasher};
1458    let mut h = std::collections::hash_map::DefaultHasher::new();
1459    vert.hash(&mut h);
1460    frag.hash(&mut h);
1461    let hash = h.finish();
1462    let dir = std::env::var("HOME")
1463        .map(|home| {
1464            PathBuf::from(home)
1465                .join(".cache")
1466                .join("yog")
1467                .join("shaders")
1468        })
1469        .ok()?;
1470    std::fs::create_dir_all(&dir).ok()?;
1471    Some(dir.join(format!("{hash:016x}.ysc")))
1472}
1473
1474/// Try restoring a program from a binary blob (`[4 LE bytes: GL format][binary…]`).
1475/// Returns `Some(handle)` if the driver accepts the binary, `None` otherwise.
1476///
1477/// Uses `glProgramBinary` (GL 4.1 / ARB_get_program_binary) via the raw pointer
1478/// captured in `nativeGlInit`.  Returns `None` if the extension is unavailable.
1479unsafe fn load_shader_binary(gl: &glow::Context, data: &[u8]) -> Option<glow::NativeProgram> {
1480    unsafe {
1481        if data.len() < 4 {
1482            return None;
1483        }
1484        type ProgramBinaryFn = unsafe extern "system" fn(u32, u32, *const c_void, i32);
1485        let fn_ptr = (*GL_PROGRAM_BINARY.get()?)?;
1486        let program_binary: ProgramBinaryFn = std::mem::transmute(fn_ptr);
1487
1488        let fmt = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
1489        let prog = gl.create_program().ok()?;
1490        program_binary(
1491            prog.0.get(),
1492            fmt,
1493            data[4..].as_ptr() as *const c_void,
1494            (data.len() - 4) as i32,
1495        );
1496        if gl.get_program_link_status(prog) {
1497            Some(prog)
1498        } else {
1499            gl.delete_program(prog);
1500            None
1501        }
1502    }
1503}
1504
1505/// Read the compiled binary of a linked program.  Returns empty `Vec` if unsupported.
1506unsafe fn get_shader_binary(prog: glow::NativeProgram) -> (Vec<u8>, u32) {
1507    unsafe {
1508        type GetProgramivFn = unsafe extern "system" fn(u32, u32, *mut i32);
1509        type GetProgramBinaryFn =
1510            unsafe extern "system" fn(u32, i32, *mut i32, *mut u32, *mut c_void);
1511
1512        let Some(get_iv_raw) = GL_GET_PROGRAM_IV.get().and_then(|v| *v) else {
1513            return (vec![], 0);
1514        };
1515        let Some(get_bin_raw) = GL_GET_PROGRAM_BINARY.get().and_then(|v| *v) else {
1516            return (vec![], 0);
1517        };
1518        let get_program_iv: GetProgramivFn = std::mem::transmute(get_iv_raw);
1519        let get_program_binary: GetProgramBinaryFn = std::mem::transmute(get_bin_raw);
1520
1521        // Query binary size via glGetProgramiv(GL_PROGRAM_BINARY_LENGTH).
1522        const PROGRAM_BINARY_LENGTH: u32 = 0x8741;
1523        let mut size: i32 = 0;
1524        get_program_iv(prog.0.get(), PROGRAM_BINARY_LENGTH, &mut size);
1525        if size <= 0 {
1526            return (vec![], 0);
1527        }
1528
1529        let mut buf = vec![0u8; size as usize];
1530        let mut actual_len: i32 = 0;
1531        let mut fmt: u32 = 0;
1532        get_program_binary(
1533            prog.0.get(),
1534            size,
1535            &mut actual_len,
1536            &mut fmt,
1537            buf.as_mut_ptr() as *mut c_void,
1538        );
1539        buf.truncate(actual_len.max(0) as usize);
1540        (buf, fmt)
1541    }
1542}
1543
1544unsafe extern "C" fn gfx_prog_create(vert: YogStr, frag: YogStr, out: *mut u32) -> bool {
1545    unsafe {
1546        let Some(g) = GL.get() else { return false };
1547        let gl = &g.0;
1548        let vert_s = vert.as_str();
1549        let frag_s = frag.as_str();
1550
1551        // Fast path: binary shader cache — avoids GLSL re-compilation on subsequent launches.
1552        let cache_path = shader_cache_path(vert_s, frag_s);
1553        if let Some(ref path) = cache_path
1554            && let Ok(data) = std::fs::read(path)
1555        {
1556            if let Some(prog) = load_shader_binary(gl, &data) {
1557                *out = prog.0.get();
1558                return true;
1559            }
1560            // Cache stale (driver updated?); remove and fall through to GLSL compile.
1561            let _ = std::fs::remove_file(path);
1562        }
1563
1564        // GLSL compile path.
1565        let vs = match compile_shader(gl, glow::VERTEX_SHADER, vert_s) {
1566            Some(s) => s,
1567            None => return false,
1568        };
1569        let fs = match compile_shader(gl, glow::FRAGMENT_SHADER, frag_s) {
1570            Some(s) => s,
1571            None => {
1572                gl.delete_shader(vs);
1573                return false;
1574            }
1575        };
1576        let prog = match gl.create_program() {
1577            Ok(p) => p,
1578            Err(e) => {
1579                yog_logging::error!("yog-gfx: create_program: {}", e);
1580                gl.delete_shader(vs);
1581                gl.delete_shader(fs);
1582                return false;
1583            }
1584        };
1585        gl.attach_shader(prog, vs);
1586        gl.attach_shader(prog, fs);
1587        gl.link_program(prog);
1588        gl.detach_shader(prog, vs);
1589        gl.detach_shader(prog, fs);
1590        gl.delete_shader(vs);
1591        gl.delete_shader(fs);
1592        if !gl.get_program_link_status(prog) {
1593            yog_logging::error!("yog-gfx: shader link: {}", gl.get_program_info_log(prog));
1594            gl.delete_program(prog);
1595            return false;
1596        }
1597
1598        // Persist binary so the next launch skips GLSL compilation entirely.
1599        if let Some(ref path) = cache_path {
1600            let (binary, fmt) = get_shader_binary(prog);
1601            if !binary.is_empty() {
1602                let mut blob = fmt.to_le_bytes().to_vec();
1603                blob.extend_from_slice(&binary);
1604                let _ = std::fs::write(path, &blob);
1605            }
1606        }
1607
1608        *out = prog.0.get();
1609        true
1610    }
1611}
1612
1613unsafe extern "C" fn gfx_prog_delete(handle: u32) {
1614    unsafe {
1615        let Some(g) = GL.get() else { return };
1616        let Some(n) = NonZeroU32::new(handle) else {
1617            return;
1618        };
1619        g.0.delete_program(glow::NativeProgram(n));
1620    }
1621}
1622
1623macro_rules! with_prog {
1624    ($handle:expr_2021, |$gl:ident, $prog:ident, $loc:ident ($name:expr_2021)| $body:expr_2021) => {{
1625        let Some(g) = GL.get() else { return };
1626        let Some(n) = NonZeroU32::new($handle) else {
1627            return;
1628        };
1629        let $gl = &g.0;
1630        let $prog = glow::NativeProgram(n);
1631        $gl.use_program(Some($prog));
1632        let $loc = $gl.get_uniform_location($prog, $name.as_str());
1633        $body
1634    }};
1635}
1636
1637unsafe extern "C" fn gfx_prog_uniform_1i(prog: u32, name: YogStr, v: i32) {
1638    unsafe {
1639        with_prog!(prog, |gl, _p, loc(name)| gl.uniform_1_i32(loc.as_ref(), v));
1640    }
1641}
1642unsafe extern "C" fn gfx_prog_uniform_1f(prog: u32, name: YogStr, v: f32) {
1643    unsafe {
1644        with_prog!(prog, |gl, _p, loc(name)| gl.uniform_1_f32(loc.as_ref(), v));
1645    }
1646}
1647unsafe extern "C" fn gfx_prog_uniform_2f(prog: u32, name: YogStr, x: f32, y: f32) {
1648    unsafe {
1649        with_prog!(prog, |gl, _p, loc(name)| gl.uniform_2_f32(
1650            loc.as_ref(),
1651            x,
1652            y
1653        ));
1654    }
1655}
1656unsafe extern "C" fn gfx_prog_uniform_3f(prog: u32, name: YogStr, x: f32, y: f32, z: f32) {
1657    unsafe {
1658        with_prog!(prog, |gl, _p, loc(name)| gl.uniform_3_f32(
1659            loc.as_ref(),
1660            x,
1661            y,
1662            z
1663        ));
1664    }
1665}
1666unsafe extern "C" fn gfx_prog_uniform_4f(prog: u32, name: YogStr, x: f32, y: f32, z: f32, w: f32) {
1667    unsafe {
1668        with_prog!(prog, |gl, _p, loc(name)| gl.uniform_4_f32(
1669            loc.as_ref(),
1670            x,
1671            y,
1672            z,
1673            w
1674        ));
1675    }
1676}
1677unsafe extern "C" fn gfx_prog_uniform_mat4(prog: u32, name: YogStr, col_major: *const f32) {
1678    unsafe {
1679        with_prog!(prog, |gl, _p, loc(name)| {
1680            let data = std::slice::from_raw_parts(col_major, 16);
1681            gl.uniform_matrix_4_f32_slice(loc.as_ref(), false, data);
1682        });
1683    }
1684}
1685
1686// ── Textures ──────────────────────────────────────────────────────────────────
1687
1688unsafe extern "C" fn gfx_tex_create(w: u32, h: u32, rgba: *const u8, linear: bool) -> u32 {
1689    unsafe {
1690        let Some(g) = GL.get() else { return 0 };
1691        let gl = &g.0;
1692        let tex = match gl.create_texture() {
1693            Ok(t) => t,
1694            Err(_) => return 0,
1695        };
1696        gl.bind_texture(glow::TEXTURE_2D, Some(tex));
1697        let filter = if linear { glow::LINEAR } else { glow::NEAREST };
1698        gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, filter as i32);
1699        gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, filter as i32);
1700        gl.tex_parameter_i32(
1701            glow::TEXTURE_2D,
1702            glow::TEXTURE_WRAP_S,
1703            glow::CLAMP_TO_EDGE as i32,
1704        );
1705        gl.tex_parameter_i32(
1706            glow::TEXTURE_2D,
1707            glow::TEXTURE_WRAP_T,
1708            glow::CLAMP_TO_EDGE as i32,
1709        );
1710        let pixels = std::slice::from_raw_parts(rgba, (w * h * 4) as usize);
1711        // The game leaves its own unpacking (row length, skips) set from its
1712        // atlas uploads: read with those, a tight buffer is overrun.
1713        unpack_tightly(gl);
1714        gl.tex_image_2d(
1715            glow::TEXTURE_2D,
1716            0,
1717            glow::RGBA8 as i32,
1718            w as i32,
1719            h as i32,
1720            0,
1721            glow::RGBA,
1722            glow::UNSIGNED_BYTE,
1723            glow::PixelUnpackData::Slice(Some(pixels)),
1724        );
1725        gl.bind_texture(glow::TEXTURE_2D, None);
1726        tex.0.get()
1727    }
1728}
1729
1730/// Pixel unpacking as a tightly packed client buffer needs it.
1731unsafe fn unpack_tightly(gl: &glow::Context) {
1732    unsafe {
1733        gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None);
1734        gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, 0);
1735        gl.pixel_store_i32(glow::UNPACK_SKIP_ROWS, 0);
1736        gl.pixel_store_i32(glow::UNPACK_SKIP_PIXELS, 0);
1737        gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 4);
1738    }
1739}
1740
1741unsafe extern "C" fn gfx_tex_delete(handle: u32) {
1742    unsafe {
1743        let Some(g) = GL.get() else { return };
1744        let Some(n) = NonZeroU32::new(handle) else {
1745            return;
1746        };
1747        g.0.delete_texture(glow::NativeTexture(n));
1748    }
1749}
1750
1751unsafe extern "C" fn gfx_tex_bind(unit: u32, handle: u32) {
1752    unsafe {
1753        let Some(g) = GL.get() else { return };
1754        let gl = &g.0;
1755        gl.active_texture(glow::TEXTURE0 + unit);
1756        match NonZeroU32::new(handle) {
1757            Some(n) => gl.bind_texture(glow::TEXTURE_2D, Some(glow::NativeTexture(n))),
1758            None => gl.bind_texture(glow::TEXTURE_2D, None),
1759        }
1760    }
1761}
1762
1763unsafe extern "C" fn gfx_tex_from_mc(id: YogStr) -> u32 {
1764    unsafe { host!("dev/yog/NativeDraw", "getMcTextureId", [S(id)]).map_or(0, |id: i32| id as u32) }
1765}
1766
1767// ── Draw calls ────────────────────────────────────────────────────────────────
1768
1769unsafe extern "C" fn gfx_draw_arrays(vao: u32, prog: u32, mode: u8, first: u32, count: u32) {
1770    unsafe {
1771        let Some(g) = GL.get() else { return };
1772        let (Some(vn), Some(pn)) = (NonZeroU32::new(vao), NonZeroU32::new(prog)) else {
1773            return;
1774        };
1775        let gl = &g.0;
1776        gl.use_program(Some(glow::NativeProgram(pn)));
1777        gl.bind_vertex_array(Some(glow::NativeVertexArray(vn)));
1778        gl.draw_arrays(gl_draw_mode(mode), first as i32, count as i32);
1779        gl.bind_vertex_array(None);
1780    }
1781}
1782
1783unsafe extern "C" fn gfx_draw_elements(
1784    vao: u32,
1785    ebo: u32,
1786    prog: u32,
1787    mode: u8,
1788    count: u32,
1789    u32_idx: bool,
1790) {
1791    unsafe {
1792        let Some(g) = GL.get() else { return };
1793        let (Some(vn), Some(pn)) = (NonZeroU32::new(vao), NonZeroU32::new(prog)) else {
1794            return;
1795        };
1796        let gl = &g.0;
1797        gl.use_program(Some(glow::NativeProgram(pn)));
1798        gl.bind_vertex_array(Some(glow::NativeVertexArray(vn)));
1799        // EBO is stored in the VAO; ebo param is informational for safety but not re-bound here.
1800        let _ = ebo;
1801        let idx_type = if u32_idx {
1802            glow::UNSIGNED_INT
1803        } else {
1804            glow::UNSIGNED_SHORT
1805        };
1806        gl.draw_elements(gl_draw_mode(mode), count as i32, idx_type, 0);
1807        gl.bind_vertex_array(None);
1808    }
1809}
1810
1811// ── Render state ──────────────────────────────────────────────────────────────
1812
1813unsafe extern "C" fn gfx_set_blend(enabled: bool, src: u32, dst: u32) {
1814    unsafe {
1815        let Some(g) = GL.get() else { return };
1816        let gl = &g.0;
1817        if enabled {
1818            gl.enable(glow::BLEND);
1819            gl.blend_func(src, dst);
1820        } else {
1821            gl.disable(glow::BLEND);
1822        }
1823    }
1824}
1825
1826unsafe extern "C" fn gfx_set_depth(test: bool, write: bool) {
1827    unsafe {
1828        let Some(g) = GL.get() else { return };
1829        let gl = &g.0;
1830        if test {
1831            gl.enable(glow::DEPTH_TEST);
1832        } else {
1833            gl.disable(glow::DEPTH_TEST);
1834        }
1835        gl.depth_mask(write);
1836        // Disable face culling: our 2D quads use CW winding (back-face) due to y-flip.
1837        if !test {
1838            gl.disable(glow::CULL_FACE);
1839        }
1840    }
1841}
1842
1843unsafe extern "C" fn gfx_set_scissor(x: i32, y: i32, w: i32, h: i32) {
1844    unsafe {
1845        let Some(g) = GL.get() else { return };
1846        let gl = &g.0;
1847        gl.enable(glow::SCISSOR_TEST);
1848        gl.scissor(x, y, w, h);
1849    }
1850}
1851
1852unsafe extern "C" fn gfx_clear_scissor() {
1853    unsafe {
1854        let Some(g) = GL.get() else { return };
1855        g.0.disable(glow::SCISSOR_TEST);
1856    }
1857}
1858
1859unsafe extern "C" fn gfx_set_viewport(x: i32, y: i32, w: i32, h: i32) {
1860    unsafe {
1861        let Some(g) = GL.get() else { return };
1862        g.0.viewport(x, y, w, h);
1863    }
1864}
1865
1866// ── 2D convenience (JNI — uses MC's DrawContext / text renderer) ──────────────
1867
1868unsafe extern "C" fn gfx_draw2d_rect(x1: f32, y1: f32, x2: f32, y2: f32, color: u32) {
1869    unsafe {
1870        let _: Option<()> = host!(
1871            "dev/yog/NativeDraw",
1872            "drawRect",
1873            [F(x1), F(y1), F(x2), F(y2), I(color as i32)]
1874        );
1875    }
1876}
1877
1878unsafe extern "C" fn gfx_draw2d_gradient(
1879    x1: f32,
1880    y1: f32,
1881    x2: f32,
1882    y2: f32,
1883    top: u32,
1884    bottom: u32,
1885) {
1886    unsafe {
1887        let _: Option<()> = host!(
1888            "dev/yog/NativeDraw",
1889            "drawGradientRect",
1890            [F(x1), F(y1), F(x2), F(y2), I(top as i32), I(bottom as i32)]
1891        );
1892    }
1893}
1894
1895unsafe extern "C" fn gfx_draw2d_text(text: YogStr, x: f32, y: f32, color: u32, shadow: bool) {
1896    unsafe {
1897        let _: Option<()> = host!(
1898            "dev/yog/NativeDraw",
1899            "drawText",
1900            [S(text), F(x), F(y), I(color as i32), Z(shadow)]
1901        );
1902    }
1903}
1904
1905unsafe extern "C" fn gfx_draw2d_text_font(
1906    text: YogStr,
1907    font: YogStr,
1908    x: f32,
1909    y: f32,
1910    color: u32,
1911    shadow: bool,
1912) {
1913    unsafe {
1914        let _: Option<()> = host!(
1915            "dev/yog/NativeDraw",
1916            "drawTextFont",
1917            [S(text), S(font), F(x), F(y), I(color as i32), Z(shadow)]
1918        );
1919    }
1920}
1921
1922unsafe extern "C" fn gfx_draw2d_text_scaled(
1923    text: YogStr,
1924    font: YogStr,
1925    x: f32,
1926    y: f32,
1927    scale: f32,
1928    color: u32,
1929    shadow: bool,
1930) {
1931    unsafe {
1932        let _: Option<()> = host!(
1933            "dev/yog/NativeDraw",
1934            "drawTextScaled",
1935            [S(text), S(font), F(x), F(y), F(scale), I(color as i32), Z(shadow)]
1936        );
1937    }
1938}
1939
1940unsafe extern "C" fn gfx_draw2d_blocks(blocks: YogStr, x: f32, y: f32, w: f32, h: f32, yaw: f32, pitch: f32) {
1941    if w <= 0.0 || h <= 0.0 {
1942        return;
1943    }
1944    unsafe {
1945        let _: Option<()> = host!(
1946            "dev/yog/NativeDraw",
1947            "drawBlocks",
1948            [S(blocks), F(x), F(y), F(w), F(h), F(yaw), F(pitch)]
1949        );
1950    }
1951}
1952
1953unsafe extern "C" fn gfx_text_width(text: YogStr, font: YogStr) -> f32 {
1954    unsafe { host!("dev/yog/NativeDraw", "textWidth", [S(text), S(font)]).unwrap_or(0.0) }
1955}
1956
1957unsafe extern "C" fn gfx_draw2d_item(id: YogStr, x: f32, y: f32, size: f32) {
1958    unsafe {
1959        let _: Option<()> = host!(
1960            "dev/yog/NativeDraw",
1961            "drawItem",
1962            [S(id), F(x), F(y), F(size)]
1963        );
1964    }
1965}
1966
1967// ── Static GFX function table (function pointers only — per-frame data filled at call time) ──
1968
1969static GFX_FN_TABLE: YogGfxApi = YogGfxApi {
1970    // Per-frame fields zeroed in the static; actual values are set on the stack per render call.
1971    screen_w: 0,
1972    screen_h: 0,
1973    delta_tick: 0.0,
1974    scale_factor: 1.0,
1975    view_proj: [0.0; 16],
1976    camera_pos: [0.0; 3],
1977    player_pos: [0.0; 3],
1978    _pad1: 0.0,
1979    buf_create: gfx_buf_create,
1980    buf_delete: gfx_buf_delete,
1981    buf_data: gfx_buf_data,
1982    buf_subdata: gfx_buf_subdata,
1983    vao_create: gfx_vao_create,
1984    vao_delete: gfx_vao_delete,
1985    vao_attrib: gfx_vao_attrib,
1986    vao_set_ebo: gfx_vao_set_ebo,
1987    prog_create: gfx_prog_create,
1988    prog_delete: gfx_prog_delete,
1989    prog_uniform_1i: gfx_prog_uniform_1i,
1990    prog_uniform_1f: gfx_prog_uniform_1f,
1991    prog_uniform_2f: gfx_prog_uniform_2f,
1992    prog_uniform_3f: gfx_prog_uniform_3f,
1993    prog_uniform_4f: gfx_prog_uniform_4f,
1994    prog_uniform_mat4: gfx_prog_uniform_mat4,
1995    tex_create: gfx_tex_create,
1996    tex_delete: gfx_tex_delete,
1997    tex_bind: gfx_tex_bind,
1998    tex_from_mc: gfx_tex_from_mc,
1999    draw_arrays: gfx_draw_arrays,
2000    draw_elements: gfx_draw_elements,
2001    set_blend: gfx_set_blend,
2002    set_depth: gfx_set_depth,
2003    set_scissor: gfx_set_scissor,
2004    clear_scissor: gfx_clear_scissor,
2005    set_viewport: gfx_set_viewport,
2006    draw2d_rect: gfx_draw2d_rect,
2007    draw2d_gradient: gfx_draw2d_gradient,
2008    draw2d_text: gfx_draw2d_text,
2009    draw2d_item: gfx_draw2d_item,
2010    draw2d_text_font: gfx_draw2d_text_font,
2011    text_width: gfx_text_width,
2012    draw2d_image: images::gfx_draw2d_image,
2013    image_size: images::gfx_image_size,
2014    draw2d_svg: svg::gfx_draw2d_svg,
2015    draw2d_entity: svg::gfx_draw2d_entity,
2016    draw2d_text_scaled: gfx_draw2d_text_scaled,
2017    draw2d_blocks: gfx_draw2d_blocks,
2018};
2019
2020// ── YogApi registration functions ─────────────────────────────────────────────
2021//
2022// ctx is *const RwLock<RuntimeHandlers> (cast from *mut c_void) — see
2023// `current_registration()` for how each entry gets tagged with the
2024// `LoadedModule` that's registering it.
2025
2026macro_rules! api_event {
2027    ($name:ident, $field:ident, $fn_ty:ty) => {
2028        unsafe extern "C" fn $name(ctx: *mut c_void, ud: *mut c_void, h: $fn_ty) {
2029            unsafe {
2030                let mut handlers = handlers_mut(ctx);
2031                handlers.$field.push((current_registration(), ud, h));
2032            }
2033        }
2034    };
2035}
2036
2037api_event!(api_on_block_break, block_break, YogBlockBreakFn);
2038api_event!(api_on_chat, chat, YogChatFn);
2039api_event!(api_on_player_join, player_join, YogPlayerFn);
2040api_event!(api_on_player_leave, player_leave, YogPlayerFn);
2041api_event!(api_on_use_item, use_item, YogUseItemFn);
2042api_event!(api_on_use_block, use_block, YogUseBlockFn);
2043api_event!(api_on_attack_entity, attack_entity, YogAttackEntityFn);
2044api_event!(api_on_entity_damage, entity_damage, YogEntityDamageFn);
2045api_event!(api_on_entity_death, entity_death, YogEntityDeathFn);
2046api_event!(api_on_entity_spawn, entity_spawn, YogEntitySpawnFn);
2047api_event!(
2048    api_on_player_place_block,
2049    player_place_block,
2050    YogPlaceBlockFn
2051);
2052api_event!(api_on_player_death, player_death, YogPlayerDeathFn);
2053api_event!(api_on_player_respawn, player_respawn, YogPlayerRespawnFn);
2054api_event!(api_on_advancement, advancement, YogAdvancementFn);
2055api_event!(api_on_entity_interact, entity_interact, YogEntityInteractFn);
2056api_event!(api_on_item_craft, item_craft, YogCraftFn);
2057api_event!(api_on_machine_craft, machine_craft, YogMachineCraftFn);
2058api_event!(api_on_explosion, explosion, YogExplosionFn);
2059api_event!(api_on_item_pickup, item_pickup, YogItemPickupFn);
2060api_event!(api_on_item_insert, item_insert, YogItemInsertFn);
2061api_event!(api_on_player_move, player_move, YogPlayerMoveFn);
2062api_event!(api_on_container_open, container_open, YogContainerOpenFn);
2063api_event!(api_on_container_close, container_close, YogContainerCloseFn);
2064api_event!(api_on_projectile_hit, projectile_hit, YogProjectileHitFn);
2065api_event!(api_on_client_tick, client_tick, YogClientFn);
2066api_event!(api_on_hud_render, hud_render, YogHudRenderFn);
2067api_event!(api_on_world_render, world_render, YogWorldRenderFn);
2068api_event!(api_on_key_press, key_press, YogKeyPressFn);
2069api_event!(api_on_screen_open, screen_open, YogScreenFn);
2070api_event!(api_on_screen_close, screen_close, YogScreenFn);
2071api_event!(api_on_server_tick, server_tick, YogServerFn);
2072api_event!(api_on_server_started, server_started, YogServerFn);
2073api_event!(api_on_server_stopping, server_stopping, YogServerFn);
2074
2075unsafe extern "C" fn api_on_packet(
2076    ctx: *mut c_void,
2077    channel: YogStr,
2078    ud: *mut c_void,
2079    h: YogPacketFn,
2080) {
2081    unsafe {
2082        let mut handlers = handlers_mut(ctx);
2083        handlers
2084            .packets
2085            .insert(channel.as_str().to_owned(), (current_registration(), ud, h));
2086    }
2087}
2088
2089unsafe extern "C" fn api_on_client_packet(
2090    ctx: *mut c_void,
2091    channel: YogStr,
2092    ud: *mut c_void,
2093    h: YogPacketFn,
2094) {
2095    unsafe {
2096        let mut handlers = handlers_mut(ctx);
2097        handlers
2098            .client_packets
2099            .insert(channel.as_str().to_owned(), (current_registration(), ud, h));
2100    }
2101}
2102
2103unsafe extern "C" fn api_register_command(
2104    ctx: *mut c_void,
2105    name: YogStr,
2106    ud: *mut c_void,
2107    h: YogCommandFn,
2108) {
2109    unsafe {
2110        let mut handlers = handlers_mut(ctx);
2111        handlers
2112            .commands
2113            .entry(name.as_str().to_owned())
2114            .or_default()
2115            .push((current_registration(), None, ud, h));
2116    }
2117}
2118
2119unsafe extern "C" fn api_register_typed_command(
2120    ctx: *mut c_void,
2121    name: YogStr,
2122    schema: YogStr,
2123    ud: *mut c_void,
2124    h: YogCommandFn,
2125) {
2126    unsafe {
2127        let mut handlers = handlers_mut(ctx);
2128        let n = name.as_str().to_owned();
2129        handlers.commands.entry(n).or_default().push((
2130            current_registration(),
2131            Some(schema.as_str().to_owned()),
2132            ud,
2133            h,
2134        ));
2135    }
2136}
2137
2138/// The handlers a registration call writes into: `ctx` is the
2139/// `RwLock<RuntimeHandlers>` the runtime handed the mod's `yog_mod_register`.
2140/// A poisoned lock (a mod panicked mid-registration) is taken as it is —
2141/// one mod's panic doesn't stop the others registering.
2142///
2143/// # Safety
2144/// `ctx` must be that registration context.
2145unsafe fn handlers_mut<'a>(ctx: *mut c_void) -> std::sync::RwLockWriteGuard<'a, RuntimeHandlers> {
2146    unsafe { (*(ctx as *const RwLock<RuntimeHandlers>)).write().unwrap_or_else(|e| e.into_inner()) }
2147}
2148
2149/// `register_content`: content of `kind` named `id`, its definition a Yog
2150/// Data blob — the one way mods hand the runtime what the hosts register.
2151unsafe extern "C" fn api_register_content(ctx: *mut c_void, kind: YogStr, id: YogStr, data: *const u8, len: usize) {
2152    unsafe {
2153        let kind = kind.as_str();
2154        let id = id.as_str().to_owned();
2155        let bytes = if data.is_null() || len == 0 { &[][..] } else { std::slice::from_raw_parts(data, len) };
2156        // Borrowed: strings stay in `bytes`, only what's kept is copied.
2157        let arena = yog_data::Bump::new();
2158        let value = match yog_data::binary::decode_ref(bytes, &arena) {
2159            Ok(v) => v,
2160            Err(e) => {
2161                yog_logging::warn!("{kind} {id}: not Yog Data: {e}");
2162                return;
2163            }
2164        };
2165        register_content(&mut handlers_mut(ctx), kind, id, &value);
2166    }
2167}
2168
2169/// Stores content of `kind` (see `register_content` in yog-abi).
2170fn register_content(h: &mut RuntimeHandlers, kind: &str, id: String, value: &yog_data::ValueRef) {
2171    *h.content_blob.get_mut().unwrap_or_else(|e| e.into_inner()) = None;
2172    fn read<T: yog_data::Schema>(kind: &str, id: &str, v: &yog_data::ValueRef) -> Option<T> {
2173        match yog_data::read_ref_value::<T>(v) {
2174            Ok(value) => Some(value),
2175            Err(problems) => {
2176                let list: Vec<String> = problems.iter().map(|p| format!("{}: {}", p.path, p.message)).collect();
2177                yog_logging::warn!("{kind} {id}: {}", list.join("; "));
2178                None
2179            }
2180        }
2181    }
2182    let none_if_empty = |s: &mut Option<String>| {
2183        if s.as_deref() == Some("") {
2184            *s = None;
2185        }
2186    };
2187    match kind {
2188        "item" => {
2189            if let Some(mut d) = read::<ItemDef>(kind, &id, value) {
2190                none_if_empty(&mut d.name);
2191                none_if_empty(&mut d.tooltip);
2192                h.items.push(d);
2193            }
2194        }
2195        "block" => {
2196            if let Some(mut d) = read::<BlockDef>(kind, &id, value) {
2197                none_if_empty(&mut d.name);
2198                none_if_empty(&mut d.sound);
2199                none_if_empty(&mut d.inventory_id);
2200                h.blocks.push(d);
2201            }
2202        }
2203        "biome_feature" => {
2204            if let Some(f) = read::<yog_registry::BiomeFeature>(kind, &id, value) {
2205                h.biome_features.push(f);
2206            }
2207        }
2208        "tool_material" => {
2209            if let Some(m) = read::<yog_registry::ToolMaterial>(kind, &id, value) {
2210                h.tool_materials.insert(m.id.clone(), m);
2211            }
2212        }
2213        "armor_material" => {
2214            if let Some(m) = read::<yog_registry::ArmorMaterial>(kind, &id, value) {
2215                h.armor_materials.insert(m.id.clone(), m);
2216            }
2217        }
2218        "sound_group" => {
2219            if let Some(g) = read::<yog_registry::SoundGroup>(kind, &id, value) {
2220                h.sound_groups.insert(g.id.clone(), g);
2221            }
2222        }
2223        "inventory" => {
2224            if let Some(d) = read::<yog_inventory::InventoryDef>(kind, &id, value) {
2225                h.inventories.push(d);
2226            }
2227        }
2228        "startup_grant" => {
2229            if let Some(mut g) = read::<yog_registry::StartupGrant>(kind, &id, value) {
2230                none_if_empty(&mut g.book);
2231                none_if_empty(&mut g.command);
2232                h.startup_grants.push(g);
2233            }
2234        }
2235        "recipe" => {
2236            let (ns, name) = id.split_once(':').unwrap_or(("minecraft", &id));
2237            let recipe = value.to_value();
2238            // Feed the recipe to the books already registered (their recipe pages).
2239            for r in h.book_renderers.lock().unwrap().values_mut() {
2240                r.add_recipe(format!("{ns}:{name}"), &recipe);
2241            }
2242            h.recipes.push((ns.to_string(), name.to_string(), recipe));
2243        }
2244        "book" => {
2245            // Read by its schema; the renderer draws it without a round trip to Java.
2246            if let Some(mut book) = read::<yog_book::Book>(kind, &id, value) {
2247                // Entries about mods that aren't here aren't shown.
2248                book.entries.retain(|e| e.requires.iter().all(|m| mod_loaded(m)));
2249                h.books.insert(id.clone());
2250                let mut renderer = yog_book::BookRenderer::new(book);
2251                // i18n books carry translation keys; shown in the game language.
2252                renderer.set_translator(translate);
2253                // Recipes may register before the book — replay them into the renderer.
2254                for (ns, name, recipe) in &h.recipes {
2255                    renderer.add_recipe(format!("{ns}:{name}"), recipe);
2256                }
2257                h.book_renderers.get_mut().unwrap_or_else(|e| e.into_inner()).insert(id, renderer);
2258            }
2259        }
2260        "dimension" => {
2261            yog_logging::info!("registered dimension type: {}", id);
2262            let mut def = value.to_value();
2263            // The terrain stays here; the host gets the dimension, its generator this one.
2264            if let Some(t) = value.get("terrain") {
2265                let Some(terrain) = read::<yog_worldgen::Terrain>(kind, &id, t) else { return };
2266                if let yog_data::Value::Map(m) = &mut def {
2267                    m.remove("terrain");
2268                    m.insert("generator_type", yog_data::Value::from(id.as_str()));
2269                }
2270                h.terrains.insert(id.clone(), Arc::new(yog_worldgen::Generator::new(terrain)));
2271            }
2272            h.dimensions.insert(id, def);
2273        }
2274        other => yog_logging::warn!("unknown kind of content `{other}` ({id}) — this runtime doesn't know it"),
2275    }
2276}
2277
2278
2279
2280
2281unsafe extern "C" fn api_schedule_once(
2282    ctx: *mut c_void,
2283    delay_ticks: u64,
2284    ud: *mut c_void,
2285    h: YogScheduledFn,
2286) {
2287    unsafe {
2288        let handlers = handlers_mut(ctx);
2289        handlers
2290            .scheduler
2291            .lock()
2292            .expect("scheduler poisoned")
2293            .once_tasks
2294            .push(OnceTask {
2295                module: current_registration(),
2296                delay_remaining: delay_ticks,
2297                ud,
2298                f: h,
2299            });
2300    }
2301}
2302
2303unsafe extern "C" fn api_schedule_repeating(
2304    ctx: *mut c_void,
2305    period_ticks: u64,
2306    ud: *mut c_void,
2307    h: YogScheduledFn,
2308) {
2309    unsafe {
2310        let handlers = handlers_mut(ctx);
2311        handlers
2312            .scheduler
2313            .lock()
2314            .expect("scheduler poisoned")
2315            .repeating_tasks
2316            .push(RepeatingTask {
2317                module: current_registration(),
2318                period: period_ticks,
2319                ticks_left: period_ticks.max(1),
2320                ud,
2321                f: h,
2322            });
2323    }
2324}
2325
2326
2327
2328
2329unsafe extern "C" fn api_register_chunk_generator(
2330    ctx: *mut c_void,
2331    id: YogStr,
2332    ud: *mut c_void,
2333    h: yog_abi::YogChunkGeneratorFn,
2334) {
2335    unsafe {
2336        let mut handlers = handlers_mut(ctx);
2337        let id = id.as_str().to_owned();
2338        yog_logging::info!("registered chunk generator for dimension: {}", id);
2339        handlers
2340            .chunk_generators
2341            .insert(id, (current_registration(), ud, h));
2342    }
2343}
2344
2345unsafe extern "C" fn api_register_ui(
2346    ctx: *mut c_void,
2347    ui_id: YogStr,
2348    _layout_json: YogStr,
2349    ud: *mut c_void,
2350    h: yog_abi::YogUIEventFn,
2351) {
2352    unsafe {
2353        let mut handlers = handlers_mut(ctx);
2354        let id = ui_id.as_str().to_owned();
2355        handlers
2356            .ui_handlers
2357            .insert(id.clone(), (current_registration(), ud, h));
2358        yog_logging::info!("registered UI handler: {}", id);
2359    }
2360}
2361
2362unsafe extern "C" fn api_on_ui_render(
2363    ctx: *mut c_void,
2364    ui_id: YogStr,
2365    ud: *mut c_void,
2366    h: YogHudRenderFn,
2367) {
2368    unsafe {
2369        let mut handlers = handlers_mut(ctx);
2370        let id = ui_id.as_str().to_owned();
2371        handlers
2372            .ui_render_handlers
2373            .entry(id)
2374            .or_default()
2375            .push((current_registration(), ud, h));
2376    }
2377}
2378
2379unsafe extern "C" fn api_on_book_page(
2380    ctx: *mut c_void,
2381    drawer: YogStr,
2382    ud: *mut c_void,
2383    f: yog_abi::YogPageDrawFn,
2384) {
2385    unsafe {
2386        let mut handlers = handlers_mut(ctx);
2387        handlers
2388            .page_drawers
2389            .insert(drawer.as_str().to_owned(), (current_registration(), ud, f));
2390    }
2391}
2392
2393unsafe extern "C" fn api_on_item_tooltip(
2394    ctx: *mut c_void,
2395    item: YogStr,
2396    ud: *mut c_void,
2397    size: yog_abi::YogTooltipSizeFn,
2398    draw: yog_abi::YogTooltipDrawFn,
2399) {
2400    unsafe {
2401        let mut handlers = handlers_mut(ctx);
2402        handlers
2403            .item_tooltips
2404            .entry(item.as_str().to_owned())
2405            .or_default()
2406            .push((current_registration(), ud, size, draw));
2407    }
2408}
2409
2410unsafe extern "C" fn api_on_item_text(ctx: *mut c_void, item: YogStr, ud: *mut c_void, f: yog_abi::YogItemTextFn) {
2411    unsafe {
2412        handlers_mut(ctx).item_texts.entry(item.as_str().to_owned()).or_default().push((current_registration(), ud, f));
2413    }
2414}
2415
2416unsafe extern "C" fn copy_text(sink: *mut c_void, text: YogStr) {
2417    // SAFETY: `sink` is the `String` `item_text` passes; `text` is valid for the call.
2418    unsafe { (*(sink as *mut String)).push_str(text.as_str()) }
2419}
2420
2421/// A stack's own name and tooltip lines (`name\nline…`), from the first
2422/// mod that gives them; `None`: the item's own.
2423fn item_text(item: &str, snbt: &str) -> Option<String> {
2424    let entries = handlers().item_texts.get(item).cloned().unwrap_or_default();
2425    for (module, ud, f) in entries {
2426        let Some(_call) = module.enter() else { continue };
2427        let mut out = String::new();
2428        let mut given = false;
2429        guard("item_text", || unsafe {
2430            given = f(ud, YogStr::from_str(item), YogStr::from_str(snbt), &mut out as *mut String as *mut c_void, copy_text);
2431        });
2432        if given {
2433            return Some(out);
2434        }
2435    }
2436    None
2437}
2438
2439#[unsafe(no_mangle)]
2440pub extern "system" fn Java_dev_yog_NativeBridge_nativeItemText<'l>(
2441    mut env: JNIEnv<'l>,
2442    _class: JClass<'l>,
2443    item: JString<'l>,
2444    snbt: JString<'l>,
2445) -> jstring {
2446    let item: String = env.get_string(&item).map(String::from).unwrap_or_default();
2447    let snbt: String = env.get_string(&snbt).map(String::from).unwrap_or_default();
2448    match item_text(&item, &snbt) {
2449        Some(t) => env.new_string(t).map(JString::into_raw).unwrap_or(std::ptr::null_mut()),
2450        None => std::ptr::null_mut(),
2451    }
2452}
2453
2454// ── Machines (ABI 2.5) ───────────────────────────────────────────────────────
2455
2456/// A machine's event about recipe `found`.
2457fn machine_event<'a>(
2458    m: &'a yog_inventory::Machine,
2459    def: &'a str,
2460    found: &'a machine::Found,
2461    dim: &'a str,
2462    pos: YogBlockPos,
2463    player: &'a str,
2464) -> YogMachineCraftEvent {
2465    YogMachineCraftEvent {
2466        machine: YogStr::from_str(def),
2467        kind: YogStr::from_str(&m.kind),
2468        recipe: YogStr::from_str(&found.recipe),
2469        result_item: YogStr::from_str(&found.result),
2470        result_count: found.count,
2471        values: YogStr::from_str(&found.values),
2472        pos,
2473        dimension: YogStr::from_str(dim),
2474        player: YogStr::from_str(player),
2475    }
2476}
2477
2478/// The recipe machine `def` at `pos` makes of `items` (by inventory slot,
2479/// `""` empty): the first that fits and every mod allows; `recipe` only it.
2480fn machine_recipe(def: &str, dim: &str, pos: YogBlockPos, items: &[String], recipe: Option<&str>) -> Option<machine::Found> {
2481    let h = handlers();
2482    let m = h.inventories.iter().find(|d| d.id == def)?.machine.as_ref()?;
2483    let has_tag = |item: &str, tag: &str| unsafe { srv_has_item_tag(std::ptr::null_mut(), YogStr::from_str(item), YogStr::from_str(tag)) };
2484    let recipes = h.recipes.iter().map(|(ns, name, r)| (format!("{ns}:{name}"), r)).filter(|(id, _)| recipe.is_none_or(|r| r == id));
2485    let srv = srv_ptr();
2486    machine::find(m, recipes, items, &has_tag).find(|found| {
2487        let ev = machine_event(m, def, found, dim, pos, "");
2488        allowed(&h.machine_craft, |ud, f| unsafe { f(ud, srv, &ev, 0) })
2489    })
2490}
2491
2492/// What machine `def` at `x y z` makes of `items` (`\n`-joined, by slot):
2493/// `recipe\nresult\ncount`, or null.
2494#[unsafe(no_mangle)]
2495pub extern "system" fn Java_dev_yog_NativeBridge_nativeMachineResult<'l>(
2496    mut env: JNIEnv<'l>,
2497    _class: JClass<'l>,
2498    def: JString<'l>,
2499    dimension: JString<'l>,
2500    x: jint,
2501    y: jint,
2502    z: jint,
2503    items: JString<'l>,
2504) -> jstring {
2505    let (def, d, items) = (jstr!(env, def, std::ptr::null_mut()), jstr!(env, dimension, std::ptr::null_mut()), jstr!(env, items, std::ptr::null_mut()));
2506    let items: Vec<String> = items.split('\n').map(str::to_string).collect();
2507    let mut found = None;
2508    guard("machine_result", || found = machine_recipe(&def, &d, YogBlockPos { x, y, z }, &items, None));
2509    match found {
2510        Some(f) => env.new_string(format!("{}\n{}\n{}", f.recipe, f.result, f.count)).map(JString::into_raw).unwrap_or(std::ptr::null_mut()),
2511        None => std::ptr::null_mut(),
2512    }
2513}
2514
2515/// `player` took machine `def`'s result, made by `recipe` of `items` (the
2516/// ingredients still in): whether it may — then the mods take their share.
2517#[unsafe(no_mangle)]
2518#[allow(clippy::too_many_arguments)]
2519pub extern "system" fn Java_dev_yog_NativeBridge_nativeMachineCraft<'l>(
2520    mut env: JNIEnv<'l>,
2521    _class: JClass<'l>,
2522    def: JString<'l>,
2523    recipe: JString<'l>,
2524    dimension: JString<'l>,
2525    x: jint,
2526    y: jint,
2527    z: jint,
2528    items: JString<'l>,
2529    player: JString<'l>,
2530) -> jni::sys::jboolean {
2531    let (def, recipe, d, items, player) =
2532        (jstr!(env, def, 0), jstr!(env, recipe, 0), jstr!(env, dimension, 0), jstr!(env, items, 0), jstr!(env, player, 0));
2533    let items: Vec<String> = items.split('\n').map(str::to_string).collect();
2534    let pos = YogBlockPos { x, y, z };
2535    let mut made = false;
2536    guard("machine_craft", || {
2537        let Some(found) = machine_recipe(&def, &d, pos, &items, Some(&recipe)) else { return };
2538        let h = handlers();
2539        let Some(m) = h.inventories.iter().find(|i| i.id == def).and_then(|i| i.machine.as_ref()) else { return };
2540        let ev = machine_event(m, &def, &found, &d, pos, &player);
2541        let srv = srv_ptr();
2542        each(&h.machine_craft, |ud, f| unsafe { f(ud, srv, &ev, 1) });
2543        made = true;
2544    });
2545    made as jni::sys::jboolean
2546}
2547
2548/// The Yog mods being loaded, by id (all of them, before the first registers).
2549static YOG_MOD_IDS: RwLock<std::collections::BTreeSet<String>> = RwLock::new(std::collections::BTreeSet::new());
2550
2551/// Whether mod `id` is loaded: a Yog mod, else one in the host's list
2552/// (Fabric's, Forge's, NeoForge's).
2553fn mod_loaded(id: &str) -> bool {
2554    YOG_MOD_IDS.read().unwrap_or_else(|e| e.into_inner()).contains(id)
2555        || HOT_RELOADER.get().is_some_and(|r| r.all_active().iter().any(|(m, _)| m == id))
2556        // SAFETY: a host call with a string argument, on the calling thread.
2557        || unsafe { bridge!("isModLoaded", [JArg::S(YogStr::from_str(id))]) }.unwrap_or(false)
2558}
2559
2560unsafe extern "C" fn api_is_mod_loaded(_ctx: *mut c_void, id: YogStr) -> bool {
2561    unsafe { mod_loaded(id.as_str()) }
2562}
2563
2564unsafe extern "C" fn api_java_call(_ctx: *mut c_void, expr: YogStr, args: YogStr) -> YogOwnedStr {
2565    unsafe {
2566        let args = match yog_data::text::parse_value(args.as_str()) {
2567            Ok(v) => v,
2568            Err(e) => {
2569                yog_logging::warn!("java_call {}: its arguments aren't Yog Data: {}", expr.as_str(), e.message);
2570                return YogOwnedStr::NONE;
2571            }
2572        };
2573        let bytes = yog_data::binary::encode(&args);
2574        let out: Option<Vec<u8>> = host!("dev/yog/data/YogJava", "call", [JArg::S(expr), JArg::Bytes(&bytes)]);
2575        match out.map(|b| yog_data::binary::decode(&b)) {
2576            Some(Ok(v)) => YogOwnedStr::from_string(yog_data::text::to_string(&v)),
2577            _ => YogOwnedStr::NONE,
2578        }
2579    }
2580}
2581
2582/// Strip TSV separators so one mod can't corrupt the line format.
2583fn tsv_clean(v: &str) -> String {
2584    v.replace(['\t', '\n', '\r'], " ")
2585}
2586
2587unsafe extern "C" fn api_ui_open(_ctx: *mut c_void, ui_id: YogStr, modal: bool, pause: bool) {
2588    unsafe {
2589        if ui_id.as_str().is_empty() {
2590            return;
2591        }
2592        // Java side schedules onto the render thread; no-op on dedicated servers.
2593        let _: Option<()> = bridge!("openUI", [S(ui_id), Z(modal), Z(pause)]);
2594    }
2595}
2596
2597unsafe extern "C" fn api_ui_close(_ctx: *mut c_void, ui_id: YogStr) {
2598    unsafe {
2599        // Java side defers onto the render thread (after the current event) and
2600        // checks the open screen is that Yog UI; no-op on dedicated servers.
2601        let _: Option<()> = bridge!("closeUI", [S(ui_id)]);
2602    }
2603}
2604
2605unsafe extern "C" fn api_ui_consume_event(_ctx: *mut c_void) {
2606    ui_jni::consume_event();
2607}
2608
2609unsafe extern "C" fn api_translate(_ctx: *mut c_void, key: YogStr) -> YogOwnedStr {
2610    unsafe { translate(key.as_str()).map_or(YogOwnedStr::NONE, YogOwnedStr::from_string) }
2611}
2612
2613/// The game's text for translation `key` in the current language — `None`
2614/// when no lang file has it (or there's no JVM).
2615fn translate(key: &str) -> Option<String> {
2616    if key.is_empty() {
2617        return None;
2618    }
2619    unsafe { bridge!("translate", [S(YogStr::from_str(key))]) }
2620}
2621
2622unsafe extern "C" fn api_mods_list(_ctx: *mut c_void) -> YogOwnedStr {
2623    unsafe {
2624        let mut lines: Vec<String> = Vec::new();
2625        let infos = MOD_INFOS.lock().expect("mod infos lock poisoned").clone();
2626        for m in &infos {
2627            // A `.yres` name/description shows in the player's language.
2628            let localized = |text: &str, key: &str| {
2629                translate(key).map_or_else(|| text.to_string(), |t| tsv_clean(&t))
2630            };
2631            lines.push(format!(
2632                "yog\t{}\t{}\t{}\t{}\t{}",
2633                m[0],
2634                localized(&m[1], &m[5]),
2635                m[2],
2636                m[3],
2637                localized(&m[4], &m[6])
2638            ));
2639        }
2640        // Loader-side (Java) mods, if the host provides them.
2641        let platform: Option<String> = bridge!("listPlatformMods", []);
2642        for line in platform.iter().flat_map(|s| s.lines()) {
2643            if !line.trim().is_empty() {
2644                lines.push(format!("platform\t{line}"));
2645            }
2646        }
2647        YogOwnedStr::from_string(lines.join("\n"))
2648    }
2649}
2650
2651unsafe extern "C" fn api_register_menu_entry(ctx: *mut c_void, label: YogStr, ui_id: YogStr) {
2652    unsafe {
2653        let mut handlers = handlers_mut(ctx);
2654        let l = label.as_str().to_owned();
2655        let u = ui_id.as_str().to_owned();
2656        handlers.menu_entries.push((l, u));
2657    }
2658}
2659
2660// ── World queries & effects (ABI minor 1) ────────────────────────────────────
2661//
2662// Each of these forwards to a `NativeBridge` static of the same shape; the JNI
2663// signature is derived from the argument list so the two cannot drift apart.
2664
2665fn vec_args(v: YogVec3) -> [JArg<'static>; 3] {
2666    [JArg::D(v.x), JArg::D(v.y), JArg::D(v.z)]
2667}
2668
2669fn pos_args(p: YogBlockPos) -> [JArg<'static>; 3] {
2670    [JArg::I(p.x), JArg::I(p.y), JArg::I(p.z)]
2671}
2672
2673unsafe extern "C" fn srv_entity_info(_ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr {
2674    unsafe { owned(bridge!("entityInfo", [JArg::S(uuid)])) }
2675}
2676
2677unsafe extern "C" fn srv_entities_in_box(
2678    _ctx: *mut c_void,
2679    dim: YogStr,
2680    min: YogVec3,
2681    max: YogVec3,
2682) -> YogOwnedStr {
2683    unsafe {
2684        let [a, b, c] = vec_args(min);
2685        let [d, e, f] = vec_args(max);
2686        owned(bridge!("entitiesInBox", [JArg::S(dim), a, b, c, d, e, f]))
2687    }
2688}
2689
2690unsafe extern "C" fn srv_raycast_block(
2691    _ctx: *mut c_void,
2692    dim: YogStr,
2693    from: YogVec3,
2694    to: YogVec3,
2695) -> YogOwnedStr {
2696    unsafe {
2697        let [a, b, c] = vec_args(from);
2698        let [d, e, f] = vec_args(to);
2699        owned(bridge!("raycastBlock", [JArg::S(dim), a, b, c, d, e, f]))
2700    }
2701}
2702
2703unsafe extern "C" fn srv_raycast_entity(
2704    _ctx: *mut c_void,
2705    dim: YogStr,
2706    from: YogVec3,
2707    to: YogVec3,
2708    exclude: YogStr,
2709) -> YogOwnedStr {
2710    unsafe {
2711        let [a, b, c] = vec_args(from);
2712        let [d, e, f] = vec_args(to);
2713        owned(bridge!(
2714            "raycastEntity",
2715            [JArg::S(dim), a, b, c, d, e, f, JArg::S(exclude)]
2716        ))
2717    }
2718}
2719
2720unsafe extern "C" fn srv_block_info(
2721    _ctx: *mut c_void,
2722    dim: YogStr,
2723    pos: YogBlockPos,
2724) -> YogOwnedStr {
2725    unsafe {
2726        let [x, y, z] = pos_args(pos);
2727        owned(bridge!("blockInfo", [JArg::S(dim), x, y, z]))
2728    }
2729}
2730
2731unsafe extern "C" fn srv_break_block(
2732    _ctx: *mut c_void,
2733    dim: YogStr,
2734    pos: YogBlockPos,
2735    drop: bool,
2736    breaker: YogStr,
2737) -> bool {
2738    unsafe {
2739        let [x, y, z] = pos_args(pos);
2740        bridge!(
2741            "breakBlock",
2742            [JArg::S(dim), x, y, z, JArg::Z(drop), JArg::S(breaker)]
2743        )
2744        .unwrap_or(false)
2745    }
2746}
2747
2748unsafe extern "C" fn srv_place_block_from_slot(
2749    _ctx: *mut c_void,
2750    player: YogStr,
2751    slot: u32,
2752    dim: YogStr,
2753    pos: YogBlockPos,
2754) -> bool {
2755    unsafe {
2756        let [x, y, z] = pos_args(pos);
2757        bridge!(
2758            "placeBlockFromSlot",
2759            [JArg::S(player), JArg::I(slot as i32), JArg::S(dim), x, y, z]
2760        )
2761        .unwrap_or(false)
2762    }
2763}
2764
2765unsafe extern "C" fn srv_bonemeal(_ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> bool {
2766    unsafe {
2767        let [x, y, z] = pos_args(pos);
2768        bridge!("bonemeal", [JArg::S(dim), x, y, z]).unwrap_or(false)
2769    }
2770}
2771
2772unsafe extern "C" fn srv_explode(
2773    _ctx: *mut c_void,
2774    dim: YogStr,
2775    pos: YogVec3,
2776    power: f32,
2777    fire: bool,
2778    source: YogStr,
2779) -> bool {
2780    unsafe {
2781        let [x, y, z] = vec_args(pos);
2782        bridge!(
2783            "explode",
2784            [
2785                JArg::S(dim),
2786                x,
2787                y,
2788                z,
2789                JArg::F(power),
2790                JArg::Z(fire),
2791                JArg::S(source),
2792            ]
2793        )
2794        .unwrap_or(false)
2795    }
2796}
2797
2798unsafe extern "C" fn srv_tellraw(_ctx: *mut c_void, player: YogStr, json: YogStr) -> bool {
2799    unsafe { bridge!("tellraw", [JArg::S(player), JArg::S(json)]).unwrap_or(false) }
2800}
2801
2802unsafe extern "C" fn srv_entity_damage(
2803    _ctx: *mut c_void,
2804    uuid: YogStr,
2805    amount: f32,
2806    damage_type: YogStr,
2807    attacker: YogStr,
2808) -> bool {
2809    unsafe {
2810        bridge!(
2811            "entityDamage",
2812            [
2813                JArg::S(uuid),
2814                JArg::F(amount),
2815                JArg::S(damage_type),
2816                JArg::S(attacker),
2817            ]
2818        )
2819        .unwrap_or(false)
2820    }
2821}
2822
2823unsafe extern "C" fn srv_entity_set_fire_ticks(
2824    _ctx: *mut c_void,
2825    uuid: YogStr,
2826    ticks: i32,
2827) -> bool {
2828    unsafe { bridge!("entitySetFireTicks", [JArg::S(uuid), JArg::I(ticks)]).unwrap_or(false) }
2829}
2830
2831unsafe extern "C" fn srv_player_add_xp(_ctx: *mut c_void, player: YogStr, amount: i32) -> bool {
2832    unsafe { bridge!("playerAddXp", [JArg::S(player), JArg::I(amount)]).unwrap_or(false) }
2833}
2834
2835unsafe extern "C" fn srv_player_set_may_fly(
2836    _ctx: *mut c_void,
2837    player: YogStr,
2838    may_fly: bool,
2839) -> bool {
2840    unsafe { bridge!("playerSetMayFly", [JArg::S(player), JArg::Z(may_fly)]).unwrap_or(false) }
2841}
2842
2843unsafe extern "C" fn srv_player_info(_ctx: *mut c_void, player: YogStr) -> YogOwnedStr {
2844    unsafe { owned(bridge!("playerInfo", [JArg::S(player)])) }
2845}
2846
2847unsafe extern "C" fn srv_spawn_item(
2848    _ctx: *mut c_void,
2849    dim: YogStr,
2850    pos: YogVec3,
2851    vel: YogVec3,
2852    item_id: YogStr,
2853    count: u32,
2854    snbt: YogStr,
2855) -> YogOwnedStr {
2856    unsafe {
2857        let [a, b, c] = vec_args(pos);
2858        let [d, e, f] = vec_args(vel);
2859        owned(bridge!(
2860            "spawnItem",
2861            [
2862                JArg::S(dim),
2863                a,
2864                b,
2865                c,
2866                d,
2867                e,
2868                f,
2869                JArg::S(item_id),
2870                JArg::I(count as i32),
2871                JArg::S(snbt),
2872            ]
2873        ))
2874    }
2875}
2876
2877unsafe extern "C" fn srv_item_entity_stack(_ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr {
2878    unsafe { owned(bridge!("itemEntityStack", [JArg::S(uuid)])) }
2879}
2880
2881unsafe extern "C" fn srv_actionbar_json(_ctx: *mut c_void, player: YogStr, json: YogStr) -> bool {
2882    unsafe { bridge!("actionbarJson", [JArg::S(player), JArg::S(json)]).unwrap_or(false) }
2883}
2884
2885unsafe extern "C" fn srv_use_item_on_block(
2886    _ctx: *mut c_void,
2887    dim: YogStr,
2888    pos: YogBlockPos,
2889    face: u8,
2890    item_id: YogStr,
2891    player: YogStr,
2892) -> bool {
2893    unsafe {
2894        let [x, y, z] = pos_args(pos);
2895        bridge!(
2896            "useItemOnBlock",
2897            [
2898                JArg::S(dim),
2899                x,
2900                y,
2901                z,
2902                JArg::I(face as i32),
2903                JArg::S(item_id),
2904                JArg::S(player),
2905            ]
2906        )
2907        .unwrap_or(false)
2908    }
2909}
2910
2911unsafe extern "C" fn srv_pour_fluid(
2912    _ctx: *mut c_void,
2913    dim: YogStr,
2914    pos: YogBlockPos,
2915    bucket_item_id: YogStr,
2916) -> bool {
2917    unsafe {
2918        let [x, y, z] = pos_args(pos);
2919        bridge!(
2920            "pourFluid",
2921            [JArg::S(dim), x, y, z, JArg::S(bucket_item_id)]
2922        )
2923        .unwrap_or(false)
2924    }
2925}
2926
2927unsafe extern "C" fn srv_has_entity_tag(
2928    _ctx: *mut c_void,
2929    entity_type: YogStr,
2930    tag: YogStr,
2931) -> bool {
2932    unsafe { bridge!("hasEntityTag", [JArg::S(entity_type), JArg::S(tag)]).unwrap_or(false) }
2933}
2934
2935unsafe extern "C" fn srv_item_block(_ctx: *mut c_void, item_id: YogStr) -> YogOwnedStr {
2936    unsafe { owned(bridge!("itemBlock", [JArg::S(item_id)])) }
2937}
2938
2939unsafe extern "C" fn srv_place_feature(
2940    _ctx: *mut c_void,
2941    dim: YogStr,
2942    pos: YogBlockPos,
2943    feature_id: YogStr,
2944) -> bool {
2945    unsafe {
2946        let [x, y, z] = pos_args(pos);
2947        bridge!("placeFeature", [JArg::S(dim), x, y, z, JArg::S(feature_id)]).unwrap_or(false)
2948    }
2949}
2950
2951unsafe extern "C" fn srv_item_entity_set_stack(
2952    _ctx: *mut c_void,
2953    uuid: YogStr,
2954    item_id: YogStr,
2955    count: u32,
2956    snbt: YogStr,
2957) -> bool {
2958    unsafe {
2959        bridge!(
2960            "itemEntitySetStack",
2961            [
2962                JArg::S(uuid),
2963                JArg::S(item_id),
2964                JArg::I(count as i32),
2965                JArg::S(snbt),
2966            ]
2967        )
2968        .unwrap_or(false)
2969    }
2970}
2971
2972// ── Table constructors ────────────────────────────────────────────────────────
2973
2974fn build_server_table() -> YogServer {
2975    YogServer {
2976        ctx: std::ptr::null_mut(),
2977        abi_version: ABI_VERSION,
2978        size: std::mem::size_of::<YogServer>() as u32,
2979        free_str: yog_free_str,
2980        broadcast: srv_broadcast,
2981        get_block: srv_get_block,
2982        set_block: srv_set_block,
2983        world_time: srv_world_time,
2984        set_time: srv_set_time,
2985        is_raining: srv_is_raining,
2986        set_weather: srv_set_weather,
2987        give_item: srv_give_item,
2988        player_teleport: srv_player_teleport,
2989        send_to_player: srv_send_to_player,
2990        send_to_server: srv_send_to_server,
2991        kick_player: srv_kick_player,
2992        set_gamemode: srv_set_gamemode,
2993        send_title: srv_send_title,
2994        send_actionbar: srv_send_actionbar,
2995        play_sound: srv_play_sound,
2996        play_sound_player: srv_play_sound_player,
2997        entity_teleport: srv_entity_teleport,
2998        entity_position: srv_entity_position,
2999        entity_rotation: srv_entity_rotation,
3000        entity_health: srv_entity_health,
3001        entity_set_health: srv_entity_set_health,
3002        entity_kill: srv_entity_kill,
3003        spawn_entity: srv_spawn_entity,
3004        entity_add_effect: srv_entity_add_effect,
3005        entity_remove_effect: srv_entity_remove_effect,
3006        entity_clear_effects: srv_entity_clear_effects,
3007        entity_velocity: srv_entity_velocity,
3008        entity_set_velocity: srv_entity_set_velocity,
3009        entity_add_velocity: srv_entity_add_velocity,
3010        has_item_tag: srv_has_item_tag,
3011        has_block_tag: srv_has_block_tag,
3012        drop_loot: srv_drop_loot,
3013        scoreboard_get: srv_scoreboard_get,
3014        scoreboard_set: srv_scoreboard_set,
3015        scoreboard_add: srv_scoreboard_add,
3016        bossbar_create: srv_bossbar_create,
3017        bossbar_remove: srv_bossbar_remove,
3018        bossbar_set_title: srv_bossbar_set_title,
3019        bossbar_set_progress: srv_bossbar_set_progress,
3020        bossbar_set_color: srv_bossbar_set_color,
3021        bossbar_add_player: srv_bossbar_add_player,
3022        bossbar_remove_player: srv_bossbar_remove_player,
3023        bossbar_set_visible: srv_bossbar_set_visible,
3024        game_dir: srv_game_dir,
3025        get_block_nbt: srv_get_block_nbt,
3026        set_block_nbt: srv_set_block_nbt,
3027        get_inventory_slot: srv_get_inventory_slot,
3028        set_inventory_slot: srv_set_inventory_slot,
3029        player_inventory: srv_player_inventory,
3030        player_set_slot: srv_player_set_slot,
3031        player_teleport_dim: srv_player_teleport_dim,
3032        entity_teleport_dim: srv_entity_teleport_dim,
3033        online_players: srv_online_players,
3034        world_entity_count: srv_world_entity_count,
3035        entity_get_nbt: srv_entity_get_nbt,
3036        entity_set_nbt: srv_entity_set_nbt,
3037        spawn_particles: srv_spawn_particles,
3038        entity_attribute_get: srv_entity_attribute_get,
3039        entity_attribute_set: srv_entity_attribute_set,
3040        get_held_item_nbt: srv_get_held_item_nbt,
3041        set_held_item_nbt: srv_set_held_item_nbt,
3042        get_offhand_item_nbt: srv_get_offhand_item_nbt,
3043        set_offhand_item_nbt: srv_set_offhand_item_nbt,
3044        get_slot_item: srv_get_slot_item,
3045        set_slot_item: srv_set_slot_item,
3046        entity_info: srv_entity_info,
3047        entities_in_box: srv_entities_in_box,
3048        raycast_block: srv_raycast_block,
3049        raycast_entity: srv_raycast_entity,
3050        block_info: srv_block_info,
3051        break_block: srv_break_block,
3052        place_block_from_slot: srv_place_block_from_slot,
3053        bonemeal: srv_bonemeal,
3054        explode: srv_explode,
3055        tellraw: srv_tellraw,
3056        entity_damage: srv_entity_damage,
3057        entity_set_fire_ticks: srv_entity_set_fire_ticks,
3058        player_add_xp: srv_player_add_xp,
3059        player_set_may_fly: srv_player_set_may_fly,
3060        player_info: srv_player_info,
3061        spawn_item: srv_spawn_item,
3062        item_entity_stack: srv_item_entity_stack,
3063        item_entity_set_stack: srv_item_entity_set_stack,
3064        actionbar_json: srv_actionbar_json,
3065        use_item_on_block: srv_use_item_on_block,
3066        pour_fluid: srv_pour_fluid,
3067        has_entity_tag: srv_has_entity_tag,
3068        item_block: srv_item_block,
3069        place_feature: srv_place_feature,
3070        set_item_cooldown: srv_set_item_cooldown,
3071        entity_vehicle: srv_entity_vehicle,
3072        entity_passengers: srv_entity_passengers,
3073        entity_mount: srv_entity_mount,
3074        entity_dismount: srv_entity_dismount,
3075        redstone_power: srv_redstone_power,
3076        entity_set_fall_distance: srv_entity_set_fall_distance,
3077        redstone_signal: srv_redstone_signal,
3078        set_redstone_output: srv_set_redstone_output,
3079        get_block_state: srv_get_block_state,
3080        set_block_state: srv_set_block_state,
3081        world_is_thundering: srv_world_is_thundering,
3082        world_set_thunder: srv_world_set_thunder,
3083        fill_region: srv_fill_region,
3084        scan_region: srv_scan_region,
3085    }
3086}
3087
3088// ── ABI minor 3 ──────────────────────────────────────────────────────────────
3089
3090unsafe extern "C" fn srv_set_item_cooldown(_ctx: *mut c_void, player: YogStr, item_id: YogStr, ticks: i32) -> bool {
3091    unsafe { bridge!("setItemCooldown", [S(player), S(item_id), I(ticks)]).unwrap_or(false) }
3092}
3093
3094unsafe extern "C" fn srv_entity_vehicle(_ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr {
3095    unsafe { owned(bridge!("entityVehicle", [S(uuid)])) }
3096}
3097
3098unsafe extern "C" fn srv_entity_passengers(_ctx: *mut c_void, uuid: YogStr) -> YogOwnedStr {
3099    unsafe { owned(bridge!("entityPassengers", [S(uuid)])) }
3100}
3101
3102unsafe extern "C" fn srv_entity_mount(_ctx: *mut c_void, rider: YogStr, vehicle: YogStr, force: bool) -> bool {
3103    unsafe { bridge!("entityMount", [S(rider), S(vehicle), Z(force)]).unwrap_or(false) }
3104}
3105
3106unsafe extern "C" fn srv_entity_dismount(_ctx: *mut c_void, uuid: YogStr) -> bool {
3107    unsafe { bridge!("entityDismount", [S(uuid)]).unwrap_or(false) }
3108}
3109
3110unsafe extern "C" fn srv_entity_set_fall_distance(_ctx: *mut c_void, uuid: YogStr, distance: f32) -> bool {
3111    unsafe { bridge!("entitySetFallDistance", [S(uuid), F(distance)]).unwrap_or(false) }
3112}
3113
3114unsafe extern "C" fn srv_redstone_signal(_ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> i32 {
3115    unsafe { bridge!("redstoneSignal", [S(dim), I(pos.x), I(pos.y), I(pos.z)]).unwrap_or(-1) }
3116}
3117
3118unsafe extern "C" fn srv_redstone_power(_ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> i32 {
3119    unsafe { bridge!("redstonePower", [S(dim), I(pos.x), I(pos.y), I(pos.z)]).unwrap_or(-1) }
3120}
3121
3122unsafe extern "C" fn srv_set_redstone_output(_ctx: *mut c_void, dim: YogStr, pos: YogBlockPos, level: i32) -> bool {
3123    unsafe { bridge!("setRedstoneOutput", [S(dim), I(pos.x), I(pos.y), I(pos.z), I(level)]).unwrap_or(false) }
3124}
3125
3126unsafe extern "C" fn srv_get_block_state(_ctx: *mut c_void, dim: YogStr, pos: YogBlockPos) -> YogOwnedStr {
3127    unsafe { owned(bridge!("getBlockState", [S(dim), I(pos.x), I(pos.y), I(pos.z)])) }
3128}
3129
3130unsafe extern "C" fn srv_set_block_state(_ctx: *mut c_void, dim: YogStr, pos: YogBlockPos, state: YogStr) -> bool {
3131    unsafe { bridge!("setBlockState", [S(dim), I(pos.x), I(pos.y), I(pos.z), S(state)]).unwrap_or(false) }
3132}
3133
3134unsafe extern "C" fn srv_world_is_thundering(_ctx: *mut c_void, dim: YogStr) -> bool {
3135    unsafe { bridge!("worldIsThundering", [S(dim)]).unwrap_or(false) }
3136}
3137
3138unsafe extern "C" fn srv_world_set_thunder(_ctx: *mut c_void, dim: YogStr, thundering: bool, duration_ticks: i32) -> bool {
3139    unsafe { bridge!("worldSetThunder", [S(dim), Z(thundering), I(duration_ticks)]).unwrap_or(false) }
3140}
3141
3142unsafe extern "C" fn srv_fill_region(
3143    _ctx: *mut c_void,
3144    dim: YogStr,
3145    from: YogBlockPos,
3146    to: YogBlockPos,
3147    state: YogStr,
3148) -> i32 {
3149    unsafe {
3150        bridge!(
3151            "fillRegion",
3152            [S(dim), I(from.x), I(from.y), I(from.z), I(to.x), I(to.y), I(to.z), S(state)]
3153        )
3154        .unwrap_or(-1)
3155    }
3156}
3157
3158unsafe extern "C" fn srv_scan_region(_ctx: *mut c_void, dim: YogStr, from: YogBlockPos, to: YogBlockPos) -> YogOwnedStr {
3159    unsafe { owned(bridge!("scanRegion", [S(dim), I(from.x), I(from.y), I(from.z), I(to.x), I(to.y), I(to.z)])) }
3160}
3161
3162// ── Inter-mod communication ──────────────────────────────────────────────────
3163
3164unsafe extern "C" fn interop_export_impl(
3165    _ctx: *mut c_void,
3166    mod_id: YogStr,
3167    symbol: YogStr,
3168    ptr: *const c_void,
3169) {
3170    unsafe {
3171        let mod_id = mod_id.as_str();
3172        let sym = symbol.as_str();
3173        let registry = INTEROP_SYMBOLS.get_or_init(|| Mutex::new(HashMap::new()));
3174        registry
3175            .lock()
3176            .expect("interop symbols lock poisoned")
3177            .entry(mod_id.to_string())
3178            .or_default()
3179            .insert(sym.to_string(), ptr as usize);
3180    }
3181}
3182
3183unsafe extern "C" fn interop_import_impl(
3184    _ctx: *mut c_void,
3185    mod_id: YogStr,
3186    symbol: YogStr,
3187) -> *const c_void {
3188    unsafe {
3189        let mod_id = mod_id.as_str();
3190        let sym = symbol.as_str();
3191        let registry = INTEROP_SYMBOLS.get_or_init(|| Mutex::new(HashMap::new()));
3192        registry
3193            .lock()
3194            .expect("interop symbols lock poisoned")
3195            .get(mod_id)
3196            .and_then(|m: &HashMap<String, usize>| m.get(sym))
3197            .map(|&ptr| ptr as *const c_void)
3198            .unwrap_or(std::ptr::null())
3199    }
3200}
3201
3202fn build_api_table(ctx: *const RwLock<RuntimeHandlers>, server: *const YogServer) -> YogApi {
3203    YogApi {
3204        abi_version: ABI_VERSION,
3205        size: std::mem::size_of::<YogApi>() as u32,
3206        ctx: ctx as *mut c_void,
3207        server,
3208        on_block_break: api_on_block_break,
3209        on_chat: api_on_chat,
3210        on_player_join: api_on_player_join,
3211        on_player_leave: api_on_player_leave,
3212        on_use_item: api_on_use_item,
3213        on_use_block: api_on_use_block,
3214        on_attack_entity: api_on_attack_entity,
3215        on_entity_damage: api_on_entity_damage,
3216        on_entity_death: api_on_entity_death,
3217        on_entity_spawn: api_on_entity_spawn,
3218        on_player_place_block: api_on_player_place_block,
3219        on_player_death: api_on_player_death,
3220        on_player_respawn: api_on_player_respawn,
3221        on_advancement: api_on_advancement,
3222        on_entity_interact: api_on_entity_interact,
3223        on_item_craft: api_on_item_craft,
3224        on_explosion: api_on_explosion,
3225        on_item_pickup: api_on_item_pickup,
3226        on_item_insert: api_on_item_insert,
3227        on_player_move: api_on_player_move,
3228        on_container_open: api_on_container_open,
3229        on_container_close: api_on_container_close,
3230        on_projectile_hit: api_on_projectile_hit,
3231        on_server_tick: api_on_server_tick,
3232        on_server_started: api_on_server_started,
3233        on_server_stopping: api_on_server_stopping,
3234        on_packet: api_on_packet,
3235        on_client_packet: api_on_client_packet,
3236        register_command: api_register_command,
3237        register_typed_command: api_register_typed_command,
3238        register_content: api_register_content,
3239        schedule_once: api_schedule_once,
3240        schedule_repeating: api_schedule_repeating,
3241        on_client_tick: api_on_client_tick,
3242        on_hud_render: api_on_hud_render,
3243        on_key_press: api_on_key_press,
3244        on_screen_open: api_on_screen_open,
3245        on_screen_close: api_on_screen_close,
3246        on_world_render: api_on_world_render,
3247        register_ui: api_register_ui,
3248        on_ui_render: api_on_ui_render,
3249        register_menu_entry: api_register_menu_entry,
3250        mods_list: api_mods_list,
3251        free_str: yog_free_str,
3252        ui_open: api_ui_open,
3253        interop_export: interop_export_impl,
3254        interop_import: interop_import_impl,
3255        register_chunk_generator: api_register_chunk_generator,
3256        ui_close: api_ui_close,
3257        ui_consume_event: api_ui_consume_event,
3258        translate: api_translate,
3259        ui_open_flags: client_api::api_ui_open_flags,
3260        client_play_sound: client_api::api_client_play_sound,
3261        client_stop_sound: client_api::api_client_stop_sound,
3262        client_held_item: client_api::api_client_held_item,
3263        client_set_block_tint: client_api::api_client_set_block_tint,
3264        client_key_down: client_api::api_client_key_down,
3265        on_book_page: api_on_book_page,
3266        on_item_tooltip: api_on_item_tooltip,
3267        is_mod_loaded: api_is_mod_loaded,
3268        java_call: api_java_call,
3269        on_item_text: api_on_item_text,
3270        client_body_track: body::api_client_body_track,
3271        client_body_track_type: body::api_client_body_track_type,
3272        on_body: body::api_on_body,
3273        on_camera: body::api_on_camera,
3274        on_travel: body::api_on_travel,
3275        on_machine_craft: api_on_machine_craft,
3276    }
3277}
3278
3279// ── Mod loading ───────────────────────────────────────────────────────────────
3280
3281fn platform_tag() -> String {
3282    format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH)
3283}
3284
3285type AbiVersionFn = unsafe extern "C" fn() -> u32;
3286/// Second arg is a null-terminated C string: the mod's `id` from its manifest.
3287type RegisterFn = unsafe extern "C" fn(*const YogApi, *const std::os::raw::c_char);
3288
3289fn load_mods(dir: &Path, api: &YogApi) {
3290    let entries = match std::fs::read_dir(dir) {
3291        Ok(e) => e,
3292        Err(_) => {
3293            yog_logging::info!("no mods directory at {} — none loaded", dir.display());
3294            return;
3295        }
3296    };
3297
3298    // First pass: collect (mod_id, dependencies, archive_path)
3299    struct ModMeta {
3300        id: String,
3301        deps: Vec<String>,
3302        yog_path: PathBuf,
3303    }
3304    let mut metas: Vec<ModMeta> = Vec::new();
3305    for entry in entries.flatten() {
3306        let path = entry.path();
3307        if path.extension().and_then(|e| e.to_str()) != Some("yog") {
3308            continue;
3309        }
3310        let info = read_mod_info(&path);
3311        let deps = read_mod_deps(&path);
3312        metas.push(ModMeta {
3313            id: info[0].clone(),
3314            deps,
3315            yog_path: path,
3316        });
3317    }
3318
3319    // Topological sort by dependencies (Kahn's algorithm)
3320    let ids: Vec<String> = metas.iter().map(|m| m.id.clone()).collect();
3321    // Known before any registers: a mod's optional integrations with a Yog
3322    // mod loaded after it still see it.
3323    YOG_MOD_IDS.write().unwrap_or_else(|e| e.into_inner()).extend(ids.iter().cloned());
3324    let deps_list: Vec<Vec<String>> = metas.iter().map(|m| m.deps.clone()).collect();
3325    let sorted = match topo_sort_mods(&ids, &deps_list) {
3326        Some(v) => v,
3327        None => {
3328            yog_logging::error!("circular dependency detected — loading in file order");
3329            (0..metas.len()).collect()
3330        }
3331    };
3332
3333    let mut loaded: Vec<LoadedModule> = Vec::new();
3334    for idx in sorted {
3335        let meta = &metas[idx];
3336        let lib_path = match extract_yog(&meta.yog_path) {
3337            Some(p) => p,
3338            None => {
3339                yog_logging::error!(
3340                    "no native for {} in {}",
3341                    platform_tag(),
3342                    meta.yog_path.display()
3343                );
3344                continue;
3345            }
3346        };
3347        if let Some(module) = load_mod_lib(&lib_path, api, &meta.id) {
3348            loaded.push(module);
3349            MOD_INFOS
3350                .lock()
3351                .expect("mod infos lock poisoned")
3352                .push(read_mod_info(&meta.yog_path));
3353        }
3354    }
3355    yog_logging::info!("loaded {} mod(s) from {}", loaded.len(), dir.display());
3356
3357    // Final import resolution pass — after all mods loaded, each mod's pending
3358    // imports can be resolved against the now-complete interop table.
3359    resolve_all_imports(api, &loaded);
3360}
3361
3362/// After all mods loaded, call `__yog_resolve_imports_final` on each library
3363/// to resolve any imports that weren't satisfied during initial registration.
3364fn resolve_all_imports(api: &YogApi, loaded: &[LoadedModule]) {
3365    for module in loaded {
3366        module.with_library(|lib| unsafe {
3367            type ResolveFn = unsafe extern "C" fn(*const YogApi);
3368            if let Ok(resolve) = lib.get::<ResolveFn>(b"__yog_resolve_imports_final") {
3369                resolve(api as *const YogApi);
3370            }
3371        });
3372    }
3373}
3374
3375/// Kahn's algorithm — returns indices in dependency order, or None on cycle.
3376fn topo_sort_mods(ids: &[String], deps_list: &[Vec<String>]) -> Option<Vec<usize>> {
3377    let n = ids.len();
3378    let name_to_idx: HashMap<&str, usize> = ids
3379        .iter()
3380        .enumerate()
3381        .map(|(i, id)| (id.as_str(), i))
3382        .collect();
3383    let mut in_degree = vec![0u32; n];
3384    let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n];
3385    for (i, deps) in deps_list.iter().enumerate() {
3386        for dep in deps {
3387            if let Some(&j) = name_to_idx.get(dep.as_str()) {
3388                adj[j].push(i);
3389                in_degree[i] += 1;
3390            }
3391        }
3392    }
3393    let mut queue: Vec<usize> = (0..n).filter(|&i| in_degree[i] == 0).collect();
3394    let mut order = Vec::with_capacity(n);
3395    while let Some(u) = queue.pop() {
3396        order.push(u);
3397        for &v in &adj[u] {
3398            in_degree[v] -= 1;
3399            if in_degree[v] == 0 {
3400                queue.push(v);
3401            }
3402        }
3403    }
3404    if order.len() == n { Some(order) } else { None }
3405}
3406
3407/// Loads `path` as a fresh generation of `mod_id` via the shared
3408/// `HotReloader`, validates its ABI, and calls its `yog_mod_register` —
3409/// tagging every handler it registers with the returned `LoadedModule` (see
3410/// `current_registration()`). On any failure the module (if it got as far
3411/// as being tracked by the `HotReloader`) is retired immediately: nothing
3412/// can be executing inside a library that never successfully registered,
3413/// so this always hard-unloads right away rather than lingering.
3414fn load_mod_lib(path: &Path, api: &YogApi, mod_id: &str) -> Option<LoadedModule> {
3415    let module = match unsafe { hot_reloader().load_initial(mod_id, path) } {
3416        Ok(m) => m,
3417        Err(e) => {
3418            yog_logging::error!("failed to load {}: {}", path.display(), e);
3419            return None;
3420        }
3421    };
3422
3423    let abi = module.with_library(|lib| unsafe {
3424        let abi: Symbol<AbiVersionFn> = lib.get(b"yog_abi_version").ok()?;
3425        Some(abi())
3426    });
3427    match abi.flatten() {
3428        Some(v) if v == ABI_VERSION => {}
3429        Some(v) => {
3430            yog_logging::error!(
3431                "{}: ABI {} incompatible with runtime ABI {}",
3432                path.display(),
3433                v,
3434                ABI_VERSION
3435            );
3436            module.retire(|| {});
3437            return None;
3438        }
3439        None => {
3440            yog_logging::error!("{} is not a Yog mod (no yog_abi_version)", path.display());
3441            module.retire(|| {});
3442            return None;
3443        }
3444    }
3445
3446    if let Err(e) = invoke_mod_register(&module, api, mod_id) {
3447        yog_logging::error!("{}: {}", path.display(), e);
3448        module.retire(|| {});
3449        return None;
3450    }
3451
3452    Some(module)
3453}
3454
3455/// Calls a loaded module's `yog_mod_register`, tagging everything it
3456/// registers with `module` via `CURRENT_REGISTRATION` for the duration of
3457/// the call — used for both a mod's initial load and a later hot-reload.
3458fn invoke_mod_register(module: &LoadedModule, api: &YogApi, mod_id: &str) -> Result<(), String> {
3459    module
3460        .with_library(|lib| unsafe {
3461            let register: Symbol<RegisterFn> = lib
3462                .get(b"yog_mod_register")
3463                .map_err(|_| "missing yog_mod_register".to_string())?;
3464            let mod_id_c = std::ffi::CString::new(mod_id).unwrap();
3465            *CURRENT_REGISTRATION.lock().unwrap() = Some(module.clone());
3466            register(api as *const YogApi, mod_id_c.as_ptr());
3467            *CURRENT_REGISTRATION.lock().unwrap() = None;
3468            Ok(())
3469        })
3470        .unwrap_or_else(|| Err("module was unloaded before registration could run".to_string()))
3471}
3472
3473/// Metadata for a mod file: [id, name, version, authors, description,
3474/// name_key, description_key].
3475/// For .yog archives this comes from the bundled yog.toml; bare native libs
3476/// fall back to the file stem.
3477fn read_mod_info(path: &Path) -> [String; 7] {
3478    let stem = path
3479        .file_stem()
3480        .map(|s| s.to_string_lossy().into_owned())
3481        .unwrap_or_default();
3482    let mut info = [
3483        stem.clone(),
3484        stem,
3485        String::new(),
3486        String::new(),
3487        String::new(),
3488        String::new(),
3489        String::new(),
3490    ];
3491    if path.extension().and_then(|e| e.to_str()) != Some("yog") {
3492        return info;
3493    }
3494    let Some(text) = (|| {
3495        let file = std::fs::File::open(path).ok()?;
3496        let mut archive = zip::ZipArchive::new(file).ok()?;
3497        let mut entry = archive.by_name("yog.toml").ok()?;
3498        let mut text = String::new();
3499        std::io::Read::read_to_string(&mut entry, &mut text).ok()?;
3500        Some(text)
3501    })() else {
3502        return info;
3503    };
3504    // Minimal parse — enough for id/name/version/description/authors without
3505    // pulling in a TOML dependency. Source manifests keep these under [mod];
3506    // the packaged manifest (written by yog-cli) has them at top level, so
3507    // keys before any section header count too.
3508    let mut in_mod = true;
3509    for line in text.lines() {
3510        let line = line.trim();
3511        if line.starts_with('[') {
3512            in_mod = line == "[mod]";
3513            continue;
3514        }
3515        if !in_mod {
3516            continue;
3517        }
3518        let Some((key, value)) = line.split_once('=') else {
3519            continue;
3520        };
3521        let key = key.trim();
3522        let value = value.trim();
3523        let unquote = |v: &str| v.trim().trim_matches('"').to_string();
3524        match key {
3525            "id" => info[0] = tsv_clean(&unquote(value)),
3526            "name" => info[1] = tsv_clean(&unquote(value)),
3527            "version" => info[2] = tsv_clean(&unquote(value)),
3528            "description" => info[4] = tsv_clean(&unquote(value)),
3529            "name_key" => info[5] = unquote(value),
3530            "description_key" => info[6] = unquote(value),
3531            "authors" => {
3532                let list = value.trim_start_matches('[').trim_end_matches(']');
3533                let authors: Vec<String> = list
3534                    .split(',')
3535                    .map(unquote)
3536                    .filter(|a| !a.is_empty())
3537                    .collect();
3538                info[3] = tsv_clean(&authors.join(", "));
3539            }
3540            _ => {}
3541        }
3542    }
3543    info
3544}
3545
3546/// Parse `[dependencies]` section from yog.toml inside a .yog archive.
3547fn read_mod_deps(path: &Path) -> Vec<String> {
3548    if path.extension().and_then(|e| e.to_str()) != Some("yog") {
3549        return Vec::new();
3550    }
3551    let text = (|| {
3552        let file = std::fs::File::open(path).ok()?;
3553        let mut archive = zip::ZipArchive::new(file).ok()?;
3554        let mut entry = archive.by_name("yog.toml").ok()?;
3555        let mut text = String::new();
3556        std::io::Read::read_to_string(&mut entry, &mut text).ok()?;
3557        Some(text)
3558    })();
3559    let Some(text) = text else { return Vec::new() };
3560    let mut deps = Vec::new();
3561    let mut in_deps = false;
3562    for line in text.lines() {
3563        let line = line.trim();
3564        if line.starts_with('[') {
3565            in_deps = line == "[dependencies]";
3566            continue;
3567        }
3568        if !in_deps {
3569            continue;
3570        }
3571        // `mod_id = "1.0"` or `mod_id = "*"` — we only care about the key (mod_id)
3572        if let Some((key, _)) = line.split_once('=') {
3573            deps.push(key.trim().to_string());
3574        }
3575    }
3576    deps
3577}
3578
3579fn extract_yog(path: &Path) -> Option<PathBuf> {
3580    let file = std::fs::File::open(path).ok()?;
3581    let mut archive = zip::ZipArchive::new(file).ok()?;
3582    let prefix = format!("natives/{}/", platform_tag());
3583    let mut entry_name = None;
3584    for i in 0..archive.len() {
3585        let f = archive.by_index(i).ok()?;
3586        let name = f.name().ok()?;
3587        if name.starts_with(&prefix) && !name.ends_with('/') {
3588            entry_name = Some(name.to_string());
3589            break;
3590        }
3591    }
3592    let entry_name = entry_name?;
3593    let ext = Path::new(&entry_name)
3594        .extension()
3595        .and_then(|e| e.to_str())
3596        .unwrap_or("bin");
3597    let stem = path.file_stem()?.to_string_lossy().into_owned();
3598    // A fresh name per extraction: a hot reload must never truncate the
3599    // library the game still has mapped, and `dlopen` of an already-loaded
3600    // path would just hand back the old module.
3601    static EXTRACTIONS: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
3602    let n = EXTRACTIONS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3603    let out =
3604        std::env::temp_dir().join(format!("yog-{}-{}-{}.{}", stem, std::process::id(), n, ext));
3605    let mut entry = archive.by_name(&entry_name).ok()?;
3606    let mut out_file = std::fs::File::create(&out).ok()?;
3607    std::io::copy(&mut entry, &mut out_file).ok()?;
3608    Some(out)
3609}
3610
3611// ── Dispatcher helpers ────────────────────────────────────────────────────────
3612
3613fn srv_ptr() -> *const YogServer {
3614    SERVER.get().expect("yog: SERVER not initialised") as *const YogServer
3615}
3616
3617// ── JNI entry points ──────────────────────────────────────────────────────────
3618
3619#[unsafe(no_mangle)]
3620pub extern "system" fn Java_dev_yog_NativeBridge_nativeInit<'l>(
3621    mut env: JNIEnv<'l>,
3622    _class: JClass<'l>,
3623    mods_dir: JString<'l>,
3624) {
3625    if let Ok(vm) = env.get_java_vm() {
3626        let _ = JAVA_VM.set(vm);
3627    }
3628
3629    let dir = env
3630        .get_string(&mods_dir)
3631        .map(String::from)
3632        .unwrap_or_default();
3633
3634    // Build YogServer and store in static (gets a stable address).
3635    let _ = SERVER.set(build_server_table());
3636    let server_ptr = SERVER.get().unwrap() as *const YogServer;
3637
3638    // Publish HANDLERS *before* any mod registers — its address is then
3639    // `'static`-stable for the runtime's entire lifetime, unlike the old
3640    // design (a temporary heap `Box` moved into `HANDLERS` only after
3641    // `nativeInit` returned, silently invalidating the `ctx` pointer any
3642    // later re-registration — e.g. a hot-reload — would have needed).
3643    let handlers_lock = HANDLERS.get_or_init(|| RwLock::new(RuntimeHandlers::default()));
3644    let handlers_ptr = handlers_lock as *const RwLock<RuntimeHandlers>;
3645
3646    let api: &'static YogApi = API_TABLE.get_or_init(|| build_api_table(handlers_ptr, server_ptr));
3647
3648    let _ = HOT_RELOADER.get_or_init(|| HotReloader::new(RuntimeModuleRegistry));
3649
3650    guard("mod loading", || {
3651        load_mods(Path::new(&dir), api);
3652    });
3653
3654    control_socket::start_if_requested();
3655
3656    yog_logging::info!("runtime initialised — the gate is open.");
3657}
3658
3659#[unsafe(no_mangle)]
3660pub extern "system" fn Java_dev_yog_NativeBridge_nativeHotReload<'l>(
3661    mut env: JNIEnv<'l>,
3662    _class: JClass<'l>,
3663    mod_id: JString<'l>,
3664    new_yog_path: JString<'l>,
3665) -> jni::sys::jboolean {
3666    let Ok(mod_id) = env.get_string(&mod_id).map(String::from) else {
3667        return 0;
3668    };
3669    let Ok(yog_path) = env.get_string(&new_yog_path).map(String::from) else {
3670        return 0;
3671    };
3672    let ok = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
3673        trigger_hot_reload(&mod_id, Path::new(&yog_path))
3674    }))
3675    .unwrap_or(false);
3676    ok as jni::sys::jboolean
3677}
3678
3679/// Rebuilds and re-registers `mod_id` from a freshly built `.yog` at
3680/// `new_yog_path`: extracts the current platform's native, loads it as a
3681/// new generation via the shared `HotReloader` (which retires the previous
3682/// generation per its hybrid soft/hard-unload policy — hard-unloaded right
3683/// away if nothing's mid-call, deferred otherwise), then calls its
3684/// `yog_mod_register` again so its handlers get tagged under the new
3685/// generation. How this actually gets triggered — a keybind, a file-watch,
3686/// an IDE-driven socket — is a separate, later concern; this only needs to
3687/// work correctly once called.
3688fn trigger_hot_reload(mod_id: &str, new_yog_path: &Path) -> bool {
3689    let Some(native_path) = extract_yog(new_yog_path) else {
3690        yog_logging::error!(
3691            "hot-reload {}: no native for {} in {}",
3692            mod_id,
3693            platform_tag(),
3694            new_yog_path.display()
3695        );
3696        return false;
3697    };
3698
3699    let api = API_TABLE.get().expect("yog: nativeInit not called yet");
3700
3701    let module = match unsafe { hot_reloader().reload(mod_id, &native_path) } {
3702        Ok(m) => m,
3703        Err(e) => {
3704            yog_logging::error!(
3705                "hot-reload {}: failed to load {}: {}",
3706                mod_id,
3707                native_path.display(),
3708                e
3709            );
3710            return false;
3711        }
3712    };
3713
3714    let abi = module.with_library(|lib| unsafe {
3715        let abi: Symbol<AbiVersionFn> = lib.get(b"yog_abi_version").ok()?;
3716        Some(abi())
3717    });
3718    match abi.flatten() {
3719        Some(v) if v == ABI_VERSION => {}
3720        other => {
3721            yog_logging::error!(
3722                "hot-reload {}: ABI {:?} incompatible with runtime ABI {} — mod has no active handlers until a compatible reload succeeds",
3723                mod_id,
3724                other,
3725                ABI_VERSION
3726            );
3727            return false;
3728        }
3729    }
3730
3731    if let Err(e) = invoke_mod_register(&module, api, mod_id) {
3732        yog_logging::error!("hot-reload {}: {}", mod_id, e);
3733        return false;
3734    }
3735
3736    yog_logging::info!("hot-reloaded mod {}", mod_id);
3737    true
3738}
3739
3740#[unsafe(no_mangle)]
3741pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnBlockBreak<'l>(
3742    mut env: JNIEnv<'l>,
3743    _class: JClass<'l>,
3744    player: JString<'l>,
3745    block: JString<'l>,
3746    x: jint,
3747    y: jint,
3748    z: jint,
3749    dimension: JString<'l>,
3750) {
3751    let (p, b, d) = (jstr!(env, player), jstr!(env, block), jstr!(env, dimension));
3752    let ev = yog_abi::YogBlockBreakEvent {
3753        player: YogStr::from_str(&p),
3754        block: YogStr::from_str(&b),
3755        pos: YogBlockPos { x, y, z },
3756        dimension: YogStr::from_str(&d),
3757    };
3758    let srv = srv_ptr();
3759    guard("on_block_break", || {
3760        each(&handlers().block_break, |ud, f| unsafe {
3761            f(ud, srv, &ev, 1)
3762        });
3763    });
3764}
3765
3766#[unsafe(no_mangle)]
3767pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnChat<'l>(
3768    mut env: JNIEnv<'l>,
3769    _class: JClass<'l>,
3770    player: JString<'l>,
3771    message: JString<'l>,
3772    dimension: JString<'l>,
3773) {
3774    let (p, m, d) = (
3775        jstr!(env, player),
3776        jstr!(env, message),
3777        jstr!(env, dimension),
3778    );
3779    let ev = yog_abi::YogChatEvent {
3780        player: YogStr::from_str(&p),
3781        message: YogStr::from_str(&m),
3782        dimension: YogStr::from_str(&d),
3783    };
3784    let srv = srv_ptr();
3785    guard("on_chat", || {
3786        each(&handlers().chat, |ud, f| unsafe { f(ud, srv, &ev, 1) });
3787    });
3788}
3789
3790#[unsafe(no_mangle)]
3791pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlayerJoin<'l>(
3792    mut env: JNIEnv<'l>,
3793    _class: JClass<'l>,
3794    player: JString<'l>,
3795    uuid: JString<'l>,
3796    dimension: JString<'l>,
3797) {
3798    let (p, u, d) = (jstr!(env, player), jstr!(env, uuid), jstr!(env, dimension));
3799    let ev = yog_abi::YogPlayerEvent {
3800        player: YogStr::from_str(&p),
3801        uuid: YogStr::from_str(&u),
3802        dimension: YogStr::from_str(&d),
3803    };
3804    let srv = srv_ptr();
3805    guard("on_player_join", || {
3806        each(&handlers().player_join, |ud, f| unsafe {
3807            f(ud, srv, &ev, 1)
3808        });
3809    });
3810    // Queue startup grants: giving items directly inside the join callback
3811    // fails for the host of a freshly created world (player not fully in the
3812    // world yet), so grants are processed a few server ticks later.
3813    let h = handlers();
3814    if !h.startup_grants.is_empty() {
3815        h.pending_grants
3816            .lock()
3817            .expect("pending_grants poisoned")
3818            .push((p, u, 0));
3819    }
3820}
3821
3822/// The command tag a player carries once startup grant `id` was given. It's
3823/// saved with the player's own data, so it follows the player — not their
3824/// name or uuid (a dev client gets a new name each launch, while a
3825/// singleplayer world's owner stays the same player).
3826fn grant_tag(id: &str) -> String {
3827    format!("yog.grant.{id}")
3828}
3829
3830/// Try to fulfil pending startup grants. Called each server tick; a grant is
3831/// only marked as given (tagged on the player) after `give_item` succeeds.
3832fn process_pending_grants() {
3833    let h = handlers();
3834    let mut pending = match h.pending_grants.try_lock() {
3835        Ok(g) => g,
3836        Err(_) => return,
3837    };
3838    if pending.is_empty() {
3839        return;
3840    }
3841    // Wait a couple of ticks after join before the first attempt.
3842    const FIRST_TRY_TICK: u32 = 2;
3843    // Give up after 30 seconds — the player probably left.
3844    const MAX_TICKS: u32 = 600;
3845
3846    let mut still_pending = Vec::new();
3847    for (p, u, ticks) in pending.drain(..) {
3848        if ticks < FIRST_TRY_TICK {
3849            still_pending.push((p, u, ticks + 1));
3850            continue;
3851        }
3852        let player = YogStr::from_str(&p);
3853        let mut all_ok = true;
3854        for sg in &h.startup_grants {
3855            let tag = grant_tag(&sg.id);
3856            let tag = YogStr::from_str(&tag);
3857            if unsafe { bridge!("playerHasTag", [S(player), S(tag)]) }.unwrap_or(false) {
3858                continue;
3859            }
3860            let book = sg.book.as_ref().map(|_| "minecraft:written_book");
3861            let mut ok = true;
3862            for item_id in sg.items.iter().map(String::as_str).chain(book) {
3863                ok &= unsafe { srv_give_item(std::ptr::null_mut(), player, YogStr::from_str(item_id), 1) };
3864            }
3865            if ok && unsafe { bridge!("addPlayerTag", [S(player), S(tag)]) }.unwrap_or(false) {
3866                yog_logging::info!("startup grant {} given to {}", sg.id, p);
3867            } else {
3868                all_ok = false;
3869            }
3870        }
3871        if !all_ok && ticks < MAX_TICKS {
3872            still_pending.push((p, u, ticks + 1));
3873        }
3874    }
3875    *pending = still_pending;
3876}
3877
3878#[unsafe(no_mangle)]
3879pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlayerLeave<'l>(
3880    mut env: JNIEnv<'l>,
3881    _class: JClass<'l>,
3882    player: JString<'l>,
3883    uuid: JString<'l>,
3884    dimension: JString<'l>,
3885) {
3886    let (p, u, d) = (jstr!(env, player), jstr!(env, uuid), jstr!(env, dimension));
3887    let ev = yog_abi::YogPlayerEvent {
3888        player: YogStr::from_str(&p),
3889        uuid: YogStr::from_str(&u),
3890        dimension: YogStr::from_str(&d),
3891    };
3892    let srv = srv_ptr();
3893    guard("on_player_leave", || {
3894        each(&handlers().player_leave, |ud, f| unsafe {
3895            f(ud, srv, &ev, 1)
3896        });
3897    });
3898}
3899
3900#[unsafe(no_mangle)]
3901pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnUseItem<'l>(
3902    mut env: JNIEnv<'l>,
3903    _class: JClass<'l>,
3904    player: JString<'l>,
3905    item: JString<'l>,
3906    sneaking: jni::sys::jboolean,
3907    dimension: JString<'l>,
3908) {
3909    let (p, i, d) = (jstr!(env, player), jstr!(env, item), jstr!(env, dimension));
3910    let ev = yog_abi::YogUseItemEvent {
3911        player: YogStr::from_str(&p),
3912        item: YogStr::from_str(&i),
3913        sneaking: sneaking != 0,
3914        dimension: YogStr::from_str(&d),
3915    };
3916    let srv = srv_ptr();
3917    guard("on_use_item", || {
3918        each(&handlers().use_item, |ud, f| unsafe { f(ud, srv, &ev, 1) });
3919    });
3920}
3921
3922#[unsafe(no_mangle)]
3923pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnUseItemPre<'l>(
3924    mut env: JNIEnv<'l>,
3925    _class: JClass<'l>,
3926    player: JString<'l>,
3927    item: JString<'l>,
3928    sneaking: jni::sys::jboolean,
3929    dimension: JString<'l>,
3930) -> jni::sys::jboolean {
3931    let h = handlers();
3932    if h.use_item.is_empty() {
3933        return 1;
3934    }
3935    let p = jstr!(env, player, 1);
3936    let i = jstr!(env, item, 1);
3937    let d = jstr!(env, dimension, 1);
3938    let ev = yog_abi::YogUseItemEvent {
3939        player: YogStr::from_str(&p),
3940        item: YogStr::from_str(&i),
3941        sneaking: sneaking != 0,
3942        dimension: YogStr::from_str(&d),
3943    };
3944    let srv = srv_ptr();
3945    allowed(&h.use_item, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
3946}
3947
3948#[unsafe(no_mangle)]
3949pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnUseBlock<'l>(
3950    mut env: JNIEnv<'l>,
3951    _class: JClass<'l>,
3952    player: JString<'l>,
3953    block: JString<'l>,
3954    x: jint,
3955    y: jint,
3956    z: jint,
3957    dimension: JString<'l>,
3958    sneaking: jni::sys::jboolean,
3959) {
3960    let (p, b, d) = (jstr!(env, player), jstr!(env, block), jstr!(env, dimension));
3961    let ev = yog_abi::YogUseBlockEvent {
3962        player: YogStr::from_str(&p),
3963        block: YogStr::from_str(&b),
3964        pos: YogBlockPos { x, y, z },
3965        dimension: YogStr::from_str(&d),
3966        sneaking: sneaking != 0,
3967    };
3968    let srv = srv_ptr();
3969    guard("on_use_block", || {
3970        each(&handlers().use_block, |ud, f| unsafe { f(ud, srv, &ev, 1) });
3971    });
3972}
3973
3974#[unsafe(no_mangle)]
3975pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnUseBlockPre<'l>(
3976    mut env: JNIEnv<'l>,
3977    _class: JClass<'l>,
3978    player: JString<'l>,
3979    block: JString<'l>,
3980    x: jint,
3981    y: jint,
3982    z: jint,
3983    dimension: JString<'l>,
3984    sneaking: jni::sys::jboolean,
3985) -> jni::sys::jboolean {
3986    let h = handlers();
3987    if h.use_block.is_empty() {
3988        return 1;
3989    }
3990    let p = jstr!(env, player, 1);
3991    let b = jstr!(env, block, 1);
3992    let d = jstr!(env, dimension, 1);
3993    let ev = yog_abi::YogUseBlockEvent {
3994        player: YogStr::from_str(&p),
3995        block: YogStr::from_str(&b),
3996        pos: YogBlockPos { x, y, z },
3997        dimension: YogStr::from_str(&d),
3998        sneaking: sneaking != 0,
3999    };
4000    let srv = srv_ptr();
4001    allowed(&h.use_block, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4002}
4003
4004#[unsafe(no_mangle)]
4005pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnAttackEntity<'l>(
4006    mut env: JNIEnv<'l>,
4007    _class: JClass<'l>,
4008    player: JString<'l>,
4009    target_type: JString<'l>,
4010    target_uuid: JString<'l>,
4011    dimension: JString<'l>,
4012) {
4013    let (p, tt, tu, d) = (
4014        jstr!(env, player),
4015        jstr!(env, target_type),
4016        jstr!(env, target_uuid),
4017        jstr!(env, dimension),
4018    );
4019    let ev = yog_abi::YogAttackEntityEvent {
4020        player: YogStr::from_str(&p),
4021        target_type: YogStr::from_str(&tt),
4022        target_uuid: YogStr::from_str(&tu),
4023        dimension: YogStr::from_str(&d),
4024    };
4025    let srv = srv_ptr();
4026    guard("on_attack_entity", || {
4027        each(&handlers().attack_entity, |ud, f| unsafe {
4028            f(ud, srv, &ev, 1)
4029        });
4030    });
4031}
4032
4033#[unsafe(no_mangle)]
4034pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntityDamage<'l>(
4035    mut env: JNIEnv<'l>,
4036    _class: JClass<'l>,
4037    entity_type: JString<'l>,
4038    uuid: JString<'l>,
4039    amount: jfloat,
4040    source: JString<'l>,
4041    dimension: JString<'l>,
4042) {
4043    let (et, u, s, d) = (
4044        jstr!(env, entity_type),
4045        jstr!(env, uuid),
4046        jstr!(env, source),
4047        jstr!(env, dimension),
4048    );
4049    let ev = yog_abi::YogEntityDamageEvent {
4050        entity_type: YogStr::from_str(&et),
4051        uuid: YogStr::from_str(&u),
4052        amount,
4053        source: YogStr::from_str(&s),
4054        dimension: YogStr::from_str(&d),
4055    };
4056    let srv = srv_ptr();
4057    guard("on_entity_damage", || {
4058        each(&handlers().entity_damage, |ud, f| unsafe {
4059            f(ud, srv, &ev, 1)
4060        });
4061    });
4062}
4063
4064#[unsafe(no_mangle)]
4065pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntityDeath<'l>(
4066    mut env: JNIEnv<'l>,
4067    _class: JClass<'l>,
4068    entity_type: JString<'l>,
4069    uuid: JString<'l>,
4070    source: JString<'l>,
4071    dimension: JString<'l>,
4072) {
4073    let (et, u, s, d) = (
4074        jstr!(env, entity_type),
4075        jstr!(env, uuid),
4076        jstr!(env, source),
4077        jstr!(env, dimension),
4078    );
4079    let ev = yog_abi::YogEntityDeathEvent {
4080        entity_type: YogStr::from_str(&et),
4081        uuid: YogStr::from_str(&u),
4082        source: YogStr::from_str(&s),
4083        dimension: YogStr::from_str(&d),
4084    };
4085    let srv = srv_ptr();
4086    guard("on_entity_death", || {
4087        each(&handlers().entity_death, |ud, f| unsafe {
4088            f(ud, srv, &ev, 1)
4089        });
4090    });
4091}
4092
4093#[unsafe(no_mangle)]
4094pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnTick<'l>(
4095    _env: JNIEnv<'l>,
4096    _class: JClass<'l>,
4097) {
4098    let h = handlers();
4099    let srv = srv_ptr();
4100    guard("on_tick", || {
4101        each(&h.server_tick, |ud, f| unsafe { f(ud, srv) });
4102    });
4103
4104    guard("startup_grants", process_pending_grants);
4105
4106    // Scheduler: what's due this tick, run outside the lock (a task may schedule more).
4107    let due = h.scheduler.lock().expect("scheduler poisoned").due();
4108    for (module, ud, f) in due {
4109        if module.is_retiring() {
4110            continue;
4111        }
4112        let Some(_call) = module.enter() else {
4113            continue;
4114        };
4115        guard("scheduled task", || unsafe { f(ud, srv) });
4116    }
4117}
4118
4119#[unsafe(no_mangle)]
4120pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnServerStarted<'l>(
4121    _env: JNIEnv<'l>,
4122    _class: JClass<'l>,
4123    _world_dir: JString<'l>,
4124) {
4125    // Grants pending for a previous world's players are void.
4126    handlers().pending_grants.lock().expect("pending_grants poisoned").clear();
4127    let srv = srv_ptr();
4128    guard("on_server_started", || {
4129        each(&handlers().server_started, |ud, f| unsafe { f(ud, srv) });
4130    });
4131}
4132
4133#[unsafe(no_mangle)]
4134pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnServerStopping<'l>(
4135    _env: JNIEnv<'l>,
4136    _class: JClass<'l>,
4137) {
4138    let srv = srv_ptr();
4139    guard("on_server_stopping", || {
4140        each(&handlers().server_stopping, |ud, f| unsafe { f(ud, srv) });
4141    });
4142}
4143
4144#[unsafe(no_mangle)]
4145pub extern "system" fn Java_dev_yog_NativeBridge_nativeCommandNames<'l>(
4146    env: JNIEnv<'l>,
4147    _class: JClass<'l>,
4148) -> jstring {
4149    let names = handlers()
4150        .commands
4151        .keys()
4152        .cloned()
4153        .collect::<Vec<_>>()
4154        .join("\n");
4155    env.new_string(names)
4156        .map(jni::objects::JString::into_raw)
4157        .unwrap_or(std::ptr::null_mut())
4158}
4159
4160#[unsafe(no_mangle)]
4161pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnBlockBreakPre<'l>(
4162    mut env: JNIEnv<'l>,
4163    _class: JClass<'l>,
4164    player: JString<'l>,
4165    block: JString<'l>,
4166    x: jint,
4167    y: jint,
4168    z: jint,
4169    dimension: JString<'l>,
4170) -> jni::sys::jboolean {
4171    let h = handlers();
4172    if h.block_break.is_empty() {
4173        return 1;
4174    }
4175    let p = jstr!(env, player, 1);
4176    let b = jstr!(env, block, 1);
4177    let d = jstr!(env, dimension, 1);
4178    let ev = yog_abi::YogBlockBreakEvent {
4179        player: YogStr::from_str(&p),
4180        block: YogStr::from_str(&b),
4181        pos: YogBlockPos { x, y, z },
4182        dimension: YogStr::from_str(&d),
4183    };
4184    let srv = srv_ptr();
4185    allowed(&h.block_break, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4186}
4187
4188#[unsafe(no_mangle)]
4189pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnChatPre<'l>(
4190    mut env: JNIEnv<'l>,
4191    _class: JClass<'l>,
4192    player: JString<'l>,
4193    message: JString<'l>,
4194    dimension: JString<'l>,
4195) -> jni::sys::jboolean {
4196    let h = handlers();
4197    if h.chat.is_empty() {
4198        return 1;
4199    }
4200    let p = jstr!(env, player, 1);
4201    let m = jstr!(env, message, 1);
4202    let d = jstr!(env, dimension, 1);
4203    let ev = yog_abi::YogChatEvent {
4204        player: YogStr::from_str(&p),
4205        message: YogStr::from_str(&m),
4206        dimension: YogStr::from_str(&d),
4207    };
4208    let srv = srv_ptr();
4209    allowed(&h.chat, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4210}
4211
4212/// Called by a registered chunk-generator closure (via `ChunkWriter::set_block`)
4213/// to place one block in the chunk currently being generated. Calls back
4214/// into `YogCallbackChunkGenerator.setBlockInGeneratingChunk` on the Java
4215/// side — same thread, since the whole round trip is synchronous.
4216unsafe extern "C" fn chunk_writer_set_block(
4217    _ctx: *mut c_void,
4218    x: i32,
4219    y: i32,
4220    z: i32,
4221    block_id: YogStr,
4222) -> bool {
4223    unsafe {
4224        host!(
4225            "dev/yog/YogCallbackChunkGenerator",
4226            "setBlockInGeneratingChunk",
4227            [I(x), I(y), I(z), S(block_id)]
4228        )
4229        .unwrap_or(false)
4230    }
4231}
4232
4233#[unsafe(no_mangle)]
4234pub extern "system" fn Java_dev_yog_NativeBridge_nativeGenerateChunk<'l>(
4235    mut env: JNIEnv<'l>,
4236    _class: JClass<'l>,
4237    generator_type_id: JString<'l>,
4238    chunk_x: jint,
4239    chunk_z: jint,
4240    min_y: jint,
4241    height: jint,
4242) {
4243    let id: String = match env.get_string(&generator_type_id) {
4244        Ok(s) => String::from(s),
4245        Err(_) => return,
4246    };
4247    let terrain = handlers().terrains.get(&id).cloned();
4248    if let Some(g) = terrain {
4249        guard("chunk_generator", || {
4250            g.generate(chunk_x, chunk_z, min_y, height, |x, y, z, block| unsafe {
4251                chunk_writer_set_block(std::ptr::null_mut(), x, y, z, YogStr::from_str(block));
4252            });
4253        });
4254        return;
4255    }
4256    let Some((module, ud, cb)) = handlers().chunk_generators.get(&id).cloned() else {
4257        return;
4258    };
4259    if module.is_retiring() {
4260        return;
4261    }
4262    let Some(_call) = module.enter() else { return };
4263    let writer = yog_abi::YogChunkWriterApi {
4264        chunk_x,
4265        chunk_z,
4266        min_y,
4267        height,
4268        ctx: std::ptr::null_mut(),
4269        set_block: chunk_writer_set_block,
4270    };
4271    guard("chunk_generator", || unsafe {
4272        cb(ud, &writer as *const _);
4273    });
4274}
4275
4276#[unsafe(no_mangle)]
4277pub extern "system" fn Java_dev_yog_NativeBridge_nativeTypedCommandSchemas<'l>(
4278    env: JNIEnv<'l>,
4279    _class: JClass<'l>,
4280) -> jstring {
4281    // One line per DISTINCT (name, schema) pair — a name can have several
4282    // typed registrations (different arg shapes); Brigadier merges repeated
4283    // `dispatcher.register()` calls for the same literal name into one tree,
4284    // so the Java side just needs every distinct schema once each.
4285    let mut lines = Vec::new();
4286    for (name, regs) in handlers().commands.iter() {
4287        let mut seen = std::collections::HashSet::new();
4288        for (_module, schema, _, _) in regs {
4289            if let Some(schema) = schema
4290                && seen.insert(schema.as_str())
4291            {
4292                lines.push(format!("{name}\t{schema}"));
4293            }
4294        }
4295    }
4296    env.new_string(lines.join("\n"))
4297        .map(jni::objects::JString::into_raw)
4298        .unwrap_or(std::ptr::null_mut())
4299}
4300
4301#[unsafe(no_mangle)]
4302pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnCommand<'l>(
4303    mut env: JNIEnv<'l>,
4304    _class: JClass<'l>,
4305    name: JString<'l>,
4306    args: JString<'l>,
4307    source: JString<'l>,
4308    uuid: JString<'l>,
4309    dimension: JString<'l>,
4310) -> jstring {
4311    let (n, a, s, u, d) = (
4312        env.get_string(&name).map(String::from).unwrap_or_default(),
4313        env.get_string(&args).map(String::from).unwrap_or_default(),
4314        env.get_string(&source)
4315            .map(String::from)
4316            .unwrap_or_default(),
4317        env.get_string(&uuid).map(String::from).unwrap_or_default(),
4318        env.get_string(&dimension)
4319            .map(String::from)
4320            .unwrap_or_default(),
4321    );
4322    let h = handlers();
4323    let srv = srv_ptr();
4324    let args: Vec<&str> = if a.is_empty() { Vec::new() } else { a.split('\t').collect() };
4325    // One handler with `args`; its reply, or `None` when it passed.
4326    let call = |ud: &*mut c_void, f: &YogCommandFn, args: &str| -> Option<String> {
4327        let ev = yog_abi::YogCommandEvent {
4328            name: YogStr::from_str(&n),
4329            args: YogStr::from_str(args),
4330            source: YogStr::from_str(&s),
4331            uuid: YogStr::from_str(&u),
4332            dimension: YogStr::from_str(&d),
4333        };
4334        let mut buf = [0u8; 4096];
4335        let mut reply_len: u32 = 0;
4336        unsafe { f(*ud, srv, &ev, buf.as_mut_ptr(), buf.len() as u32, &mut reply_len) };
4337        (reply_len > 0).then(|| String::from_utf8_lossy(&buf[..reply_len as usize]).into_owned())
4338    };
4339    let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
4340        let Some(regs) = h.commands.get(&n) else {
4341            return String::new();
4342        };
4343        let live = regs.iter().filter(|(module, ..)| !module.is_retiring());
4344        // 1. Typed handlers whose schema has exactly these many args.
4345        // 2. Ones ending in a `string`, with the extra words joined into it
4346        //    (the game may have parsed them as separate words).
4347        // 3. Plain (schema-less) handlers, with whatever was typed.
4348        for pass in 0..3 {
4349            for (module, schema, ud, f) in live.clone() {
4350                let joined;
4351                let these = match (pass, schema) {
4352                    (0, Some(schema)) if schema.split_whitespace().count() == args.len() => a.as_str(),
4353                    (1, Some(schema)) => {
4354                        let types: Vec<&str> = schema.split_whitespace().collect();
4355                        let k = types.len();
4356                        if types.last() != Some(&"string") || k == 0 || args.len() <= k {
4357                            continue;
4358                        }
4359                        let mut parts: Vec<String> = args[..k - 1].iter().map(|x| x.to_string()).collect();
4360                        parts.push(args[k - 1..].join(" "));
4361                        joined = parts.join("\t");
4362                        joined.as_str()
4363                    }
4364                    (2, None) => a.as_str(),
4365                    _ => continue,
4366                };
4367                let Some(_call) = module.enter() else {
4368                    continue;
4369                };
4370                if let Some(r) = call(ud, f, these) {
4371                    return r;
4372                }
4373            }
4374        }
4375        String::new()
4376    }))
4377    .unwrap_or_else(|_| {
4378        yog_logging::error!("a mod panicked handling command `{}`", n);
4379        String::new()
4380    });
4381
4382    env.new_string(reply)
4383        .map(jni::objects::JString::into_raw)
4384        .unwrap_or(std::ptr::null_mut())
4385}
4386
4387/// Everything the host registers as one Yog Data blob (see `content_blob`),
4388/// built on the first call and kept.
4389#[unsafe(no_mangle)]
4390pub extern "system" fn Java_dev_yog_NativeBridge_nativeContent<'l>(
4391    env: JNIEnv<'l>,
4392    _class: JClass<'l>,
4393) -> jni::sys::jbyteArray {
4394    let h = handlers();
4395    let bytes = {
4396        let mut cached = h.content_blob.lock().unwrap_or_else(|e| e.into_inner());
4397        cached.get_or_insert_with(|| std::sync::Arc::new(content_blob::blob(&h))).clone()
4398    };
4399    env.byte_array_from_slice(&bytes)
4400        .map(|a| a.into_raw())
4401        .unwrap_or(std::ptr::null_mut())
4402}
4403
4404#[unsafe(no_mangle)]
4405pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPacket<'l>(
4406    mut env: JNIEnv<'l>,
4407    _class: JClass<'l>,
4408    channel: JString<'l>,
4409    player: JString<'l>,
4410    payload: JByteArray<'l>,
4411) {
4412    let ch = env
4413        .get_string(&channel)
4414        .map(String::from)
4415        .unwrap_or_default();
4416    let pl = env
4417        .get_string(&player)
4418        .map(String::from)
4419        .unwrap_or_default();
4420    let data = env.convert_byte_array(&payload).unwrap_or_default();
4421    let ev = yog_abi::YogPacketEvent {
4422        channel: YogStr::from_str(&ch),
4423        player: YogStr::from_str(&pl),
4424        payload: data.as_ptr(),
4425        payload_len: data.len() as u32,
4426    };
4427    let h = handlers();
4428    let srv = srv_ptr();
4429    guard("on_packet", || {
4430        if let Some((module, ud, f)) = h.packets.get(&ch) {
4431            if module.is_retiring() {
4432                return;
4433            }
4434            let Some(_call) = module.enter() else { return };
4435            unsafe { f(*ud, srv, &ev) };
4436        }
4437    });
4438}
4439
4440#[unsafe(no_mangle)]
4441pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnClientPacket<'l>(
4442    mut env: JNIEnv<'l>,
4443    _class: JClass<'l>,
4444    channel: JString<'l>,
4445    payload: JByteArray<'l>,
4446) {
4447    let ch = env
4448        .get_string(&channel)
4449        .map(String::from)
4450        .unwrap_or_default();
4451    let data = env.convert_byte_array(&payload).unwrap_or_default();
4452    let ev = yog_abi::YogPacketEvent {
4453        channel: YogStr::from_str(&ch),
4454        player: YogStr::EMPTY,
4455        payload: data.as_ptr(),
4456        payload_len: data.len() as u32,
4457    };
4458    let h = handlers();
4459    let srv = srv_ptr();
4460    guard("on_client_packet", || {
4461        if let Some((module, ud, f)) = h.client_packets.get(&ch) {
4462            if module.is_retiring() {
4463                return;
4464            }
4465            let Some(_call) = module.enter() else { return };
4466            unsafe { f(*ud, srv, &ev) };
4467        }
4468    });
4469}
4470
4471#[unsafe(no_mangle)]
4472pub extern "system" fn Java_dev_yog_NativeBridge_nativePacketChannels<'l>(
4473    env: JNIEnv<'l>,
4474    _class: JClass<'l>,
4475) -> jstring {
4476    let s = handlers()
4477        .packets
4478        .keys()
4479        .cloned()
4480        .collect::<Vec<_>>()
4481        .join("\n");
4482    env.new_string(s)
4483        .map(jni::objects::JString::into_raw)
4484        .unwrap_or(std::ptr::null_mut())
4485}
4486
4487#[unsafe(no_mangle)]
4488pub extern "system" fn Java_dev_yog_NativeBridge_nativeClientPacketChannels<'l>(
4489    env: JNIEnv<'l>,
4490    _class: JClass<'l>,
4491) -> jstring {
4492    let s = handlers()
4493        .client_packets
4494        .keys()
4495        .cloned()
4496        .collect::<Vec<_>>()
4497        .join("\n");
4498    env.new_string(s)
4499        .map(jni::objects::JString::into_raw)
4500        .unwrap_or(std::ptr::null_mut())
4501}
4502
4503#[unsafe(no_mangle)]
4504pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntitySpawn<'l>(
4505    mut env: JNIEnv<'l>,
4506    _class: JClass<'l>,
4507    entity_type: JString<'l>,
4508    uuid: JString<'l>,
4509    dimension: JString<'l>,
4510) {
4511    let h = handlers();
4512    if h.entity_spawn.is_empty() {
4513        return;
4514    }
4515    let (et, u, d) = (
4516        jstr!(env, entity_type),
4517        jstr!(env, uuid),
4518        jstr!(env, dimension),
4519    );
4520    let ev = yog_abi::YogEntitySpawnEvent {
4521        entity_type: YogStr::from_str(&et),
4522        uuid: YogStr::from_str(&u),
4523        dimension: YogStr::from_str(&d),
4524    };
4525    let srv = srv_ptr();
4526    guard("on_entity_spawn", || {
4527        each(&h.entity_spawn, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4528    });
4529}
4530
4531#[unsafe(no_mangle)]
4532pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntitySpawnPre<'l>(
4533    mut env: JNIEnv<'l>,
4534    _class: JClass<'l>,
4535    entity_type: JString<'l>,
4536    uuid: JString<'l>,
4537    dimension: JString<'l>,
4538) -> jni::sys::jboolean {
4539    let h = handlers();
4540    if h.entity_spawn.is_empty() {
4541        return 1;
4542    }
4543    let et = jstr!(env, entity_type, 1);
4544    let u = jstr!(env, uuid, 1);
4545    let d = jstr!(env, dimension, 1);
4546    let ev = yog_abi::YogEntitySpawnEvent {
4547        entity_type: YogStr::from_str(&et),
4548        uuid: YogStr::from_str(&u),
4549        dimension: YogStr::from_str(&d),
4550    };
4551    let srv = srv_ptr();
4552    allowed(&h.entity_spawn, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4553}
4554
4555#[unsafe(no_mangle)]
4556pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntityDamagePre<'l>(
4557    mut env: JNIEnv<'l>,
4558    _class: JClass<'l>,
4559    entity_type: JString<'l>,
4560    uuid: JString<'l>,
4561    amount: jfloat,
4562    source: JString<'l>,
4563    dimension: JString<'l>,
4564) -> jni::sys::jboolean {
4565    let h = handlers();
4566    if h.entity_damage.is_empty() {
4567        return 1;
4568    }
4569    let et = jstr!(env, entity_type, 1);
4570    let u = jstr!(env, uuid, 1);
4571    let s = jstr!(env, source, 1);
4572    let d = jstr!(env, dimension, 1);
4573    let ev = yog_abi::YogEntityDamageEvent {
4574        entity_type: YogStr::from_str(&et),
4575        uuid: YogStr::from_str(&u),
4576        amount,
4577        source: YogStr::from_str(&s),
4578        dimension: YogStr::from_str(&d),
4579    };
4580    let srv = srv_ptr();
4581    allowed(&h.entity_damage, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4582}
4583
4584#[unsafe(no_mangle)]
4585pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlaceBlockPre<'l>(
4586    mut env: JNIEnv<'l>,
4587    _class: JClass<'l>,
4588    player: JString<'l>,
4589    block: JString<'l>,
4590    x: jint,
4591    y: jint,
4592    z: jint,
4593    dimension: JString<'l>,
4594    sneaking: jni::sys::jboolean,
4595) -> jni::sys::jboolean {
4596    let h = handlers();
4597    if h.player_place_block.is_empty() {
4598        return 1;
4599    }
4600    let p = jstr!(env, player, 1);
4601    let b = jstr!(env, block, 1);
4602    let d = jstr!(env, dimension, 1);
4603    let ev = YogPlaceBlockEvent {
4604        player: YogStr::from_str(&p),
4605        block: YogStr::from_str(&b),
4606        pos: YogBlockPos { x, y, z },
4607        dimension: YogStr::from_str(&d),
4608        sneaking: sneaking != 0,
4609    };
4610    let srv = srv_ptr();
4611    allowed(&h.player_place_block, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4612}
4613
4614#[unsafe(no_mangle)]
4615pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlaceBlock<'l>(
4616    mut env: JNIEnv<'l>,
4617    _class: JClass<'l>,
4618    player: JString<'l>,
4619    block: JString<'l>,
4620    x: jint,
4621    y: jint,
4622    z: jint,
4623    dimension: JString<'l>,
4624    sneaking: jni::sys::jboolean,
4625) {
4626    let h = handlers();
4627    if h.player_place_block.is_empty() {
4628        return;
4629    }
4630    let (p, b, d) = (jstr!(env, player), jstr!(env, block), jstr!(env, dimension));
4631    let ev = YogPlaceBlockEvent {
4632        player: YogStr::from_str(&p),
4633        block: YogStr::from_str(&b),
4634        pos: YogBlockPos { x, y, z },
4635        dimension: YogStr::from_str(&d),
4636        sneaking: sneaking != 0,
4637    };
4638    let srv = srv_ptr();
4639    guard("on_player_place_block", || {
4640        each(&h.player_place_block, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4641    });
4642}
4643
4644#[unsafe(no_mangle)]
4645pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlayerDeathPre<'l>(
4646    mut env: JNIEnv<'l>,
4647    _class: JClass<'l>,
4648    player: JString<'l>,
4649    uuid: JString<'l>,
4650    source: JString<'l>,
4651    dimension: JString<'l>,
4652) -> jni::sys::jboolean {
4653    let h = handlers();
4654    if h.player_death.is_empty() {
4655        return 1;
4656    }
4657    let p = jstr!(env, player, 1);
4658    let u = jstr!(env, uuid, 1);
4659    let s = jstr!(env, source, 1);
4660    let d = jstr!(env, dimension, 1);
4661    let ev = YogPlayerDeathEvent {
4662        player: YogStr::from_str(&p),
4663        uuid: YogStr::from_str(&u),
4664        source: YogStr::from_str(&s),
4665        dimension: YogStr::from_str(&d),
4666    };
4667    let srv = srv_ptr();
4668    allowed(&h.player_death, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4669}
4670
4671#[unsafe(no_mangle)]
4672pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlayerDeath<'l>(
4673    mut env: JNIEnv<'l>,
4674    _class: JClass<'l>,
4675    player: JString<'l>,
4676    uuid: JString<'l>,
4677    source: JString<'l>,
4678    dimension: JString<'l>,
4679) {
4680    let h = handlers();
4681    if h.player_death.is_empty() {
4682        return;
4683    }
4684    let (p, u, s, d) = (
4685        jstr!(env, player),
4686        jstr!(env, uuid),
4687        jstr!(env, source),
4688        jstr!(env, dimension),
4689    );
4690    let ev = YogPlayerDeathEvent {
4691        player: YogStr::from_str(&p),
4692        uuid: YogStr::from_str(&u),
4693        source: YogStr::from_str(&s),
4694        dimension: YogStr::from_str(&d),
4695    };
4696    let srv = srv_ptr();
4697    guard("on_player_death", || {
4698        each(&h.player_death, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4699    });
4700}
4701
4702#[unsafe(no_mangle)]
4703pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlayerRespawn<'l>(
4704    mut env: JNIEnv<'l>,
4705    _class: JClass<'l>,
4706    player: JString<'l>,
4707    uuid: JString<'l>,
4708    at_anchor: jni::sys::jboolean,
4709    dimension: JString<'l>,
4710) {
4711    let h = handlers();
4712    if h.player_respawn.is_empty() {
4713        return;
4714    }
4715    let (p, u, d) = (jstr!(env, player), jstr!(env, uuid), jstr!(env, dimension));
4716    let ev = YogPlayerRespawnEvent {
4717        player: YogStr::from_str(&p),
4718        uuid: YogStr::from_str(&u),
4719        at_anchor: at_anchor != 0,
4720        dimension: YogStr::from_str(&d),
4721    };
4722    let srv = srv_ptr();
4723    guard("on_player_respawn", || {
4724        each(&h.player_respawn, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4725    });
4726}
4727
4728#[unsafe(no_mangle)]
4729pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnAdvancement<'l>(
4730    mut env: JNIEnv<'l>,
4731    _class: JClass<'l>,
4732    player: JString<'l>,
4733    uuid: JString<'l>,
4734    advancement: JString<'l>,
4735    dimension: JString<'l>,
4736) {
4737    let h = handlers();
4738    if h.advancement.is_empty() {
4739        return;
4740    }
4741    let (p, u, a, d) = (
4742        jstr!(env, player),
4743        jstr!(env, uuid),
4744        jstr!(env, advancement),
4745        jstr!(env, dimension),
4746    );
4747    let ev = YogAdvancementEvent {
4748        player: YogStr::from_str(&p),
4749        uuid: YogStr::from_str(&u),
4750        advancement: YogStr::from_str(&a),
4751        dimension: YogStr::from_str(&d),
4752    };
4753    let srv = srv_ptr();
4754    guard("on_advancement", || {
4755        each(&h.advancement, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4756    });
4757}
4758
4759#[unsafe(no_mangle)]
4760pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntityInteractPre<'l>(
4761    mut env: JNIEnv<'l>,
4762    _class: JClass<'l>,
4763    player: JString<'l>,
4764    player_uuid: JString<'l>,
4765    entity_type: JString<'l>,
4766    entity_uuid: JString<'l>,
4767    hand: JString<'l>,
4768    dimension: JString<'l>,
4769) -> jni::sys::jboolean {
4770    let h = handlers();
4771    if h.entity_interact.is_empty() {
4772        return 1;
4773    }
4774    let p = jstr!(env, player, 1);
4775    let pu = jstr!(env, player_uuid, 1);
4776    let et = jstr!(env, entity_type, 1);
4777    let eu = jstr!(env, entity_uuid, 1);
4778    let ha = jstr!(env, hand, 1);
4779    let d = jstr!(env, dimension, 1);
4780    let ev = YogEntityInteractEvent {
4781        player: YogStr::from_str(&p),
4782        player_uuid: YogStr::from_str(&pu),
4783        entity_type: YogStr::from_str(&et),
4784        entity_uuid: YogStr::from_str(&eu),
4785        hand: YogStr::from_str(&ha),
4786        dimension: YogStr::from_str(&d),
4787    };
4788    let srv = srv_ptr();
4789    allowed(&h.entity_interact, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4790}
4791
4792#[unsafe(no_mangle)]
4793pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnEntityInteract<'l>(
4794    mut env: JNIEnv<'l>,
4795    _class: JClass<'l>,
4796    player: JString<'l>,
4797    player_uuid: JString<'l>,
4798    entity_type: JString<'l>,
4799    entity_uuid: JString<'l>,
4800    hand: JString<'l>,
4801    dimension: JString<'l>,
4802) {
4803    let h = handlers();
4804    if h.entity_interact.is_empty() {
4805        return;
4806    }
4807    let (p, pu) = (jstr!(env, player), jstr!(env, player_uuid));
4808    let (et, eu, ha, d) = (
4809        jstr!(env, entity_type),
4810        jstr!(env, entity_uuid),
4811        jstr!(env, hand),
4812        jstr!(env, dimension),
4813    );
4814    let ev = YogEntityInteractEvent {
4815        player: YogStr::from_str(&p),
4816        player_uuid: YogStr::from_str(&pu),
4817        entity_type: YogStr::from_str(&et),
4818        entity_uuid: YogStr::from_str(&eu),
4819        hand: YogStr::from_str(&ha),
4820        dimension: YogStr::from_str(&d),
4821    };
4822    let srv = srv_ptr();
4823    guard("on_entity_interact", || {
4824        each(&h.entity_interact, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4825    });
4826}
4827
4828#[unsafe(no_mangle)]
4829pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnItemCraft<'l>(
4830    mut env: JNIEnv<'l>,
4831    _class: JClass<'l>,
4832    player: JString<'l>,
4833    player_uuid: JString<'l>,
4834    result_item: JString<'l>,
4835    result_count: jint,
4836    dimension: JString<'l>,
4837) {
4838    let h = handlers();
4839    if h.item_craft.is_empty() {
4840        return;
4841    }
4842    let (p, pu, ri, d) = (
4843        jstr!(env, player),
4844        jstr!(env, player_uuid),
4845        jstr!(env, result_item),
4846        jstr!(env, dimension),
4847    );
4848    let ev = YogCraftEvent {
4849        player: YogStr::from_str(&p),
4850        player_uuid: YogStr::from_str(&pu),
4851        result_item: YogStr::from_str(&ri),
4852        result_count: result_count as u32,
4853        dimension: YogStr::from_str(&d),
4854    };
4855    let srv = srv_ptr();
4856    guard("on_item_craft", || {
4857        each(&h.item_craft, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4858    });
4859}
4860
4861#[unsafe(no_mangle)]
4862pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnExplosionPre<'l>(
4863    mut env: JNIEnv<'l>,
4864    _class: JClass<'l>,
4865    dimension: JString<'l>,
4866    x: jdouble,
4867    y: jdouble,
4868    z: jdouble,
4869    power: jfloat,
4870    cause_uuid: JString<'l>,
4871) -> jni::sys::jboolean {
4872    let h = handlers();
4873    if h.explosion.is_empty() {
4874        return 1;
4875    }
4876    let d = jstr!(env, dimension, 1);
4877    let cu = jstr!(env, cause_uuid, 1);
4878    let ev = YogExplosionEvent {
4879        dimension: YogStr::from_str(&d),
4880        x,
4881        y,
4882        z,
4883        power,
4884        cause_uuid: YogStr::from_str(&cu),
4885    };
4886    let srv = srv_ptr();
4887    allowed(&h.explosion, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4888}
4889
4890#[unsafe(no_mangle)]
4891pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnExplosion<'l>(
4892    mut env: JNIEnv<'l>,
4893    _class: JClass<'l>,
4894    dimension: JString<'l>,
4895    x: jdouble,
4896    y: jdouble,
4897    z: jdouble,
4898    power: jfloat,
4899    cause_uuid: JString<'l>,
4900) {
4901    let h = handlers();
4902    if h.explosion.is_empty() {
4903        return;
4904    }
4905    let (d, cu) = (jstr!(env, dimension), jstr!(env, cause_uuid));
4906    let ev = YogExplosionEvent {
4907        dimension: YogStr::from_str(&d),
4908        x,
4909        y,
4910        z,
4911        power,
4912        cause_uuid: YogStr::from_str(&cu),
4913    };
4914    let srv = srv_ptr();
4915    guard("on_explosion", || {
4916        each(&h.explosion, |ud, f| unsafe { f(ud, srv, &ev, 1) });
4917    });
4918}
4919
4920// ── item insert JNI entry points ─────────────────────────────────────────────
4921
4922/// A hopper or dropper offers an item to a block that accepts items:
4923/// `simulate` asks whether it would take it (Pre), else it takes it (Post).
4924#[unsafe(no_mangle)]
4925pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnItemInsert<'l>(
4926    mut env: JNIEnv<'l>,
4927    _class: JClass<'l>,
4928    block: JString<'l>,
4929    x: jint,
4930    y: jint,
4931    z: jint,
4932    dimension: JString<'l>,
4933    item_id: JString<'l>,
4934    count: jint,
4935    nbt: JString<'l>,
4936    simulate: jni::sys::jboolean,
4937) -> jni::sys::jboolean {
4938    let h = handlers();
4939    if h.item_insert.is_empty() {
4940        return 0;
4941    }
4942    let (b, d, i, n) = (jstr!(env, block, 0), jstr!(env, dimension, 0), jstr!(env, item_id, 0), jstr!(env, nbt, 0));
4943    let ev = YogItemInsertEvent {
4944        block: YogStr::from_str(&b),
4945        pos: YogBlockPos { x, y, z },
4946        dimension: YogStr::from_str(&d),
4947        item_id: YogStr::from_str(&i),
4948        count: count.max(0) as u32,
4949        nbt: YogStr::from_str(&n),
4950    };
4951    let srv = srv_ptr();
4952    if simulate != 0 {
4953        // Some handler takes it.
4954        !allowed(&h.item_insert, |ud, f| unsafe { !f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4955    } else {
4956        let mut taken = false;
4957        guard("on_item_insert", || {
4958            each(&h.item_insert, |ud, f| taken |= unsafe { f(ud, srv, &ev, 1) });
4959        });
4960        taken as jni::sys::jboolean
4961    }
4962}
4963
4964// ── ABI minor 9 JNI entry points ─────────────────────────────────────────────
4965
4966#[unsafe(no_mangle)]
4967pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnItemPickupPre<'l>(
4968    mut env: JNIEnv<'l>,
4969    _class: JClass<'l>,
4970    player: JString<'l>,
4971    player_uuid: JString<'l>,
4972    item_id: JString<'l>,
4973    item_count: jint,
4974    entity_uuid: JString<'l>,
4975    dimension: JString<'l>,
4976) -> jni::sys::jboolean {
4977    let h = handlers();
4978    if h.item_pickup.is_empty() {
4979        return 1;
4980    }
4981    let p = jstr!(env, player, 1);
4982    let pu = jstr!(env, player_uuid, 1);
4983    let ii = jstr!(env, item_id, 1);
4984    let eu = jstr!(env, entity_uuid, 1);
4985    let d = jstr!(env, dimension, 1);
4986    let ev = YogItemPickupEvent {
4987        player: YogStr::from_str(&p),
4988        player_uuid: YogStr::from_str(&pu),
4989        item_id: YogStr::from_str(&ii),
4990        item_count: item_count as u32,
4991        entity_uuid: YogStr::from_str(&eu),
4992        dimension: YogStr::from_str(&d),
4993    };
4994    let srv = srv_ptr();
4995    allowed(&h.item_pickup, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
4996}
4997
4998#[unsafe(no_mangle)]
4999pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnItemPickup<'l>(
5000    mut env: JNIEnv<'l>,
5001    _class: JClass<'l>,
5002    player: JString<'l>,
5003    player_uuid: JString<'l>,
5004    item_id: JString<'l>,
5005    item_count: jint,
5006    entity_uuid: JString<'l>,
5007    dimension: JString<'l>,
5008) {
5009    let h = handlers();
5010    if h.item_pickup.is_empty() {
5011        return;
5012    }
5013    let (p, pu) = (jstr!(env, player), jstr!(env, player_uuid));
5014    let (ii, eu, d) = (
5015        jstr!(env, item_id),
5016        jstr!(env, entity_uuid),
5017        jstr!(env, dimension),
5018    );
5019    let ev = YogItemPickupEvent {
5020        player: YogStr::from_str(&p),
5021        player_uuid: YogStr::from_str(&pu),
5022        item_id: YogStr::from_str(&ii),
5023        item_count: item_count as u32,
5024        entity_uuid: YogStr::from_str(&eu),
5025        dimension: YogStr::from_str(&d),
5026    };
5027    let srv = srv_ptr();
5028    guard("on_item_pickup", || {
5029        each(&h.item_pickup, |ud, f| unsafe { f(ud, srv, &ev, 1) });
5030    });
5031}
5032
5033#[unsafe(no_mangle)]
5034pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnPlayerMove<'l>(
5035    mut env: JNIEnv<'l>,
5036    _class: JClass<'l>,
5037    player: JString<'l>,
5038    player_uuid: JString<'l>,
5039    x: jdouble,
5040    y: jdouble,
5041    z: jdouble,
5042    yaw: jfloat,
5043    pitch: jfloat,
5044    dimension: JString<'l>,
5045) {
5046    let h = handlers();
5047    if h.player_move.is_empty() {
5048        return;
5049    }
5050    let (p, pu, d) = (
5051        jstr!(env, player),
5052        jstr!(env, player_uuid),
5053        jstr!(env, dimension),
5054    );
5055    let ev = YogPlayerMoveEvent {
5056        player: YogStr::from_str(&p),
5057        player_uuid: YogStr::from_str(&pu),
5058        x,
5059        y,
5060        z,
5061        yaw,
5062        pitch,
5063        dimension: YogStr::from_str(&d),
5064    };
5065    let srv = srv_ptr();
5066    guard("on_player_move", || {
5067        each(&h.player_move, |ud, f| unsafe { f(ud, srv, &ev, 1) });
5068    });
5069}
5070
5071#[unsafe(no_mangle)]
5072pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnContainerOpenPre<'l>(
5073    mut env: JNIEnv<'l>,
5074    _class: JClass<'l>,
5075    player: JString<'l>,
5076    player_uuid: JString<'l>,
5077    dimension: JString<'l>,
5078) -> jni::sys::jboolean {
5079    let h = handlers();
5080    if h.container_open.is_empty() {
5081        return 1;
5082    }
5083    let p = jstr!(env, player, 1);
5084    let pu = jstr!(env, player_uuid, 1);
5085    let d = jstr!(env, dimension, 1);
5086    let ev = YogContainerOpenEvent {
5087        player: YogStr::from_str(&p),
5088        player_uuid: YogStr::from_str(&pu),
5089        container_type: YogStr::EMPTY,
5090        dimension: YogStr::from_str(&d),
5091    };
5092    let srv = srv_ptr();
5093    allowed(&h.container_open, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
5094}
5095
5096#[unsafe(no_mangle)]
5097pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnContainerOpen<'l>(
5098    mut env: JNIEnv<'l>,
5099    _class: JClass<'l>,
5100    player: JString<'l>,
5101    player_uuid: JString<'l>,
5102    container_type: JString<'l>,
5103    dimension: JString<'l>,
5104) {
5105    let h = handlers();
5106    if h.container_open.is_empty() {
5107        return;
5108    }
5109    let (p, pu, ct, d) = (
5110        jstr!(env, player),
5111        jstr!(env, player_uuid),
5112        jstr!(env, container_type),
5113        jstr!(env, dimension),
5114    );
5115    let ev = YogContainerOpenEvent {
5116        player: YogStr::from_str(&p),
5117        player_uuid: YogStr::from_str(&pu),
5118        container_type: YogStr::from_str(&ct),
5119        dimension: YogStr::from_str(&d),
5120    };
5121    let srv = srv_ptr();
5122    guard("on_container_open", || {
5123        each(&h.container_open, |ud, f| unsafe { f(ud, srv, &ev, 1) });
5124    });
5125}
5126
5127#[unsafe(no_mangle)]
5128pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnContainerClose<'l>(
5129    mut env: JNIEnv<'l>,
5130    _class: JClass<'l>,
5131    player: JString<'l>,
5132    player_uuid: JString<'l>,
5133    dimension: JString<'l>,
5134) {
5135    let h = handlers();
5136    if h.container_close.is_empty() {
5137        return;
5138    }
5139    let (p, pu, d) = (
5140        jstr!(env, player),
5141        jstr!(env, player_uuid),
5142        jstr!(env, dimension),
5143    );
5144    let ev = YogContainerCloseEvent {
5145        player: YogStr::from_str(&p),
5146        player_uuid: YogStr::from_str(&pu),
5147        dimension: YogStr::from_str(&d),
5148    };
5149    let srv = srv_ptr();
5150    guard("on_container_close", || {
5151        each(&h.container_close, |ud, f| unsafe { f(ud, srv, &ev, 1) });
5152    });
5153}
5154
5155#[unsafe(no_mangle)]
5156pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnProjectileHitPre<'l>(
5157    mut env: JNIEnv<'l>,
5158    _class: JClass<'l>,
5159    projectile_type: JString<'l>,
5160    projectile_uuid: JString<'l>,
5161    shooter_uuid: JString<'l>,
5162    hit_type: JString<'l>,
5163    hit_entity_uuid: JString<'l>,
5164    x: jdouble,
5165    y: jdouble,
5166    z: jdouble,
5167    dimension: JString<'l>,
5168) -> jni::sys::jboolean {
5169    let h = handlers();
5170    if h.projectile_hit.is_empty() {
5171        return 1;
5172    }
5173    let pt = jstr!(env, projectile_type, 1);
5174    let pu = jstr!(env, projectile_uuid, 1);
5175    let su = jstr!(env, shooter_uuid, 1);
5176    let ht = jstr!(env, hit_type, 1);
5177    let heu = jstr!(env, hit_entity_uuid, 1);
5178    let dim = jstr!(env, dimension, 1);
5179    let ev = YogProjectileHitEvent {
5180        projectile_type: YogStr::from_str(&pt),
5181        projectile_uuid: YogStr::from_str(&pu),
5182        shooter_uuid: YogStr::from_str(&su),
5183        hit_type: YogStr::from_str(&ht),
5184        hit_entity_uuid: YogStr::from_str(&heu),
5185        x,
5186        y,
5187        z,
5188        dimension: YogStr::from_str(&dim),
5189    };
5190    let srv = srv_ptr();
5191    allowed(&h.projectile_hit, |ud, f| unsafe { f(ud, srv, &ev, 0) }) as jni::sys::jboolean
5192}
5193
5194#[unsafe(no_mangle)]
5195pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnProjectileHit<'l>(
5196    mut env: JNIEnv<'l>,
5197    _class: JClass<'l>,
5198    projectile_type: JString<'l>,
5199    projectile_uuid: JString<'l>,
5200    shooter_uuid: JString<'l>,
5201    hit_type: JString<'l>,
5202    hit_entity_uuid: JString<'l>,
5203    x: jdouble,
5204    y: jdouble,
5205    z: jdouble,
5206    dimension: JString<'l>,
5207) {
5208    let h = handlers();
5209    if h.projectile_hit.is_empty() {
5210        return;
5211    }
5212    let (pt, pu) = (jstr!(env, projectile_type), jstr!(env, projectile_uuid));
5213    let (su, ht) = (jstr!(env, shooter_uuid), jstr!(env, hit_type));
5214    let (heu, dim) = (jstr!(env, hit_entity_uuid), jstr!(env, dimension));
5215    let ev = YogProjectileHitEvent {
5216        projectile_type: YogStr::from_str(&pt),
5217        projectile_uuid: YogStr::from_str(&pu),
5218        shooter_uuid: YogStr::from_str(&su),
5219        hit_type: YogStr::from_str(&ht),
5220        hit_entity_uuid: YogStr::from_str(&heu),
5221        x,
5222        y,
5223        z,
5224        dimension: YogStr::from_str(&dim),
5225    };
5226    let srv = srv_ptr();
5227    guard("on_projectile_hit", || {
5228        each(&h.projectile_hit, |ud, f| unsafe { f(ud, srv, &ev, 1) });
5229    });
5230}
5231
5232// ── ABI minor 10 — client-side JNI entry points ───────────────────────────────
5233
5234#[unsafe(no_mangle)]
5235pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnClientTick<'l>(
5236    _env: JNIEnv<'l>,
5237    _class: JClass<'l>,
5238) {
5239    let h = handlers();
5240    if h.client_tick.is_empty() {
5241        return;
5242    }
5243    guard("on_client_tick", || {
5244        each(&h.client_tick, |ud, f| unsafe { f(ud) });
5245    });
5246}
5247
5248/// The host's GL function pointer for `sym` (0 if it has none).
5249fn gl_proc_address(sym: &str) -> usize {
5250    let ptr: Option<i64> = unsafe { bridge!("glProcAddress", [S(YogStr::from_str(sym))]) };
5251    ptr.unwrap_or(0) as usize
5252}
5253
5254#[unsafe(no_mangle)]
5255pub extern "system" fn Java_dev_yog_NativeBridge_nativeGlInit<'l>(
5256    _env: JNIEnv<'l>,
5257    _class: JClass<'l>,
5258) {
5259    if GL.get().is_some() {
5260        return;
5261    }
5262    let mut raw_get_binary: usize = 0;
5263    let mut raw_prog_binary: usize = 0;
5264    let gl = unsafe {
5265        glow::Context::from_loader_function(|sym| {
5266            let ptr = gl_proc_address(sym);
5267            // Capture extension pointers while the loader runs.
5268            match sym {
5269                "glGetProgramBinary" => raw_get_binary = ptr,
5270                "glProgramBinary" => raw_prog_binary = ptr,
5271                _ => {}
5272            }
5273            ptr as *const _
5274        })
5275    };
5276    let _ = GL.set(GlCtx(gl));
5277    let _ = GL_GET_PROGRAM_BINARY.set((raw_get_binary != 0).then_some(raw_get_binary));
5278    let _ = GL_PROGRAM_BINARY.set((raw_prog_binary != 0).then_some(raw_prog_binary));
5279    // `glGetProgramiv` is a core function; always available.  We look it up once here
5280    // to avoid depending on glow internals for the PROGRAM_BINARY_LENGTH query.
5281    let get_program_iv = gl_proc_address("glGetProgramiv");
5282    let _ = GL_GET_PROGRAM_IV.set((get_program_iv != 0).then_some(get_program_iv));
5283}
5284
5285#[unsafe(no_mangle)]
5286pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnHudRender<'l>(
5287    _env: JNIEnv<'l>,
5288    _class: JClass<'l>,
5289    delta_tick: jfloat,
5290    screen_w: jint,
5291    screen_h: jint,
5292    scale_factor: jfloat,
5293    player_x: jfloat,
5294    player_y: jfloat,
5295    player_z: jfloat,
5296) {
5297    let h = handlers();
5298    let mut gfx = GFX_FN_TABLE;
5299    gfx.screen_w = screen_w;
5300    gfx.screen_h = screen_h;
5301    gfx.delta_tick = delta_tick;
5302    gfx.scale_factor = scale_factor;
5303    gfx.player_pos = [player_x, player_y, player_z];
5304
5305    // Mod hud_render callbacks.
5306    if !h.hud_render.is_empty() {
5307        guard("on_hud_render", || {
5308            each(&h.hud_render, |ud, f| unsafe { f(ud, &gfx) });
5309        });
5310    }
5311
5312    // Render active book UIs.
5313    let active_layers: Vec<UiLayer> = h.active_uis.lock().unwrap().clone();
5314    let sw = screen_w as f32;
5315    let sh = screen_h as f32;
5316    let ctx = unsafe { yog_gfx::GfxContext::from_raw(&gfx as *const _) };
5317    let mut renderers = h.book_renderers.lock().unwrap();
5318    for layer in &active_layers {
5319        if !layer.visible {
5320            continue;
5321        }
5322        if let Some(renderer) = renderers.get_mut(&layer.id) {
5323            renderer.render(&ctx, sw, sh);
5324        }
5325    }
5326}
5327
5328#[unsafe(no_mangle)]
5329pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnWorldRender<'l>(
5330    env: JNIEnv<'l>,
5331    _class: JClass<'l>,
5332    delta_tick: jfloat,
5333    screen_w: jint,
5334    screen_h: jint,
5335    scale_factor: jfloat,
5336    view_proj_arr: JFloatArray<'l>,
5337    cam_x: jfloat,
5338    cam_y: jfloat,
5339    cam_z: jfloat,
5340    player_x: jfloat,
5341    player_y: jfloat,
5342    player_z: jfloat,
5343) {
5344    let h = handlers();
5345    if h.world_render.is_empty() {
5346        return;
5347    }
5348    let mut view_proj = [0f32; 16];
5349    if env
5350        .get_float_array_region(&view_proj_arr, 0, &mut view_proj)
5351        .is_err()
5352    {
5353        return;
5354    }
5355    let mut gfx = GFX_FN_TABLE;
5356    gfx.screen_w = screen_w;
5357    gfx.screen_h = screen_h;
5358    gfx.delta_tick = delta_tick;
5359    gfx.scale_factor = scale_factor;
5360    gfx.view_proj = view_proj;
5361    gfx.camera_pos = [cam_x, cam_y, cam_z];
5362    gfx.player_pos = [player_x, player_y, player_z];
5363    guard("on_world_render", || {
5364        each(&h.world_render, |ud, f| unsafe { f(ud, &gfx) });
5365    });
5366}
5367
5368#[unsafe(no_mangle)]
5369pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnKeyPress<'l>(
5370    _env: JNIEnv<'l>,
5371    _class: JClass<'l>,
5372    key_code: jint,
5373    scan_code: jint,
5374    action: jint,
5375    modifiers: jint,
5376) -> jni::sys::jboolean {
5377    let h = handlers();
5378    if h.key_press.is_empty() {
5379        return 1;
5380    }
5381    let ev = YogKeyPressEvent {
5382        key_code,
5383        scan_code,
5384        action,
5385        modifiers,
5386    };
5387    allowed(&h.key_press, |ud, f| unsafe { f(ud, &ev) }) as jni::sys::jboolean
5388}
5389
5390#[unsafe(no_mangle)]
5391pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnScreenOpen<'l>(
5392    mut env: JNIEnv<'l>,
5393    _class: JClass<'l>,
5394    screen_class: JString<'l>,
5395) {
5396    let h = handlers();
5397    if h.screen_open.is_empty() {
5398        return;
5399    }
5400    let sc = match env.get_string(&screen_class) {
5401        Ok(s) => String::from(s),
5402        Err(_) => return,
5403    };
5404    guard("on_screen_open", || {
5405        each(&h.screen_open, |ud, f| unsafe {
5406            f(ud, YogStr::from_str(&sc))
5407        });
5408    });
5409}
5410
5411#[unsafe(no_mangle)]
5412pub extern "system" fn Java_dev_yog_NativeBridge_nativeOnScreenClose<'l>(
5413    mut env: JNIEnv<'l>,
5414    _class: JClass<'l>,
5415    screen_class: JString<'l>,
5416) {
5417    let h = handlers();
5418    if h.screen_close.is_empty() {
5419        return;
5420    }
5421    let sc = match env.get_string(&screen_class) {
5422        Ok(s) => String::from(s),
5423        Err(_) => return,
5424    };
5425    guard("on_screen_close", || {
5426        each(&h.screen_close, |ud, f| unsafe {
5427            f(ud, YogStr::from_str(&sc))
5428        });
5429    });
5430}
5431
5432mod body;
5433mod client_api;
5434mod content_blob;
5435mod machine;
5436mod control_socket;
5437mod images;
5438mod svg;
5439mod jcall;
5440mod ui_jni;
5441// Entry points for the benchmarks only: never in a build of the loader.
5442#[cfg(feature = "bench")]
5443#[doc(hidden)]
5444pub mod bench_support;
5445#[cfg(test)]
5446mod tests;