Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / games / voxels / src / main / rust / src / lib_jni_inventory.rs
lib_jni_inventory.rs113 lines · 4.3 KiBRaw
| 1 | /// JNI bridge for `VoxelsNative#selectSlot`: selects a hotbar slot, clamped |
| 2 | /// to the valid range. |
| 3 | #[no_mangle] |
| 4 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_selectSlot<'l>( |
| 5 | _env: EnvUnowned<'l>, |
| 6 | _class: JClass<'l>, |
| 7 | slot: jint, |
| 8 | ) { |
| 9 | engine::with_engine(|s| { |
| 10 | s.inventory.select(slot.max(0).min(8) as usize); |
| 11 | }); |
| 12 | } |
| 13 | |
| 14 | /// JNI bridge for `VoxelsNative#moveItem`: moves an inventory stack from one |
| 15 | /// slot to another. |
| 16 | #[no_mangle] |
| 17 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_moveItem<'l>( |
| 18 | _env: EnvUnowned<'l>, _class: JClass<'l>, from: jint, to: jint, |
| 19 | ) { |
| 20 | if from >= 0 && to >= 0 { engine::inventory_move(from as usize, to as usize); } |
| 21 | } |
| 22 | |
| 23 | /// JNI bridge for `VoxelsNative#giveBlock`: gives the player one of the given |
| 24 | /// block id. |
| 25 | /// |
| 26 | /// The only id that crosses JNI as a number rather than as JSON. `as` would |
| 27 | /// wrap a jint that doesn't fit into a different, real block, so refuse it |
| 28 | /// instead. |
| 29 | #[no_mangle] |
| 30 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_giveBlock<'l>( |
| 31 | _env: EnvUnowned<'l>, _class: JClass<'l>, id: jint, |
| 32 | ) { |
| 33 | if let Ok(id) = crate::world::block::Id::try_from(id) { if id > 0 { engine::inventory_give(id); } } |
| 34 | } |
| 35 | |
| 36 | /// JNI bridge for `VoxelsNative#craft`: crafts the given recipe index. |
| 37 | /// Returns true if crafting succeeded. |
| 38 | #[no_mangle] |
| 39 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_craft<'l>( |
| 40 | _env: EnvUnowned<'l>, _class: JClass<'l>, recipe: jint, |
| 41 | ) -> jboolean { |
| 42 | recipe >= 0 && engine::inventory_craft(recipe as usize) |
| 43 | } |
| 44 | |
| 45 | /// JNI bridge for `VoxelsNative#startSmelt`: starts the given furnace recipe, |
| 46 | /// optionally in blast-furnace mode. Returns true if smelting started. |
| 47 | #[no_mangle] |
| 48 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_startSmelt<'l>( |
| 49 | _env: EnvUnowned<'l>, _class: JClass<'l>, recipe: jint, blast: jboolean, |
| 50 | ) -> jboolean { |
| 51 | engine::start_smelt(recipe.max(0) as usize, blast) |
| 52 | } |
| 53 | |
| 54 | /// JNI bridge for `VoxelsNative#stopSmelt`: stops the active furnace recipe. |
| 55 | #[no_mangle] |
| 56 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_stopSmelt<'l>( |
| 57 | _env: EnvUnowned<'l>, _class: JClass<'l>, |
| 58 | ) { |
| 59 | engine::stop_smelt(); |
| 60 | } |
| 61 | |
| 62 | /// JNI bridge for `VoxelsNative#containerTake`: takes the stack at the given |
| 63 | /// open-container slot into the player inventory. |
| 64 | #[no_mangle] |
| 65 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_containerTake<'l>( |
| 66 | _env: EnvUnowned<'l>, _class: JClass<'l>, idx: jint, |
| 67 | ) -> jboolean { |
| 68 | engine::container_take(idx.max(0) as usize) as jboolean |
| 69 | } |
| 70 | |
| 71 | /// JNI bridge for `VoxelsNative#containerPut`: puts the carried stack into the |
| 72 | /// given open-container slot. |
| 73 | #[no_mangle] |
| 74 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_containerPut<'l>( |
| 75 | _env: EnvUnowned<'l>, _class: JClass<'l>, idx: jint, |
| 76 | ) -> jboolean { |
| 77 | engine::container_put(idx.max(0) as usize) as jboolean |
| 78 | } |
| 79 | |
| 80 | /// JNI bridge for `VoxelsNative#closeContainer`: closes the open container. |
| 81 | #[no_mangle] |
| 82 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_closeContainer<'l>( |
| 83 | _env: EnvUnowned<'l>, _class: JClass<'l>, |
| 84 | ) { |
| 85 | engine::close_container(); |
| 86 | } |
| 87 | |
| 88 | /// JNI bridge for `VoxelsNative#attuneBlessing`: attunes the blessing at the |
| 89 | /// given catalog index. Returns true if attunement succeeded. |
| 90 | #[no_mangle] |
| 91 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_attuneBlessing<'l>( |
| 92 | _env: EnvUnowned<'l>, _class: JClass<'l>, idx: jint, |
| 93 | ) -> jboolean { |
| 94 | engine::attune_blessing(idx.max(0) as usize) as jboolean |
| 95 | } |
| 96 | |
| 97 | /// JNI bridge for `VoxelsNative#releaseBlessing`: releases the blessing in the |
| 98 | /// given attunement slot. Returns true if a blessing was released. |
| 99 | #[no_mangle] |
| 100 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_releaseBlessing<'l>( |
| 101 | _env: EnvUnowned<'l>, _class: JClass<'l>, slot: jint, |
| 102 | ) -> jboolean { |
| 103 | engine::release_blessing(slot.max(0) as usize) as jboolean |
| 104 | } |
| 105 | |
| 106 | /// JNI bridge for `VoxelsNative#cut`: applies the stonecutter recipe at the |
| 107 | /// given index. Returns true if cutting succeeded. |
| 108 | #[no_mangle] |
| 109 | pub extern "system" fn Java_com_vayunmathur_games_voxels_util_VoxelsNative_cut<'l>( |
| 110 | _env: EnvUnowned<'l>, _class: JClass<'l>, idx: jint, |
| 111 | ) -> jboolean { |
| 112 | idx >= 0 && engine::do_cut(idx as usize) |
| 113 | } |