Use hashmap for plugin
This commit is contained in:
parent
e45fa6ab9a
commit
87d95885eb
@ -92,14 +92,22 @@ async fn on_load_inner(plugin: &mut PatchBukkitPlugin, server: Arc<Context>) ->
|
|||||||
loop {
|
loop {
|
||||||
match r.rx().recv() {
|
match r.rx().recv() {
|
||||||
Ok(ret) => {
|
Ok(ret) => {
|
||||||
let result = jvm
|
let _: anyhow::Result<()> = (|| {
|
||||||
.invoke(&ret, "getCallbackName", InvocationArg::empty())
|
let result = jvm.invoke(&ret, "getCallbackName", InvocationArg::empty())?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let callback_name: String = jvm.to_rust(result).unwrap();
|
let callback_name: String = jvm.to_rust(result)?;
|
||||||
match callback_name {
|
match callback_name.as_str() {
|
||||||
|
"REGISTER_EVENT_CALLBACK" => {
|
||||||
|
let listener =
|
||||||
|
jvm.invoke(&ret, "getArg", &[&InvocationArg::try_from(0)?])?;
|
||||||
|
let plugin =
|
||||||
|
jvm.invoke(&ret, "getArg", &[&InvocationArg::try_from(1)?])?;
|
||||||
|
}
|
||||||
_ => log::warn!("Received unknown callback: {:?}", callback_name),
|
_ => log::warn!("Received unknown callback: {:?}", callback_name),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Callback channel closed: {}", e);
|
log::error!("Callback channel closed: {}", e);
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
@ -58,18 +59,21 @@ pub struct Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct PluginManager {
|
pub struct PluginManager {
|
||||||
plugins: Arc<Mutex<Vec<Plugin>>>,
|
plugins: Arc<Mutex<HashMap<String, Plugin>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PluginManager {
|
impl PluginManager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
plugins: Arc::new(Mutex::new(Vec::new())),
|
plugins: Arc::new(Mutex::new(HashMap::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_plugin(&self, plugin: Plugin) {
|
pub fn add_plugin(&self, plugin: Plugin) {
|
||||||
self.plugins.lock().unwrap().push(plugin);
|
self.plugins
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(plugin.name.clone(), plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_paper_plugin<P: AsRef<Path>>(
|
pub fn load_paper_plugin<P: AsRef<Path>>(
|
||||||
@ -127,7 +131,7 @@ impl PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn enable_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
pub fn enable_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
||||||
for plugin in &mut *self.plugins.lock().unwrap() {
|
for (_plugin_name, plugin) in &mut *self.plugins.lock().unwrap() {
|
||||||
let result = jvm.invoke(
|
let result = jvm.invoke(
|
||||||
plugin.instance.as_ref().unwrap(),
|
plugin.instance.as_ref().unwrap(),
|
||||||
"onEnable",
|
"onEnable",
|
||||||
@ -153,7 +157,7 @@ impl PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn disable_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
pub fn disable_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
||||||
for plugin in &mut *self.plugins.lock().unwrap() {
|
for (_plugin_name, plugin) in &mut *self.plugins.lock().unwrap() {
|
||||||
let result = jvm.invoke(
|
let result = jvm.invoke(
|
||||||
plugin.instance.as_ref().unwrap(),
|
plugin.instance.as_ref().unwrap(),
|
||||||
"onDisable",
|
"onDisable",
|
||||||
@ -179,7 +183,7 @@ impl PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
pub fn load_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
||||||
for plugin in &mut *self.plugins.lock().unwrap() {
|
for (_plugin_name, plugin) in &mut *self.plugins.lock().unwrap() {
|
||||||
let result = jvm.invoke_static(
|
let result = jvm.invoke_static(
|
||||||
"org.patchbukkit.loader.PatchBukkitPluginLoader",
|
"org.patchbukkit.loader.PatchBukkitPluginLoader",
|
||||||
"createPlugin",
|
"createPlugin",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user