Skip to content

Commit

Permalink
0.3.1 - AutoExit & AutoReconnect Plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayBox committed Oct 29, 2024
1 parent e1fe0a0 commit 6c121b9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shaysbot"
version = "0.3.0"
version = "0.3.1"
authors = ["Shayne Hartford <shaybox@shaybox.com>"]
edition = "2021"
description = "My personal Minecraft bot using Azalea"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ My personal Minecraft bot using Azalea
- [PearlCommandPlugin](src/minecraft/commands/pearl.rs)
- [AntiAfkPlugin](src/minecraft/anti_afk.rs)
- [AutoEatPlugin](src/minecraft/auto_eat.rs)
- [AutoExitPlugin](src/minecraft/auto_exit.rs)
- [AutoLookPlugin](src/minecraft/auto_look.rs)
- [AutoPearlPlugin](src/minecraft/auto_pearl.rs)
- [PearlTrackerPlugin](src/minecraft/pearl_tracker.rs)
Expand Down
27 changes: 27 additions & 0 deletions src/minecraft/auto_exit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use azalea::{
app::{App, Plugin, Update},
disconnect::DisconnectEvent,
ecs::prelude::*,
};

pub struct AutoExitPlugin;

impl Plugin for AutoExitPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, handle_disconnect_event);
}
}

pub fn handle_disconnect_event(mut events: EventReader<DisconnectEvent>) {
for event in events.read() {
let Some(reason) = &event.reason else {
continue;
};

println!("Disconnect Reason: {}", reason.to_ansi());
if ["AutoDisconnect"].contains(&&*reason.to_string()) {
eprintln!("Exiting to stay disconnected...");
std::process::exit(1);
}
}
}
2 changes: 0 additions & 2 deletions src/minecraft/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ pub fn handle_chat_received_event(
commands: Res<Commands>,
) {
for event in events.read() {
println!("{}", event.packet.message().to_ansi());

let Ok(settings) = query.get(event.entity) else {
continue;
};
Expand Down
59 changes: 43 additions & 16 deletions src/minecraft/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::sync::Arc;

use azalea::{Account, ClientBuilder};
use anyhow::Result;
use azalea::{
ecs::prelude::*,
prelude::*,
swarm::{Swarm, SwarmBuilder, SwarmEvent},
Account,
};
use derive_config::{DeriveTomlConfig, DeriveYamlConfig};
use parking_lot::RwLock;

Expand All @@ -16,18 +22,22 @@ pub mod prelude;

mod anti_afk;
mod auto_eat;
mod auto_exit;
mod auto_look;
mod auto_pearl;
mod auto_totem;
mod commands;
mod pearl_tracker;

#[derive(Clone, Component, Default, Resource)]
pub struct SwarmState;

/// # Create and start the Minecraft bot client
///
/// # Errors
/// Will return `Err` if `ClientBuilder::start` fails.
#[allow(clippy::future_not_send)]
pub async fn start() -> anyhow::Result<()> {
pub async fn start() -> Result<()> {
let settings = Settings::load().unwrap_or_default();
let trapdoors = Trapdoors::load().unwrap_or_default();
let address = settings.server_address.clone();
Expand All @@ -40,18 +50,35 @@ pub async fn start() -> anyhow::Result<()> {
settings.save()?;
let settings = Arc::new(RwLock::new(settings));
let trapdoors = Arc::new(RwLock::new(trapdoors));
let client = ClientBuilder::new().add_plugins((
CommandsPlugin,
PearlCommandPlugin,
AntiAfkPlugin,
AutoEatPlugin,
AutoLookPlugin,
AutoPearlPlugin,
AutoTotemPlugin,
PearlTrackerPlugin,
SettingsPlugin(settings),
TrapdoorsPlugin(trapdoors),
));

client.start(account, address).await?
let client = SwarmBuilder::new()
.set_swarm_handler(swarm_handler)
.add_account(account)
.add_plugins((CommandsPlugin, PearlCommandPlugin))
.add_plugins((
AntiAfkPlugin,
AutoEatPlugin,
AutoExitPlugin,
AutoLookPlugin,
AutoPearlPlugin,
AutoTotemPlugin,
PearlTrackerPlugin,
SettingsPlugin(settings),
TrapdoorsPlugin(trapdoors),
));

client.start(address).await?
}

/// # Errors
/// Will return `Err` if `Swarm::add_with_opts` fails.
pub async fn swarm_handler(mut swarm: Swarm, event: SwarmEvent, state: SwarmState) -> Result<()> {
match event {
SwarmEvent::Chat(chat_packet) => println!("{}", chat_packet.message().to_ansi()),
SwarmEvent::Disconnect(account, options) => {
swarm.add_with_opts(&account, state, &options).await?;
}
_ => {}
}

Ok(())
}
1 change: 1 addition & 0 deletions src/minecraft/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use super::{
anti_afk::*,
auto_eat::*,
auto_exit::*,
auto_look::*,
auto_pearl::*,
auto_totem::*,
Expand Down

0 comments on commit 6c121b9

Please sign in to comment.