Skip to content

Commit

Permalink
0.2.1 - AutoPearl & Settings Plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayBox committed Oct 25, 2024
1 parent fd28e29 commit ccee3fb
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 143 deletions.
146 changes: 96 additions & 50 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "shaysbot"
version = "0.2.0"
version = "0.2.1"
authors = ["Shayne Hartford <shaybox@shaybox.com>"]
edition = "2021"
description = "Minecraft Pearl Stasis Bot"
description = "My personal Minecraft bot using Azalea"
readme = "README.md"
homepage = "https://git.shaybox.com/shaysbot/releases/latest"
repository = "https://github.com/ShayBox/ShaysBot"
Expand All @@ -15,10 +15,12 @@ license = "MIT"
anyhow = "1"
async-trait = "0.1"
azalea = { git = "https://github.com/Shays-Forks/azalea.git" }
bevy-discord = "0.2"
derive-config = { version = "2", features = ["toml", "yaml"] }
derive_more = { version = "1", features = ["full"] }
dyn-clonable = "0.9"
lazy-regex = "3"
parking_lot = "0.12"
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
serde = "1"
serde_with = "3"
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

# ShaysBot

Minecraft Pearl Stasis Bot
My personal Minecraft bot using Azalea

## TODO
## Plugins

- AutoEatPlugin
- AutoReconnectPlugin
- AutoTotemPlugin
- [AntiAfkPlugin](src/plugins/anti_afk.rs)
- [AutoEatPlugin](src/plugins/auto_eat.rs)
- [AutoLookPlugin](src/plugins/auto_look.rs)
- [AutoPearlPlugin](src/plugins/auto_pearl.rs)
- [AutoTotemPlugin](src/plugins/auto_totem.rs)
- [SettingsPlugin](src/plugins/settings.rs)
44 changes: 5 additions & 39 deletions src/commands/pearl.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
use std::collections::VecDeque;

use anyhow::Result;
use azalea::{
core::direction::Direction,
packet_handling::game::SendPacketEvent,
pathfinder::goals::ReachBlockPosGoal,
prelude::*,
protocol::packets::game::{
serverbound_interact_packet::InteractionHand,
serverbound_use_item_on_packet::{BlockHit, ServerboundUseItemOnPacket},
ServerboundGamePacket,
},
BlockPos,
Vec3,
};
use azalea::{prelude::*, BlockPos};

use super::{CommandHandler, CommandResponse};
use crate::State;
use crate::{plugins::prelude::AutoPearlClientExt, State};

#[derive(Clone)]
pub struct Pearl;
Expand Down Expand Up @@ -44,7 +32,7 @@ impl CommandHandler for Pearl {
return Ok(CommandResponse::Whisper(message));
};

let trapdoors = state.trapdoors.read().await.0.clone();
let trapdoors = state.trapdoors.read().0.clone();
let Some(trapdoor) = trapdoors
.clone()
.into_values()
Expand All @@ -69,35 +57,13 @@ impl CommandHandler for Pearl {
return Ok(CommandResponse::Whisper(message));
};

if !state.settings.read().await.quiet {
client.pearl(trapdoor.block_pos);
if !state.settings.read().quiet {
let command = format!("w {username} [202] I'm on my way!");
client.send_command_packet(&command);
}

state.wait_for_pathfinder(client).await?;
client.goto_without_mining(ReachBlockPosGoal {
chunk_storage: client.world().read().chunks.clone(),
pos: trapdoor.block_pos,
});

state.wait_for_pathfinder(&client).await?;
client.ecs.lock().send_event(SendPacketEvent {
entity: client.entity,
packet: ServerboundGamePacket::UseItemOn(ServerboundUseItemOnPacket {
hand: InteractionHand::MainHand,
sequence: 0,
block_hit: BlockHit {
block_pos: trapdoor.block_pos,
direction: Direction::Down,
inside: true,
location: Vec3 {
x: f64::from(trapdoor.block_pos.x) + 0.5,
y: f64::from(trapdoor.block_pos.y) + 0.5,
z: f64::from(trapdoor.block_pos.z) + 0.5,
},
},
}),
});

let message = String::from("[200] OK");
Ok(CommandResponse::Whisper(message))
Expand Down
13 changes: 5 additions & 8 deletions src/events/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ impl EventHandler for Chat {
let (sender, content) = self.0.split_sender_and_content();
let (sender, content) = if let Some(sender) = sender {
(sender, content)
} else if let Some((_whole, sender, content)) = /* 2B2T Whisper Format */
regex_captures!("^([a-zA-Z_0-9]{1,16}) (?:whispers: )?(.+)$", &content)
{
(sender.to_string(), content.to_string())
} else if let Some((_whole, sender, content)) = /* VengeanceCraft Chat Format */
regex_captures!(r"^\[.+\] ([a-zA-Z_0-9]{1,16}) > (.+)$", &content)
{
} else if let Some((_whole, sender, content)) = regex_captures!(
r"^(?:\[.+\])? ([a-zA-Z_0-9]{1,16}) (?:> )?(?:whispers: )?(.+)$",
&content
) {
(sender.to_string(), content.to_string())
} else {
return Ok(());
Expand All @@ -54,7 +51,7 @@ impl EventHandler for Chat {
CommandResponse::None => return Ok(()),
};

if !state.settings.read().await.quiet {
if !state.settings.read().quiet {
client.send_command_packet(&command);
}

Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use std::sync::Arc;

use anyhow::Result;
use azalea::prelude::*;
use tokio::sync::RwLock;
use parking_lot::RwLock;

pub use crate::{
plugins::ShaysPluginGroup,
plugins::{prelude::*, ShaysPluginGroup},
settings::Settings,
trapdoor::{Trapdoor, Trapdoors},
};
Expand Down Expand Up @@ -76,7 +76,7 @@ impl State {
/// Will return `Err` if `ClientBuilder::start` fails.
#[allow(clippy::future_not_send)]
pub async fn start(self) -> Result<()> {
let settings = self.settings.read().await.clone();
let settings = self.settings.read().clone();
let account = if settings.online {
Account::microsoft(&settings.username).await?
} else {
Expand All @@ -85,6 +85,7 @@ impl State {

let client = ClientBuilder::new()
.add_plugins(ShaysPluginGroup)
.add_plugins(SettingsPlugin(self.settings.clone()))
.set_handler(Self::handler)
.set_state(self);

Expand Down
2 changes: 1 addition & 1 deletion src/packets/block_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl PacketHandler for BlockUpdate<'_> {
}
};

let mut trapdoors = state.trapdoors.write().await;
let mut trapdoors = state.trapdoors.write();

trapdoors
.0
Expand Down
2 changes: 1 addition & 1 deletion src/packets/entity_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl PacketHandler for EntityAdd<'_> {
profile.uuid
};

let mut trapdoors = state.trapdoors.write().await;
let mut trapdoors = state.trapdoors.write();
let new_trapdoor = Trapdoor::new(block_pos, self.0.id, owner_uuid);

if let Some(old_trapdoor) = trapdoors.0.get_mut(&self.0.uuid) {
Expand Down
4 changes: 2 additions & 2 deletions src/packets/entity_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl PacketHandler for EntityRemove<'_> {
/// Will return `Err` if `DeriveYamlConfig::save` fails.
async fn execute(self, client: Client, state: State) -> Result<()> {
let client_pos = client.position();
let view_distance = state.settings.read().await.pearl_view_distance;
let view_distance = state.settings.read().pearl_view_distance;
let view_distance_sqr = f64::from(view_distance.pow(2));
let mut trapdoors = state.trapdoors.write().await;
let mut trapdoors = state.trapdoors.write();

trapdoors.0.retain(|_, trapdoor| {
let trapdoor_pos = trapdoor.block_pos.to_vec3_floored();
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/anti_afk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ impl Plugin for AntiAfkPlugin {
app.add_systems(
GameTick,
handle_anti_afk
.before(handle_swing_arm_event)
.after(PhysicsSet),
.after(PhysicsSet)
.before(handle_swing_arm_event),
);
}
}

#[derive(Default)]
struct Ticks(u8);
pub struct Ticks(pub u8);

impl Iterator for Ticks {
type Item = u8;
Expand All @@ -37,8 +37,8 @@ impl Iterator for Ticks {
}

#[derive(Component, Default)]
struct AntiAfk {
ticks: Ticks,
pub struct AntiAfk {
pub ticks: Ticks,
}

type InitQueryData = Entity;
Expand All @@ -47,7 +47,7 @@ type InitQueryFilter = (With<Player>, With<LocalEntity>, Without<AntiAfk>);
type RunQueryData<'a> = (Entity, &'a mut AntiAfk);
type RunQueryFilter = (With<Player>, With<LocalEntity>, With<AntiAfk>);

fn handle_anti_afk(
pub fn handle_anti_afk(
mut init_query: Query<InitQueryData, InitQueryFilter>,
mut commands: Commands,

Expand Down
28 changes: 15 additions & 13 deletions src/plugins/auto_eat.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use azalea::{
app::{App, Plugin},
app::{App, Plugin, Update},
chunks::handle_chunk_batch_finished_event,
ecs::prelude::*,
entity::{metadata::Player, LocalEntity, LookDirection},
entity::{clamp_look_direction, metadata::Player, LocalEntity, LookDirection},
inventory::{
handle_container_click_event,
operations::{ClickOperation, SwapClick},
ContainerClickEvent,
Inventory,
InventorySet,
},
movement::send_position,
packet_handling::game::{handle_send_packet_event, SendPacketEvent},
prelude::*,
protocol::packets::game::{
serverbound_interact_packet::InteractionHand,
serverbound_use_item_packet::ServerboundUseItemPacket,
Expand All @@ -27,12 +27,14 @@ pub struct AutoEatPlugin;
impl Plugin for AutoEatPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
GameTick,
Update,
handle_auto_eat
.after(send_position)
.ambiguous_with(handle_auto_totem)
.after(clamp_look_direction)
.before(handle_chunk_batch_finished_event)
.before(handle_container_click_event)
.before(handle_send_packet_event),
.before(handle_send_packet_event)
.before(handle_auto_totem)
.before(InventorySet),
);
}
}
Expand All @@ -43,8 +45,8 @@ type QueryFilter = (With<Player>, With<LocalEntity>);
#[allow(clippy::needless_pass_by_value)]
pub fn handle_auto_eat(
mut query: Query<QueryData, QueryFilter>,
mut send_packet_events: EventWriter<SendPacketEvent>,
mut send_container_click_events: EventWriter<ContainerClickEvent>,
mut packet_events: EventWriter<SendPacketEvent>,
mut container_click_events: EventWriter<ContainerClickEvent>,
) {
for (entity, hunger, inventory, direction) in &mut query {
if hunger.food >= 18 {
Expand Down Expand Up @@ -72,7 +74,7 @@ pub fn handle_auto_eat(
}
})
.for_each(|event| {
send_container_click_events.send(event);
container_click_events.send(event);
});
}

Expand All @@ -83,11 +85,11 @@ pub fn handle_auto_eat(
sequence: 0,
});

send_packet_events.send(SendPacketEvent { entity, packet });
packet_events.send(SendPacketEvent { entity, packet });
}
}

const FOOD_ITEMS: [Item; 32] = [
pub const FOOD_ITEMS: [Item; 32] = [
Item::Apple,
Item::BakedPotato,
Item::Beef,
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/auto_look.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use azalea::{
app::{App, Plugin},
ecs::prelude::*,
entity::{metadata::Player, EyeHeight, LocalEntity, Position},
entity::{clamp_look_direction, metadata::Player, EyeHeight, LocalEntity, Position},
nearest_entity::EntityFinder,
physics::PhysicsSet,
prelude::*,
prelude::GameTick,
LookAtEvent,
};

pub struct AutoLookPlugin;

impl Plugin for AutoLookPlugin {
fn build(&self, app: &mut App) {
app.add_systems(GameTick, handle_auto_look.before(PhysicsSet));
app.add_systems(
GameTick,
handle_auto_look
.after(clamp_look_direction)
.before(PhysicsSet),
);
}
}

#[allow(clippy::needless_pass_by_value)]
fn handle_auto_look(
pub fn handle_auto_look(
mut query: Query<Entity, (With<LocalEntity>, With<Player>)>,
entities: EntityFinder<With<Player>>,
targets: Query<(&Position, Option<&EyeHeight>)>,
Expand Down
Loading

0 comments on commit ccee3fb

Please sign in to comment.