diff options
| author | mat <github@matdoes.dev> | 2023-05-04 20:38:10 +0000 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2023-05-04 20:38:10 +0000 |
| commit | c7a923ccc8ae825deca62ac2cc8c80c01484d5b6 (patch) | |
| tree | 787c6eb579d0b126f0e9dfa161bdea758fe9aa0a /azalea/examples | |
| parent | ff6d43458cef8ac6a23e6e8accd4b71c2a04aef6 (diff) | |
| parent | 634cb8d72c6608512aedba19e5cd669104bc35ea (diff) | |
| download | azalea-drasl-c7a923ccc8ae825deca62ac2cc8c80c01484d5b6.tar.xz | |
merge main
Diffstat (limited to 'azalea/examples')
| -rw-r--r-- | azalea/examples/steal.rs | 76 | ||||
| -rw-r--r-- | azalea/examples/testbot.rs | 81 | ||||
| -rw-r--r-- | azalea/examples/todo/craft_dig_straight_down.rs | 8 |
3 files changed, 144 insertions, 21 deletions
diff --git a/azalea/examples/steal.rs b/azalea/examples/steal.rs new file mode 100644 index 00000000..3302079a --- /dev/null +++ b/azalea/examples/steal.rs @@ -0,0 +1,76 @@ +//! Steal all the diamonds from all the nearby chests. + +use azalea::{prelude::*, BlockPos}; +use azalea_inventory::operations::QuickMoveClick; +use azalea_inventory::ItemSlot; +use parking_lot::Mutex; +use std::sync::Arc; + +#[tokio::main] +async fn main() { + let account = Account::offline("bot"); + // or let bot = Account::microsoft("email").await; + + ClientBuilder::new() + .set_handler(handle) + .start(account, "localhost") + .await + .unwrap(); +} + +#[derive(Default, Clone, Component)] +struct State { + pub checked_chests: Arc<Mutex<Vec<BlockPos>>>, +} + +async fn handle(mut bot: Client, event: Event, state: State) -> anyhow::Result<()> { + match event { + Event::Chat(m) => { + if m.username() == Some(bot.profile.name.clone()) { + return Ok(()); + }; + if m.content() != "go" { + return Ok(()); + } + { + state.checked_chests.lock().clear(); + } + + let chest_block = bot + .world() + .read() + .find_block(bot.position(), &azalea::Block::Chest.into()); + // TODO: update this when find_blocks is implemented + let Some(chest_block) = chest_block else { + bot.chat("No chest found"); + return Ok(()); + }; + // bot.goto(BlockPosGoal::from(chest_block)); + let Some(chest) = bot.open_container(chest_block).await else { + println!("Couldn't open chest"); + return Ok(()); + }; + + println!("Getting contents"); + for (index, slot) in chest + .contents() + .expect("we just opened the chest") + .iter() + .enumerate() + { + println!("Checking slot {index}: {slot:?}"); + if let ItemSlot::Present(item) = slot { + if item.kind == azalea::Item::Diamond { + println!("clicking slot ^"); + chest.click(QuickMoveClick::Left { slot: index as u16 }); + } + } + } + + println!("Done"); + } + _ => {} + } + + Ok(()) +} diff --git a/azalea/examples/testbot.rs b/azalea/examples/testbot.rs index a25b28e3..3fe9253c 100644 --- a/azalea/examples/testbot.rs +++ b/azalea/examples/testbot.rs @@ -4,7 +4,9 @@ use azalea::ecs::query::With; use azalea::entity::metadata::Player; -use azalea::entity::Position; +use azalea::entity::{EyeHeight, Position}; +use azalea::interact::HitResultComponent; +use azalea::inventory::ItemSlot; use azalea::pathfinder::BlockPosGoal; use azalea::{prelude::*, swarm::prelude::*, BlockPos, GameProfileComponent, WalkDirection}; use azalea::{Account, Client, Event}; @@ -46,7 +48,7 @@ async fn main() -> anyhow::Result<()> { let mut accounts = Vec::new(); let mut states = Vec::new(); - for i in 0..5 { + for i in 0..1 { accounts.push(Account::offline(&format!("bot{i}"))); states.push(State::default()); } @@ -112,7 +114,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result< bot.chat(&format!("You're at {pos:?}",)); } "whereareyou" => { - let pos = bot.component::<Position>(); + let pos = bot.position(); bot.chat(&format!("I'm at {pos:?}",)); } "goto" => { @@ -122,10 +124,11 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result< bot.goto(BlockPosGoal::from(target_pos)); } "look" => { - let entity_pos = bot.entity_component::<Position>(entity); - let target_pos: BlockPos = entity_pos.into(); - println!("target_pos: {target_pos:?}"); - bot.look_at(target_pos.center()); + let entity_pos = bot + .entity_component::<Position>(entity) + .up(bot.entity_component::<EyeHeight>(entity).into()); + println!("entity_pos: {entity_pos:?}"); + bot.look_at(entity_pos); } "jump" => { bot.set_jumping(true); @@ -140,18 +143,21 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result< "lag" => { std::thread::sleep(Duration::from_millis(1000)); } + "inventory" => { + println!("inventory: {:?}", bot.menu()); + } "findblock" => { - let target_pos = bot.world().read().find_block( - bot.component::<Position>(), - &azalea_registry::Block::DiamondBlock.into(), - ); + let target_pos = bot + .world() + .read() + .find_block(bot.position(), &azalea::Block::DiamondBlock.into()); bot.chat(&format!("target_pos: {target_pos:?}",)); } "gotoblock" => { - let target_pos = bot.world().read().find_block( - bot.component::<Position>(), - &azalea_registry::Block::DiamondBlock.into(), - ); + let target_pos = bot + .world() + .read() + .find_block(bot.position(), &azalea::Block::DiamondBlock.into()); if let Some(target_pos) = target_pos { // +1 to stand on top of the block bot.goto(BlockPosGoal::from(target_pos.up(1))); @@ -159,6 +165,49 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result< bot.chat("no diamond block found"); } } + "lever" => { + let target_pos = bot + .world() + .read() + .find_block(bot.position(), &azalea::Block::Lever.into()); + let Some(target_pos) = target_pos else { + bot.chat("no lever found"); + return Ok(()) + }; + bot.goto(BlockPosGoal::from(target_pos)); + bot.look_at(target_pos.center()); + bot.block_interact(target_pos); + } + "hitresult" => { + let hit_result = bot.get_component::<HitResultComponent>(); + bot.chat(&format!("hit_result: {hit_result:?}",)); + } + "chest" => { + let target_pos = bot + .world() + .read() + .find_block(bot.position(), &azalea::Block::Chest.into()); + let Some(target_pos) = target_pos else { + bot.chat("no chest found"); + return Ok(()) + }; + bot.look_at(target_pos.center()); + let container = bot.open_container(target_pos).await; + println!("container: {:?}", container); + if let Some(container) = container { + if let Some(contents) = container.contents() { + for item in contents { + if let ItemSlot::Present(item) = item { + println!("item: {:?}", item); + } + } + } else { + println!("container was immediately closed"); + } + } else { + println!("no container found"); + } + } _ => {} } } @@ -196,7 +245,7 @@ async fn swarm_handle( SwarmEvent::Chat(m) => { println!("swarm chat message: {}", m.message().to_ansi()); if m.message().to_string() == "<py5> world" { - for (name, world) in &swarm.world_container.read().worlds { + for (name, world) in &swarm.instance_container.read().worlds { println!("world name: {name}"); if let Some(w) = world.upgrade() { for chunk_pos in w.read().chunks.chunks.values() { diff --git a/azalea/examples/todo/craft_dig_straight_down.rs b/azalea/examples/todo/craft_dig_straight_down.rs index 4c980ccf..116cbcc2 100644 --- a/azalea/examples/todo/craft_dig_straight_down.rs +++ b/azalea/examples/todo/craft_dig_straight_down.rs @@ -38,17 +38,15 @@ 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_block(azalea_registry::Block::Chest)) + .open_container(&bot.world().find_block(azalea::Block::Chest)) .await .unwrap(); - bot.take_amount(&chest, 5, |i| i.id == "#minecraft:planks") + bot.take_amount_from_container(&chest, 5, |i| i.id == "#minecraft:planks") .await; chest.close().await; let crafting_table = bot - .open_crafting_table( - &bot.world.find_block(azalea_registry::Block::CraftingTable), - ) + .open_crafting_table(&bot.world.find_block(azalea::Block::CraftingTable)) .await .unwrap(); bot.craft(&crafting_table, &bot.recipe_for("minecraft:sticks")) |
