Compare commits

..

3 Commits

16 changed files with 581 additions and 670 deletions

1006
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -30,7 +30,7 @@ function auto_fish()
sleep(3000)
end
hold_fishing_rod()
client:start_use_item()
client: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:start_use_item()
client:use_item()
end
end
end, "auto-fish")
@@ -54,11 +54,11 @@ function auto_fish()
if os.time() - FishLastCaught >= 60 then
hold_fishing_rod()
client:start_use_item()
client:use_item()
end
end, "auto-fish_watchdog")
client:start_use_item()
client: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:start_use_item()
client:use_item()
end
end
@@ -131,6 +131,6 @@ function check_food(hunger)
sleep(1000)
LastEaten = current_time
end
client:start_use_item()
client:use_item()
end
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,15 +23,20 @@ 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;
}

View File

@@ -35,7 +35,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
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}");
@@ -87,7 +86,6 @@ 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)?;
@@ -103,7 +101,6 @@ 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)?;
@@ -120,7 +117,6 @@ 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
@@ -129,6 +125,7 @@ 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
}
@@ -204,12 +201,6 @@ 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");
@@ -221,6 +212,9 @@ 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?;

View File

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

View File

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

View File

@@ -4,6 +4,7 @@ use azalea::{
prelude::ContainerClientExt,
protocol::packets::game::ServerboundSetCarriedItem,
};
use log::error;
use mlua::{Lua, Result, UserDataRef, Value};
use super::{Client, Container, ContainerRef, ItemStack, Vec3};
@@ -125,8 +126,11 @@ pub fn set_held_slot(_lua: &Lua, client: &Client, slot: u8) -> Result<()> {
inventory.selected_hotbar_slot = slot;
};
client.write_packet(ServerboundSetCarriedItem {
if let Err(error) = client.write_packet(ServerboundSetCarriedItem {
slot: u16::from(slot),
});
}) {
error!("failed to send SetCarriedItem packet: {error:?}");
}
Ok(())
}

View File

@@ -1,7 +1,9 @@
use azalea::{
BlockPos, BotClientExt, interact::StartUseItemEvent,
protocol::packets::game::s_interact::InteractionHand, world::MinecraftEntityId,
BlockPos, BotClientExt,
protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand},
world::MinecraftEntityId,
};
use log::error;
use mlua::{Lua, Result, UserDataRef};
use super::{Client, Vec3};
@@ -38,8 +40,8 @@ pub async fn mine(_lua: Lua, client: UserDataRef<Client>, position: Vec3) -> Res
Ok(())
}
pub fn set_mining(_lua: &Lua, client: &Client, state: bool) -> Result<()> {
client.left_click_mine(state);
pub fn set_mining(_lua: &Lua, client: &Client, mining: bool) -> Result<()> {
client.left_click_mine(mining);
Ok(())
}
@@ -53,14 +55,18 @@ pub fn start_mining(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
Ok(())
}
pub fn start_use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
client.ecs.lock().send_event(StartUseItemEvent {
entity: client.entity,
pub fn use_item(_lua: &Lua, client: &Client, hand: Option<u8>) -> Result<()> {
let direction = client.direction();
if let Err(error) = client.write_packet(ServerboundUseItem {
hand: match hand {
Some(1) => InteractionHand::OffHand,
_ => InteractionHand::MainHand,
},
force_block: None,
});
sequence: 0,
yaw: direction.0,
pitch: direction.1,
}) {
error!("failed to send UseItem packet: {error:?}");
}
Ok(())
}

View File

@@ -64,11 +64,14 @@ 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_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);
@@ -89,9 +92,9 @@ impl UserData for Client {
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("walk", movement::walk);
}
}

View File

@@ -1,15 +1,15 @@
use azalea::{
BlockPos, BotClientExt, SprintDirection, WalkDirection,
core::hit_result::HitResult,
entity::Position,
interact::HitResultComponent,
pathfinder::{
ExecutingPath, Pathfinder, PathfinderClientExt,
ExecutingPath, GotoEvent, 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};
@@ -38,13 +38,11 @@ fn to_goal(lua: &Lua, client: &Client, data: Table, options: &Table, kind: u8) -
})
}
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(),
))
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")?,
@@ -72,7 +70,11 @@ 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<()> {
pub async fn go_to_wait_until_reached(
_lua: Lua,
client: UserDataRef<Client>,
(): (),
) -> Result<()> {
client.wait_until_goto_target_reached().await;
Ok(())
}
@@ -119,7 +121,11 @@ pub async fn start_go_to(
} else {
client.start_goto(goal);
}
let _ = client.get_tick_broadcaster().recv().await;
while client.get_tick_broadcaster().recv().await.is_ok() {
if client.ecs.lock().get::<GotoEvent>(client.entity).is_none() {
break;
}
}
Ok(())
}
@@ -142,19 +148,16 @@ pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
}
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
Ok(
if let HitResult::Block(ref result) = *client.component::<HitResultComponent>() {
let result = client.component::<HitResultComponent>();
Ok(if result.miss {
None
} else {
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("inside", result.inside)?;
table.set("world_border", result.world_border)?;
Some(table)
} else {
None
},
)
})
}
pub fn look_at(_lua: &Lua, client: &Client, position: Vec3) -> Result<()> {
@@ -212,7 +215,7 @@ pub fn set_position(_lua: &Lua, client: &Client, new_position: Vec3) -> Result<(
}
pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
client.write_packet(ServerboundPlayerCommand {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: if sneaking {
Action::PressShiftKey
@@ -220,7 +223,9 @@ pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
Action::ReleaseShiftKey
},
data: 0,
});
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
Ok(())
}
@@ -239,11 +244,13 @@ pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
}
pub fn stop_sleeping(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.write_packet(ServerboundPlayerCommand {
if let Err(error) = client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(),
action: Action::StopSleeping,
data: 0,
});
}) {
error!("failed to send PlayerCommand packet: {error:?}");
}
Ok(())
}

View File

@@ -1,7 +1,7 @@
use azalea::{
ClientInformation,
entity::metadata::{AirSupply, Score},
pathfinder::debug::PathfinderDebugParticles,
pathfinder::PathfinderDebugParticles,
protocol::common::client_information::ModelCustomization,
};
use mlua::{Error, Lua, Result, Table, UserDataRef};
@@ -40,22 +40,21 @@ 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;
Ok(())
.await
.map_err(Error::external)
}
pub fn set_component(

View File

@@ -28,10 +28,7 @@ pub fn blocks(
nearest_to.z as i32,
),
&BlockStates {
set: block_states
.into_iter()
.flat_map(BlockState::try_from)
.collect(),
set: block_states.iter().map(|&id| BlockState { id }).collect(),
},
)
.map(Vec3::from)

View File

@@ -3,19 +3,16 @@ mod queries;
pub mod find;
use azalea::{BlockPos, auto_tool::AutoToolClientExt, blocks::BlockState, world::InstanceName};
use mlua::{Lua, Result, Table, Value};
use mlua::{Lua, Result, Table};
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);
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 });
let table = lua.create_table()?;
table.set("index", result.index)?;
table.set("percentage_per_tick", result.percentage_per_tick)?;
Ok(Value::Table(table))
Ok(table)
}
pub fn dimension(_lua: &Lua, client: &Client) -> Result<String> {
@@ -32,7 +29,7 @@ pub fn get_block_state(_lua: &Lua, client: &Client, position: Vec3) -> Result<Op
position.y as i32,
position.z as i32,
))
.map(|block| block.id()))
.map(|block| block.id))
}
#[allow(clippy::cast_possible_truncation)]

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

@@ -22,7 +22,7 @@ use std::{
sync::Arc,
};
use anyhow::{Context, Result};
use anyhow::Context;
use arguments::Arguments;
use azalea::{
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
@@ -55,7 +55,7 @@ struct State {
}
#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> anyhow::Result<()> {
#[cfg(feature = "console-subscriber")]
console_subscriber::init();
@@ -131,7 +131,8 @@ async fn main() -> Result<()> {
} else {
Account::offline(&username)
};
let Err(err) = ClientBuilder::new_without_plugins()
let Err(error) = ClientBuilder::new_without_plugins()
.add_plugins(DefaultBotPlugins)
.add_plugins(HacksPlugin)
.add_plugins(default_plugins)
@@ -144,7 +145,7 @@ async fn main() -> Result<()> {
})
.start(account, server)
.await;
eprintln!("{err}");
eprintln!("{error}");
Ok(())
}

View File

@@ -3,15 +3,17 @@
use std::sync::Arc;
use azalea::{
ecs::event::EventReader,
ecs::{event::EventReader, system::Query},
packet::{
config::ReceiveConfigPacketEvent, game::ReceiveGamePacketEvent,
login::ReceiveLoginPacketEvent,
config::ReceiveConfigPacketEvent,
game::emit_receive_packet_events,
login::{LoginPacketEvent, process_packet_events},
},
protocol::packets::login::ClientboundLoginPacket,
raw_connection::RawConnection,
};
use bevy_app::{App, First, Plugin};
use bevy_ecs::system::ResMut;
use bevy_ecs::{schedule::IntoSystemConfigs, system::ResMut};
use log::error;
use parking_lot::Mutex;
@@ -26,16 +28,19 @@ impl Plugin for RecordPlugin {
let recorder = self.recorder.lock().take();
if let Some(recorder) = recorder {
app.insert_resource(recorder)
.add_systems(First, record_login_packets)
.add_systems(First, record_login_packets.before(process_packet_events))
.add_systems(First, record_configuration_packets)
.add_systems(First, record_game_packets);
.add_systems(
First,
record_game_packets.before(emit_receive_packet_events),
);
}
}
}
fn record_login_packets(
recorder: Option<ResMut<Recorder>>,
mut events: EventReader<ReceiveLoginPacketEvent>,
mut events: EventReader<LoginPacketEvent>,
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
@@ -58,20 +63,20 @@ fn record_configuration_packets(
) {
if let Some(mut recorder) = recorder {
for event in events.read() {
if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
if let Err(error) = recorder.save_packet(&event.packet) {
error!("failed to record configuration packet: {error:?}");
}
}
}
}
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()) {
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) {
error!("failed to record game packet: {error:?}");
}
}