diff options
| author | mat <git@matdoes.dev> | 2026-05-06 18:38:23 -0545 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-05-07 08:05:58 -1200 |
| commit | cabc8b60a729ba17f5b75f7a7956c6d1ddcc8919 (patch) | |
| tree | 237fd12a9768fe7431ce42dfbdde60f4c7850e06 /azalea/examples/testbot/commands/movement.rs | |
| parent | 9ffd0e80bbb3feace231553d6539124585b03e3c (diff) | |
| download | azalea-drasl-cabc8b60a729ba17f5b75f7a7956c6d1ddcc8919.tar.xz | |
azalea-brigadier now allows commands to return a Result
Diffstat (limited to 'azalea/examples/testbot/commands/movement.rs')
| -rw-r--r-- | azalea/examples/testbot/commands/movement.rs | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/azalea/examples/testbot/commands/movement.rs b/azalea/examples/testbot/commands/movement.rs index 500e17b0..df3b7418 100644 --- a/azalea/examples/testbot/commands/movement.rs +++ b/azalea/examples/testbot/commands/movement.rs @@ -6,11 +6,11 @@ use azalea::{ pathfinder::goals::{BlockPosGoal, RadiusGoal, XZGoal}, prelude::*, }; -use parking_lot::Mutex; -use super::{CommandSource, Ctx}; +use super::Ctx; +use crate::commands::Dispatcher; -pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { +pub fn register(commands: &mut Dispatcher) { commands.register( literal("goto") .executes(|ctx: &Ctx| { @@ -19,14 +19,14 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { // look for the sender let Some(entity) = source.entity() else { source.reply("I can't see you!"); - return 0; + return Ok(0); }; - let position = entity.position(); + let position = entity.position()?; source.reply("ok"); source .bot .start_goto(BlockPosGoal(BlockPos::from(position.up(0.5)))); - 1 + Ok(1) }) .then(literal("xz").then(argument("x", integer()).then( argument("z", integer()).executes(|ctx: &Ctx| { @@ -36,7 +36,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { println!("goto xz {x} {z}"); source.reply("ok"); source.bot.start_goto(XZGoal { x, z }); - 1 + Ok(1) }), ))) .then(literal("radius").then(argument("radius", float()).then( @@ -53,7 +53,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { pos: BlockPos::new(x, y, z).center(), radius, }); - 1 + Ok(1) }), )), ))) @@ -66,7 +66,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { println!("goto xyz {x} {y} {z}"); source.reply("ok"); source.bot.start_goto(BlockPosGoal(BlockPos::new(x, y, z))); - 1 + Ok(1) }), ))), ); @@ -77,23 +77,23 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { // look for the sender let Some(entity) = source.entity() else { source.reply("I can't see you!"); - return 0; + return Ok(0); }; source.reply("ok"); *source.state.following_entity.lock() = Some(entity); - 1 + Ok(1) })); commands.register(literal("down").executes(|ctx: &Ctx| { let source = ctx.source.clone(); + let bot = source.lock().bot.clone(); + let position = BlockPos::from(bot.position()?); tokio::spawn(async move { - let bot = source.lock().bot.clone(); - let position = BlockPos::from(bot.position()); source.lock().reply("mining..."); bot.mine(position.down(1)).await; source.lock().reply("done"); }); - 1 + Ok(1) })); commands.register( @@ -103,11 +103,11 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { let source = ctx.source.lock(); let Some(entity) = source.entity() else { source.reply("I can't see you!"); - return 0; + return Ok(0); }; - let eye_position = entity.eye_position(); + let eye_position = entity.eye_position()?; source.bot.look_at(eye_position); - 1 + Ok(1) }) .then(argument("x", integer()).then(argument("y", integer()).then( argument("z", integer()).executes(|ctx: &Ctx| { @@ -119,7 +119,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { println!("{pos:?}"); let source = ctx.source.lock(); source.bot.look_at(pos.center()); - 1 + Ok(1) }), ))), ); @@ -142,7 +142,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { bot.walk(WalkDirection::None); }); source.reply(format!("ok, walking for {seconds} seconds")); - 1 + Ok(1) })), ); commands.register( @@ -156,33 +156,33 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { bot.walk(WalkDirection::None); }); source.reply(format!("ok, sprinting for {seconds} seconds")); - 1 + Ok(1) })), ); commands.register(literal("north").executes(|ctx: &Ctx| { let source = ctx.source.lock(); - source.bot.set_direction(180., 0.); + source.bot.set_direction(180., 0.)?; source.reply("ok"); - 1 + Ok(1) })); commands.register(literal("south").executes(|ctx: &Ctx| { let source = ctx.source.lock(); - source.bot.set_direction(0., 0.); + source.bot.set_direction(0., 0.)?; source.reply("ok"); - 1 + Ok(1) })); commands.register(literal("east").executes(|ctx: &Ctx| { let source = ctx.source.lock(); - source.bot.set_direction(-90., 0.); + source.bot.set_direction(-90., 0.)?; source.reply("ok"); - 1 + Ok(1) })); commands.register(literal("west").executes(|ctx: &Ctx| { let source = ctx.source.lock(); - source.bot.set_direction(90., 0.); + source.bot.set_direction(90., 0.)?; source.reply("ok"); - 1 + Ok(1) })); commands.register( literal("jump") @@ -190,27 +190,27 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { let source = ctx.source.lock(); source.bot.jump(); source.reply("ok"); - 1 + Ok(1) }) .then(argument("enabled", bool()).executes(|ctx: &Ctx| { let jumping = get_bool(ctx, "enabled").unwrap(); let source = ctx.source.lock(); - source.bot.set_jumping(jumping); - 1 + source.bot.set_jumping(jumping)?; + Ok(1) })), ); let sneak = |ctx: &Ctx| { let source = ctx.source.lock(); - source.bot.set_crouching(!source.bot.crouching()); + source.bot.set_crouching(!source.bot.crouching())?; source.reply("ok"); - 1 + Ok(1) }; let sneak_enabled = argument("enabled", bool()).executes(|ctx: &Ctx| { let sneaking = get_bool(ctx, "enabled").unwrap(); let source = ctx.source.lock(); - source.bot.set_crouching(sneaking); - 1 + source.bot.set_crouching(sneaking)?; + Ok(1) }); commands.register(literal("sneak").executes(sneak).then(sneak_enabled.clone())); commands.register(literal("crouch").executes(sneak).then(sneak_enabled)); @@ -220,13 +220,13 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { source.bot.stop_pathfinding(); source.reply("ok"); *source.state.following_entity.lock() = None; - 1 + Ok(1) })); commands.register(literal("forcestop").executes(|ctx: &Ctx| { let source = ctx.source.lock(); source.bot.force_stop_pathfinding(); source.reply("ok"); *source.state.following_entity.lock() = None; - 1 + Ok(1) })); } |
