Skip to content

Commit ef7e1e7

Browse files
committed
🗿 🥶 Day3
1 parent ecfc3ac commit ef7e1e7

File tree

14 files changed

+385
-51
lines changed

14 files changed

+385
-51
lines changed

rust/src/java.rs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
mod macros;
22
pub mod primitives;
33

4+
use std::marker::PhantomData;
5+
46
use jni::{
57
objects::{JObject, JValueOwned},
8+
sys::jobject,
69
JNIEnv,
710
};
811

@@ -13,6 +16,18 @@ pub trait IntoJValue {
1316
fn into_jvalue<'local>(self, env: &mut JNIEnv<'local>) -> JValueOwned<'local>;
1417
}
1518

19+
impl IntoJValue for i32 {
20+
fn into_jvalue<'local>(self, _: &mut JNIEnv<'local>) -> JValueOwned<'local> {
21+
JValueOwned::Int(self)
22+
}
23+
}
24+
25+
impl IntoJValue for f64 {
26+
fn into_jvalue<'local>(self, _: &mut JNIEnv<'local>) -> JValueOwned<'local> {
27+
JValueOwned::Double(self)
28+
}
29+
}
30+
1631
impl IntoJValue for String {
1732
fn into_jvalue<'local>(self, env: &mut JNIEnv<'local>) -> JValueOwned<'local> {
1833
env.new_string(self).unwrap().into()
@@ -25,6 +40,16 @@ impl IntoJValue for JObject<'_> {
2540
}
2641
}
2742

43+
pub trait FromJValue {
44+
fn from_jvalue<'local>(value: JValueOwned<'local>) -> Self;
45+
}
46+
47+
impl FromJValue for bool {
48+
fn from_jvalue<'local>(value: JValueOwned<'local>) -> Self {
49+
value.z().unwrap()
50+
}
51+
}
52+
2853
pub trait JSignature {
2954
fn sig() -> String;
3055

@@ -40,20 +65,36 @@ pub trait JSignature {
4065
}
4166
}
4267

43-
new_class!{RustBridge: "me/apika/apikaprobe/RustBridge" {
68+
pub struct Field<T>(
69+
pub(crate) jobject,
70+
pub(crate) &'static str,
71+
pub(crate) PhantomData<T>,
72+
);
73+
74+
impl<T: JSignature + FromJValue> Field<T> {
75+
pub fn get<'local>(&self, env: &mut JNIEnv<'local>) -> T {
76+
let obj = unsafe { JObject::<'_>::from_raw(self.0) };
77+
let field = env.get_field(obj, self.1, T::sig_type()).unwrap();
78+
T::from_jvalue(field)
79+
}
80+
}
81+
82+
new_class! {RustBridge: "me/apika/apikaprobe/RustBridge" {
4483
static registerGroupEvent fn register_group_event(event: Event, item: Item);
4584
}}
4685

86+
new_class! {SerjioItem: "me/apika/apikaprobe/SerjioItem" {}}
87+
4788
new_class! {Event: "net/fabricmc/fabric/api/event/Event" {}}
4889

49-
impl Event<'_> {
90+
impl Event {
5091
pub fn register<'local>(self, env: &mut JNIEnv<'local>, item: Item) {
5192
RustBridge::register_group_event(env, self, item)
5293
}
5394
}
5495

5596
new_class! {Identifier: "net/minecraft/util/Identifier" {
56-
static fn of(mod_id: String, id: String) -> Identifier<'local>;
97+
static fn of(mod_id: String, id: String) -> Identifier;
5798
}}
5899

59100
new_class! {Item: "net/minecraft/item/Item" {}}
@@ -63,28 +104,34 @@ new_enum! {ItemGroups [RegistryKey]: "net/minecraft/item/ItemGroups" {
63104
}}
64105

65106
new_class! {ItemGroupEvents: "net/fabricmc/fabric/api/itemgroup/v1/ItemGroupEvents" {
66-
static modifyEntriesEvent fn modify_entries_event(registry_key: ItemGroups) -> Event<'local>;
107+
static modifyEntriesEvent fn modify_entries_event(registry_key: ItemGroups) -> Event;
67108
}}
68109

69110
new_class! {ItemSettings: "net/minecraft/item/Item$Settings" {}}
70111

71112
new_class! {Items: "net/minecraft/item/Items" {
72-
static register fn register_raw(key: RegistryKey<'local>, factory: Function<'local>, settings: ItemSettings<'local>) -> Item<'local>;
113+
static register fn register_raw(key: RegistryKey, factory: Function, settings: ItemSettings) -> Item;
73114
}}
74115

75-
impl Items<'_> {
116+
impl Items {
76117
pub fn register<'local>(
77118
env: &mut JNIEnv<'local>,
78-
key: RegistryKey<'local>,
79-
settings: ItemSettings<'local>,
80-
) -> Item<'local> {
119+
key: RegistryKey,
120+
settings: ItemSettings,
121+
) -> Item {
81122
let factory = {
82123
let class = RustBridge::class(env);
83-
84-
env.call_static_method(class, "getItemNew", "()Ljava/util/function/Function;", &[])
85-
.unwrap()
86-
.l()
87-
.unwrap()
124+
let item = SerjioItem::class(env);
125+
126+
env.call_static_method(
127+
class,
128+
"itemFactory",
129+
"(Ljava/lang/Class;)Ljava/util/function/Function;",
130+
&[(&item).into()],
131+
)
132+
.unwrap()
133+
.l()
134+
.unwrap()
88135
};
89136

90137
Self::register_raw(env, key, factory.into(), settings)
@@ -94,7 +141,7 @@ impl Items<'_> {
94141
new_class! {ModifyEntries: "net/fabricmc/fabric/api/itemgroup/v1/ItemGroupEvents$ModifyEntries" {}}
95142

96143
new_class! {RegistryKey: "net/minecraft/registry/RegistryKey" {
97-
static fn of(kind: RegistryKeys, id: Identifier<'local>) -> RegistryKey<'local>;
144+
static fn of(kind: RegistryKeys, id: Identifier) -> RegistryKey;
98145
}}
99146

100147
new_enum! {RegistryKeys [RegistryKey]: "net/minecraft/registry/RegistryKeys" {

0 commit comments

Comments
 (0)