Skip to content

Commit

Permalink
fix brigadier booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Jul 15, 2023
1 parent cde7e35 commit a839c6a
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 42 deletions.
12 changes: 9 additions & 3 deletions azalea-brigadier/src/arguments/bool_argument_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rc<dyn Any>, CommandSyntaxException> {
Ok(Rc::new(reader.read_boolean()))
Ok(Rc::new(reader.read_boolean()?))
}
}

pub fn bool() -> impl ArgumentType {
Boolean
}
pub fn get_bool<S>(context: &CommandContext<S>, name: &str) -> Option<bool> {
context
.argument(name)
.unwrap()
.expect("argument with name not found")
.downcast_ref::<bool>()
.cloned()
}
1 change: 1 addition & 0 deletions azalea-brigadier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
11 changes: 10 additions & 1 deletion azalea-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -550,6 +550,15 @@ impl Client {
pub fn position(&self) -> Vec3 {
Vec3::from(&self.component::<Position>())
}

/// Get the position of this client's eyes.
///
/// This is a shortcut for
/// `bot.position().up(bot.component::<EyeHeight>())`.
pub fn eye_position(&self) -> Vec3 {
self.position().up((*self.component::<EyeHeight>()) as f64)
}

/// Get the health of this client.
///
/// This is a shortcut for `*bot.component::<Health>()`.
Expand Down
12 changes: 9 additions & 3 deletions azalea-client/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -143,7 +148,8 @@ fn chat_listener(query: Query<&LocalPlayerEvents>, mut events: EventReader<ChatR
}
}

fn tick_listener(query: Query<&LocalPlayerEvents>) {
// only tick if we're in a world
fn tick_listener(query: Query<&LocalPlayerEvents, With<InstanceName>>) {
for local_player_events in &query {
local_player_events.send(Event::Tick).unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion azalea-client/src/packet_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventWriter<DisconnectEvent>> =
SystemState::new(ecs);
let mut disconnect_events = system_state.get_mut(ecs);
Expand Down
2 changes: 1 addition & 1 deletion azalea/examples/testbot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
7 changes: 4 additions & 3 deletions azalea/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<WaitingForInventoryOpen>(self.entity).is_none() {
/// if ecs.get::<WaitingForInventoryOpen>(bot.entity).is_none() {
/// break;
/// }
/// }
Expand Down
4 changes: 2 additions & 2 deletions azalea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

mod auto_respawn;
mod bot;
mod container;
pub mod container;
pub mod pathfinder;
pub mod prelude;
pub mod swarm;
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions azalea/src/pathfinder/goals.rs
Original file line number Diff line number Diff line change
@@ -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<BlockPos> for BlockPosGoal {
fn from(pos: BlockPos) -> Self {
Self { pos }
}
}
30 changes: 2 additions & 28 deletions azalea/src/pathfinder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod astar;
pub mod goals;
mod moves;

use crate::bot::{JumpEvent, LookAtEvent};
Expand Down Expand Up @@ -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)));
/// # }
Expand Down Expand Up @@ -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<BlockPos> for BlockPosGoal {
fn from(pos: BlockPos) -> Self {
Self { pos }
}
}

0 comments on commit a839c6a

Please sign in to comment.