Get callbacks working
This commit is contained in:
parent
bb23fce01e
commit
4cca142617
@ -1,5 +1,34 @@
|
||||
package org.patchbukkit;
|
||||
|
||||
import org.astonbitecode.j4rs.api.invocation.NativeCallbackToRustChannelSupport;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class NativeCallbacks extends NativeCallbackToRustChannelSupport {}
|
||||
public class NativeCallbacks extends NativeCallbackToRustChannelSupport {
|
||||
|
||||
private static NativeCallbacks instance;
|
||||
|
||||
public NativeCallbacks() {
|
||||
NativeCallbacks.setInstance(this);
|
||||
}
|
||||
|
||||
private static synchronized void setInstance(
|
||||
NativeCallbacks nativeCallbacks
|
||||
) {
|
||||
if (instance == null) {
|
||||
instance = nativeCallbacks;
|
||||
}
|
||||
}
|
||||
|
||||
public static NativeCallbacks getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void doRegisterCallback(
|
||||
@NotNull Listener listener,
|
||||
@NotNull Plugin plugin
|
||||
) {
|
||||
doCallback(new Object[] { listener, plugin });
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,9 +98,7 @@ public class PatchBukkitPluginManager implements PluginManager {
|
||||
@NotNull Listener listener,
|
||||
@NotNull Plugin plugin
|
||||
) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Unimplemented method 'registerEvents'"
|
||||
);
|
||||
NativeCallbacks.getInstance().doRegisterCallback(listener, plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use j4rs::Jvm;
|
||||
use j4rs::{InvocationArg, Jvm};
|
||||
use pumpkin::plugin::Context;
|
||||
use pumpkin_api_macros::{plugin_impl, plugin_method};
|
||||
|
||||
@ -75,6 +75,31 @@ async fn on_load_inner(plugin: &mut PatchBukkitPlugin, server: Arc<Context>) ->
|
||||
let jvm = initialize_jvm(&dirs.j4rs)?;
|
||||
setup_patchbukkit_server(&jvm)?;
|
||||
|
||||
let i = jvm
|
||||
.create_instance("org.patchbukkit.NativeCallbacks", InvocationArg::empty())
|
||||
.map_err(|err| err.to_string())?;
|
||||
let r = jvm.init_callback_channel(&i).unwrap();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
if let Err(e) = Jvm::attach_thread() {
|
||||
log::error!("Failed to attach callback thread to JVM: {}", e);
|
||||
return;
|
||||
}
|
||||
|
||||
loop {
|
||||
match r.rx().recv() {
|
||||
Ok(ret) => {
|
||||
plugin.plugin_manager.add_listener();
|
||||
log::info!("Received callback: {:?}", ret.class_name());
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Callback channel closed: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
plugin
|
||||
.plugin_manager
|
||||
.load_all_plugins(&jvm)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user