diff --git a/src/bot.rs b/src/bot.rs index e21d7f4..b80dfcd 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -27,6 +27,8 @@ pub enum Command { LastOnline, FollowPlayer, StopFollowPlayer, + LookPlayer, + StopLookPlayer, Goto, StopGoto, Say, @@ -81,6 +83,8 @@ pub async fn process_command( "last_online" => command = Command::LastOnline, "follow_player" => command = Command::FollowPlayer, "stop_follow_player" => command = Command::StopFollowPlayer, + "look_player" => command = Command::LookPlayer, + "stop_look_player" => command = Command::StopLookPlayer, "goto" => command = Command::Goto, "stop_goto" => command = Command::StopGoto, "say" => command = Command::Say, @@ -385,9 +389,9 @@ pub async fn process_command( } } if found { - return format!("I am now following {}...", segments[0]); + return format!("I am now following {}!", segments[0]); } else { - return format!("I was unable to find {}...", segments[0]); + return format!("I was unable to find {}!", segments[0]); } } Command::StopFollowPlayer => { @@ -402,6 +406,29 @@ pub async fn process_command( }); "I am no longer following anyone!".to_string() } + Command::LookPlayer => { + if segments.len() < 1 { + return "Please tell me the name of a player!".to_string(); + }; + + let mut found = true; + let player_locations = state.player_locations.lock().unwrap().to_owned(); + for (player, _) in player_locations { + if player.username == segments[0] || player.uuid.to_string() == segments[0] { + found = true; + *state.looked_player.lock().unwrap() = Some(player.to_owned()); + } + } + if found { + return format!("I am now looking at {}!", segments[0]); + } else { + return format!("I was unable to find {}!", segments[0]); + } + } + Command::StopLookPlayer => { + *state.looked_player.lock().unwrap() = None; + "I am no longer looking at anyone!".to_string() + } Command::Goto => { if segments.len() < 3 { return "Please give me X, Y, and Z coordinates to go to!".to_string(); diff --git a/src/main.rs b/src/main.rs index 2110b12..ded86ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod logging; mod matrix; use azalea::pathfinder::BlockPosGoal; -use azalea::{prelude::*, BlockPos, ClientInformation}; +use azalea::{prelude::*, BlockPos, ClientInformation, Vec3}; use azalea_protocol::packets::game::serverbound_client_command_packet::{ Action::PerformRespawn, ServerboundClientCommandPacket, }; @@ -114,6 +114,7 @@ async fn main() { alert_second_counter: Arc::new(Mutex::new(0)), cleanup_second_counter: Arc::new(Mutex::new(0)), followed_player: Arc::new(Mutex::new(None)), + looked_player: Arc::new(Mutex::new(None)), player_locations: Arc::new(Mutex::new(HashMap::new())), mob_locations: Arc::new(Mutex::new(HashMap::new())), player_timestamps: Arc::new(Mutex::new(HashMap::new())), @@ -216,6 +217,7 @@ pub struct State { alert_second_counter: Arc>, cleanup_second_counter: Arc>, followed_player: Arc>>, + looked_player: Arc>>, player_locations: Arc>>, mob_locations: Arc>>, player_timestamps: Arc>>, @@ -311,6 +313,19 @@ async fn handle(mut client: Client, event: Event, state: Arc) -> anyhow:: None => *state.followed_player.lock().unwrap() = None, } } + + let looked_player = state.looked_player.lock().unwrap().to_owned(); + if looked_player.is_some() { + let player_locations = state.player_locations.lock().unwrap().to_owned(); + match player_locations.get(&looked_player.unwrap()) { + Some(position_time_data) => client.look_at(&Vec3 { + x: position_time_data.position[0] as f64, + y: position_time_data.position[1] as f64, + z: position_time_data.position[2] as f64, + }), + None => *state.looked_player.lock().unwrap() = None, + } + } } if *state.alert_second_counter.lock().unwrap() as u32