From 97f56afe52abab06c8467635f12f9f4fb1277f11 Mon Sep 17 00:00:00 2001 From: Shayne Hartford Date: Fri, 15 Nov 2024 15:56:08 -0500 Subject: [PATCH] 0.7.3 - Join & Leave Visual Range --- Cargo.lock | 9 ++- Cargo.toml | 8 ++- src/commands/pearl.rs | 4 +- src/commands/playtime.rs | 2 +- src/commands/seen.rs | 2 +- src/commands/whitelist.rs | 2 +- src/encryption.rs | 2 +- src/main.rs | 1 + src/plugins/auto_exit.rs | 10 +++- src/plugins/block_state_tracker.rs | 10 ++-- src/plugins/discord_event_logger.rs | 81 ++++++++++++++++++++++++++- src/plugins/mod.rs | 3 + src/plugins/player_profile_tracker.rs | 12 ++-- src/settings.rs | 1 + 14 files changed, 118 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 767cf14..050d7b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,7 +214,6 @@ dependencies = [ "parking_lot", "priority-queue", "rustc-hash 2.0.0", - "serde", "thiserror 1.0.69", "tokio", "tracing", @@ -333,7 +332,6 @@ dependencies = [ "azalea-world", "bevy_app", "bevy_ecs", - "bevy_log", "bevy_tasks", "bevy_time", "derive_more", @@ -3088,9 +3086,9 @@ dependencies = [ [[package]] name = "serenity" -version = "0.12.3" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8f0088eeaf12d92d0a7b6cbbde5c14a2ca9016f51f71c4b54ebcf66c69dc472" +checksum = "3d72ec4323681bf9a3cabe40fd080abc2435859b502a1b5aa9bf693f125bfa76" dependencies = [ "arrayvec", "async-trait", @@ -3167,7 +3165,7 @@ dependencies = [ [[package]] name = "shaysbot" -version = "0.7.2" +version = "0.7.3" dependencies = [ "anyhow", "azalea", @@ -3187,6 +3185,7 @@ dependencies = [ "terminal-link", "tokio", "tracing", + "tracing-subscriber", "ureq", "url", "uuid", diff --git a/Cargo.toml b/Cargo.toml index 877cebb..92b86ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shaysbot" -version = "0.7.2" +version = "0.7.3" authors = ["Shayne Hartford "] edition = "2021" description = "My personal Minecraft bot using Azalea" @@ -13,10 +13,11 @@ license = "MIT" [dependencies] anyhow = "1" -#azalea = { path = "../azalea/azalea" } -azalea = { git = "https://github.com/Shays-Forks/azalea.git" } +#azalea = { path = "../azalea/azalea", default-features = false } +azalea = { git = "https://github.com/Shays-Forks/azalea.git", default-features = false } bevy-discord = { version = "0.3", features = ["full"] } chrono = "0.4" +#derive-config = { path = "../Derive-Config/derive-config", features = ["toml", "yaml"] } derive-config = { version = "2", features = ["toml", "yaml"] } lazy-regex = "3" #ncr = { path = "../ncr-rs", features = ["cfb8", "ecb", "gcm"] } @@ -32,6 +33,7 @@ strum = { version = "0.26", features = ["derive"] } terminal-link = "0.1" tokio = { version = "1", features = ["full"] } tracing = "0.1" +tracing-subscriber = "0.3" ureq = { version = "2", features = ["json"] } url = "2" uuid = "1" diff --git a/src/commands/pearl.rs b/src/commands/pearl.rs index 344808e..6cc5e9d 100644 --- a/src/commands/pearl.rs +++ b/src/commands/pearl.rs @@ -58,7 +58,7 @@ pub fn handle_pearl_command_event( let mut whisper_event = WhisperEvent { entity: event.entity, source: event.source, - sender: event.sender.clone(), + sender: event.sender, content: String::new(), }; @@ -98,7 +98,7 @@ pub fn handle_pearl_command_event( } } - uuid.clone() + *uuid } }; diff --git a/src/commands/playtime.rs b/src/commands/playtime.rs index 41d13e9..97a42ca 100644 --- a/src/commands/playtime.rs +++ b/src/commands/playtime.rs @@ -44,7 +44,7 @@ pub fn handle_playtime_command_event( let mut whisper_event = WhisperEvent { entity: event.entity, source: event.source, - sender: event.sender.clone(), + sender: event.sender, content: String::new(), }; diff --git a/src/commands/seen.rs b/src/commands/seen.rs index 811ee84..83c49b8 100644 --- a/src/commands/seen.rs +++ b/src/commands/seen.rs @@ -45,7 +45,7 @@ pub fn handle_seen_command_event( let mut whisper_event = WhisperEvent { entity: event.entity, source: event.source, - sender: event.sender.clone(), + sender: event.sender, content: String::new(), }; diff --git a/src/commands/whitelist.rs b/src/commands/whitelist.rs index 644cce7..d3a33fb 100644 --- a/src/commands/whitelist.rs +++ b/src/commands/whitelist.rs @@ -58,7 +58,7 @@ pub fn handle_whitelist_command_event( let mut whisper_event = WhisperEvent { entity: event.entity, source: event.source, - sender: event.sender.clone(), + sender: event.sender, content: String::new(), }; diff --git a/src/encryption.rs b/src/encryption.rs index 57e1bd4..d48967c 100644 --- a/src/encryption.rs +++ b/src/encryption.rs @@ -120,7 +120,7 @@ pub fn try_encrypt( } let key = AesKey::decode_base64(&chat_encryption.key).unwrap_or_else(|_| KEY.clone()); - let plaintext = prepend_header(&content); + let plaintext = prepend_header(content); if let Some(encryption) = type_encryption { if let Ok(ciphertext) = encryption.encrypt(&plaintext, &key) { diff --git a/src/main.rs b/src/main.rs index 9606ac5..c03aecb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,5 +10,6 @@ async fn main() -> anyhow::Result<()> { println!("{link}"); } + tracing_subscriber::fmt().with_target(false).init(); shaysbot::start().await } diff --git a/src/plugins/auto_exit.rs b/src/plugins/auto_exit.rs index 050de8a..cbde3b6 100644 --- a/src/plugins/auto_exit.rs +++ b/src/plugins/auto_exit.rs @@ -2,6 +2,7 @@ use azalea::{ app::{App, Plugin, Update}, disconnect::DisconnectEvent, ecs::prelude::*, + events::disconnect_listener, packet_handling::game::PacketEvent, protocol::packets::game::ClientboundGamePacket, registry::EntityKind, @@ -16,7 +17,14 @@ pub struct AutoExitPlugin; impl Plugin for AutoExitPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, (handle_disconnect_event, handle_add_entity_packet)); + app.add_systems( + Update, + ( + handle_add_entity_packet.before(disconnect_listener), + handle_disconnect_event, + ) + .chain(), + ); } } diff --git a/src/plugins/block_state_tracker.rs b/src/plugins/block_state_tracker.rs index 11fc8b8..13b07f2 100644 --- a/src/plugins/block_state_tracker.rs +++ b/src/plugins/block_state_tracker.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use azalea::{ - app::{App, Plugin, PostUpdate, PreUpdate}, + app::{App, Plugin, PostUpdate, Update}, blocks::BlockState, ecs::prelude::*, packet_handling::game::PacketEvent, @@ -15,15 +15,15 @@ pub struct BlockStateTrackerPlugin; impl Plugin for BlockStateTrackerPlugin { fn build(&self, app: &mut App) { app.insert_resource(BlockStates::default()) - .add_systems(PreUpdate, handle_block_update_packet) - .add_systems(PostUpdate, handle_block_break_packet); + .add_systems(Update, handle_add_block_state) + .add_systems(PostUpdate, handle_remove_block_state); } } #[derive(Default, Resource)] pub struct BlockStates(pub HashMap); -fn handle_block_update_packet( +fn handle_add_block_state( mut packet_events: EventReader, mut block_states: ResMut, ) { @@ -36,7 +36,7 @@ fn handle_block_update_packet( } } -fn handle_block_break_packet( +fn handle_remove_block_state( mut packet_events: EventReader, mut block_states: ResMut, ) { diff --git a/src/plugins/discord_event_logger.rs b/src/plugins/discord_event_logger.rs index bcfeeaa..ced666b 100644 --- a/src/plugins/discord_event_logger.rs +++ b/src/plugins/discord_event_logger.rs @@ -10,7 +10,12 @@ use azalea::{ use bevy_discord::{http::DiscordHttpResource, runtime::tokio_runtime}; use serenity::json::json; -use crate::{plugins::block_state_tracker::BlockStates, PlayerProfiles, Settings}; +use crate::{ + handle_add_player_profiles, + plugins::block_state_tracker::BlockStates, + PlayerProfiles, + Settings, +}; pub struct DiscordEventLoggerPlugin; @@ -23,6 +28,8 @@ impl Plugin for DiscordEventLoggerPlugin { handle_block_break_packet, handle_block_update_packet, handle_remove_entities_packet, + handle_player_info_remove_packet, + handle_player_info_update_packet.after(handle_add_player_profiles), ), ); } @@ -129,7 +136,7 @@ fn handle_block_update_packet( fn handle_remove_entities_packet( mut packet_events: EventReader, - mut player_profiles: ResMut, + player_profiles: Res, discord: Res, settings: Res, ) { @@ -139,7 +146,7 @@ fn handle_remove_entities_packet( }; for entity_id in &packet.entity_ids { - let Some(profile) = player_profiles.0.remove(entity_id) else { + let Some(profile) = player_profiles.0.get(entity_id) else { continue; }; @@ -158,3 +165,71 @@ fn handle_remove_entities_packet( } } } +fn handle_player_info_remove_packet( + mut packet_events: EventReader, + player_profiles: Res, + discord: Res, + settings: Res, +) { + for event in packet_events.read() { + let ClientboundGamePacket::PlayerInfoRemove(packet) = event.packet.as_ref() else { + continue; + }; + + for profile_uuid in &packet.profile_ids { + let Some((_, profile)) = player_profiles + .0 + .iter() + .find(|(_, profile)| &profile.uuid == profile_uuid) + else { + continue; + }; + + let client = discord.client(); + let username = profile.name.clone(); + let channel_id = settings.discord_channel; + tokio_runtime().spawn(async move { + let map = json!({ + "content": format!("{username} logged out in visual range"), + }); + + if let Err(error) = client.send_message(channel_id, vec![], &map).await { + error!("{error}"); + }; + }); + } + } +} +fn handle_player_info_update_packet( + mut packet_events: EventReader, + player_profiles: Res, + discord: Res, + settings: Res, +) { + for event in packet_events.read().cloned() { + let ClientboundGamePacket::PlayerInfoUpdate(packet) = event.packet.as_ref() else { + continue; + }; + + let profiles = packet.entries.clone().into_iter().filter_map(|info| { + player_profiles.0.iter().find(|(_, profile)| { + profile.uuid == info.profile.uuid && info.display_name.is_some() + }) + }); + + for (_, profile) in profiles { + let client = discord.client(); + let username = profile.name.clone(); + let channel_id = settings.discord_channel; + tokio_runtime().spawn(async move { + let map = json!({ + "content": format!("{username} joined in visual range"), + }); + + if let Err(error) = client.send_message(channel_id, vec![], &map).await { + error!("{error}"); + }; + }); + } + } +} diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 0f9cd69..9e6d5e4 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -20,16 +20,19 @@ pub struct ShaysPluginGroup; impl PluginGroup for ShaysPluginGroup { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() + /* Commands */ .add(PearlCommandPlugin) .add(PlaytimeCommandPlugin) .add(SeenCommandPlugin) .add(WhitelistCommandPlugin) + /* Plugins */ .add(AntiAfkPlugin) .add(AutoEatPlugin) .add(AutoExitPlugin) .add(AutoLookPlugin) .add(AutoPearlPlugin) .add(AutoTotemPlugin) + /* Trackers */ .add(BlockStateTrackerPlugin) .add(EnderPearlTrackerPlugin) .add(PlayerProfileTrackerPlugin) diff --git a/src/plugins/player_profile_tracker.rs b/src/plugins/player_profile_tracker.rs index e1952d0..30f93d1 100644 --- a/src/plugins/player_profile_tracker.rs +++ b/src/plugins/player_profile_tracker.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use azalea::{ - app::{App, Plugin, PostUpdate, PreUpdate}, + app::{App, Plugin, PostUpdate, Update}, auth::game_profile::GameProfile, ecs::prelude::*, packet_handling::game::PacketEvent, @@ -16,15 +16,15 @@ pub struct PlayerProfileTrackerPlugin; impl Plugin for PlayerProfileTrackerPlugin { fn build(&self, app: &mut App) { app.insert_resource(PlayerProfiles::default()) - .add_systems(PreUpdate, handle_add_entity_packet) - .add_systems(PostUpdate, handle_remove_entities_packet); + .add_systems(Update, handle_add_player_profiles) + .add_systems(PostUpdate, handle_remove_player_profiles); } } #[derive(Default, Resource)] pub struct PlayerProfiles(pub HashMap); -fn handle_add_entity_packet( +pub fn handle_add_player_profiles( mut packet_events: EventReader, mut player_profiles: ResMut, query: Query<&TabList>, @@ -46,11 +46,11 @@ fn handle_add_entity_packet( continue; }; - player_profiles.0.insert(packet.data, info.profile.clone()); + player_profiles.0.insert(packet.id, info.profile.clone()); } } -fn handle_remove_entities_packet( +pub fn handle_remove_player_profiles( mut packet_events: EventReader, mut player_profiles: ResMut, ) { diff --git a/src/settings.rs b/src/settings.rs index e0944f8..7ca1784 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -22,6 +22,7 @@ pub enum EncryptionMode { strike! { #[strikethrough[serde_as]] + #[strikethrough[allow(clippy::derive_partial_eq_without_eq)]] #[strikethrough[derive(Clone, PartialEq, Deserialize, Serialize, SmartDefault)]] #[strikethrough[serde(default)]] #[derive(DeriveTomlConfig, Resource)]