aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples
diff options
context:
space:
mode:
Diffstat (limited to 'azalea/examples')
-rw-r--r--azalea/examples/steal.rs69
-rw-r--r--azalea/examples/testbot/commands/movement.rs10
2 files changed, 52 insertions, 27 deletions
diff --git a/azalea/examples/steal.rs b/azalea/examples/steal.rs
index 464d94f8..1277fab2 100644
--- a/azalea/examples/steal.rs
+++ b/azalea/examples/steal.rs
@@ -2,6 +2,7 @@
use std::sync::Arc;
+use azalea::pathfinder::goals::RadiusGoal;
use azalea::{BlockPos, prelude::*};
use azalea_inventory::ItemStack;
use azalea_inventory::operations::QuickMoveClick;
@@ -21,6 +22,7 @@ async fn main() {
#[derive(Default, Clone, Component)]
struct State {
+ pub is_stealing: Arc<Mutex<bool>>,
pub checked_chests: Arc<Mutex<Vec<BlockPos>>>,
}
@@ -32,43 +34,64 @@ async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
if m.content() != "go" {
return Ok(());
}
- {
- state.checked_chests.lock().clear();
+
+ steal(bot, state).await?;
+ }
+
+ Ok(())
+}
+
+async fn steal(bot: Client, state: State) -> anyhow::Result<()> {
+ {
+ let mut is_stealing = state.is_stealing.lock();
+ if *is_stealing {
+ bot.chat("Already stealing");
+ return Ok(());
}
+ *is_stealing = true;
+ }
+
+ state.checked_chests.lock().clear();
+ loop {
let chest_block = bot
.world()
.read()
- .find_block(bot.position(), &azalea::registry::Block::Chest.into());
- // TODO: update this when find_blocks is implemented
+ .find_blocks(bot.position(), &azalea::registry::Block::Chest.into())
+ .filter(
+ // filter for chests that haven't been checked
+ |block_pos| !state.checked_chests.lock().contains(&block_pos),
+ )
+ .next();
let Some(chest_block) = chest_block else {
- bot.chat("No chest found");
- return Ok(());
+ break;
};
- // bot.goto(BlockPosGoal(chest_block));
+
+ state.checked_chests.lock().push(chest_block);
+
+ bot.goto(RadiusGoal::new(chest_block.center(), 3.)).await;
+
let Some(chest) = bot.open_container_at(chest_block).await else {
- println!("Couldn't open chest");
- return Ok(());
+ println!("Couldn't open chest at {chest_block:?}");
+ continue;
};
- println!("Getting contents");
- for (index, slot) in chest
- .contents()
- .expect("we just opened the chest")
- .iter()
- .enumerate()
- {
+ println!("Getting contents of chest at {chest_block:?}");
+ for (index, slot) in chest.contents().unwrap_or_default().iter().enumerate() {
println!("Checking slot {index}: {slot:?}");
- if let ItemStack::Present(item) = slot {
- if item.kind == azalea::registry::Item::Diamond {
- println!("clicking slot ^");
- chest.click(QuickMoveClick::Left { slot: index as u16 });
- }
+ let ItemStack::Present(item) = slot else {
+ continue;
+ };
+ if item.kind == azalea::registry::Item::Diamond {
+ println!("clicking slot ^");
+ chest.click(QuickMoveClick::Left { slot: index as u16 });
}
}
-
- println!("Done");
}
+ bot.chat("Done");
+
+ *state.is_stealing.lock() = false;
+
Ok(())
}
diff --git a/azalea/examples/testbot/commands/movement.rs b/azalea/examples/testbot/commands/movement.rs
index a400809b..1596ce89 100644
--- a/azalea/examples/testbot/commands/movement.rs
+++ b/azalea/examples/testbot/commands/movement.rs
@@ -28,7 +28,9 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
return 0;
};
source.reply("ok");
- source.bot.goto(BlockPosGoal(BlockPos::from(position)));
+ source
+ .bot
+ .start_goto(BlockPosGoal(BlockPos::from(position)));
1
})
.then(literal("xz").then(argument("x", integer()).then(
@@ -38,7 +40,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
let z = get_integer(ctx, "z").unwrap();
println!("goto xz {x} {z}");
source.reply("ok");
- source.bot.goto(XZGoal { x, z });
+ source.bot.start_goto(XZGoal { x, z });
1
}),
)))
@@ -52,7 +54,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
let z = get_integer(ctx, "z").unwrap();
println!("goto radius {radius}, position: {x} {y} {z}");
source.reply("ok");
- source.bot.goto(RadiusGoal {
+ source.bot.start_goto(RadiusGoal {
pos: BlockPos::new(x, y, z).center(),
radius,
});
@@ -68,7 +70,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
let z = get_integer(ctx, "z").unwrap();
println!("goto xyz {x} {y} {z}");
source.reply("ok");
- source.bot.goto(BlockPosGoal(BlockPos::new(x, y, z)));
+ source.bot.start_goto(BlockPosGoal(BlockPos::new(x, y, z)));
1
}),
))),