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 {
|
||||
match r.rx().recv() {
|
||||
Ok(ret) => {
|
||||
let result = jvm
|
||||
.invoke(&ret, "getCallbackName", InvocationArg::empty())
|
||||
.unwrap();
|
||||
let _: anyhow::Result<()> = (|| {
|
||||
let result = jvm.invoke(&ret, "getCallbackName", InvocationArg::empty())?;
|
||||
|
||||
let callback_name: String = jvm.to_rust(result).unwrap();
|
||||
match callback_name {
|
||||
_ => log::warn!("Received unknown callback: {:?}", callback_name),
|
||||
}
|
||||
let callback_name: String = jvm.to_rust(result)?;
|
||||
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),
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
})();
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Callback channel closed: {}", e);
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
@ -58,18 +59,21 @@ pub struct Plugin {
|
||||
}
|
||||
|
||||
pub struct PluginManager {
|
||||
plugins: Arc<Mutex<Vec<Plugin>>>,
|
||||
plugins: Arc<Mutex<HashMap<String, Plugin>>>,
|
||||
}
|
||||
|
||||
impl PluginManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
plugins: Arc::new(Mutex::new(Vec::new())),
|
||||
plugins: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
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>>(
|
||||
@ -127,7 +131,7 @@ impl PluginManager {
|
||||
}
|
||||
|
||||
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(
|
||||
plugin.instance.as_ref().unwrap(),
|
||||
"onEnable",
|
||||
@ -153,7 +157,7 @@ impl PluginManager {
|
||||
}
|
||||
|
||||
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(
|
||||
plugin.instance.as_ref().unwrap(),
|
||||
"onDisable",
|
||||
@ -179,7 +183,7 @@ impl PluginManager {
|
||||
}
|
||||
|
||||
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(
|
||||
"org.patchbukkit.loader.PatchBukkitPluginLoader",
|
||||
"createPlugin",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user