From 98474c52162ea4e6ceb7a7c211f9ede459414242 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Fri, 13 Jan 2023 15:20:33 +0800 Subject: [PATCH] Add jump and interact commands --- Cargo.lock | 1 + Cargo.toml | 1 + src/bot.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8157a78..6e3e90f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -667,6 +667,7 @@ dependencies = [ "anyhow", "azalea", "azalea-block", + "azalea-core", "azalea-protocol", "chrono", "colored", diff --git a/Cargo.toml b/Cargo.toml index f699160..48a3453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" azalea = "0.5.0" azalea-protocol = "0.5.0" azalea-block = "0.5.0" +azalea-core = "0.5.0" toml = "0.5.10" serde = { version = "1.0", features = ["derive"] } tokio = "1.24.1" diff --git a/src/bot.rs b/src/bot.rs index 48e926a..24e6914 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,5 +1,8 @@ use crate::{logging::log_error, State}; -use azalea::{pathfinder::BlockPosGoal, prelude::*, BlockPos, SprintDirection, WalkDirection}; +use azalea::{ + pathfinder::BlockPosGoal, prelude::*, BlockPos, SprintDirection, Vec3, WalkDirection, +}; +use azalea_core::Direction; use azalea_protocol::packets::game::{ self, serverbound_interact_packet::InteractionHand, ServerboundGamePacket, }; @@ -26,8 +29,10 @@ pub enum Command { Look, Sneak, Unsneak, - Interact, + InteractBlock, + InteractEntity, Attack, + Jump, Walk, Sprint, DropItem, @@ -70,8 +75,10 @@ pub async fn process_command( "look" => command = Command::Look, "sneak" => command = Command::Sneak, "unsneak" => command = Command::Unsneak, - "interact" => command = Command::Interact, + "interact_block" => command = Command::InteractBlock, + "interact_entity" => command = Command::InteractEntity, "attack" => command = Command::Attack, + "jump" => command = Command::Jump, "walk" => command = Command::Walk, "sprint" => command = Command::Sprint, "drop_item" => command = Command::DropItem, @@ -371,7 +378,55 @@ pub async fn process_command( ); return "I am no longer sneaking!".to_string(); } - Command::Interact => { + Command::InteractBlock => { + if segments.len() < 4 { + return "Please give me block coordinates (and block faces) to interact with!" + .to_string(); + } + + let mut coordinates: Vec = Vec::new(); + for segment in &segments[0..3] { + coordinates.push(match segment.parse() { + Ok(number) => number, + Err(error) => return format!("Unable to parse coordinates: {}", error), + }) + } + let block_face = match segments[3].to_lowercase().as_str() { + "up" | "top" => Direction::Up, + "down" | "bottom" => Direction::Down, + "north" => Direction::North, + "east" => Direction::East, + "south" => Direction::South, + "west" => Direction::West, + _ => return "Please give me a valid block face!".to_string(), + }; + log_error( + client + .write_packet(ServerboundGamePacket::UseItemOn( + game::serverbound_use_item_on_packet::ServerboundUseItemOnPacket { + hand: InteractionHand::MainHand, + block_hit: game::serverbound_use_item_on_packet::BlockHitResult { + block_pos: BlockPos { + x: coordinates[0], + y: coordinates[1], + z: coordinates[2], + }, + direction: block_face, + location: Vec3 { + x: coordinates[0] as f64, + y: coordinates[1] as f64, + z: coordinates[2] as f64, + }, + inside: false, + }, + sequence: 0, + }, + )) + .await, + ); + return "I have successfully interacted with the block!".to_string(); + } + Command::InteractEntity => { if segments.len() < 1 { return "Please give me IDs to interact with!".to_string(); } @@ -481,6 +536,10 @@ pub async fn process_command( return "Unable to find entity!".to_string(); } } + Command::Jump => { + client.jump(); + return "I have successfully jumped!".to_string(); + } Command::Walk => { if segments.len() < 2 { return "Please give me a direction (and duration) to walk in!".to_string();