1+ mod macros;
2+ pub mod primitives;
3+
14use jni:: {
2- objects:: { JObject , JString } ,
5+ objects:: { JObject , JValueOwned } ,
36 JNIEnv ,
47} ;
58
6- pub const JAVA_STRING : & str = "java/lang/String" ;
9+ pub use macros:: * ;
10+ use primitives:: Function ;
711
8- pub struct Identifier ;
12+ pub trait IntoJValue {
13+ fn into_jvalue < ' local > ( self , env : & mut JNIEnv < ' local > ) -> JValueOwned < ' local > ;
14+ }
915
10- impl Identifier {
11- pub const MINECRAFT_IDENTIFIER : & str = "net/minecraft/util/Identifier" ;
12- pub const MINECRAFT_IDENTIFIER_OF : & str =
13- "(Ljava/lang/String;Ljava/lang/String;)Lnet/minecraft/util/Identifier;" ;
16+ impl IntoJValue for String {
17+ fn into_jvalue < ' local > ( self , env : & mut JNIEnv < ' local > ) -> JValueOwned < ' local > {
18+ env. new_string ( self ) . unwrap ( ) . into ( )
19+ }
20+ }
1421
15- pub fn of < ' local > (
16- env : & mut JNIEnv < ' local > ,
17- mod_id : JString < ' local > ,
18- id : JString < ' local > ,
19- ) -> JObject < ' local > {
20- let identifier_class = env
21- . find_class ( Identifier :: MINECRAFT_IDENTIFIER )
22- . expect ( "Cannot get Identifier class" ) ;
23-
24- env. call_static_method (
25- identifier_class,
26- "of" ,
27- Identifier :: MINECRAFT_IDENTIFIER_OF ,
28- & [ ( & mod_id) . into ( ) , ( & id) . into ( ) ] ,
29- )
30- . expect ( "Cannot generate Identifier" )
31- . l ( )
32- . expect ( "Cannot convert to JObject" )
22+ impl IntoJValue for JObject < ' _ > {
23+ fn into_jvalue < ' local > ( self , env : & mut JNIEnv < ' local > ) -> JValueOwned < ' local > {
24+ env. new_local_ref ( self ) . unwrap ( ) . into ( )
3325 }
3426}
3527
36- pub struct Items ;
28+ pub trait JSignature {
29+ fn sig ( ) -> String ;
3730
38- impl Items {
39- pub const MINECRAFT_ITEM : & str = "net/minecraft/item/Item" ;
40- pub const MINECRAFT_ITEMS : & str = "net/minecraft/item/Items" ;
41- pub const MINECRAFT_ITEMS_REGISTER : & str =
42- "(Lnet/minecraft/registry/RegistryKey;Ljava/util/function/Function;Lnet/minecraft/item/Item$Settings;)Lnet/minecraft/item/Item;" ;
31+ fn sig_type ( ) -> String {
32+ let sig = Self :: sig ( ) ;
33+ let mut out = String :: with_capacity ( sig. len ( ) + 2 ) ;
4334
44- pub fn register < ' local > (
45- env : & mut JNIEnv < ' local > ,
46- key : JObject < ' local > ,
47- settings : JObject < ' local > ,
48- ) -> JObject < ' local > {
49- let items_class = env
50- . find_class ( Items :: MINECRAFT_ITEMS )
51- . expect ( "Cannot get Items class" ) ;
35+ out. push ( 'L' ) ;
36+ out. push_str ( & sig) ;
37+ out. push ( ';' ) ;
38+
39+ out
40+ }
41+ }
42+
43+ new_class ! { RustBridge : "com/apika_probe_1/RustBridge" {
44+ static registerGroupEvent fn register_group_event( event: Event , item: Item ) ;
45+ } }
46+
47+ new_class ! { Event : "net/fabricmc/fabric/api/event/Event" { } }
48+
49+ impl Event < ' _ > {
50+ pub fn register < ' local > ( self , env : & mut JNIEnv < ' local > , item : Item ) {
51+ RustBridge :: register_group_event ( env, self , item)
52+ }
53+ }
54+
55+ new_class ! { Identifier : "net/minecraft/util/Identifier" {
56+ static fn of( mod_id: String , id: String ) -> Identifier <' local>;
57+ } }
58+
59+ new_class ! { Item : "net/minecraft/item/Item" { } }
60+
61+ new_enum ! { ItemGroups [ RegistryKey ] : "net/minecraft/item/ItemGroups" {
62+ REDSTONE
63+ } }
64+
65+ new_class ! { ItemGroupEvents : "net/fabricmc/fabric/api/itemgroup/v1/ItemGroupEvents" {
66+ static modifyEntriesEvent fn modify_entries_event( registry_key: ItemGroups ) -> Event <' local>;
67+ } }
68+
69+ new_class ! { ItemSettings : "net/minecraft/item/Item$Settings" { } }
5270
53- let item_class = env
54- . find_class ( Items :: MINECRAFT_ITEM )
55- . expect ( "Cannot get Item class" ) ;
71+ new_class ! { Items : "net/minecraft/item/Items" {
72+ static register fn register_raw ( key : RegistryKey < ' local> , factory : Function < ' local> , settings : ItemSettings < ' local> ) -> Item < ' local> ;
73+ } }
5674
75+ impl Items < ' _ > {
76+ pub fn register < ' local > (
77+ env : & mut JNIEnv < ' local > ,
78+ key : RegistryKey < ' local > ,
79+ settings : ItemSettings < ' local > ,
80+ ) -> Item < ' local > {
5781 let factory = {
5882 let class = env. find_class ( "com/apika_probe_1/RustBridge" ) . unwrap ( ) ;
5983
@@ -63,62 +87,16 @@ impl Items {
6387 . unwrap ( )
6488 } ;
6589
66- // let factory = env
67- // // .get_static_field(&items_class, "new", "Ljava/util/function/Function;")
68- // .get_static_field(&items_class, "new", "Lnet/minecraft/item/Item$Item;")
69- // // .get_static_method_id(&items_class, "new", "(Lnet/minecraft/item/Item$Settings;)Lnet/minecraft/item/Item;")
70- // .unwrap();
71-
72- // let factory = unsafe {
73- // let jni_interface = (*env.get_native_interface()).as_ref().unwrap();
74- // // fn(_: *mut *const JNINativeInterface_, _: *mut _jobject, _: *mut _jmethodID, _: u8)
75- // let to_reflected_method = jni_interface.ToReflectedMethod.unwrap();
76- // let factory = to_reflected_method(
77- // env.get_native_interface(),
78- // **items_class,
79- // factory.into_raw(),
80- // 0,
81- // );
82- // JObject::from_raw(factory)
83- // };
84-
85- env. call_static_method (
86- items_class,
87- "register" ,
88- Items :: MINECRAFT_ITEMS_REGISTER ,
89- & [ ( & key) . into ( ) , ( & factory) . into ( ) , ( & settings) . into ( ) ] ,
90- )
91- . expect ( "Cannot generate Item" )
92- . l ( )
93- . expect ( "Cannot convert to JObject" )
90+ Self :: register_raw ( env, key, factory. into ( ) , settings)
9491 }
9592}
9693
97- pub struct RegistryKey ;
98-
99- impl RegistryKey {
100- pub const MINECRAFT_REGISTRYKEY : & str = "net/minecraft/registry/RegistryKey" ;
101- pub const MINECRAFT_REGISTRYKEYS : & str = "net/minecraft/registry/RegistryKeys" ;
94+ new_class ! { ModifyEntries : "net/fabricmc/fabric/api/itemgroup/v1/ItemGroupEvents$ModifyEntries" { } }
10295
103- pub const MINECRAFT_REGISTRYKEY_OF : & str = "(Lnet/minecraft/registry/RegistryKey;Lnet/minecraft/util/Identifier;)Lnet/minecraft/registry/RegistryKey;" ;
96+ new_class ! { RegistryKey : "net/minecraft/registry/RegistryKey" {
97+ static fn of( kind: RegistryKeys , id: Identifier <' local>) -> RegistryKey <' local>;
98+ } }
10499
105- pub fn of < ' local > (
106- env : & mut JNIEnv < ' local > ,
107- kind : JObject < ' local > ,
108- id : JObject < ' local > ,
109- ) -> JObject < ' local > {
110- let registry_class = env
111- . find_class ( RegistryKey :: MINECRAFT_REGISTRYKEY )
112- . expect ( "Cannot get RegistryKey class" ) ;
113-
114- env. call_static_method (
115- registry_class,
116- "of" ,
117- RegistryKey :: MINECRAFT_REGISTRYKEY_OF ,
118- & [ ( & kind) . into ( ) , ( & id) . into ( ) ] ,
119- )
120- . expect ( "Cannot generate RegistryKey" )
121- . l ( )
122- . expect ( "Cannot convert to JObject" )
123- }
124- }
100+ new_enum ! { RegistryKeys [ RegistryKey ] : "net/minecraft/registry/RegistryKeys" {
101+ ITEM
102+ } }
0 commit comments