Skip to content

Commit

Permalink
fix some docs/examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Nov 27, 2022
1 parent 1a21789 commit 36a1122
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
6 changes: 3 additions & 3 deletions azalea/examples/craft_dig_straight_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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")
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions azalea/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
6 changes: 2 additions & 4 deletions azalea/examples/mine_a_chunk.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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![],
Expand Down
45 changes: 31 additions & 14 deletions azalea/examples/pvp.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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());
}
Expand All @@ -52,4 +67,6 @@ async fn swarm_handle<S>(swarm: Swarm<S>, event: Event, state: State) {
}
_ => {}
}

Ok(())
}
4 changes: 2 additions & 2 deletions azalea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand All @@ -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.
Expand Down

0 comments on commit 36a1122

Please sign in to comment.