Compare commits

...

6 Commits

24 changed files with 1178 additions and 778 deletions

1421
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

@@ -35,9 +35,6 @@ end
function steal(item_name) function steal(item_name)
for _, chest_pos in ipairs(client:find_blocks(client.position, get_block_states({ "chest" }))) do 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 }) 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) client:look_at(chest_pos)
local container = client:open_container_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)) info(string.format("currently in nether, going to %.2f %.2f", nether_pos.x, nether_pos.z))
client:go_to(nether_pos, { type = XZ_GOAL }) 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") info("arrived, looking for nearest portal")
local portals_nether = client:find_blocks(client.position, portal_block_states) local portals_nether = client:find_blocks(client.position, portal_block_states)
@@ -144,10 +141,6 @@ function interact_bed()
end end
client:go_to({ position = bed, radius = 2 }, { type = RADIUS_GOAL, options = { without_mining = true } }) 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:look_at(bed)
client:block_interact(bed) client:block_interact(bed)
end end

View File

@@ -33,7 +33,7 @@ impl CommandSource {
} }
self.client.chat( self.client.chat(
&(if self.message.is_whisper() &(if self.message.is_whisper()
&& let Some(username) = self.message.username() && let Some(username) = self.message.sender()
{ {
format!("/w {username} {chunk}") format!("/w {username} {chunk}")
} else { } else {
@@ -50,7 +50,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move { tokio::spawn(async move {
let source = source.lock().await; let source = source.lock().await;
source.reply( 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")), .map_or_else(|error| error.to_string(), |()| String::from("ok")),
); );
}); });
@@ -64,7 +64,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move { tokio::spawn(async move {
let source = source.lock().await; let source = source.lock().await;
source.reply( source.reply(
&eval(&source.state.lua, &code, source.message.username()) &eval(&source.state.lua, &code, source.message.sender())
.await .await
.unwrap_or_else(|error| error.to_string()), .unwrap_or_else(|error| error.to_string()),
); );
@@ -80,7 +80,7 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
tokio::spawn(async move { tokio::spawn(async move {
let source = source.lock().await; let source = source.lock().await;
source.reply( source.reply(
&exec(&source.state.lua, &code, source.message.username()) &exec(&source.state.lua, &code, source.message.sender())
.await .await
.map_or_else(|error| error.to_string(), |()| String::from("ok")), .map_or_else(|error| error.to_string(), |()| String::from("ok")),
); );

View File

@@ -23,7 +23,7 @@ use crate::{
replay::recorder::Recorder, 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<()> { pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> {
match event { match event {
Event::AddPlayer(player_info) => { Event::AddPlayer(player_info) => {
@@ -32,9 +32,10 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
Event::Chat(message) => { Event::Chat(message) => {
let globals = state.lua.globals(); let globals = state.lua.globals();
let (sender, mut content) = message.split_sender_and_content(); 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 is_whisper = message.is_whisper();
let text = message.message(); let text = message.message();
let html_text = text.to_html();
let ansi_text = text.to_ansi(); let ansi_text = text.to_ansi();
info!("{ansi_text}"); info!("{ansi_text}");
@@ -86,6 +87,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("text", text.to_string())?; table.set("text", text.to_string())?;
table.set("ansi_text", ansi_text)?; table.set("ansi_text", ansi_text)?;
table.set("html_text", html_text)?;
table.set("sender", sender)?; table.set("sender", sender)?;
table.set("content", content)?; table.set("content", content)?;
table.set("uuid", uuid)?; table.set("uuid", uuid)?;
@@ -101,6 +103,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let message_table = state.lua.create_table()?; let message_table = state.lua.create_table()?;
message_table.set("text", packet.message.to_string())?; message_table.set("text", packet.message.to_string())?;
message_table.set("ansi_text", packet.message.to_ansi())?; message_table.set("ansi_text", packet.message.to_ansi())?;
message_table.set("html_text", packet.message.to_html())?;
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("message", message_table)?; table.set("message", message_table)?;
table.set("player_id", packet.player_id.0)?; table.set("player_id", packet.player_id.0)?;
@@ -117,6 +120,7 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
let table = state.lua.create_table()?; let table = state.lua.create_table()?;
table.set("text", message.to_string())?; table.set("text", message.to_string())?;
table.set("ansi_text", message.to_ansi())?; table.set("ansi_text", message.to_ansi())?;
table.set("html_text", message.to_html())?;
Ok(table) Ok(table)
}) })
.await .await
@@ -125,7 +129,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
} }
} }
Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await, Event::KeepAlive(id) => call_listeners(&state, "keep_alive", || Ok(id)).await,
Event::Login => call_listeners(&state, "login", || Ok(())).await,
Event::RemovePlayer(player_info) => { Event::RemovePlayer(player_info) => {
call_listeners(&state, "remove_player", || Ok(Player::from(player_info))).await call_listeners(&state, "remove_player", || Ok(Player::from(player_info))).await
} }
@@ -201,6 +204,12 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
} }
_ => Ok(()), _ => Ok(()),
}, },
Event::Login => {
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
call_listeners(&state, "login", || Ok(())).await
}
Event::Init => { Event::Init => {
debug!("received init event"); debug!("received init event");
@@ -212,9 +221,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
exit(0); exit(0);
})?; })?;
#[cfg(feature = "matrix")]
matrix_init(&client, state.clone());
let globals = state.lua.globals(); let globals = state.lua.globals();
lua_init(client, &state, &globals).await?; lua_init(client, &state, &globals).await?;

View File

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

View File

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

View File

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

View File

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

View File

@@ -41,6 +41,7 @@ impl UserData for Client {
f.add_field_method_get("dimension", world::dimension); f.add_field_method_get("dimension", world::dimension);
f.add_field_method_get("direction", movement::direction); f.add_field_method_get("direction", movement::direction);
f.add_field_method_get("eye_position", movement::eye_position); 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("has_attack_cooldown", interaction::has_attack_cooldown);
f.add_field_method_get("health", state::health); f.add_field_method_get("health", state::health);
f.add_field_method_get("held_item", container::held_item); f.add_field_method_get("held_item", container::held_item);
@@ -66,30 +67,32 @@ impl UserData for Client {
m.add_async_method("mine", interaction::mine); m.add_async_method("mine", interaction::mine);
m.add_async_method("open_container_at", container::open_container_at); 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("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("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("chat", chat);
m.add_method("disconnect", disconnect); m.add_method("disconnect", disconnect);
m.add_method("find_blocks", world::find::blocks); m.add_method("find_blocks", world::find::blocks);
m.add_method("get_block_state", world::get_block_state); m.add_method("get_block_state", world::get_block_state);
m.add_method("get_fluid_state", world::get_fluid_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_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_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_mining", interaction::set_mining);
m.add_method("set_position", movement::set_position); m.add_method("set_position", movement::set_position);
m.add_method("set_sneaking", movement::set_sneaking); 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_pathfinding", movement::stop_pathfinding);
m.add_method("stop_sleeping", movement::stop_sleeping); m.add_method("stop_sleeping", movement::stop_sleeping);
m.add_method("use_item", interaction::use_item); m.add_method("walk", movement::walk);
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);
} }
} }
@@ -98,7 +101,7 @@ fn chat(_lua: &Lua, client: &Client, message: String) -> Result<()> {
Ok(()) Ok(())
} }
fn disconnect(_lua: &Lua, client: &Client, _: ()) -> Result<()> { fn disconnect(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.disconnect(); client.disconnect();
Ok(()) Ok(())
} }

View File

@@ -1,19 +1,129 @@
use azalea::{ use azalea::{
BlockPos, BotClientExt, SprintDirection, WalkDirection, BlockPos, BotClientExt, SprintDirection, WalkDirection,
core::hit_result::HitResult,
entity::Position, entity::Position,
interact::HitResultComponent, interact::HitResultComponent,
pathfinder::{ pathfinder::{
ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt, ExecutingPath, Pathfinder, PathfinderClientExt,
goals::{BlockPosGoal, Goal, InverseGoal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal}, goals::{BlockPosGoal, Goal, InverseGoal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal},
}, },
protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action}, protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action},
world::MinecraftEntityId, world::MinecraftEntityId,
}; };
use log::error;
use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value}; use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value};
use super::{Client, Direction, Vec3}; 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> { pub fn direction(_lua: &Lua, client: &Client) -> Result<Direction> {
let direction = client.direction(); let direction = client.direction();
Ok(Direction { Ok(Direction {
@@ -26,96 +136,28 @@ pub fn eye_position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(client.eye_position())) Ok(Vec3::from(client.eye_position()))
} }
pub async fn go_to( pub fn jump(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
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<()> {
client.jump(); client.jump();
Ok(()) Ok(())
} }
pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> { pub fn looking_at(lua: &Lua, client: &Client) -> Result<Option<Table>> {
let result = client.component::<HitResultComponent>(); Ok(
Ok(if result.miss { if let HitResult::Block(ref result) = *client.component::<HitResultComponent>() {
None let table = lua.create_table()?;
} else { table.set("direction", Vec3::from(result.direction.normal()))?;
let table = lua.create_table()?; table.set("inside", result.inside)?;
table.set("position", Vec3::from(result.block_pos))?; table.set("location", Vec3::from(result.location))?;
table.set("inside", result.inside)?; table.set("position", Vec3::from(result.block_pos))?;
table.set("world_border", result.world_border)?; table.set("world_border", result.world_border)?;
Some(table) 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)); client.look_at(azalea::Vec3::new(position.x, position.y, position.z));
Ok(()) Ok(())
} }
@@ -150,12 +192,12 @@ pub fn position(_lua: &Lua, client: &Client) -> Result<Vec3> {
Ok(Vec3::from(&client.component::<Position>())) 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); client.set_direction(direction.y, direction.x);
Ok(()) 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); client.set_jumping(jumping);
Ok(()) Ok(())
} }
@@ -170,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<()> { 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>(), id: client.component::<MinecraftEntityId>(),
action: if sneaking { action: if sneaking {
Action::PressShiftKey Action::PressShiftKey
@@ -178,13 +220,11 @@ pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> {
Action::ReleaseShiftKey Action::ReleaseShiftKey
}, },
data: 0, data: 0,
}) { });
error!("failed to send PlayerCommand packet: {error:?}");
}
Ok(()) 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 { client.sprint(match direction {
5 => SprintDirection::ForwardRight, 5 => SprintDirection::ForwardRight,
6 => SprintDirection::ForwardLeft, 6 => SprintDirection::ForwardLeft,
@@ -193,23 +233,21 @@ pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> {
Ok(()) Ok(())
} }
pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> { pub fn stop_pathfinding(_lua: &Lua, client: &Client, (): ()) -> Result<()> {
client.stop_pathfinding(); client.stop_pathfinding();
Ok(()) 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 { client.write_packet(ServerboundPlayerCommand {
id: client.component::<MinecraftEntityId>(), id: client.component::<MinecraftEntityId>(),
action: Action::StopSleeping, action: Action::StopSleeping,
data: 0, data: 0,
}) { });
error!("failed to send PlayerCommand packet: {error:?}");
}
Ok(()) 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 { client.walk(match direction {
1 => WalkDirection::Forward, 1 => WalkDirection::Forward,
2 => WalkDirection::Backward, 2 => WalkDirection::Backward,

View File

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

View File

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

View File

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

View File

@@ -71,9 +71,7 @@ impl UserData for ItemStack {
} }
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) { fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
m.add_method_mut("split", |_, this, count: u32| { m.add_method_mut("split", |_, this, count: u32| Ok(Self(this.0.split(count))));
Ok(ItemStack(this.0.split(count)))
});
m.add_method_mut("update_empty", |_, this, (): ()| { m.add_method_mut("update_empty", |_, this, (): ()| {
this.0.update_empty(); this.0.update_empty();
Ok(()) 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>)| { move |_, (event_type, callback, optional_id): (String, Function, Option<String>)| {
let m = m.clone(); let m = m.clone();
let id = optional_id.unwrap_or_else(|| { let id = optional_id.unwrap_or_else(|| {
callback.info().name.unwrap_or(format!( callback.info().name.unwrap_or_else(|| {
"anonymous @ {}", format!(
SystemTime::now() "anonymous @ {}",
.duration_since(UNIX_EPOCH) SystemTime::now()
.unwrap_or_default() .duration_since(UNIX_EPOCH)
.as_millis() .unwrap_or_default()
)) .as_millis()
)
})
}); });
tokio::spawn(async move { tokio::spawn(async move {
m.write() m.write()
@@ -40,12 +42,10 @@ pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap
let m = m.clone(); let m = m.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut m = m.write().await; 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.retain(|(id, _)| target_id != *id);
listeners.is_empty() listeners.is_empty()
} else { });
false
};
if empty { if empty {
m.remove(&event_type); m.remove(&event_type);
} }

View File

@@ -37,12 +37,7 @@ impl UserData for Room {
.members(RoomMemberships::all()) .members(RoomMemberships::all())
.await .await
.map_err(Error::external) .map_err(Error::external)
.map(|members| { .map(|members| members.into_iter().map(Member).collect::<Vec<_>>())
members
.into_iter()
.map(|member| Member(member.clone()))
.collect::<Vec<_>>()
})
}); });
m.add_async_method( m.add_async_method(
"kick_user", "kick_user",

View File

@@ -38,12 +38,12 @@ impl Display for Error {
formatter, formatter,
"failed to {}", "failed to {}",
match self { match self {
Error::CreateEnv(error) => format!("create environment: {error}"), Self::CreateEnv(error) => format!("create environment: {error}"),
Error::EvalChunk(error) => format!("evaluate chunk: {error}"), Self::EvalChunk(error) => format!("evaluate chunk: {error}"),
Error::ExecChunk(error) => format!("execute chunk: {error}"), Self::ExecChunk(error) => format!("execute chunk: {error}"),
Error::LoadChunk(error) => format!("load chunk: {error}"), Self::LoadChunk(error) => format!("load chunk: {error}"),
Error::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"), Self::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"),
Error::ReadFile(error) => format!("read script file: {error}"), Self::ReadFile(error) => format!("read script file: {error}"),
} }
) )
} }

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
use azalea::{entity::particle::Particle, registry::ParticleKind}; use azalea::{entity::particle::Particle, registry::ParticleKind};
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
pub fn to_kind(particle: &Particle) -> ParticleKind { pub const fn to_kind(particle: &Particle) -> ParticleKind {
match particle { match particle {
Particle::AngryVillager => ParticleKind::AngryVillager, Particle::AngryVillager => ParticleKind::AngryVillager,
Particle::Block(_) => ParticleKind::Block, Particle::Block(_) => ParticleKind::Block,

View File

@@ -3,17 +3,15 @@
use std::sync::Arc; use std::sync::Arc;
use azalea::{ use azalea::{
ecs::{event::EventReader, system::Query}, ecs::event::EventReader,
packet::{ packet::{
config::ReceiveConfigPacketEvent, config::ReceiveConfigPacketEvent, game::ReceiveGamePacketEvent,
game::emit_receive_packet_events, login::ReceiveLoginPacketEvent,
login::{LoginPacketEvent, process_packet_events},
}, },
protocol::packets::login::ClientboundLoginPacket, 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 bevy_ecs::system::ResMut;
use log::error; use log::error;
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -24,22 +22,20 @@ pub struct RecordPlugin {
} }
impl Plugin for RecordPlugin { impl Plugin for RecordPlugin {
fn build(&self, app: &mut bevy_app::App) { fn build(&self, app: &mut App) {
if let Some(recorder) = self.recorder.lock().take() { let recorder = self.recorder.lock().take();
if let Some(recorder) = recorder {
app.insert_resource(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_configuration_packets)
.add_systems( .add_systems(First, record_game_packets);
First,
record_game_packets.before(emit_receive_packet_events),
);
} }
} }
} }
fn record_login_packets( fn record_login_packets(
recorder: Option<ResMut<Recorder>>, recorder: Option<ResMut<Recorder>>,
mut events: EventReader<LoginPacketEvent>, mut events: EventReader<ReceiveLoginPacketEvent>,
) { ) {
if let Some(mut recorder) = recorder { if let Some(mut recorder) = recorder {
for event in events.read() { for event in events.read() {
@@ -62,20 +58,20 @@ fn record_configuration_packets(
) { ) {
if let Some(mut recorder) = recorder { if let Some(mut recorder) = recorder {
for event in events.read() { 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:?}"); error!("failed to record configuration packet: {error:?}");
} }
} }
} }
} }
fn record_game_packets(recorder: Option<ResMut<Recorder>>, query: Query<&RawConnection>) { fn record_game_packets(
if let Some(mut recorder) = recorder recorder: Option<ResMut<Recorder>>,
&& let Ok(raw_conn) = query.get_single() mut events: EventReader<ReceiveGamePacketEvent>,
{ ) {
let queue = raw_conn.incoming_packet_queue(); if let Some(mut recorder) = recorder {
for raw_packet in queue.lock().iter() { for event in events.read() {
if let Err(error) = recorder.save_raw_packet(raw_packet) { if let Err(error) = recorder.save_packet(event.packet.as_ref()) {
error!("failed to record game packet: {error:?}"); error!("failed to record game packet: {error:?}");
} }
} }