azalea/client_impl/interact.rs
1use azalea_client::interact::{EntityInteractEvent, StartUseItemEvent, pick::HitResultComponent};
2use azalea_core::{hit_result::HitResult, position::BlockPos};
3use azalea_protocol::packets::game::s_interact::InteractionHand;
4use bevy_ecs::entity::Entity;
5
6use crate::Client;
7
8impl Client {
9 /// Returns the current [`HitResult`], which is the block or entity in the
10 /// client's crosshair.
11 pub fn hit_result(&self) -> HitResult {
12 (**self.component::<HitResultComponent>()).clone()
13 }
14
15 /// Right-click a block.
16 ///
17 /// The behavior of this depends on the target block,
18 /// and it'll either place the block you're holding in your hand or use the
19 /// block you clicked (like toggling a lever).
20 ///
21 /// Note that this may trigger anticheats as it doesn't take into account
22 /// whether you're actually looking at the block.
23 pub fn block_interact(&self, position: BlockPos) {
24 self.ecs.write().write_message(StartUseItemEvent {
25 entity: self.entity,
26 hand: InteractionHand::MainHand,
27 force_block: Some(position),
28 });
29 }
30
31 /// Right-click an entity.
32 ///
33 /// This can click through walls, which may trigger anticheats. If that
34 /// behavior isn't desired, consider using [`Client::start_use_item`]
35 /// instead.
36 pub fn entity_interact(&self, entity: Entity) {
37 self.ecs.write().trigger(EntityInteractEvent {
38 client: self.entity,
39 target: entity,
40 location: None,
41 });
42 }
43
44 /// Right-click the currently held item.
45 ///
46 /// If the item is consumable, then it'll act as if right-click was held
47 /// until the item finishes being consumed. You can use this to eat food.
48 ///
49 /// If we're looking at a block or entity, then it will be clicked. Also see
50 /// [`Client::block_interact`] and [`Client::entity_interact`].
51 pub fn start_use_item(&self) {
52 self.ecs.write().write_message(StartUseItemEvent {
53 entity: self.entity,
54 hand: InteractionHand::MainHand,
55 force_block: None,
56 });
57 }
58}