|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use azalea::{ |
| 4 | + brigadier::prelude::*, |
| 5 | + entity::{EyeHeight, Position}, |
| 6 | + pathfinder::goals::{BlockPosGoal, XZGoal}, |
| 7 | + prelude::*, |
| 8 | + BlockPos, SprintDirection, WalkDirection, |
| 9 | +}; |
| 10 | +use parking_lot::Mutex; |
| 11 | + |
| 12 | +use crate::BotTask; |
| 13 | + |
| 14 | +use super::{CommandSource, Ctx}; |
| 15 | + |
| 16 | +pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { |
| 17 | + commands.register( |
| 18 | + literal("goto") |
| 19 | + .executes(|ctx: &Ctx| { |
| 20 | + let mut source = ctx.source.lock(); |
| 21 | + println!("got goto"); |
| 22 | + // look for the sender |
| 23 | + let Some(entity) = source.entity() else { |
| 24 | + source.reply("I can't see you!"); |
| 25 | + return 0; |
| 26 | + }; |
| 27 | + let Some(position) = source.bot.get_entity_component::<Position>(entity) else { |
| 28 | + source.reply("I can't see you!"); |
| 29 | + return 0; |
| 30 | + }; |
| 31 | + source.reply("ok"); |
| 32 | + source.bot.goto(BlockPosGoal(BlockPos::from(position))); |
| 33 | + 1 |
| 34 | + }) |
| 35 | + .then(literal("xz").then(argument("x", integer()).then( |
| 36 | + argument("z", integer()).executes(|ctx: &Ctx| { |
| 37 | + let source = ctx.source.lock(); |
| 38 | + let x = get_integer(ctx, "x").unwrap(); |
| 39 | + let z = get_integer(ctx, "z").unwrap(); |
| 40 | + println!("goto xz {x} {z}"); |
| 41 | + source.reply("ok"); |
| 42 | + source.bot.goto(XZGoal { x, z }); |
| 43 | + 1 |
| 44 | + }), |
| 45 | + ))) |
| 46 | + .then(argument("x", integer()).then(argument("y", integer()).then( |
| 47 | + argument("z", integer()).executes(|ctx: &Ctx| { |
| 48 | + let source = ctx.source.lock(); |
| 49 | + let x = get_integer(ctx, "x").unwrap(); |
| 50 | + let y = get_integer(ctx, "y").unwrap(); |
| 51 | + let z = get_integer(ctx, "z").unwrap(); |
| 52 | + println!("goto xyz {x} {y} {z}"); |
| 53 | + source.reply("ok"); |
| 54 | + source.bot.goto(BlockPosGoal(BlockPos::new(x, y, z))); |
| 55 | + 1 |
| 56 | + }), |
| 57 | + ))), |
| 58 | + ); |
| 59 | + |
| 60 | + commands.register(literal("down").executes(|ctx: &Ctx| { |
| 61 | + let source = ctx.source.clone(); |
| 62 | + tokio::spawn(async move { |
| 63 | + let mut bot = source.lock().bot.clone(); |
| 64 | + let position = BlockPos::from(bot.position()); |
| 65 | + source.lock().reply("mining..."); |
| 66 | + bot.mine(position.down(1)).await; |
| 67 | + source.lock().reply("done"); |
| 68 | + }); |
| 69 | + 1 |
| 70 | + })); |
| 71 | + |
| 72 | + commands.register( |
| 73 | + literal("look") |
| 74 | + .executes(|ctx: &Ctx| { |
| 75 | + // look for the sender |
| 76 | + let mut source = ctx.source.lock(); |
| 77 | + let Some(entity) = source.entity() else { |
| 78 | + source.reply("I can't see you!"); |
| 79 | + return 0; |
| 80 | + }; |
| 81 | + let Some(position) = source.bot.get_entity_component::<Position>(entity) else { |
| 82 | + source.reply("I can't see you!"); |
| 83 | + return 0; |
| 84 | + }; |
| 85 | + let eye_height = source |
| 86 | + .bot |
| 87 | + .get_entity_component::<EyeHeight>(entity) |
| 88 | + .map(|h| *h) |
| 89 | + .unwrap_or_default(); |
| 90 | + source.bot.look_at(position.up(eye_height as f64)); |
| 91 | + 1 |
| 92 | + }) |
| 93 | + .then(argument("x", integer()).then(argument("y", integer()).then( |
| 94 | + argument("z", integer()).executes(|ctx: &Ctx| { |
| 95 | + let pos = BlockPos::new( |
| 96 | + get_integer(ctx, "x").unwrap(), |
| 97 | + get_integer(ctx, "y").unwrap(), |
| 98 | + get_integer(ctx, "z").unwrap(), |
| 99 | + ); |
| 100 | + println!("{:?}", pos); |
| 101 | + let mut source = ctx.source.lock(); |
| 102 | + source.bot.look_at(pos.center()); |
| 103 | + 1 |
| 104 | + }), |
| 105 | + ))), |
| 106 | + ); |
| 107 | + |
| 108 | + commands.register( |
| 109 | + literal("walk").then(argument("seconds", float()).executes(|ctx: &Ctx| { |
| 110 | + let mut seconds = get_float(ctx, "seconds").unwrap(); |
| 111 | + let source = ctx.source.lock(); |
| 112 | + let mut bot = source.bot.clone(); |
| 113 | + |
| 114 | + if seconds < 0. { |
| 115 | + bot.walk(WalkDirection::Backward); |
| 116 | + seconds = -seconds; |
| 117 | + } else { |
| 118 | + bot.walk(WalkDirection::Forward); |
| 119 | + } |
| 120 | + |
| 121 | + tokio::spawn(async move { |
| 122 | + tokio::time::sleep(Duration::from_secs_f32(seconds)).await; |
| 123 | + bot.walk(WalkDirection::None); |
| 124 | + }); |
| 125 | + source.reply(&format!("ok, walking for {seconds} seconds")); |
| 126 | + 1 |
| 127 | + })), |
| 128 | + ); |
| 129 | + commands.register( |
| 130 | + literal("sprint").then(argument("seconds", float()).executes(|ctx: &Ctx| { |
| 131 | + let seconds = get_float(ctx, "seconds").unwrap(); |
| 132 | + let source = ctx.source.lock(); |
| 133 | + let mut bot = source.bot.clone(); |
| 134 | + bot.sprint(SprintDirection::Forward); |
| 135 | + tokio::spawn(async move { |
| 136 | + tokio::time::sleep(Duration::from_secs_f32(seconds)).await; |
| 137 | + bot.walk(WalkDirection::None); |
| 138 | + }); |
| 139 | + source.reply(&format!("ok, spriting for {seconds} seconds")); |
| 140 | + 1 |
| 141 | + })), |
| 142 | + ); |
| 143 | + |
| 144 | + commands.register(literal("north").executes(|ctx: &Ctx| { |
| 145 | + let mut source = ctx.source.lock(); |
| 146 | + source.bot.set_direction(180., 0.); |
| 147 | + source.reply("ok"); |
| 148 | + 1 |
| 149 | + })); |
| 150 | + commands.register(literal("south").executes(|ctx: &Ctx| { |
| 151 | + let mut source = ctx.source.lock(); |
| 152 | + source.bot.set_direction(0., 0.); |
| 153 | + source.reply("ok"); |
| 154 | + 1 |
| 155 | + })); |
| 156 | + commands.register(literal("east").executes(|ctx: &Ctx| { |
| 157 | + let mut source = ctx.source.lock(); |
| 158 | + source.bot.set_direction(-90., 0.); |
| 159 | + source.reply("ok"); |
| 160 | + 1 |
| 161 | + })); |
| 162 | + commands.register(literal("west").executes(|ctx: &Ctx| { |
| 163 | + let mut source = ctx.source.lock(); |
| 164 | + source.bot.set_direction(90., 0.); |
| 165 | + source.reply("ok"); |
| 166 | + 1 |
| 167 | + })); |
| 168 | + commands.register( |
| 169 | + literal("jump") |
| 170 | + .executes(|ctx: &Ctx| { |
| 171 | + let mut source = ctx.source.lock(); |
| 172 | + source.bot.jump(); |
| 173 | + source.reply("ok"); |
| 174 | + 1 |
| 175 | + }) |
| 176 | + .then(argument("enabled", bool()).executes(|ctx: &Ctx| { |
| 177 | + let jumping = get_bool(ctx, "enabled").unwrap(); |
| 178 | + let mut source = ctx.source.lock(); |
| 179 | + source.bot.set_jumping(jumping); |
| 180 | + 1 |
| 181 | + })), |
| 182 | + ); |
| 183 | + |
| 184 | + commands.register(literal("stop").executes(|ctx: &Ctx| { |
| 185 | + let source = ctx.source.lock(); |
| 186 | + source.bot.stop_pathfinding(); |
| 187 | + source.reply("ok"); |
| 188 | + *source.state.task.lock() = BotTask::None; |
| 189 | + 1 |
| 190 | + })); |
| 191 | +} |
0 commit comments