This commit is contained in:
Just Monika 2024-10-27 18:34:03 +08:00
commit add37b44ac
2 changed files with 58 additions and 0 deletions

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "registitemuserust"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
jni = { version = "0.21.1", features = ["invocation"] }

48
src/lib.rs Normal file
View File

@ -0,0 +1,48 @@
use jni::JNIEnv;
use jni::objects::{JClass, JObject, JString, JValue, JValueGen, JValueOwned};
use jni::sys::jint;
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "system" fn Java_com_example_FFI_init(
mut env:JNIEnv,
_class: JClass
) -> jint
{
println!("Hello From Rust!");
//all class we need here
let JClass_Registries = env.find_class("net/minecraft/registry/Registries").expect("registries find err");
let JClass_Registry = env.find_class("net/minecraft/registry/Registry").expect("registry key find err");
let JClass_Identifier = env.find_class("net/minecraft/util/Identifier").expect("Couldn't find Iden class");
let JClass_Item = env.find_class("net/minecraft/item/Item").expect("Couldn't find `Item` class!");
let JClass_Item_Settings = env.find_class("net/minecraft/item/Item$Settings").expect("Couldn't find `Item settings` class!");
//Item my_dirt = new Item(new Item.settings());
let item_settings_obj = env.new_object(JClass_Item_Settings,"()V",&[]).expect("create item_settings_obj error!");
let Item_my_dirt = env.new_object(JClass_Item,"(Lnet/minecraft/item/Item$Settings;)V",&[JValueGen::from(&item_settings_obj)]).expect("create item obj error!");
//Identifier item_id =Identifier.of("modid","my_shi");
let modid = env.new_string("modid").expect("Couldn't create string");
let modid_obj = JObject::from(modid);
let my_dirt = env.new_string("dirt").expect("Couldn't create string");
let my_dirt_obj = JObject::from(my_dirt);
let a = [JValueGen::from(&modid_obj),JValueGen::from(&my_dirt_obj)];
let Iden_item_id =
env.call_static_method(JClass_Identifier, "of",
"(Ljava/lang/String;Ljava/lang/String;)Lnet/minecraft/util/Identifier;",
&a).expect("Couldn't call Identifier");
//Item regItem = Registry.register(Registries.ITEM,itemID,my_dirt);
let ITEMfield = env.get_static_field(JClass_Registries,"ITEM","Lnet/minecraft/registry/Registries;ITEM:Lnet/minecraft/registry/DefaultedRegistry;").expect("ITEM field find err");
let register_interface = env.call_static_method(JClass_Registry,"register","Lnet/minecraft/registry/Registry;register(Lnet/minecraft/registry/Registry;Lnet/minecraft/util/Identifier;Ljava/lang/Object;)Ljava/lang/Object;",
&[ITEMfield.borrow(),Iden_item_id.borrow(),JValueGen::from(&Item_my_dirt)]
).unwrap();
println!("Done From Rust!");
return 0;
}