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

make some stuff simpler

This commit is contained in:
mat 2023-03-12 22:15:41 -05:00
parent df87702875
commit 59f65cb046
6 changed files with 49 additions and 23 deletions

View file

@ -17,6 +17,7 @@ use crate::{
use azalea_auth::{game_profile::GameProfile, sessionserver::ClientSessionServerError};
use azalea_chat::FormattedText;
use azalea_core::Vec3;
use azalea_physics::{PhysicsPlugin, PhysicsSet};
use azalea_protocol::{
connect::{Connection, ConnectionError},
@ -39,7 +40,7 @@ use azalea_protocol::{
resolver, ServerAddress,
};
use azalea_world::{
entity::{EntityPlugin, EntityUpdateSet, Local, WorldName},
entity::{EntityPlugin, EntityUpdateSet, Local, Position, WorldName},
Instance, InstanceContainer, PartialInstance,
};
use bevy_app::{App, CoreSchedule, Plugin, PluginGroup, PluginGroupBuilder};
@ -497,6 +498,15 @@ impl Client {
}
}
impl Client {
/// Get the position of this client.
///
/// This is a shortcut for `Vec3::from(&bot.component::<Position>())`.
pub fn position(&self) -> Vec3 {
Vec3::from(&self.component::<Position>())
}
}
/// A bundle for the components that are present on a local player that received
/// a login packet. If you want to filter for this, just use [`Local`].
#[derive(Bundle)]

View file

@ -142,6 +142,11 @@ impl Debug for EntityUuid {
/// automatically.
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Deref, DerefMut)]
pub struct Position(Vec3);
impl From<&Position> for Vec3 {
fn from(value: &Position) -> Self {
value.0
}
}
impl From<Position> for ChunkPos {
fn from(value: Position) -> Self {
ChunkPos::from(&value.0)
@ -166,6 +171,11 @@ impl From<&Position> for BlockPos {
/// The last position of the entity that was sent over the network.
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Deref, DerefMut)]
pub struct LastSentPosition(Vec3);
impl From<&LastSentPosition> for Vec3 {
fn from(value: &LastSentPosition) -> Self {
value.0
}
}
impl From<LastSentPosition> for ChunkPos {
fn from(value: LastSentPosition) -> Self {
ChunkPos::from(&value.0)

View file

@ -195,6 +195,12 @@ impl Instance {
///
/// Note that this is sorted by `x+y+z` and not `x^2+y^2+z^2`, for
/// optimization purposes.
///
/// ```
/// # fn example(client: &azalea_client::Client) {
/// client.find_block(client.position(), &azalea_registry::Block::Chest.into());
/// # }
/// ```
pub fn find_block(
&self,
nearest_to: impl Into<BlockPos>,

View file

@ -1,7 +1,7 @@
//! Take wool from a chest and put one random piece of wool in every inventory
//! slot
use azalea::prelude::*;
use azalea::{entity::Position, prelude::*};
use parking_lot::Mutex;
use std::sync::Arc;
@ -10,18 +10,14 @@ async fn main() {
let account = Account::offline("bot");
// or let bot = Account::microsoft("email").await;
azalea::start(azalea::Options {
account,
address: "localhost",
state: State::default(),
plugins: plugins![],
handle,
})
.await
.unwrap();
ClientBuilder::new()
.set_handler(handle)
.start(account, "localhost")
.await
.unwrap();
}
#[derive(Default, Clone)]
#[derive(Default, Clone, Component)]
struct State {
pub started: Arc<Mutex<bool>>,
}
@ -43,12 +39,16 @@ async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
*state.started.lock() = true;
}
let chest_block = bot.world().find_one_block(|b| b.id == "minecraft:chest");
bot.goto(chest_block);
let chest = bot
.open_container(&bot.world().find_one_block(|b| b.id == "minecraft:chest"))
.await
.unwrap();
let chest_block = bot
.world()
.read()
.find_block(bot.position(), &azalea::Block::Chest.into());
let Some(chest_block) = chest_block else {
bot.chat("No chest found");
return Ok(());
};
bot.goto(chest_block.into());
let chest = bot.open_container(&chest_block).await.unwrap();
bot.take_amount_from_container(&chest, 5, |i| i.id == "#minecraft:planks")
.await;
chest.close().await;
@ -69,7 +69,7 @@ async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
bot.hold(&pickaxe);
loop {
if let Err(e) = bot.dig(bot.entity().feet_pos().down(1)).await {
if let Err(e) = bot.dig(bot.position().down(1.).into()).await {
println!("{:?}", e);
break;
}

View file

@ -114,7 +114,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
bot.chat(&format!("You're at {pos:?}",));
}
"whereareyou" => {
let pos = bot.component::<Position>();
let pos = bot.position();
bot.chat(&format!("I'm at {pos:?}",));
}
"goto" => {
@ -149,14 +149,14 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
}
"findblock" => {
let target_pos = bot.world().read().find_block(
bot.component::<Position>(),
bot.position(),
&azalea_registry::Block::DiamondBlock.into(),
);
bot.chat(&format!("target_pos: {target_pos:?}",));
}
"gotoblock" => {
let target_pos = bot.world().read().find_block(
bot.component::<Position>(),
bot.position(),
&azalea_registry::Block::DiamondBlock.into(),
);
if let Some(target_pos) = target_pos {

View file

@ -11,7 +11,7 @@ pub use azalea_block as blocks;
pub use azalea_client::*;
pub use azalea_core::{BlockPos, Vec3};
pub use azalea_protocol as protocol;
pub use azalea_registry::EntityKind;
pub use azalea_registry::{Block, EntityKind};
pub use azalea_world::{entity, Instance};
use bot::DefaultBotPlugins;
use ecs::component::Component;