Skip to content

Commit

Permalink
0.7.3 - Join & Leave Visual Range
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayBox committed Nov 15, 2024
1 parent 736f0ce commit 97f56af
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 29 deletions.
9 changes: 4 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shaysbot"
version = "0.7.2"
version = "0.7.3"
authors = ["Shayne Hartford <shaybox@shaybox.com>"]
edition = "2021"
description = "My personal Minecraft bot using Azalea"
Expand All @@ -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"] }
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/commands/pearl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};

Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn handle_pearl_command_event(
}
}

uuid.clone()
*uuid
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/playtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/seen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};

Expand Down
2 changes: 1 addition & 1 deletion src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ async fn main() -> anyhow::Result<()> {
println!("{link}");
}

tracing_subscriber::fmt().with_target(false).init();
shaysbot::start().await
}
10 changes: 9 additions & 1 deletion src/plugins/auto_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/block_state_tracker.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<BlockPos, BlockState>);

fn handle_block_update_packet(
fn handle_add_block_state(
mut packet_events: EventReader<PacketEvent>,
mut block_states: ResMut<BlockStates>,
) {
Expand All @@ -36,7 +36,7 @@ fn handle_block_update_packet(
}
}

fn handle_block_break_packet(
fn handle_remove_block_state(
mut packet_events: EventReader<PacketEvent>,
mut block_states: ResMut<BlockStates>,
) {
Expand Down
81 changes: 78 additions & 3 deletions src/plugins/discord_event_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
),
);
}
Expand Down Expand Up @@ -129,7 +136,7 @@ fn handle_block_update_packet(

fn handle_remove_entities_packet(
mut packet_events: EventReader<PacketEvent>,
mut player_profiles: ResMut<PlayerProfiles>,
player_profiles: Res<PlayerProfiles>,
discord: Res<DiscordHttpResource>,
settings: Res<Settings>,
) {
Expand All @@ -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;
};

Expand All @@ -158,3 +165,71 @@ fn handle_remove_entities_packet(
}
}
}
fn handle_player_info_remove_packet(
mut packet_events: EventReader<PacketEvent>,
player_profiles: Res<PlayerProfiles>,
discord: Res<DiscordHttpResource>,
settings: Res<Settings>,
) {
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<PacketEvent>,
player_profiles: Res<PlayerProfiles>,
discord: Res<DiscordHttpResource>,
settings: Res<Settings>,
) {
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}");
};
});
}
}
}
3 changes: 3 additions & 0 deletions src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ pub struct ShaysPluginGroup;
impl PluginGroup for ShaysPluginGroup {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
/* 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)
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/player_profile_tracker.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<u32, GameProfile>);

fn handle_add_entity_packet(
pub fn handle_add_player_profiles(
mut packet_events: EventReader<PacketEvent>,
mut player_profiles: ResMut<PlayerProfiles>,
query: Query<&TabList>,
Expand All @@ -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<PacketEvent>,
mut player_profiles: ResMut<PlayerProfiles>,
) {
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 97f56af

Please sign in to comment.