Compare commits

...

19 Commits

Author SHA1 Message Date
2cf4265732 refactor(commands): directly use native encryption function 2025-04-29 08:11:52 -04:00
ca8c9d4d1c style(events): allow clippy::cognitive_complexity for handler 2025-04-28 21:01:16 -04:00
64c96450a4 perf(commands): move ncr data to binding 2025-04-28 20:49:14 -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
24 changed files with 771 additions and 424 deletions

764
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -26,7 +26,7 @@ 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"
@@ -41,7 +41,7 @@ ncr = { version = "0", features = ["cfb8", "ecb", "gcm"] }
parking_lot = "0"
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

@@ -61,8 +61,6 @@ function update_listeners()
message = function()
info("bot successfully logged in!")
end,
},
spawn = {
eat = function()
sleep(5000)
check_food()

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

@@ -1,11 +1,15 @@
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
use futures::lock::Mutex;
use mlua::{Function, Table};
use ncr::utils::prepend_header;
use mlua::{Error, Result, Table, UserDataRef};
use ncr::{
encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding},
encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
utils::prepend_header,
};
use crate::{
State,
lua::{eval, exec, reload},
State, crypt,
lua::{eval, exec, nochatreports::key::AesKey, reload},
};
pub type Ctx = CommandContext<Mutex<CommandSource>>;
@@ -19,21 +23,26 @@ pub struct CommandSource {
impl CommandSource {
pub fn reply(&self, message: &str) {
fn encrypt(options: &Table, plaintext: &str) -> Result<String> {
Ok(crypt!(encrypt, options, &prepend_header(plaintext)))
}
for mut chunk in message
.chars()
.collect::<Vec<char>>()
.chunks(if self.ncr_options.is_some() { 150 } else { 236 })
.map(|chars| chars.iter().collect::<String>())
{
if let Some(options) = &self.ncr_options
&& let Ok(encrypt) = self.state.lua.globals().get::<Function>("ncr_encrypt")
&& let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
if let Some(ciphertext) = self
.ncr_options
.as_ref()
.and_then(|options| encrypt(options, &chunk).ok())
{
chunk = ciphertext;
}
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 {
@@ -50,7 +59,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")),
);
});
@@ -64,7 +73,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()),
);
@@ -80,7 +89,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

@@ -11,9 +11,9 @@ use log::{debug, error, info, trace};
use mlua::{Error, Function, IntoLuaMulti, Table};
use ncr::utils::trim_header;
use tokio::net::TcpListener;
#[cfg(feature = "matrix")]
use crate::matrix;
use {crate::matrix, std::time::Duration, tokio::time::sleep};
use crate::{
State,
commands::CommandSource,
@@ -23,7 +23,7 @@ use crate::{
replay::recorder::Recorder,
};
#[allow(clippy::too_many_lines)]
#[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) => {
@@ -32,7 +32,7 @@ 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 ansi_text = text.to_ansi();
@@ -284,8 +284,12 @@ fn matrix_init(client: &Client, state: State) {
if let Ok(options) = globals.get::<Table>("MatrixOptions") {
let name = client.username();
tokio::spawn(async move {
if let Err(error) = matrix::login(state, options, 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

@@ -49,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

@@ -108,7 +108,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))
}

View File

@@ -8,12 +8,12 @@ use mlua::{Lua, Result, UserDataRef};
use super::{Client, Vec3};
pub fn attack(_lua: &Lua, client: &mut Client, entity_id: i32) -> Result<()> {
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,
@@ -45,7 +45,7 @@ pub fn set_mining(_lua: &Lua, client: &Client, mining: bool) -> Result<()> {
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,

View File

@@ -41,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);
@@ -63,33 +64,38 @@ impl UserData for Client {
m.add_async_method("find_entities", world::find::entities);
m.add_async_method("find_players", world::find::players);
m.add_async_method("go_to", movement::go_to);
m.add_async_method(
"go_to_wait_until_reached",
movement::go_to_wait_until_reached,
);
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_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("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);
}
}
@@ -98,7 +104,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

@@ -14,6 +14,122 @@ 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 pos = Vec3::from_lua(Value::Table(data), lua)?;
Box::new(ReachBlockPosGoal {
pos: BlockPos::new(pos.x as i32, pos.y as i32, pos.z as i32),
chunk_storage: 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 go_to_wait_until_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);
}
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
Ok(())
}
pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let direction = client.direction();
Ok(Direction {
@@ -26,78 +142,7 @@ 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(())
}
@@ -115,7 +160,7 @@ pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
})
}
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(())
}
@@ -150,12 +195,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(())
}
@@ -184,7 +229,7 @@ pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
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,
@@ -193,12 +238,12 @@ 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<()> {
pub fn stop_sleeping(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: Action::StopSleeping,
@@ -209,7 +254,7 @@ pub fn stop_sleeping(_lua: &Lua, client: &Client, _: ()) -> Result<()> {
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

@@ -32,21 +32,20 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
.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

@@ -71,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

@@ -13,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()
@@ -40,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

@@ -37,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",

View File

@@ -38,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

@@ -3,9 +3,8 @@ macro_rules! crypt {
($op:ident, $options:expr, $text:expr) => {{
macro_rules! crypt_with {
($algo:ident) => {{
let encoding = $options.get("encoding").unwrap_or_default();
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
match encoding {
match $options.get("encoding").unwrap_or_default() {
1 => $algo::<Base64Encoding>::$op($text, &key),
2 => $algo::<Base64rEncoding>::$op($text, &key),
_ => $algo::<NewBase64rEncoding>::$op($text, &key),

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;
@@ -46,7 +48,7 @@ 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>>>,

View File

@@ -99,10 +99,7 @@ pub async fn on_stripped_state_member(
{
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,7 @@
mod bot;
mod verification;
use std::{path::Path, sync::Arc};
use std::{path::Path, sync::Arc, time::Duration};
use anyhow::{Context as _, Result};
use bot::{on_regular_room_message, on_stripped_state_member};
@@ -14,10 +14,10 @@ use serde::{Deserialize, Serialize};
use tokio::fs;
use verification::{on_device_key_verification_request, on_room_message_verification_request};
use crate::{State, lua::matrix::client::Client as LuaClient};
use crate::{State, events::call_listeners, lua::matrix::client::Client as LuaClient};
#[derive(Clone)]
pub struct Context {
struct Context {
state: State,
name: String,
}
@@ -56,13 +56,13 @@ async fn persist_sync_token(
Ok(())
}
pub async fn login(state: State, options: Table, globals: Table, name: String) -> Result<()> {
let (homeserver_url, username, password) = (
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")
@@ -79,8 +79,12 @@ pub async fn login(state: State, options: Table, globals: Table, name: String) -
}
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
@@ -106,7 +110,10 @@ pub async fn login(state: State, options: Table, globals: Table, name: String) -
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 {
@@ -127,6 +134,7 @@ pub async fn login(state: State, options: Table, globals: Table, name: String) -
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,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

@@ -12,7 +12,7 @@ use azalea::{
protocol::packets::login::ClientboundLoginPacket,
raw_connection::RawConnection,
};
use bevy_app::{First, Plugin};
use bevy_app::{App, First, Plugin};
use bevy_ecs::{schedule::IntoSystemConfigs, system::ResMut};
use log::error;
use parking_lot::Mutex;
@@ -24,8 +24,9 @@ pub struct RecordPlugin {
}
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_configuration_packets)