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

fix some docs/examples

This commit is contained in:
mat 2022-11-27 17:51:50 -06:00
parent 1a2178959d
commit 36a1122010
5 changed files with 40 additions and 25 deletions

View file

@ -27,7 +27,7 @@ async fn main() {
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
match event {
Event::Chat(m) => {
if m.username() == Some(bot.game_profile.name) {
if m.username() == Some(bot.profile.name) {
return Ok(());
};
if m.content() == "go" {
@ -42,7 +42,7 @@ async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
bot.goto(pathfinder::Goals::NearXZ(5, azalea::BlockXZ(0, 0)))
.await;
let chest = bot
.open_container(&bot.world.find_one_block(|b| b.id == "minecraft:chest"))
.open_container(&bot.world().find_one_block(|b| b.id == "minecraft:chest"))
.await
.unwrap();
bot.take_amount(&chest, 5, |i| i.id == "#minecraft:planks")
@ -65,7 +65,7 @@ async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
bot.hold(&pickaxe);
loop {
if let Err(e) = bot.dig(bot.entity.feet_pos().down(1)).await {
if let Err(e) = bot.dig(bot.entity().feet_pos().down(1)).await {
println!("{:?}", e);
break;
}

View file

@ -21,11 +21,11 @@ async fn main() {
#[derive(Default, Clone)]
pub struct State {}
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
async fn handle(bot: Client, event: Event, _state: State) -> anyhow::Result<()> {
match event {
Event::Chat(m) => {
if let (Some(sender), content) = m.split_sender_and_content() {
if sender == bot.game_profile.name {
if sender == bot.profile.name {
return Ok(()); // ignore our own messages
}
bot.chat(&content).await?;

View file

@ -1,7 +1,5 @@
use azalea::{prelude::*, SwarmEvent};
use azalea::{Account, Client, Event, Swarm};
use parking_lot::Mutex;
use std::sync::Arc;
#[tokio::main]
async fn main() {
@ -10,14 +8,14 @@ async fn main() {
for i in 0..10 {
accounts.push(Account::offline(&format!("bot{}", i)));
states.push(Arc::new(Mutex::new(State::default())));
states.push(State::default());
}
azalea::start_swarm(azalea::SwarmOptions {
accounts,
address: "localhost",
swarm_state: State::default(),
swarm_state: SwarmState::default(),
states,
swarm_plugins: plugins![],

View file

@ -1,25 +1,30 @@
use azalea::{pathfinder, Account, Client, Event};
use azalea::{pathfinder, Account, Client, Event, SwarmEvent};
use azalea::{prelude::*, Swarm};
#[tokio::main]
async fn main() {
let accounts = Vec::new();
let mut accounts = Vec::new();
let mut states = Vec::new();
for i in 0..10 {
accounts.push(Account::offline(&format!("bot{}", i)));
states.push(State::default());
}
azalea::start_swarm(azalea::SwarmOptions {
accounts,
address: "localhost",
swarm_state: State::default(),
state: State::default(),
swarm_state: SwarmState::default(),
states,
swarm_plugins: plugins![pathfinder::Plugin],
swarm_plugins: swarm_plugins![pathfinder::Plugin],
plugins: plugins![],
handle: Box::new(handle),
swarm_handle: Box::new(swarm_handle),
handle,
swarm_handle,
join_delay: None,
})
.await
.unwrap();
@ -31,19 +36,29 @@ struct State {}
#[derive(Default, Clone)]
struct SwarmState {}
async fn handle(bot: Client, event: Event, state: State) {}
async fn swarm_handle<S>(swarm: Swarm<S>, event: Event, state: State) {
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
Ok(())
}
async fn swarm_handle(
swarm: Swarm<State>,
event: SwarmEvent,
state: SwarmState,
) -> anyhow::Result<()> {
match event {
Event::Tick => {
SwarmEvent::Tick => {
// choose an arbitrary player within render distance to target
if let Some(target) = swarm.world.find_one_entity(|e| e.id == "minecraft:player") {
for bot in swarm {
if let Some(target) = swarm
.worlds
.read()
.find_one_entity(|e| e.id == "minecraft:player")
{
for (bot, bot_state) 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) {
if bot.entity().can_reach(target.bounding_box) {
bot.swing();
}
if !bot.using_held_item() && bot.state.lock().hunger <= 17 {
if !bot.using_held_item() && bot.hunger() <= 17 {
bot.hold(azalea::ItemGroup::Food);
tokio::task::spawn(bot.use_held_item());
}
@ -52,4 +67,6 @@ async fn swarm_handle<S>(swarm: Swarm<S>, event: Event, state: State) {
}
_ => {}
}
Ok(())
}

View file

@ -15,7 +15,7 @@
//! Latest bleeding-edge version:
//! `azalea = { git="https://github.com/mat-1/Cargo.toml" }`\
//! Latest "stable" release:
//! `azalea = "0.3"`
//! `azalea = "0.6"`
//!
//! ## Optimization
//!
@ -28,7 +28,7 @@
//! ```toml
//! [profile.dev]
//! opt-level = 1
//! [profile.dev.package."*""]
//! [profile.dev.package."*"]
//! opt-level = 3
//! ```
//! to your Cargo.toml. You may have to install the LLD linker.