mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
fix some docs/examples
This commit is contained in:
parent
1a2178959d
commit
36a1122010
5 changed files with 40 additions and 25 deletions
|
@ -27,7 +27,7 @@ async fn main() {
|
||||||
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
|
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
|
||||||
match event {
|
match event {
|
||||||
Event::Chat(m) => {
|
Event::Chat(m) => {
|
||||||
if m.username() == Some(bot.game_profile.name) {
|
if m.username() == Some(bot.profile.name) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
if m.content() == "go" {
|
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)))
|
bot.goto(pathfinder::Goals::NearXZ(5, azalea::BlockXZ(0, 0)))
|
||||||
.await;
|
.await;
|
||||||
let chest = bot
|
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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
bot.take_amount(&chest, 5, |i| i.id == "#minecraft:planks")
|
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);
|
bot.hold(&pickaxe);
|
||||||
loop {
|
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);
|
println!("{:?}", e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,11 +21,11 @@ async fn main() {
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct State {}
|
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 {
|
match event {
|
||||||
Event::Chat(m) => {
|
Event::Chat(m) => {
|
||||||
if let (Some(sender), content) = m.split_sender_and_content() {
|
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
|
return Ok(()); // ignore our own messages
|
||||||
}
|
}
|
||||||
bot.chat(&content).await?;
|
bot.chat(&content).await?;
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use azalea::{prelude::*, SwarmEvent};
|
use azalea::{prelude::*, SwarmEvent};
|
||||||
use azalea::{Account, Client, Event, Swarm};
|
use azalea::{Account, Client, Event, Swarm};
|
||||||
use parking_lot::Mutex;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -10,14 +8,14 @@ async fn main() {
|
||||||
|
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
accounts.push(Account::offline(&format!("bot{}", i)));
|
accounts.push(Account::offline(&format!("bot{}", i)));
|
||||||
states.push(Arc::new(Mutex::new(State::default())));
|
states.push(State::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
azalea::start_swarm(azalea::SwarmOptions {
|
azalea::start_swarm(azalea::SwarmOptions {
|
||||||
accounts,
|
accounts,
|
||||||
address: "localhost",
|
address: "localhost",
|
||||||
|
|
||||||
swarm_state: State::default(),
|
swarm_state: SwarmState::default(),
|
||||||
states,
|
states,
|
||||||
|
|
||||||
swarm_plugins: plugins![],
|
swarm_plugins: plugins![],
|
||||||
|
|
|
@ -1,25 +1,30 @@
|
||||||
use azalea::{pathfinder, Account, Client, Event};
|
use azalea::{pathfinder, Account, Client, Event, SwarmEvent};
|
||||||
|
use azalea::{prelude::*, Swarm};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let accounts = Vec::new();
|
let mut accounts = Vec::new();
|
||||||
|
let mut states = Vec::new();
|
||||||
|
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
accounts.push(Account::offline(&format!("bot{}", i)));
|
accounts.push(Account::offline(&format!("bot{}", i)));
|
||||||
|
states.push(State::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
azalea::start_swarm(azalea::SwarmOptions {
|
azalea::start_swarm(azalea::SwarmOptions {
|
||||||
accounts,
|
accounts,
|
||||||
address: "localhost",
|
address: "localhost",
|
||||||
|
|
||||||
swarm_state: State::default(),
|
swarm_state: SwarmState::default(),
|
||||||
state: State::default(),
|
states,
|
||||||
|
|
||||||
swarm_plugins: plugins![pathfinder::Plugin],
|
swarm_plugins: swarm_plugins![pathfinder::Plugin],
|
||||||
plugins: plugins![],
|
plugins: plugins![],
|
||||||
|
|
||||||
handle: Box::new(handle),
|
handle,
|
||||||
swarm_handle: Box::new(swarm_handle),
|
swarm_handle,
|
||||||
|
|
||||||
|
join_delay: None,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -31,19 +36,29 @@ struct State {}
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
struct SwarmState {}
|
struct SwarmState {}
|
||||||
|
|
||||||
async fn handle(bot: Client, event: Event, state: State) {}
|
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
|
||||||
async fn swarm_handle<S>(swarm: Swarm<S>, event: Event, state: State) {
|
Ok(())
|
||||||
|
}
|
||||||
|
async fn swarm_handle(
|
||||||
|
swarm: Swarm<State>,
|
||||||
|
event: SwarmEvent,
|
||||||
|
state: SwarmState,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
match event {
|
match event {
|
||||||
Event::Tick => {
|
SwarmEvent::Tick => {
|
||||||
// choose an arbitrary player within render distance to target
|
// choose an arbitrary player within render distance to target
|
||||||
if let Some(target) = swarm.world.find_one_entity(|e| e.id == "minecraft:player") {
|
if let Some(target) = swarm
|
||||||
for bot in 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));
|
bot.tick_goto_goal(pathfinder::Goals::Reach(target.bounding_box));
|
||||||
// if target.bounding_box.distance(bot.eyes) < bot.reach_distance() {
|
// 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();
|
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);
|
bot.hold(azalea::ItemGroup::Food);
|
||||||
tokio::task::spawn(bot.use_held_item());
|
tokio::task::spawn(bot.use_held_item());
|
||||||
}
|
}
|
||||||
|
@ -52,4 +67,6 @@ async fn swarm_handle<S>(swarm: Swarm<S>, event: Event, state: State) {
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
//! Latest bleeding-edge version:
|
//! Latest bleeding-edge version:
|
||||||
//! `azalea = { git="https://github.com/mat-1/Cargo.toml" }`\
|
//! `azalea = { git="https://github.com/mat-1/Cargo.toml" }`\
|
||||||
//! Latest "stable" release:
|
//! Latest "stable" release:
|
||||||
//! `azalea = "0.3"`
|
//! `azalea = "0.6"`
|
||||||
//!
|
//!
|
||||||
//! ## Optimization
|
//! ## Optimization
|
||||||
//!
|
//!
|
||||||
|
@ -28,7 +28,7 @@
|
||||||
//! ```toml
|
//! ```toml
|
||||||
//! [profile.dev]
|
//! [profile.dev]
|
||||||
//! opt-level = 1
|
//! opt-level = 1
|
||||||
//! [profile.dev.package."*""]
|
//! [profile.dev.package."*"]
|
||||||
//! opt-level = 3
|
//! opt-level = 3
|
||||||
//! ```
|
//! ```
|
||||||
//! to your Cargo.toml. You may have to install the LLD linker.
|
//! to your Cargo.toml. You may have to install the LLD linker.
|
||||||
|
|
Loading…
Add table
Reference in a new issue