Add LookPlayer command

This commit is contained in:
ErrorNoInternet 2023-01-28 22:47:26 +08:00
parent 8d4b076eac
commit 2314713a2c
Signed by untrusted user who does not match committer: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
2 changed files with 45 additions and 3 deletions

View File

@ -27,6 +27,8 @@ pub enum Command {
LastOnline, LastOnline,
FollowPlayer, FollowPlayer,
StopFollowPlayer, StopFollowPlayer,
LookPlayer,
StopLookPlayer,
Goto, Goto,
StopGoto, StopGoto,
Say, Say,
@ -81,6 +83,8 @@ pub async fn process_command(
"last_online" => command = Command::LastOnline, "last_online" => command = Command::LastOnline,
"follow_player" => command = Command::FollowPlayer, "follow_player" => command = Command::FollowPlayer,
"stop_follow_player" => command = Command::StopFollowPlayer, "stop_follow_player" => command = Command::StopFollowPlayer,
"look_player" => command = Command::LookPlayer,
"stop_look_player" => command = Command::StopLookPlayer,
"goto" => command = Command::Goto, "goto" => command = Command::Goto,
"stop_goto" => command = Command::StopGoto, "stop_goto" => command = Command::StopGoto,
"say" => command = Command::Say, "say" => command = Command::Say,
@ -385,9 +389,9 @@ pub async fn process_command(
} }
} }
if found { if found {
return format!("I am now following {}...", segments[0]); return format!("I am now following {}!", segments[0]);
} else { } else {
return format!("I was unable to find {}...", segments[0]); return format!("I was unable to find {}!", segments[0]);
} }
} }
Command::StopFollowPlayer => { Command::StopFollowPlayer => {
@ -402,6 +406,29 @@ pub async fn process_command(
}); });
"I am no longer following anyone!".to_string() "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 => { Command::Goto => {
if segments.len() < 3 { if segments.len() < 3 {
return "Please give me X, Y, and Z coordinates to go to!".to_string(); return "Please give me X, Y, and Z coordinates to go to!".to_string();

View File

@ -3,7 +3,7 @@ mod logging;
mod matrix; mod matrix;
use azalea::pathfinder::BlockPosGoal; use azalea::pathfinder::BlockPosGoal;
use azalea::{prelude::*, BlockPos, ClientInformation}; use azalea::{prelude::*, BlockPos, ClientInformation, Vec3};
use azalea_protocol::packets::game::serverbound_client_command_packet::{ use azalea_protocol::packets::game::serverbound_client_command_packet::{
Action::PerformRespawn, ServerboundClientCommandPacket, Action::PerformRespawn, ServerboundClientCommandPacket,
}; };
@ -114,6 +114,7 @@ async fn main() {
alert_second_counter: Arc::new(Mutex::new(0)), alert_second_counter: Arc::new(Mutex::new(0)),
cleanup_second_counter: Arc::new(Mutex::new(0)), cleanup_second_counter: Arc::new(Mutex::new(0)),
followed_player: Arc::new(Mutex::new(None)), followed_player: Arc::new(Mutex::new(None)),
looked_player: Arc::new(Mutex::new(None)),
player_locations: Arc::new(Mutex::new(HashMap::new())), player_locations: Arc::new(Mutex::new(HashMap::new())),
mob_locations: Arc::new(Mutex::new(HashMap::new())), mob_locations: Arc::new(Mutex::new(HashMap::new())),
player_timestamps: 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<Mutex<u16>>, alert_second_counter: Arc<Mutex<u16>>,
cleanup_second_counter: Arc<Mutex<u16>>, cleanup_second_counter: Arc<Mutex<u16>>,
followed_player: Arc<Mutex<Option<Player>>>, followed_player: Arc<Mutex<Option<Player>>>,
looked_player: Arc<Mutex<Option<Player>>>,
player_locations: Arc<Mutex<HashMap<Player, PositionTimeData>>>, player_locations: Arc<Mutex<HashMap<Player, PositionTimeData>>>,
mob_locations: Arc<Mutex<HashMap<Entity, PositionTimeData>>>, mob_locations: Arc<Mutex<HashMap<Entity, PositionTimeData>>>,
player_timestamps: Arc<Mutex<HashMap<String, PlayerTimeData>>>, player_timestamps: Arc<Mutex<HashMap<String, PlayerTimeData>>>,
@ -311,6 +313,19 @@ async fn handle(mut client: Client, event: Event, state: Arc<State>) -> anyhow::
None => *state.followed_player.lock().unwrap() = None, 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 if *state.alert_second_counter.lock().unwrap() as u32