yog_api/java.rs
1//! Other mods — Yog's and Java ones — from a Yog mod: whether one is
2//! loaded ([`crate::mod_loaded`]), and calls into a Java mod's API by
3//! reflection ([`call`]).
4//!
5//! ```ignore
6//! use yog_api::java;
7//! use yog_api::yog_data::Value;
8//! if yog_api::mod_loaded("pehkui") {
9//! let entity = Value::Ref(yog_api::yog_data::Ref::new("entity", uuid));
10//! let scale = java::call("virtuoel.pehkui.api.ScaleTypes.BASE.getScaleData($0).getScale()", &[entity])
11//! .and_then(|v| v.as_f64());
12//! }
13//! ```
14
15use yog_data::Value;
16
17/// Whether mod `id` is loaded: a Yog mod, or a Java mod in Fabric's,
18/// Forge's or NeoForge's mod list. Content and code for another mod —
19/// an optional integration — go behind it.
20pub fn mod_loaded(id: &str) -> bool {
21 crate::call(&yog_event::game::ModLoaded { id: id.to_owned() }).unwrap_or(false)
22}
23
24/// Calls a Java mod's API: `expr` is a chain from a class —
25/// `virtuoel.pehkui.api.ScaleTypes.BASE.getScaleData($0).getScale()` —
26/// fields and methods, `$n` the `n`-th of `args`. Numbers, strings,
27/// booleans and null cross as they are; an entity is
28/// `Value::Ref(Ref::new("entity", uuid))` both ways. `None` when the call
29/// failed (the log says why) or answered null. For other mods' APIs only: Minecraft's own
30/// classes are renamed at run time on some loaders.
31pub fn call(expr: &str, args: &[Value]) -> Option<Value> {
32 crate::call(&yog_event::jvm::Call {
33 expr: expr.to_owned(),
34 args: args.to_vec(),
35 })
36 .flatten()
37}