From 70c1a83eb2f65acd62e7059a02ba080edad4c72d Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Fri, 21 Feb 2025 23:00:29 -0500 Subject: [PATCH] feat(client): bring a few commands from v0.1.0 --- src/lua/client/interaction.rs | 24 +++++++++++++++++++++++- src/lua/client/mod.rs | 3 +++ src/lua/client/movement.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/lua/client/interaction.rs b/src/lua/client/interaction.rs index 3090038..5215c37 100644 --- a/src/lua/client/interaction.rs +++ b/src/lua/client/interaction.rs @@ -1,5 +1,11 @@ use super::{Client, Vec3}; -use azalea::{BlockPos, BotClientExt, attack::AttackEvent, world::MinecraftEntityId}; +use azalea::{ + BlockPos, BotClientExt, + attack::AttackEvent, + protocol::packets::game::{ServerboundUseItem, s_interact::InteractionHand}, + world::MinecraftEntityId, +}; +use log::error; use mlua::{Lua, Result, UserDataRef}; pub async fn attack(_lua: Lua, client: UserDataRef, entity_id: u32) -> Result<()> { @@ -60,3 +66,19 @@ pub fn start_mining(_lua: &Lua, client: &mut Client, position: Vec3) -> Result<( )); Ok(()) } + +pub fn use_item(_lua: &Lua, client: &Client, hand: Option) -> Result<()> { + let d = client.direction(); + if let Err(error) = client.write_packet(ServerboundUseItem { + hand: match hand { + Some(1) => InteractionHand::OffHand, + _ => InteractionHand::MainHand, + }, + sequence: 0, + yaw: d.0, + pitch: d.1, + }) { + error!("failed to send UseItem packet: {error:?}"); + } + Ok(()) +} diff --git a/src/lua/client/mod.rs b/src/lua/client/mod.rs index 8540b79..cae8fc1 100644 --- a/src/lua/client/mod.rs +++ b/src/lua/client/mod.rs @@ -72,7 +72,10 @@ impl UserData for Client { m.add_method("get_fluid_state", world::get_fluid_state); m.add_method("set_held_slot", container::set_held_slot); m.add_method("set_mining", interaction::set_mining); + m.add_method("set_sneaking", movement::set_sneaking); 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("block_interact", interaction::block_interact); m.add_method_mut("jump", movement::jump); m.add_method_mut("open_inventory", container::open_inventory); diff --git a/src/lua/client/movement.rs b/src/lua/client/movement.rs index 7a208a6..8d909ea 100644 --- a/src/lua/client/movement.rs +++ b/src/lua/client/movement.rs @@ -7,7 +7,9 @@ use azalea::{ ExecutingPath, GotoEvent, Pathfinder, PathfinderClientExt, goals::{BlockPosGoal, Goal, RadiusGoal, ReachBlockPosGoal, XZGoal, YGoal}, }, + protocol::packets::game::{ServerboundPlayerCommand, s_player_command::Action}, }; +use log::error; use mlua::{FromLua, Lua, Result, Table, UserDataRef, Value}; pub fn direction(_lua: &Lua, client: &Client) -> Result { @@ -182,6 +184,21 @@ pub fn set_jumping(_lua: &Lua, client: &mut Client, jumping: bool) -> Result<()> Ok(()) } +pub fn set_sneaking(_lua: &Lua, client: &Client, sneaking: bool) -> Result<()> { + if let Err(error) = client.write_packet(ServerboundPlayerCommand { + id: client.entity.index(), + action: if sneaking { + Action::PressShiftKey + } else { + Action::ReleaseShiftKey + }, + data: 0, + }) { + error!("failed to send PlayerCommand packet: {error:?}"); + } + Ok(()) +} + pub fn sprint(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> { client.sprint(match direction { 5 => SprintDirection::ForwardRight, @@ -196,6 +213,17 @@ pub fn stop_pathfinding(_lua: &Lua, client: &Client, _: ()) -> Result<()> { Ok(()) } +pub fn stop_sleeping(_lua: &Lua, client: &Client, _: ()) -> Result<()> { + if let Err(error) = client.write_packet(ServerboundPlayerCommand { + id: client.entity.index(), + action: Action::StopSleeping, + data: 0, + }) { + error!("failed to send PlayerCommand packet: {error:?}"); + } + Ok(()) +} + pub fn walk(_lua: &Lua, client: &mut Client, direction: u8) -> Result<()> { client.walk(match direction { 1 => WalkDirection::Forward,