Compare commits

...

29 Commits

Author SHA1 Message Date
b3b2252220 build(deps)!: update azalea and fix ecs changes 2025-05-19 21:27:52 -04:00
505b1a26af build(deps)!: update azalea and refactor go_to 2025-04-16 02:14:19 -04:00
f9495a36f2 refactor: cargo clippy improvements 2025-04-15 12:37:44 -04:00
85e1f082a7 fix(lib): avoid multiple food checks 2025-04-12 13:39:20 -04:00
33838e5aed refactor(client): remove redundant mut's 2025-04-12 12:34:05 -04:00
7cf7254dce chore(deps): update everything 2025-04-12 12:29:52 -04:00
94d1727d87 feat(matrix): make sync_timeout configurable 2025-04-03 20:30:44 -04:00
cc03ba6e72 chore(deps): disable termination feature for ctrlc 2025-03-31 16:54:55 -04:00
e213578646 feat(matrix): add matrix_init event 2025-03-31 16:54:52 -04:00
49a4400246 chore(deps): update for azalea entity deindexing fix 2025-03-27 21:22:10 -04:00
5e57678d5c refactor(http): directly use Error from import 2025-03-27 18:01:25 -04:00
1dc6519d0c chore(deps): update everything
Should work on 1.21.5 now.
2025-03-26 17:39:03 -04:00
170c1194ef refactor: remove redundant pub 2025-03-26 17:09:52 -04:00
e3d3e7fe5d refactor(matrix): increase sync timeout 2025-03-26 08:05:00 -04:00
7a365eab42 refactor: remove unused imports in minimal feature 2025-03-25 17:04:36 -04:00
65c4654e72 chore(deps): enable all features on tokio 2025-03-25 16:41:25 -04:00
940b4eb49e refactor(matrix): keep trying to log in 2025-03-25 16:41:25 -04:00
709b4a1d0d feat(lua/matrix): allow sending html messages 2025-03-24 20:20:56 -04:00
2fd0dec502 chore: use nightly toolchain 2025-03-24 16:55:06 -04:00
4da563ae0e style: set group_imports rustfmt option 2025-03-24 16:55:06 -04:00
1eca3ab5a4 refactor(replay)!: capitalize generator field in metadata 2025-03-24 16:55:06 -04:00
e618a8a27b chore(deps): clean up versions 2025-03-22 20:09:39 -04:00
ad24daae33 refactor(matrix): check prefix before owner 2025-03-21 07:58:47 -04:00
2814f4f43a refactor(lib): utilize spawn event 2025-03-20 22:33:55 -04:00
c8aec76075 chore(deps): update azalea 2025-03-20 22:33:54 -04:00
685f0a9cca refactor!(matrix): move options into a table 2025-03-20 22:33:54 -04:00
228b3e3e54 refactor: expand a few variable names 2025-03-20 18:32:07 -04:00
e7133ecc5f chore(deps): update everything 2025-03-19 18:22:35 -04:00
c9a5640436 feat(item_stack): add custom consumable component 2025-03-19 18:22:32 -04:00
37 changed files with 1622 additions and 1036 deletions

1809
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -26,22 +26,22 @@ bevy_ecs = "0"
bevy_log = "0"
clap = { version = "4", features = ["derive", "string"] }
console-subscriber = { version = "0", optional = true }
ctrlc = { version = "3", features = ["termination"] }
ctrlc = "3"
dirs = { version = "6", optional = true }
futures = "0"
futures-locks = "0"
http-body-util = "0"
hyper = { version = "1", features = ["server"] }
hyper-util = "0"
log = { version = "0" }
log = "0"
matrix-sdk = { version = "0", features = ["anyhow"], optional = true }
mimalloc = { version = "0", optional = true }
mlua = { version = "0", features = ["async", "luajit", "send"] }
ncr = { version = "0", features = ["cfb8", "ecb", "gcm"] }
parking_lot = "0"
serde = { version = "1", features = ["derive"] }
serde = "1"
serde_json = "1"
tokio = { version = "1", features = ["macros"] }
tokio = { version = "1", features = ["full"] }
zip = { version = "2", default-features = false, features = ["flate2"] }
[features]

View File

@@ -30,7 +30,7 @@ function auto_fish()
sleep(3000)
end
hold_fishing_rod()
client:use_item()
client:start_use_item()
end
end, "auto-fish_watch-bobber")
@@ -41,7 +41,7 @@ function auto_fish()
end)[1]
if distance(current_bobber.position, particle.position) <= 0.75 then
FishLastCaught = os.time()
client:use_item()
client:start_use_item()
end
end
end, "auto-fish")
@@ -54,11 +54,11 @@ function auto_fish()
if os.time() - FishLastCaught >= 60 then
hold_fishing_rod()
client:use_item()
client:start_use_item()
end
end, "auto-fish_watchdog")
client:use_item()
client:start_use_item()
end
function stop_auto_fish()
@@ -71,7 +71,7 @@ function stop_auto_fish()
return e.id == FishingBobber.id
end)[1] then
FishingBobber = nil
client:use_item()
client:start_use_item()
end
end
@@ -111,6 +111,10 @@ function attack_entities(target_kind, minimum)
end
function check_food(hunger)
if not hunger then
hunger = client.hunger
end
if hunger.food >= 20 then
return
end
@@ -127,6 +131,6 @@ function check_food(hunger)
sleep(1000)
LastEaten = current_time
end
client:use_item()
client:start_use_item()
end
end

View File

@@ -63,7 +63,7 @@ function update_listeners()
end,
eat = function()
sleep(5000)
check_food(client.hunger)
check_food()
end,
},
death = {

View File

@@ -35,9 +35,6 @@ end
function steal(item_name)
for _, chest_pos in ipairs(client:find_blocks(client.position, get_block_states({ "chest" }))) do
client:go_to({ position = chest_pos, radius = 3 }, { type = RADIUS_GOAL })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(500)
end
client:look_at(chest_pos)
local container = client:open_container_at(chest_pos)

View File

@@ -93,9 +93,6 @@ function nether_travel(pos, go_to_opts)
info(string.format("currently in nether, going to %.2f %.2f", nether_pos.x, nether_pos.z))
client:go_to(nether_pos, { type = XZ_GOAL })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(1000)
end
info("arrived, looking for nearest portal")
local portals_nether = client:find_blocks(client.position, portal_block_states)
@@ -144,10 +141,6 @@ function interact_bed()
end
client:go_to({ position = bed, radius = 2 }, { type = RADIUS_GOAL, options = { without_mining = true } })
while client.pathfinder.is_calculating or client.pathfinder.is_executing do
sleep(500)
end
client:look_at(bed)
client:block_interact(bed)
end

View File

@@ -2,7 +2,7 @@ Server = "localhost"
Username = "ErrorNoWatcher"
HttpAddress = "127.0.0.1:8080"
Owners = { "ErrorNoInternet" }
MatrixOwners = { "@errornointernet:envs.net" }
MatrixOptions = { owners = { "@errornointernet:envs.net" } }
for _, module in ipairs({
"lib",

2
rust-toolchain.toml Normal file
View File

@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

1
rustfmt.toml Normal file
View File

@@ -0,0 +1 @@
group_imports = "StdExternalCrate"

View File

@@ -1,7 +1,9 @@
use crate::build_info;
use clap::Parser;
use std::path::PathBuf;
use clap::Parser;
use crate::build_info;
/// A Minecraft bot with Lua scripting support
#[derive(Parser)]
#[command(version = build_info::version_formatted())]

View File

@@ -1,12 +1,13 @@
use crate::{
State,
lua::{eval, exec, reload},
};
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
use futures::lock::Mutex;
use mlua::{Function, Table};
use ncr::utils::prepend_header;
use crate::{
State,
lua::{eval, exec, reload},
};
pub type Ctx = CommandContext<Mutex<CommandSource>>;
pub struct CommandSource {
@@ -32,7 +33,7 @@ impl CommandSource {
}
self.client.chat(
&(if self.message.is_whisper()
&& let Some(username) = self.message.username()
&& let Some(username) = self.message.sender()
{
format!("/w {username} {chunk}")
} else {
@@ -49,7 +50,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move {
let source = source.lock().await;
source.reply(
&reload(&source.state.lua, source.message.username())
&reload(&source.state.lua, source.message.sender())
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
);
});
@@ -63,7 +64,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move {
let source = source.lock().await;
source.reply(
&eval(&source.state.lua, &code, source.message.username())
&eval(&source.state.lua, &code, source.message.sender())
.await
.unwrap_or_else(|error| error.to_string()),
);
@@ -79,7 +80,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move {
let source = source.lock().await;
source.reply(
&exec(&source.state.lua, &code, source.message.username())
&exec(&source.state.lua, &code, source.message.sender())
.await
.map_or_else(|error| error.to_string(), |()| String::from("ok")),
);

View File

@@ -1,11 +1,5 @@
use crate::{
State,
commands::CommandSource,
http::serve,
lua::{client, direction::Direction, player::Player, vec3::Vec3},
particle,
replay::recorder::Recorder,
};
use std::{net::SocketAddr, process::exit};
use anyhow::{Context, Result};
use azalea::{
brigadier::exceptions::BuiltInExceptions::DispatcherUnknownCommand, prelude::*,
@@ -16,13 +10,20 @@ use hyper_util::rt::TokioIo;
use log::{debug, error, info, trace};
use mlua::{Error, Function, IntoLuaMulti, Table};
use ncr::utils::trim_header;
use std::{net::SocketAddr, process::exit};
use tokio::net::TcpListener;
#[cfg(feature = "matrix")]
use crate::matrix;
use {crate::matrix, std::time::Duration, tokio::time::sleep};
#[allow(clippy::too_many_lines)]
use crate::{
State,
commands::CommandSource,
http::serve,
lua::{client, direction::Direction, player::Player, vec3::Vec3},
particle,
replay::recorder::Recorder,
};
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> {
match event {
Event::AddPlayer(player_info) => {
@@ -31,9 +32,10 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
Event::Chat(message) => {
let globals = state.lua.globals();
let (sender, mut content) = message.split_sender_and_content();
let uuid = message.uuid().map(|uuid| uuid.to_string());
let uuid = message.sender_uuid().map(|uuid| uuid.to_string());
let is_whisper = message.is_whisper();
let text = message.message();
let html_text = text.to_html();
let ansi_text = text.to_ansi();
info!("{ansi_text}");
@@ -46,7 +48,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
.call::<String>((options.clone(), content.clone()))
.ok()
.as_deref()
.and_then(|s| trim_header(s).ok())
.and_then(|string| trim_header(string).ok())
{
is_encrypted = true;
ncr_options = Some(options);
@@ -85,6 +87,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let table = state.lua.create_table()?;
table.set("text", text.to_string())?;
table.set("ansi_text", ansi_text)?;
table.set("html_text", html_text)?;
table.set("sender", sender)?;
table.set("content", content)?;
table.set("uuid", uuid)?;
@@ -100,6 +103,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let message_table = state.lua.create_table()?;
message_table.set("text", packet.message.to_string())?;
message_table.set("ansi_text", packet.message.to_ansi())?;
message_table.set("html_text", packet.message.to_html())?;
let table = state.lua.create_table()?;
table.set("message", message_table)?;
table.set("player_id", packet.player_id.0)?;
@@ -116,6 +120,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let table = state.lua.create_table()?;
table.set("text", message.to_string())?;
table.set("ansi_text", message.to_ansi())?;
table.set("html_text", message.to_html())?;
Ok(table)
})
.await
@@ -124,10 +129,10 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
}
}
Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await,
Event::Login => call_listeners(&state, "login", || Ok(())).await,
Event::RemovePlayer(player_info) => {
call_listeners(&state, "remove_player", || Ok(Player::from(player_info))).await
}
Event::Spawn => call_listeners(&state, "spawn", || Ok(())).await,
Event::Tick => call_listeners(&state, "tick", || Ok(())).await,
Event::UpdatePlayer(player_info) => {
call_listeners(&state, "update_player", || Ok(Player::from(player_info))).await
@@ -199,6 +204,12 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
}
_ => Ok(()),
},
Event::Login => {
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
call_listeners(&state, "login", || Ok(())).await
}
Event::Init => {
debug!("received init event");
@@ -210,9 +221,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
exit(0);
})?;
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
let globals = state.lua.globals();
lua_init(client, &state, &globals).await?;
@@ -255,6 +263,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
});
}
}
_ => todo!(),
}
}
@@ -278,16 +287,15 @@ async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()>
#[cfg(feature = "matrix")]
fn matrix_init(client: &Client, state: State) {
let globals = state.lua.globals();
if let Ok(homeserver_url) = globals.get::<String>("MatrixHomeserverUrl")
&& let Ok(username) = globals.get::<String>("MatrixUsername")
&& let Ok(password) = globals.get::<String>("MatrixPassword")
{
if let Ok(options) = globals.get::<Table>("MatrixOptions") {
let name = client.username();
tokio::spawn(async move {
if let Err(error) =
matrix::login(homeserver_url, username, &password, state, globals, name).await
{
error!("failed to log into matrix account: {error:?}");
loop {
let name = name.clone();
if let Err(error) = matrix::login(&state, &options, &globals, name).await {
error!("failed to log into matrix: {error:?}");
}
sleep(Duration::from_secs(10)).await;
}
});
}

View File

@@ -3,9 +3,9 @@
pub mod anti_knockback;
use anti_knockback::anti_knockback;
use azalea::{movement::handle_knockback, packet_handling::game::process_packet_events};
use azalea::{connection::read_packets, movement::handle_knockback};
use bevy_app::{App, Plugin, PreUpdate};
use bevy_ecs::schedule::IntoSystemConfigs;
use bevy_ecs::schedule::IntoScheduleConfigs;
pub struct HacksPlugin;
@@ -13,9 +13,7 @@ impl Plugin for HacksPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
PreUpdate,
anti_knockback
.after(process_packet_events)
.before(handle_knockback),
anti_knockback.after(read_packets).before(handle_knockback),
);
}
}

View File

@@ -1,13 +1,14 @@
use crate::{
State,
lua::{eval, exec, reload},
};
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
use hyper::{
Error, Method, Request, Response, StatusCode,
body::{Bytes, Incoming},
};
use crate::{
State,
lua::{eval, exec, reload},
};
pub async fn serve(
request: Request<Incoming>,
state: State,
@@ -48,13 +49,13 @@ fn status_code_response(
response
}
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Error> {
Full::new(chunk.into())
.map_err(|never| match never {})
.boxed()
}
fn empty() -> BoxBody<Bytes, hyper::Error> {
fn empty() -> BoxBody<Bytes, Error> {
Empty::<Bytes>::new()
.map_err(|never| match never {})
.boxed()

View File

@@ -60,7 +60,7 @@ pub async fn get_block_states(
true
})
{
matched.push(block.id);
matched.push(block.id());
}
}
}

View File

@@ -1,13 +1,13 @@
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
use azalea::{
BlockPos,
inventory::{Inventory, Menu, Player, SlotList},
prelude::ContainerClientExt,
protocol::packets::game::ServerboundSetCarriedItem,
};
use log::error;
use mlua::{Lua, Result, UserDataRef, Value};
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
pub fn container(_lua: &Lua, client: &Client) -> Result<Option<ContainerRef>> {
Ok(client.get_open_container().map(ContainerRef))
}
@@ -22,9 +22,10 @@ pub fn held_slot(_lua: &Lua, client: &Client) -> Result<u8> {
#[allow(clippy::too_many_lines)]
pub fn menu(lua: &Lua, client: &Client) -> Result<Value> {
fn from_slot_list<const N: usize>(s: SlotList<N>) -> Vec<ItemStack> {
s.iter()
.map(|i| ItemStack(i.to_owned()))
fn from_slot_list<const N: usize>(slot_list: SlotList<N>) -> Vec<ItemStack> {
slot_list
.iter()
.map(|item_stack| ItemStack(item_stack.to_owned()))
.collect::<Vec<_>>()
}
@@ -106,7 +107,7 @@ pub async fn open_container_at(
.map(Container))
}
pub fn open_inventory(_lua: &Lua, client: &mut Client, _: ()) -> Result<Option<Container>> {
pub fn open_inventory(_lua: &Lua, client: &Client, (): ()) -> Result<Option<Container>> {
Ok(client.open_inventory().map(Container))
}
@@ -124,11 +125,8 @@ pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
inventory.selected_hotbar_slot = slot;
};
if let Err(error) = client.write_packet(ServerboundSetCarriedItem {
client.write_packet(ServerboundSetCarriedItem {
slot: u16::from(slot),
}) {
error!("failed to send SetCarriedItem packet: {error:?}");
}
});
Ok(())
}

View File

@@ -1,18 +1,17 @@
use super::{Client, Vec3};
use azalea::{
BlockPos, BotClientExt,
protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand},
world::MinecraftEntityId,
BlockPos, BotClientExt, interact::StartUseItemEvent,
protocol::packets::game::s_interact::InteractionHand, world::MinecraftEntityId,
};
use log::error;
use mlua::{Lua, Result, UserDataRef};
pub fn attack(_lua: &Lua, client: &mut Client, entity_id: i32) -> Result<()> {
use super::{Client, Vec3};
pub fn attack(_lua: &Lua, client: &Client, entity_id: i32) -> Result<()> {
client.attack(MinecraftEntityId(entity_id));
Ok(())
}
pub fn block_interact(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
pub fn block_interact(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)]
client.block_interact(BlockPos::new(
position.x as i32,
@@ -39,12 +38,12 @@ pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Res
Ok(())
}
pub fn set_mining(_lua: &Lua, client: &Client, mining: bool) -> Result<()> {
client.left_click_mine(mining);
pub fn set_mining(_lua: &Lua, client: &Client, state: bool) -> Result<()> {
client.left_click_mine(state);
Ok(())
}
pub fn start_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
pub fn start_mining(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
#[allow(clippy::cast_possible_truncation)]
client.start_mining(BlockPos::new(
position.x as i32,
@@ -54,18 +53,14 @@ pub fn start_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<(
Ok(())
}
pub fn use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
let direction = client.direction();
if let Err(error) = client.write_packet(ServerboundUseItem {
pub fn start_use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
client.ecs.lock().send_event(StartUseItemEvent {
entity: client.entity,
hand: match hand {
Some(1) => InteractionHand::OffHand,
_ => InteractionHand::MainHand,
},
sequence: 0,
yaw: direction.0,
pitch: direction.1,
}) {
error!("failed to send UseItem packet: {error:?}");
}
force_block: None,
});
Ok(())
}

View File

@@ -6,15 +6,17 @@ mod movement;
mod state;
mod world;
use std::ops::{Deref, DerefMut};
use azalea::{Client as AzaleaClient, world::MinecraftEntityId};
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use super::{
container::{Container, ContainerRef, item_stack::ItemStack},
direction::Direction,
player::Player,
vec3::Vec3,
};
use azalea::{Client as AzaleaClient, world::MinecraftEntityId};
use mlua::{Lua, Result, UserData, UserDataFields, UserDataMethods};
use std::ops::{Deref, DerefMut};
pub struct Client(pub Option<AzaleaClient>);
@@ -39,6 +41,7 @@ impl UserData for Client {
f.add_field_method_get("dimension", world::dimension);
f.add_field_method_get("direction", movement::direction);
f.add_field_method_get("eye_position", movement::eye_position);
f.add_field_method_get("go_to_reached", movement::go_to_reached);
f.add_field_method_get("has_attack_cooldown", interaction::has_attack_cooldown);
f.add_field_method_get("health", state::health);
f.add_field_method_get("held_item", container::held_item);
@@ -64,30 +67,32 @@ impl UserData for Client {
m.add_async_method("mine", interaction::mine);
m.add_async_method("open_container_at", container::open_container_at);
m.add_async_method("set_client_information", state::set_client_information);
m.add_async_method("start_go_to", movement::start_go_to);
m.add_async_method("wait_until_goal_reached", movement::wait_until_goal_reached);
m.add_method("attack", interaction::attack);
m.add_method("best_tool_for_block", world::best_tool_for_block);
m.add_method("block_interact", interaction::block_interact);
m.add_method("chat", chat);
m.add_method("disconnect", disconnect);
m.add_method("find_blocks", world::find::blocks);
m.add_method("get_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_state);
m.add_method("jump", movement::jump);
m.add_method("look_at", movement::look_at);
m.add_method("open_inventory", container::open_inventory);
m.add_method("set_component", state::set_component);
m.add_method("set_direction", movement::set_direction);
m.add_method("set_held_slot", container::set_held_slot);
m.add_method("set_jumping", movement::set_jumping);
m.add_method("set_mining", interaction::set_mining);
m.add_method("set_position", movement::set_position);
m.add_method("set_sneaking", movement::set_sneaking);
m.add_method("sprint", movement::sprint);
m.add_method("start_mining", interaction::start_mining);
m.add_method("start_use_item", interaction::start_use_item);
m.add_method("stop_pathfinding", movement::stop_pathfinding);
m.add_method("stop_sleeping", movement::stop_sleeping);
m.add_method("use_item", interaction::use_item);
m.add_method_mut("attack", interaction::attack);
m.add_method_mut("block_interact", interaction::block_interact);
m.add_method_mut("jump", movement::jump);
m.add_method_mut("look_at", movement::look_at);
m.add_method_mut("open_inventory", container::open_inventory);
m.add_method_mut("set_direction", movement::set_direction);
m.add_method_mut("set_jumping", movement::set_jumping);
m.add_method_mut("sprint", movement::sprint);
m.add_method_mut("start_mining", interaction::start_mining);
m.add_method_mut("walk", movement::walk);
m.add_method("walk", movement::walk);
}
}
@@ -96,7 +101,7 @@ fn chat(_lua: &Lua, client: &Client, message: String) -> Result<()> {
Ok(())
}
fn disconnect(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
fn disconnect(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.disconnect();
Ok(())
}

View File

@@ -1,18 +1,129 @@
use super::{Client, Direction, Vec3};
use azalea::{
BlockPos, BotClientExt, SprintDirection, WalkDirection,
core::hit_result::HitResult,
entity::Position,
interact::HitResultComponent,
pathfinder::{
ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt,
ExecutingPath, Pathfinder, PathfinderClientExt,
goals::{BlockPosGoal, Goal, InverseGoal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
},
protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action},
world::MinecraftEntityId,
};
use log::error;
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
use super::{Client, Direction, Vec3};
#[derive(Debug)]
struct AnyGoal(Box<dyn Goal>);
impl Goal for AnyGoal {
fn success(&self, n: BlockPos) -> bool {
self.0.success(n)
}
fn heuristic(&self, n: BlockPos) -> f32 {
self.0.heuristic(n)
}
}
#[allow(clippy::cast_possible_truncation)]
fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -> Result<AnyGoal> {
let goal: Box<dyn Goal> = match kind {
1 => {
let pos = Vec3::from_lua(data.get("position")?, lua)?;
Box::new(RadiusGoal {
pos: azalea::Vec3::new(pos.x, pos.y, pos.z),
radius: data.get("radius")?,
})
}
2 => {
let distance = data.get("distance").unwrap_or(4.5);
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(ReachBlockPosGoal::new_with_distance(
BlockPos::new(pos.x as i32, pos.y as i32, pos.z as i32),
distance,
client.world().read().chunks.clone(),
))
}
3 => Box::new(XZGoal {
x: data.get("x")?,
z: data.get("z")?,
}),
4 => Box::new(YGoal { y: data.get("y")? }),
_ => {
let pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(BlockPosGoal(BlockPos::new(
pos.x as i32,
pos.y as i32,
pos.z as i32,
)))
}
};
Ok(AnyGoal(if options.get("inverse").unwrap_or_default() {
Box::new(InverseGoal(AnyGoal(goal)))
} else {
goal
}))
}
pub fn go_to_reached(_lua: &Lua, client: &Client) -> Result<bool> {
Ok(client.is_goto_target_reached())
}
pub async fn wait_until_goal_reached(_lua: Lua, client: UserDataRef<Client>, (): ()) -> Result<()> {
client.wait_until_goto_target_reached().await;
Ok(())
}
pub async fn go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
let metadata = metadata.unwrap_or(lua.create_table()?);
let options = metadata.get("options").unwrap_or(lua.create_table()?);
let goal = to_goal(
&lua,
&client,
data,
&options,
metadata.get("type").unwrap_or_default(),
)?;
if options.get("without_mining").unwrap_or_default() {
client.start_goto_without_mining(goal);
client.wait_until_goto_target_reached().await;
} else {
client.goto(goal).await;
}
Ok(())
}
pub async fn start_go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
let metadata = metadata.unwrap_or(lua.create_table()?);
let options = metadata.get("options").unwrap_or(lua.create_table()?);
let goal = to_goal(
&lua,
&client,
data,
&options,
metadata.get("type").unwrap_or_default(),
)?;
if options.get("without_mining").unwrap_or_default() {
client.start_goto_without_mining(goal);
} else {
client.start_goto(goal);
}
let _ = client.get_tick_broadcaster().recv().await;
Ok(())
}
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let direction = client.direction();
Ok(Direction {
@@ -25,96 +136,28 @@ pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(client.eye_position()))
}
pub async fn go_to(
lua: Lua,
client: UserDataRef<Client>,
(data, metadata): (Table, Option<Table>),
) -> Result<()> {
fn goto_with_options<G: Goal + Send + Sync + 'static>(
client: &Client,
options: &Table,
goal: G,
) {
if options.get("without_mining").unwrap_or_default() {
client.goto_without_mining(goal);
} else {
client.goto(goal);
}
}
let table = metadata.unwrap_or(lua.create_table()?);
let goal_type = table.get("type").unwrap_or_default();
let options = table.get("options").unwrap_or(lua.create_table()?);
macro_rules! goto {
($goal:expr) => {
if options.get("inverse").unwrap_or_default() {
goto_with_options(&client, &options, InverseGoal($goal));
} else {
goto_with_options(&client, &options, $goal);
}
};
}
#[allow(clippy::cast_possible_truncation)]
match goal_type {
1 => {
let p = Vec3::from_lua(data.get("position")?, &lua)?;
goto!(RadiusGoal {
pos: azalea::Vec3::new(p.x, p.y, p.z),
radius: data.get("radius")?,
});
}
2 => {
let p = Vec3::from_lua(Value::Table(data), &lua)?;
goto!(ReachBlockPosGoal {
pos: BlockPos::new(p.x as i32, p.y as i32, p.z as i32),
chunk_storage: client.world().read().chunks.clone(),
});
}
3 => {
goto!(XZGoal {
x: data.get("x")?,
z: data.get("z")?,
});
}
4 => goto!(YGoal { y: data.get("y")? }),
_ => {
let p = Vec3::from_lua(Value::Table(data), &lua)?;
goto!(BlockPosGoal(BlockPos::new(
p.x as i32, p.y as i32, p.z as i32
)));
}
}
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
Ok(())
}
pub fn jump(_lua: &Lua, client: &mut Client, _: ()) -> Result<()> {
pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.jump();
Ok(())
}
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
let result = client.component::<HitResultComponent>();
Ok(if result.miss {
None
} else {
let table = lua.create_table()?;
table.set("position", Vec3::from(result.block_pos))?;
table.set("inside", result.inside)?;
table.set("world_border", result.world_border)?;
Some(table)
})
Ok(
if let HitResult::Block(ref result) = *client.component::<HitResultComponent>() {
let table = lua.create_table()?;
table.set("direction", Vec3::from(result.direction.normal()))?;
table.set("inside", result.inside)?;
table.set("location", Vec3::from(result.location))?;
table.set("position", Vec3::from(result.block_pos))?;
table.set("world_border", result.world_border)?;
Some(table)
} else {
None
},
)
}
pub fn look_at(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<()> {
pub fn look_at(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
client.look_at(azalea::Vec3::new(position.x, position.y, position.z));
Ok(())
}
@@ -149,12 +192,12 @@ pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(&client.component::<Position>()))
}
pub fn set_direction(_lua: &Lua, client: &mut Client, direction: Direction) -> Result<()> {
pub fn set_direction(_lua: &Lua, client: &Client, direction: Direction) -> Result<()> {
client.set_direction(direction.y, direction.x);
Ok(())
}
pub fn set_jumping(_lua: &Lua, client: &mut Client, jumping: bool) -> Result<()> {
pub fn set_jumping(_lua: &Lua, client: &Client, jumping: bool) -> Result<()> {
client.set_jumping(jumping);
Ok(())
}
@@ -169,7 +212,7 @@ pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<(
}
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: if sneaking {
Action::PressShiftKey
@@ -177,13 +220,11 @@ pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
Action::ReleaseShiftKey
},
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
});
Ok(())
}
pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
pub fn sprint(_lua: &Lua, client: &Client, direction: u8) -> Result<()> {
client.sprint(match direction {
5 => SprintDirection::ForwardRight,
6 => SprintDirection::ForwardLeft,
@@ -192,23 +233,21 @@ pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
Ok(())
}
pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.stop_pathfinding();
Ok(())
}
pub fn stop_sleeping(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
pub fn stop_sleeping(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: Action::StopSleeping,
data: 0,
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
});
Ok(())
}
pub fn walk(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
pub fn walk(_lua: &Lua, client: &Client, direction: u8) -> Result<()> {
client.walk(match direction {
1 => WalkDirection::Forward,
2 => WalkDirection::Backward,

View File

@@ -1,14 +1,14 @@
use crate::hacks::anti_knockback::AntiKnockback;
use super::Client;
use azalea::{
ClientInformation,
entity::metadata::{AirSupply, Score},
pathfinder::PathfinderDebugParticles,
pathfinder::debug::PathfinderDebugParticles,
protocol::common::client_information::ModelCustomization,
};
use mlua::{Error, Lua, Result, Table, UserDataRef};
use super::Client;
use crate::hacks::anti_knockback::AntiKnockback;
pub fn air_supply(_lua: &Lua, client: &Client) -> Result<i32> {
Ok(client.component::<AirSupply>().0)
}
@@ -40,21 +40,22 @@ pub async fn set_client_information(
allows_listing: info.get("allows_listing")?,
model_customization: info
.get::<Table>("model_customization")
.as_ref()
.map(|t| ModelCustomization {
cape: get_bool(&t, "cape"),
jacket: get_bool(&t, "jacket"),
left_sleeve: get_bool(&t, "left_sleeve"),
right_sleeve: get_bool(&t, "right_sleeve"),
left_pants: get_bool(&t, "left_pants"),
right_pants: get_bool(&t, "right_pants"),
hat: get_bool(&t, "hat"),
cape: get_bool(t, "cape"),
jacket: get_bool(t, "jacket"),
left_sleeve: get_bool(t, "left_sleeve"),
right_sleeve: get_bool(t, "right_sleeve"),
left_pants: get_bool(t, "left_pants"),
right_pants: get_bool(t, "right_pants"),
hat: get_bool(t, "hat"),
})
.unwrap_or_default(),
view_distance: info.get("view_distance").unwrap_or(8),
..ClientInformation::default()
})
.await
.map_err(Error::external)
.await;
Ok(())
}
pub fn set_component(

View File

@@ -1,4 +1,3 @@
use super::{Client, Direction, Vec3};
use azalea::{
BlockPos,
blocks::{BlockState, BlockStates},
@@ -11,6 +10,8 @@ use azalea::{
};
use mlua::{Function, Lua, Result, Table, UserDataRef};
use super::{Client, Direction, Vec3};
pub fn blocks(
_lua: &Lua,
client: &Client,
@@ -27,7 +28,10 @@ pub fn blocks(
nearest_to.z as i32,
),
&BlockStates {
set: block_states.iter().map(|&id| BlockState { id }).collect(),
set: block_states
.into_iter()
.flat_map(BlockState::try_from)
.collect(),
},
)
.map(Vec3::from)

View File

@@ -2,16 +2,20 @@
mod queries;
pub mod find;
use super::{Client, Direction, Vec3};
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
use mlua::{Lua, Result, Table};
use mlua::{Lua, Result, Table, Value};
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Table> {
let result = client.best_tool_in_hotbar_for_block(BlockState { id: block_state });
use super::{Client, Direction, Vec3};
pub fn best_tool_for_block(lua: &Lua, client: &Client, block_state: u16) -> Result<Value> {
let Ok(block) = BlockState::try_from(block_state) else {
return Ok(Value::Nil);
};
let result = client.best_tool_in_hotbar_for_block(block);
let table = lua.create_table()?;
table.set("index", result.index)?;
table.set("percentage_per_tick", result.percentage_per_tick)?;
Ok(table)
Ok(Value::Table(table))
}
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
@@ -28,24 +32,23 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
position.y as i32,
position.z as i32,
))
.map(|b| b.id))
.map(|block| block.id()))
}
#[allow(clippy::cast_possible_truncation)]
pub fn get_fluid_state(lua: &Lua, client: &Client, position: Vec3) -> Result<Option<Table>> {
#[allow(clippy::cast_possible_truncation)]
Ok(
if let Some(state) = client.world().read().get_fluid_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
)) {
let table = lua.create_table()?;
table.set("kind", state.kind as u8)?;
table.set("amount", state.amount)?;
table.set("falling", state.falling)?;
Some(table)
} else {
None
},
)
let fluid_state = client.world().read().get_fluid_state(&BlockPos::new(
position.x as i32,
position.y as i32,
position.z as i32,
));
Ok(if let Some(state) = fluid_state {
let table = lua.create_table()?;
table.set("kind", state.kind as u8)?;
table.set("amount", state.amount)?;
table.set("falling", state.falling)?;
Some(table)
} else {
None
})
}

View File

@@ -1,6 +1,6 @@
use azalea::inventory::{
self,
components::{CustomName, Damage, Food, MaxDamage},
components::{Consumable, CustomName, Damage, Food, MaxDamage},
};
use mlua::{UserData, UserDataFields, UserDataMethods};
@@ -32,6 +32,24 @@ impl UserData for ItemStack {
.map(|data| data.components.get::<MaxDamage>().map(|d| d.amount)))
});
f.add_field_method_get("consumable", |lua, this| {
Ok(
if let Some(consumable) = this
.0
.as_present()
.and_then(|data| data.components.get::<Consumable>())
{
let table = lua.create_table()?;
table.set("animation", consumable.animation as u8)?;
table.set("consume_seconds", consumable.consume_seconds)?;
table.set("has_consume_particles", consumable.has_consume_particles)?;
Some(table)
} else {
None
},
)
});
f.add_field_method_get("food", |lua, this| {
Ok(
if let Some(food) = this
@@ -53,9 +71,7 @@ impl UserData for ItemStack {
}
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_method_mut("split", |_, this, count: u32| {
Ok(ItemStack(this.0.split(count)))
});
m.add_method_mut("split", |_, this, count: u32| Ok(Self(this.0.split(count))));
m.add_method_mut("update_empty", |_, this, (): ()| {
this.0.update_empty();
Ok(())

View File

@@ -1,7 +1,9 @@
use crate::ListenerMap;
use std::time::{SystemTime, UNIX_EPOCH};
use futures::executor::block_on;
use mlua::{Function, Lua, Result, Table};
use std::time::{SystemTime, UNIX_EPOCH};
use crate::ListenerMap;
pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> {
let m = event_listeners.clone();
@@ -11,13 +13,15 @@ pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap
move |_, (event_type, callback, optional_id): (String, Function, Option<String>)| {
let m = m.clone();
let id = optional_id.unwrap_or_else(|| {
callback.info().name.unwrap_or(format!(
"anonymous @ {}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis()
))
callback.info().name.unwrap_or_else(|| {
format!(
"anonymous @ {}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis()
)
})
});
tokio::spawn(async move {
m.write()
@@ -38,12 +42,10 @@ pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap
let m = m.clone();
tokio::spawn(async move {
let mut m = m.write().await;
let empty = if let Some(listeners) = m.get_mut(&event_type) {
let empty = m.get_mut(&event_type).is_some_and(|listeners| {
listeners.retain(|(id, _)| target_id != *id);
listeners.is_empty()
} else {
false
};
});
if empty {
m.remove(&event_type);
}

View File

@@ -1,10 +1,12 @@
use super::room::Room;
use std::sync::Arc;
use matrix_sdk::{
Client as MatrixClient,
ruma::{RoomId, UserId},
};
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
use std::sync::Arc;
use super::room::Room;
pub struct Client(pub Arc<MatrixClient>);

View File

@@ -1,4 +1,3 @@
use super::member::Member;
use matrix_sdk::{
RoomMemberships,
room::Room as MatrixRoom,
@@ -6,6 +5,8 @@ use matrix_sdk::{
};
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
use super::member::Member;
pub struct Room(pub MatrixRoom);
impl UserData for Room {
@@ -36,12 +37,7 @@ impl UserData for Room {
.members(RoomMemberships::all())
.await
.map_err(Error::external)
.map(|members| {
members
.into_iter()
.map(|member| Member(member.clone()))
.collect::<Vec<_>>()
})
.map(|members| members.into_iter().map(Member).collect::<Vec<_>>())
});
m.add_async_method(
"kick_user",
@@ -79,5 +75,15 @@ impl UserData for Room {
.map_err(Error::external)
.map(|response| response.event_id.to_string())
});
m.add_async_method(
"send_html",
async |_, this, (body, html_body): (String, String)| {
this.0
.send(RoomMessageEventContent::text_html(body, html_body))
.await
.map_err(Error::external)
.map(|response| response.event_id.to_string())
},
);
}
}

View File

@@ -13,13 +13,15 @@ pub mod vec3;
#[cfg(feature = "matrix")]
pub mod matrix;
use crate::{ListenerMap, build_info::built};
use mlua::{Lua, Table};
use std::{
fmt::{self, Display, Formatter},
io,
};
use mlua::{Lua, Table};
use crate::{ListenerMap, build_info::built};
#[derive(Debug)]
pub enum Error {
CreateEnv(mlua::Error),
@@ -36,12 +38,12 @@ impl Display for Error {
formatter,
"failed to {}",
match self {
Error::CreateEnv(error) => format!("create environment: {error}"),
Error::EvalChunk(error) => format!("evaluate chunk: {error}"),
Error::ExecChunk(error) => format!("execute chunk: {error}"),
Error::LoadChunk(error) => format!("load chunk: {error}"),
Error::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"),
Error::ReadFile(error) => format!("read script file: {error}"),
Self::CreateEnv(error) => format!("create environment: {error}"),
Self::EvalChunk(error) => format!("evaluate chunk: {error}"),
Self::ExecChunk(error) => format!("execute chunk: {error}"),
Self::LoadChunk(error) => format!("load chunk: {error}"),
Self::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"),
Self::ReadFile(error) => format!("read script file: {error}"),
}
)
}

View File

@@ -1,11 +1,12 @@
use log::error;
use mlua::{Lua, Result, Table};
use std::{
ffi::OsString,
process::{Command, Stdio},
thread,
};
use log::error;
use mlua::{Lua, Result, Table};
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"system",

View File

@@ -1,5 +1,6 @@
use mlua::{Error, Function, Lua, Result, Table};
use std::time::Duration;
use mlua::{Error, Function, Lua, Result, Table};
use tokio::time::{sleep, timeout};
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {

View File

@@ -40,7 +40,7 @@ impl From<&Position> for Vec3 {
impl From<BlockPos> for Vec3 {
fn from(p: BlockPos) -> Self {
Vec3 {
Self {
x: f64::from(p.x),
y: f64::from(p.y),
z: f64::from(p.z),

View File

@@ -1,4 +1,6 @@
#![feature(if_let_guard, let_chains)]
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::significant_drop_tightening)]
mod arguments;
mod build_info;
@@ -13,7 +15,14 @@ mod replay;
#[cfg(feature = "matrix")]
mod matrix;
use anyhow::Context;
use std::{
collections::HashMap,
env,
fs::{OpenOptions, read_to_string},
sync::Arc,
};
use anyhow::{Context, Result};
use arguments::Arguments;
use azalea::{
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
@@ -31,12 +40,6 @@ use hacks::HacksPlugin;
use log::debug;
use mlua::{Function, Lua, Table};
use replay::{plugin::RecordPlugin, recorder::Recorder};
use std::{
collections::HashMap,
env,
fs::{OpenOptions, read_to_string},
sync::Arc,
};
#[cfg(feature = "mimalloc")]
#[global_allocator]
@@ -45,14 +48,14 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
type ListenerMap = Arc<RwLock<HashMap<String, Vec<(String, Function)>>>>;
#[derive(Default, Clone, Component)]
pub struct State {
struct State {
lua: Arc<Lua>,
event_listeners: ListenerMap,
commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> Result<()> {
#[cfg(feature = "console-subscriber")]
console_subscriber::init();
@@ -128,8 +131,7 @@ async fn main() -> anyhow::Result<()> {
} else {
Account::offline(&username)
};
let Err(error) = ClientBuilder::new_without_plugins()
let Err(err) = ClientBuilder::new_without_plugins()
.add_plugins(DefaultBotPlugins)
.add_plugins(HacksPlugin)
.add_plugins(default_plugins)
@@ -142,7 +144,7 @@ async fn main() -> anyhow::Result<()> {
})
.start(account, server)
.await;
eprintln!("{error}");
eprintln!("{err}");
Ok(())
}

View File

@@ -1,8 +1,5 @@
use super::Context;
use crate::{
events::call_listeners,
lua::{eval, exec, matrix::room::Room as LuaRoom, reload},
};
use std::time::Duration;
use anyhow::Result;
use log::{debug, error};
use matrix_sdk::{
@@ -13,9 +10,14 @@ use matrix_sdk::{
message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent},
},
};
use std::time::Duration;
use tokio::time::sleep;
use super::Context;
use crate::{
events::call_listeners,
lua::{eval, exec, matrix::room::Room as LuaRoom, reload},
};
pub async fn on_regular_room_message(
event: OriginalSyncRoomMessageEvent,
room: Room,
@@ -28,15 +30,7 @@ pub async fn on_regular_room_message(
return Ok(());
};
if ctx
.state
.lua
.globals()
.get::<Vec<String>>("MatrixOwners")
.unwrap_or_default()
.contains(&event.sender.to_string())
&& text_content.body.starts_with(&ctx.name)
{
if text_content.body.starts_with(&ctx.name) && ctx.is_owner(&event.sender.to_string()) {
let body = text_content.body[ctx.name.len()..]
.trim_start_matches(':')
.trim();
@@ -101,20 +95,11 @@ pub async fn on_stripped_state_member(
) -> Result<()> {
if let Some(user_id) = client.user_id()
&& member.state_key == user_id
&& ctx
.state
.lua
.globals()
.get::<Vec<String>>("MatrixOwners")
.unwrap_or_default()
.contains(&member.sender.to_string())
&& ctx.is_owner(&member.sender.to_string())
{
debug!("joining room {}", room.room_id());
while let Err(error) = room.join().await {
error!(
"failed to join room {}: {error:?}, retrying...",
room.room_id()
);
error!("failed to join room {}: {error:?}", room.room_id());
sleep(Duration::from_secs(10)).await;
}
debug!("successfully joined room {}", room.room_id());

View File

@@ -1,7 +1,8 @@
mod bot;
mod verification;
use crate::{State, lua::matrix::client::Client as LuaClient};
use std::{path::Path, sync::Arc, time::Duration};
use anyhow::{Context as _, Result};
use bot::{on_regular_room_message, on_stripped_state_member};
use log::{error, warn};
@@ -10,16 +11,34 @@ use matrix_sdk::{
};
use mlua::Table;
use serde::{Deserialize, Serialize};
use std::{path::Path, sync::Arc};
use tokio::fs;
use verification::{on_device_key_verification_request, on_room_message_verification_request};
use crate::{State, events::call_listeners, lua::matrix::client::Client as LuaClient};
#[derive(Clone)]
pub struct Context {
struct Context {
state: State,
name: String,
}
impl Context {
fn is_owner(&self, name: &String) -> bool {
self.state
.lua
.globals()
.get::<Table>("MatrixOptions")
.ok()
.and_then(|options| {
options
.get::<Vec<String>>("owners")
.ok()
.and_then(|owners| owners.contains(name).then_some(()))
})
.is_some()
}
}
#[derive(Clone, Serialize, Deserialize)]
struct Session {
#[serde(skip_serializing_if = "Option::is_none")]
@@ -37,14 +56,13 @@ async fn persist_sync_token(
Ok(())
}
pub async fn login(
homeserver_url: String,
username: String,
password: &str,
state: State,
globals: Table,
name: String,
) -> Result<()> {
pub async fn login(state: &State, options: &Table, globals: &Table, name: String) -> Result<()> {
let (homeserver_url, username, password, sync_timeout) = (
options.get::<String>("homeserver_url")?,
options.get::<String>("username")?,
&options.get::<String>("password")?,
options.get::<u64>("sync_timeout"),
);
let root_dir = dirs::data_dir()
.context("no data directory")?
.join("errornowatcher")
@@ -61,8 +79,12 @@ pub async fn login(
}
let client = builder.build().await?;
let mut sync_settings = SyncSettings::new();
if let Ok(seconds) = sync_timeout {
sync_settings = sync_settings.timeout(Duration::from_secs(seconds));
}
let mut new_session;
let mut sync_settings = SyncSettings::default();
let session_file = root_dir.join("session.json");
if let Some(session) = fs::read_to_string(&session_file)
.await
@@ -88,7 +110,10 @@ pub async fn login(
fs::write(&session_file, serde_json::to_string(&new_session)?).await?;
}
client.add_event_handler_context(Context { state, name });
client.add_event_handler_context(Context {
state: state.to_owned(),
name,
});
client.add_event_handler(on_stripped_state_member);
loop {
match client.sync_once(sync_settings.clone()).await {
@@ -109,6 +134,7 @@ pub async fn login(
let client = Arc::new(client);
globals.set("matrix", LuaClient(client.clone()))?;
call_listeners(state, "matrix_init", || Ok(())).await?;
client
.sync_with_result_callback(sync_settings, |sync_result| async {

View File

@@ -1,3 +1,5 @@
use std::time::Duration;
use anyhow::{Context, Result};
use futures::StreamExt;
use log::{error, info, warn};
@@ -15,7 +17,6 @@ use matrix_sdk::{
},
},
};
use std::time::Duration;
use tokio::time::sleep;
async fn confirm_emojis(sas: SasVerification, emoji: [Emoji; 7]) {

View File

@@ -1,7 +1,7 @@
use azalea::{entity::particle::Particle, registry::ParticleKind};
#[allow(clippy::too_many_lines)]
pub fn to_kind(particle: &Particle) -> ParticleKind {
pub const fn to_kind(particle: &Particle) -> ParticleKind {
match particle {
Particle::AngryVillager => ParticleKind::AngryVillager,
Particle::Block(_) => ParticleKind::Block,
@@ -34,6 +34,7 @@ pub fn to_kind(particle: &Particle) -> ParticleKind {
Particle::Flame => ParticleKind::Flame,
Particle::CherryLeaves => ParticleKind::CherryLeaves,
Particle::PaleOakLeaves => ParticleKind::PaleOakLeaves,
Particle::TintedLeaves => ParticleKind::TintedLeaves,
Particle::SculkSoul => ParticleKind::SculkSoul,
Particle::SculkCharge(_) => ParticleKind::SculkCharge,
Particle::SculkChargePop => ParticleKind::SculkChargePop,
@@ -115,5 +116,6 @@ pub fn to_kind(particle: &Particle) -> ParticleKind {
Particle::TrialOmen => ParticleKind::TrialOmen,
Particle::Trail => ParticleKind::Trail,
Particle::BlockCrumble => ParticleKind::BlockCrumble,
Particle::Firefly => ParticleKind::Firefly,
}
}

View File

@@ -1,40 +1,41 @@
#![allow(clippy::needless_pass_by_value)]
use super::recorder::Recorder;
use std::sync::Arc;
use azalea::{
ecs::{event::EventReader, system::Query},
packet_handling::{
configuration::ConfigurationEvent,
game::send_packet_events,
login::{LoginPacketEvent, process_packet_events},
ecs::event::EventReader,
packet::{
config::ReceiveConfigPacketEvent, game::ReceiveGamePacketEvent,
login::ReceiveLoginPacketEvent,
},
protocol::packets::login::ClientboundLoginPacket,
raw_connection::RawConnection,
};
use bevy_app::{First, Plugin};
use bevy_ecs::{schedule::IntoSystemConfigs, system::ResMut};
use bevy_app::{App, First, Plugin};
use bevy_ecs::system::ResMut;
use log::error;
use parking_lot::Mutex;
use std::sync::Arc;
use super::recorder::Recorder;
pub struct RecordPlugin {
pub recorder: Arc<Mutex<Option<Recorder>>>,
}
impl Plugin for RecordPlugin {
fn build(&self, app: &mut bevy_app::App) {
if let Some(recorder) = self.recorder.lock().take() {
fn build(&self, app: &mut App) {
let recorder = self.recorder.lock().take();
if let Some(recorder) = recorder {
app.insert_resource(recorder)
.add_systems(First, record_login_packets.before(process_packet_events))
.add_systems(First, record_login_packets)
.add_systems(First, record_configuration_packets)
.add_systems(First, record_game_packets.before(send_packet_events));
.add_systems(First, record_game_packets);
}
}
}
fn record_login_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<LoginPacketEvent>,
mut events: EventReader<ReceiveLoginPacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
@@ -53,24 +54,24 @@ fn record_login_packets(
fn record_configuration_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<ConfigurationEvent>,
mut events: EventReader<ReceiveConfigPacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
if let Err(error) = recorder.save_packet(&event.packet) {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
error!("failed to record configuration packet: {error:?}");
}
}
}
}
fn record_game_packets(recorder: Option<ResMut<Recorder>>, query: Query<&RawConnection>) {
if let Some(mut recorder) = recorder
&& let Ok(raw_conn) = query.get_single()
{
let queue = raw_conn.incoming_packet_queue();
for raw_packet in queue.lock().iter() {
if let Err(error) = recorder.save_raw_packet(raw_packet) {
fn record_game_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<ReceiveGamePacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
error!("failed to record game packet: {error:?}");
}
}

View File

@@ -1,4 +1,9 @@
use crate::build_info;
use std::{
fs::File,
io::{BufWriter, Write},
time::{Instant, SystemTime, UNIX_EPOCH},
};
use anyhow::Result;
use azalea::{
buf::AzaleaWriteVar,
@@ -7,13 +12,10 @@ use azalea::{
};
use log::debug;
use serde_json::json;
use std::{
fs::File,
io::{BufWriter, Write},
time::{Instant, SystemTime, UNIX_EPOCH},
};
use zip::{ZipWriter, write::SimpleFileOptions};
use crate::build_info;
#[derive(Resource)]
pub struct Recorder {
zip_writer: BufWriter<ZipWriter<File>>,
@@ -56,7 +58,7 @@ impl Recorder {
"fileFormat": "MCPR",
"fileFormatVersion": 14,
"protocol": PROTOCOL_VERSION,
"generator": format!("errornowatcher {}", build_info::version_formatted()),
"generator": format!("ErrorNoWatcher {}", build_info::version_formatted()),
})
.to_string()
.as_bytes(),