1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

fix Plugins::get

This commit is contained in:
mat 2022-10-31 23:26:40 -05:00
parent cd01188b8c
commit 88e1164daf
5 changed files with 22 additions and 9 deletions

View file

@ -5,6 +5,8 @@
//! [`azalea_protocol`]: https://crates.io/crates/azalea-protocol
//! [`azalea`]: https://crates.io/crates/azalea
#![feature(trait_upcasting)]
mod account;
mod chat;
mod client;

View file

@ -36,7 +36,7 @@ impl Plugins {
self.map
.as_ref()
.and_then(|map| map.get(&TypeId::of::<T>()))
.and_then(|boxed| (&*boxed as &(dyn Any + 'static)).downcast_ref())
.and_then(|boxed| (boxed.as_ref() as &dyn Any).downcast_ref::<T>())
}
}
@ -54,7 +54,7 @@ impl IntoIterator for Plugins {
/// Plugins can keep their own personal state, listen to events, and add new functions to Client.
#[async_trait]
pub trait Plugin: Send + Sync + PluginClone + 'static {
pub trait Plugin: Send + Sync + PluginClone + Any + 'static {
async fn handle(self: Box<Self>, event: Event, bot: Client);
}

View file

@ -44,6 +44,7 @@ impl Trait for azalea_client::Client {
pos: BlockPos::from(self.entity().pos()),
};
let end = goal.goal_node();
println!("start: {:?}, end: {:?}", start, end);
let successors = |node: &Node| {
let mut edges = Vec::new();
@ -76,6 +77,10 @@ impl Trait for azalea_client::Client {
|n| goal.success(n),
);
let p = pf.find_path();
let state = self.plugins.get::<Plugin>().unwrap().state.clone();
// convert the Option<Vec<Node>> to a VecDeque<Node>
*state.path.lock() = p.expect("no path").into_iter().collect();
}
}

View file

@ -158,10 +158,11 @@ pub async fn start<
let (mut bot, mut rx) = Client::join(&options.account, address).await?;
bot.plugins = Arc::new(options.plugins);
let mut plugins = options.plugins;
plugins.add(bot::Plugin::default());
bot.plugins = Arc::new(plugins);
let state = options.state;
let bot_plugin = bot::Plugin::default();
while let Some(event) = rx.recv().await {
let cloned_plugins = (*bot.plugins).clone();
@ -169,8 +170,9 @@ pub async fn start<
tokio::spawn(plugin.handle(event.clone(), bot.clone()));
}
let bot_plugin = bot.plugins.get::<bot::Plugin>().unwrap().clone();
tokio::spawn(bot::Plugin::handle(
Box::new(bot_plugin.clone()),
Box::new(bot_plugin),
event.clone(),
bot.clone(),
));
@ -180,7 +182,6 @@ pub async fn start<
Ok(())
}
/// A helper macro that generates a [`Plugins`] struct from a list of objects
/// that implement [`Plugin`].
///
@ -191,7 +192,7 @@ pub async fn start<
macro_rules! plugins {
($($plugin:expr),*) => {
{
let mut plugins = azalea_client::Plugins::new();
let mut plugins = azalea::Plugins::new();
$(
plugins.add($plugin);
)*

View file

@ -15,7 +15,7 @@ async fn main() -> anyhow::Result<()> {
account,
address: "localhost",
state: State::default(),
plugins: vec![Box::new(azalea_pathfinder::Plugin::default())],
plugins: plugins![azalea_pathfinder::Plugin::default()],
handle,
})
.await
@ -28,7 +28,12 @@ async fn handle(bot: Client, event: Event, _state: State) -> anyhow::Result<()>
match event {
Event::Login => {
bot.chat("Hello world").await?;
bot.goto(BlockPosGoal::from(BlockPos::new(0, -60, 12)));
}
Event::Chat(m) => {
println!("{}", m.message().to_ansi(None));
if m.message().to_string() == "<py5> goto" {
bot.goto(BlockPosGoal::from(BlockPos::new(0, -60, 12)));
}
}
Event::Initialize => {
println!("initialized");