Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Modular code (with plugins) (#4)
Browse files Browse the repository at this point in the history
* Added Anti-AFK Plugin

* Added Auto-Mine Plugin

* Added Task Manager Plugin

* Updated examples and `aether-core` to use plugins

* Added ViaVersion

* Upgraded Version of `aether-core`
  • Loading branch information
AS1100K authored Jun 19, 2024
1 parent 0746a8d commit 98696c8
Show file tree
Hide file tree
Showing 33 changed files with 875 additions and 260 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[workspace]
members = [
"aether-core"
, "examples/stone-miner"]
"aether-core",
"examples/anti-afk",
"examples/stone-miner",
"plugins/anti-afk",
"plugins/auto-mine",
"plugins/task-manager"
]

resolver = "2"
8 changes: 6 additions & 2 deletions aether-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aether-core"
version = "0.3.0-alpha.4"
version = "0.3.0-alpha.5"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -16,7 +16,11 @@ lto = true

[dependencies]
anyhow = "1.0.83"
azalea = { git = "https://github.com/azalea-rs/azalea", rev = "dd2f046" }
azalea = { git = "https://github.com/azalea-rs/azalea" }
# This is temp dependecy until, the PR got merge
azalea-viaversion = { git = "https://github.com/EnderKill98/azalea-viaversion", branch = "feature/update-via-proxy" }
azalea-task-manager = { path = "../plugins/task-manager", features = ["anti-afk"] }
azalea-anti-afk = { path = "../plugins/anti-afk" }
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = ["macros"] }
serde = { version = "1.0.201", features = ["derive"] }
Expand Down
47 changes: 0 additions & 47 deletions aether-core/src/afk.rs

This file was deleted.

2 changes: 2 additions & 0 deletions aether-core/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{msg, State};
use azalea::chat::ChatPacket;
use azalea::Client;
use log::info;
use azalea_anti_afk::AntiAFKClientExt;

pub async fn handle_chat(client: Client, chat: ChatPacket, mut state: State) -> anyhow::Result<()> {
let (username, content, is_whisper) = parse_chat_content(&chat);
Expand All @@ -21,6 +22,7 @@ pub async fn handle_chat(client: Client, chat: ChatPacket, mut state: State) ->
} else if content == "Connected to the server.".to_string() {
info!("Connected to the Server, updating the state.");
state.game_information.set_connection_state(true);
client.set_anti_afk(true);
} else if content == "You have lost connection to the server" {
{
info!("Lost Connection to the server, back to queue");
Expand Down
2 changes: 2 additions & 0 deletions aether-core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use azalea::protocol::packets::game::serverbound_client_command_packet::Serverbo
use azalea::{Client, ClientInformation};
use log::info;
use std::sync::Arc;
use azalea_anti_afk::AntiAFKClientExt;

pub async fn handle_init(client: Client, state: State) -> anyhow::Result<()> {
info!("Initialized bot");
Expand All @@ -32,6 +33,7 @@ pub async fn handle_death(
};

client.write_packet(respawn_command_packet.get())?;
client.set_anti_afk(true);

Ok(())
}
68 changes: 16 additions & 52 deletions aether-core/src/commands/pearl/load.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,44 @@
use std::time::Duration;
use crate::msg;
use crate::utils::stop_pathfinding_when_reachable;
use crate::State;
use azalea::pathfinder::goals::BlockPosGoal;
use azalea::pathfinder::PathfinderClientExt;
use azalea::{BlockPos, Client};
use azalea::Client;
use log::info;
use azalea_task_manager::client::TaskManagerExt;
use azalea_task_manager::task_manager_queue::Task;

pub async fn handle_load(username: String, client: Client, state: State) {
info!("Received Pearl Loading Command from {}", username);

{
let mut ongoing_task = state.client_information.ongoing_task.lock();
let mut is_afk = state.client_information.is_afk.lock();
if *ongoing_task && !*is_afk {
let ongoing_task = client.len_tasks() > 0;
if ongoing_task {
msg!(
client,
username,
"I am currently going somewhere, please resend the command after a while."
);
return;
}
*ongoing_task = true;
*is_afk = false;
}

if let Some(trapdoor) = state.config.pearl_locations.get(&username) {
msg!(client, username, "Teleporting...");

let trapdoor = *trapdoor;

client.goto(BlockPosGoal(trapdoor));

stop_pathfinding_when_reachable(
client.clone(),
state.clone(),
trapdoor.to_vec3_floored(),
None,
Some(flip_trapdoor),
Some((client, state, username, trapdoor))
)
let _ = client
.new_task(Task::SetAntiAFK(false))
.new_task(Task::SendChatMessage(format!("/msg {} Teleporting...", username)))
.new_task(Task::GotoTask(trapdoor, false, 4.0))
.new_task(Task::InteractWithBlock(trapdoor))
.new_task(Task::SendChatMessage(format!("/msg {} Pearl Loaded", username)))
.new_task(Task::Delay(Duration::from_secs(1)))
.new_task(Task::SendChatMessage(format!("/msg {} Make sure to put your pearl back!", username)))
.new_task(Task::Delay(Duration::from_secs(2)))
.new_task(Task::GotoTask(state.config.afk_location, false, 2.0))
.new_task(Task::SetAntiAFK(true));
} else {
msg!(
client,
username,
"Unable to find your trapdoor coordinates, use !pearl set x y z"
);

let mut ongoing_task = state.client_information.ongoing_task.lock();
*ongoing_task = false;
}
}

async fn flip_trapdoor(args: Option<(Client, State, String, BlockPos)>) {
if let Some((mut client, state, username, trapdoor)) = args {
client.block_interact(trapdoor);
msg!(client, username, "Pearl Loaded");
msg!(client, username, "Make sure to put your pearl back!");

tokio::time::sleep(Duration::from_secs(2)).await;
{
client.goto(BlockPosGoal(state.config.afk_location));
stop_pathfinding_when_reachable(
client,
state.clone(),
state.config.afk_location.to_vec3_floored(),
Some(1.5),
Some(set_afk),
Some(state),
);
}
}
}

async fn set_afk(args: Option<State>) {
if let Some(mut state) = args {
state.client_information.set_afk(true);
}
}
9 changes: 5 additions & 4 deletions aether-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ mod commands;
mod config;
mod handle_command;
mod state;
mod tick;
mod utils;
mod afk;

use crate::chat::handle_chat;
use crate::client::{handle_death, handle_init};
use std::time::Duration;

use azalea::{prelude::*, swarm::prelude::*};
use log::info;
use azalea_anti_afk::AntiAFKPlugin;
use azalea_task_manager::TaskManagerPlugin;

use crate::config::{Config, Mode};
use crate::state::State;
use crate::tick::handle_tick;

#[tokio::main]
async fn main() {
Expand All @@ -37,6 +36,9 @@ async fn main() {
SwarmBuilder::new()
.set_handler(handle)
.set_swarm_handler(swarm_handle)
.add_plugins(azalea_viaversion::ViaVersionPlugin::start("1.20.6").await)
.add_plugins(AntiAFKPlugin)
.add_plugins(TaskManagerPlugin)
.add_account(account)
.join_delay(Duration::from_secs(3))
.start(server_url.as_str())
Expand All @@ -47,7 +49,6 @@ async fn main() {
async fn handle(client: Client, event: Event, state: State) -> anyhow::Result<()> {
match event {
Event::Chat(chat) => handle_chat(client, chat, state).await?,
Event::Tick => handle_tick(client, state).await?,
Event::Init => handle_init(client, state).await?,
Event::Death(death) => handle_death(client, state, death).await?,
_ => {}
Expand Down
26 changes: 0 additions & 26 deletions aether-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@ use crate::config::Config;
use azalea::prelude::*;
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Instant;

#[derive(Clone, Component, Resource, Debug)]
pub struct State {
pub config: Config,
pub client_information: ClientInformation,
pub game_information: GameInformation,
}

#[derive(Clone, Component, Resource, Debug)]
pub struct ClientInformation {
pub ongoing_task: Arc<Mutex<bool>>,
pub is_afk: Arc<Mutex<bool>>,
}

#[derive(Clone, Component, Resource, Debug)]
pub struct GameInformation {
pub last_afk_tick: Arc<Mutex<Instant>>,
pub is_connected: Arc<Mutex<bool>>,
}

impl Default for GameInformation {
fn default() -> Self {
Self {
last_afk_tick: Arc::new(Mutex::new(Instant::now())),
is_connected: Arc::new(Mutex::new(false)),
}
}
Expand All @@ -38,26 +28,10 @@ impl GameInformation {
}
}

impl Default for ClientInformation {
fn default() -> Self {
Self {
ongoing_task: Arc::new(Mutex::new(false)),
is_afk: Arc::new(Mutex::new(true)),
}
}
}

impl ClientInformation {
pub fn set_afk(&mut self, afk_state: bool) {
*self.is_afk.lock() = afk_state;
}
}

impl Default for State {
fn default() -> Self {
Self {
config: Config::default(),
client_information: ClientInformation::default(),
game_information: GameInformation::default(),
}
}
Expand Down
31 changes: 0 additions & 31 deletions aether-core/src/tick.rs

This file was deleted.

Loading

0 comments on commit 98696c8

Please sign in to comment.