1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

fix brigadier booleans

This commit is contained in:
mat 2023-07-15 16:35:23 -05:00
parent cde7e35046
commit a839c6a923
10 changed files with 69 additions and 42 deletions

View file

@ -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()
}

View file

@ -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},

View file

@ -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::<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>()`.

View file

@ -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<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();
}

View file

@ -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);

View file

@ -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};

View file

@ -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;
/// }
/// }

View file

@ -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;

View file

@ -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 }
}
}

View file

@ -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<BlockPos> for BlockPosGoal {
fn from(pos: BlockPos) -> Self {
Self { pos }
}
}