From 2eade86cf7a12a6ec64496aedbfc3d3a3bd44e1a Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 23 Oct 2022 16:51:49 -0500 Subject: make `handle` cleaner Arc -> Event, Arc> -> State Items in State now need to have interior mutability (i.e. Arc>), but it's a worthwhile tradeoff since it allows the user to customize it for each field --- azalea/examples/pvp.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'azalea/examples/pvp.rs') diff --git a/azalea/examples/pvp.rs b/azalea/examples/pvp.rs index a2f070f0..8c133576 100644 --- a/azalea/examples/pvp.rs +++ b/azalea/examples/pvp.rs @@ -7,7 +7,7 @@ async fn main() { let accounts = Accounts::new(); for i in 0..10 { - accounts.add(Account::offline(format!("bot{}", i))); + accounts.add(Account::offline(&format!("bot{}", i))); } azalea::start_swarm(azalea::SwarmOptions { @@ -21,24 +21,28 @@ async fn main() { plugins: vec![], handle: Box::new(handle), - swarm_handle: Box::new(handle), + swarm_handle: Box::new(swarm_handle), }) .await .unwrap(); } +#[derive(Default, Clone)] struct State {} + +#[derive(Default, Clone)] struct SwarmState {} -async fn handle(bots: Client, event: Arc, state: Arc>) { - match *event { +async fn handle(bot: Client, event: Event, state: State) {} +async fn swarm_handle(swarm: Swarm, event: Event, state: State) { + match event { Event::Tick => { // choose an arbitrary player within render distance to target - if let Some(target) = bots - .dimension() + if let Some(target) = swarm + .dimension .find_one_entity(|e| e.id == "minecraft:player") { - for bot in bots { + for bot in swarm { bot.tick_goto_goal(pathfinder::Goals::Reach(target.bounding_box)); // if target.bounding_box.distance(bot.eyes) < bot.reach_distance() { if bot.entity.can_reach(target.bounding_box) { -- cgit v1.2.3