From a839c6a923a737fab61536cec0258fdd83106368 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 15 Jul 2023 16:35:23 -0500 Subject: [PATCH] fix brigadier booleans --- .../src/arguments/bool_argument_type.rs | 12 ++++++-- azalea-brigadier/src/lib.rs | 1 + azalea-client/src/client.rs | 11 ++++++- azalea-client/src/events.rs | 12 ++++++-- azalea-client/src/packet_handling.rs | 2 +- azalea/examples/testbot.rs | 2 +- azalea/src/bot.rs | 7 +++-- azalea/src/lib.rs | 4 +-- azalea/src/pathfinder/goals.rs | 30 +++++++++++++++++++ azalea/src/pathfinder/mod.rs | 30 ++----------------- 10 files changed, 69 insertions(+), 42 deletions(-) create mode 100644 azalea/src/pathfinder/goals.rs diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs index 57fa8a03..1bff8aa3 100644 --- a/azalea-brigadier/src/arguments/bool_argument_type.rs +++ b/azalea-brigadier/src/arguments/bool_argument_type.rs @@ -6,16 +6,22 @@ use crate::{ use super::ArgumentType; -impl ArgumentType for bool { +#[derive(Default)] +struct Boolean; + +impl ArgumentType for Boolean { fn parse(&self, reader: &mut StringReader) -> Result, CommandSyntaxException> { - Ok(Rc::new(reader.read_boolean())) + Ok(Rc::new(reader.read_boolean()?)) } } +pub fn bool() -> impl ArgumentType { + Boolean +} pub fn get_bool(context: &CommandContext, name: &str) -> Option { context .argument(name) - .unwrap() + .expect("argument with name not found") .downcast_ref::() .cloned() } diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs index 161ef83a..4f704d46 100755 --- a/azalea-brigadier/src/lib.rs +++ b/azalea-brigadier/src/lib.rs @@ -14,6 +14,7 @@ pub mod tree; pub mod prelude { pub use crate::{ arguments::{ + bool_argument_type::{bool, get_bool}, double_argument_type::{double, get_double}, float_argument_type::{float, get_float}, integer_argument_type::{get_integer, integer}, diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index f7fcb16c..a7cceaed 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -21,7 +21,7 @@ use crate::{ use azalea_auth::{game_profile::GameProfile, sessionserver::ClientSessionServerError}; use azalea_chat::FormattedText; use azalea_core::Vec3; -use azalea_entity::{metadata::Health, EntityPlugin, EntityUpdateSet, Local, Position}; +use azalea_entity::{metadata::Health, EntityPlugin, EntityUpdateSet, EyeHeight, Local, Position}; use azalea_physics::{PhysicsPlugin, PhysicsSet}; use azalea_protocol::{ connect::{Connection, ConnectionError}, @@ -550,6 +550,15 @@ impl Client { pub fn position(&self) -> Vec3 { Vec3::from(&self.component::()) } + + /// Get the position of this client's eyes. + /// + /// This is a shortcut for + /// `bot.position().up(bot.component::())`. + pub fn eye_position(&self) -> Vec3 { + self.position().up((*self.component::()) as f64) + } + /// Get the health of this client. /// /// This is a shortcut for `*bot.component::()`. diff --git a/azalea-client/src/events.rs b/azalea-client/src/events.rs index 900f559f..b581fcee 100644 --- a/azalea-client/src/events.rs +++ b/azalea-client/src/events.rs @@ -6,9 +6,14 @@ use std::sync::Arc; use azalea_protocol::packets::game::{ clientbound_player_combat_kill_packet::ClientboundPlayerCombatKillPacket, ClientboundGamePacket, }; -use azalea_world::MinecraftEntityId; +use azalea_world::{InstanceName, MinecraftEntityId}; use bevy_app::{App, FixedUpdate, Plugin, Update}; -use bevy_ecs::{component::Component, event::EventReader, query::Added, system::Query}; +use bevy_ecs::{ + component::Component, + event::EventReader, + query::{Added, With}, + system::Query, +}; use derive_more::{Deref, DerefMut}; use tokio::sync::mpsc; @@ -143,7 +148,8 @@ fn chat_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader) { +// only tick if we're in a world +fn tick_listener(query: Query<&LocalPlayerEvents, With>) { for local_player_events in &query { local_player_events.send(Event::Tick).unwrap(); } diff --git a/azalea-client/src/packet_handling.rs b/azalea-client/src/packet_handling.rs index b4f4e045..70bf2778 100644 --- a/azalea-client/src/packet_handling.rs +++ b/azalea-client/src/packet_handling.rs @@ -327,7 +327,7 @@ fn process_packet_events(ecs: &mut World) { debug!("Got update tags packet"); } ClientboundGamePacket::Disconnect(p) => { - debug!("Got disconnect packet {:?}", p); + warn!("Got disconnect packet {:?}", p); let mut system_state: SystemState> = SystemState::new(ecs); let mut disconnect_events = system_state.get_mut(ecs); diff --git a/azalea/examples/testbot.rs b/azalea/examples/testbot.rs index 75a6ca67..40ce7dce 100644 --- a/azalea/examples/testbot.rs +++ b/azalea/examples/testbot.rs @@ -7,7 +7,7 @@ use azalea::entity::metadata::Player; use azalea::entity::{EyeHeight, Position}; use azalea::interact::HitResultComponent; use azalea::inventory::ItemSlot; -use azalea::pathfinder::BlockPosGoal; +use azalea::pathfinder::goals::BlockPosGoal; use azalea::protocol::packets::game::ClientboundGamePacket; use azalea::{prelude::*, swarm::prelude::*, BlockPos, GameProfileComponent, WalkDirection}; use azalea::{Account, Client, Event}; diff --git a/azalea/src/bot.rs b/azalea/src/bot.rs index 71d96c4d..1624db78 100644 --- a/azalea/src/bot.rs +++ b/azalea/src/bot.rs @@ -99,11 +99,12 @@ impl BotClientExt for azalea_client::Client { /// ``` /// # use azalea::prelude::*; - /// # async fn example(mut bot: azalea::Client) { - /// let mut ticks = self.get_tick_broadcaster(); + /// # use azalea::container::WaitingForInventoryOpen; + /// # async fn example(bot: &mut azalea::Client) { + /// let mut ticks = bot.get_tick_broadcaster(); /// while ticks.recv().await.is_ok() { /// let ecs = bot.ecs.lock(); - /// if ecs.get::(self.entity).is_none() { + /// if ecs.get::(bot.entity).is_none() { /// break; /// } /// } diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 0aff2a56..f564cd9b 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -5,7 +5,7 @@ mod auto_respawn; mod bot; -mod container; +pub mod container; pub mod pathfinder; pub mod prelude; pub mod swarm; @@ -20,7 +20,7 @@ pub use azalea_core::{BlockPos, Vec3}; pub use azalea_entity as entity; pub use azalea_protocol as protocol; pub use azalea_registry::{Block, EntityKind, Item}; -pub use azalea_world::Instance; +pub use azalea_world as world; pub use bot::DefaultBotPlugins; use ecs::component::Component; use futures::Future; diff --git a/azalea/src/pathfinder/goals.rs b/azalea/src/pathfinder/goals.rs new file mode 100644 index 00000000..95f1b74f --- /dev/null +++ b/azalea/src/pathfinder/goals.rs @@ -0,0 +1,30 @@ +use azalea_core::BlockPos; + +use super::{Goal, Node, VerticalVel}; + +pub struct BlockPosGoal { + pub pos: BlockPos, +} +impl Goal for BlockPosGoal { + fn heuristic(&self, n: &Node) -> f32 { + let dx = (self.pos.x - n.pos.x) as f32; + let dy = (self.pos.y - n.pos.y) as f32; + let dz = (self.pos.z - n.pos.z) as f32; + dx * dx + dy * dy + dz * dz + } + fn success(&self, n: &Node) -> bool { + n.pos == self.pos + } + fn goal_node(&self) -> Node { + Node { + pos: self.pos, + vertical_vel: VerticalVel::None, + } + } +} + +impl From for BlockPosGoal { + fn from(pos: BlockPos) -> Self { + Self { pos } + } +} diff --git a/azalea/src/pathfinder/mod.rs b/azalea/src/pathfinder/mod.rs index f4e4d599..27314d07 100644 --- a/azalea/src/pathfinder/mod.rs +++ b/azalea/src/pathfinder/mod.rs @@ -1,4 +1,5 @@ mod astar; +pub mod goals; mod moves; use crate::bot::{JumpEvent, LookAtEvent}; @@ -75,7 +76,7 @@ pub trait PathfinderClientExt { impl PathfinderClientExt for azalea_client::Client { /// ``` /// # use azalea::prelude::*; - /// # use azalea::{BlockPos, pathfinder::BlockPosGoal}; + /// # use azalea::{BlockPos, pathfinder::goals::BlockPosGoal}; /// # fn example(bot: &Client) { /// bot.goto(BlockPosGoal::from(BlockPos::new(0, 70, 0))); /// # } @@ -320,30 +321,3 @@ impl Node { } } } - -pub struct BlockPosGoal { - pub pos: BlockPos, -} -impl Goal for BlockPosGoal { - fn heuristic(&self, n: &Node) -> f32 { - let dx = (self.pos.x - n.pos.x) as f32; - let dy = (self.pos.y - n.pos.y) as f32; - let dz = (self.pos.z - n.pos.z) as f32; - dx * dx + dy * dy + dz * dz - } - fn success(&self, n: &Node) -> bool { - n.pos == self.pos - } - fn goal_node(&self) -> Node { - Node { - pos: self.pos, - vertical_vel: VerticalVel::None, - } - } -} - -impl From for BlockPosGoal { - fn from(pos: BlockPos) -> Self { - Self { pos } - } -}