mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
start adding interact function
This commit is contained in:
parent
7124e65857
commit
c12592f6ad
4 changed files with 76 additions and 0 deletions
68
azalea-client/src/interact.rs
Normal file
68
azalea-client/src/interact.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use azalea_core::BlockPos;
|
||||
use azalea_ecs::{
|
||||
app::{App, Plugin},
|
||||
entity::Entity,
|
||||
event::EventReader,
|
||||
system::Query,
|
||||
};
|
||||
use azalea_protocol::packets::game::{
|
||||
serverbound_interact_packet::InteractionHand,
|
||||
serverbound_use_item_on_packet::ServerboundUseItemOnPacket,
|
||||
};
|
||||
use log::warn;
|
||||
|
||||
use crate::{Client, LocalPlayer};
|
||||
|
||||
/// A plugin that allows clients to interact with blocks in the world.
|
||||
pub struct InteractPlugin;
|
||||
impl Plugin for InteractPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_event::<BlockInteractEvent>()
|
||||
.add_system(handle_block_interact_event);
|
||||
}
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Right click a block. The behavior of this depends on the target block,
|
||||
/// and it'll either place the block you're holding in your hand or use the
|
||||
/// block you clicked (like toggling a lever).
|
||||
///
|
||||
/// Note that this may trigger anticheats as it doesn't take into account
|
||||
/// whether you're actually looking at the block.
|
||||
pub fn block_interact(&mut self, position: BlockPos) {
|
||||
self.ecs.lock().send_event(BlockInteractEvent {
|
||||
entity: self.entity,
|
||||
position,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Right click a block. The behavior of this depends on the target block,
|
||||
/// and it'll either place the block you're holding in your hand or use the
|
||||
/// block you clicked (like toggling a lever).
|
||||
pub struct BlockInteractEvent {
|
||||
/// The local player entity that's opening the container.
|
||||
pub entity: Entity,
|
||||
/// The coordinates of the container.
|
||||
pub position: BlockPos,
|
||||
}
|
||||
|
||||
fn handle_block_interact_event(
|
||||
mut events: EventReader<BlockInteractEvent>,
|
||||
query: Query<&LocalPlayer>,
|
||||
) {
|
||||
for event in events.iter() {
|
||||
let Ok(local_player) = query.get(event.entity) else {
|
||||
warn!("Sent BlockInteractEvent for entity that isn't LocalPlayer");
|
||||
continue;
|
||||
};
|
||||
|
||||
// TODO: check to make sure we're within the world border
|
||||
|
||||
local_player.write_packet(ServerboundUseItemOnPacket {
|
||||
hand: InteractionHand::MainHand,
|
||||
block_hit: BlockHitResult,
|
||||
sequence: 0,
|
||||
})
|
||||
}
|
||||
}
|
|
@ -63,6 +63,7 @@ impl InventoryComponent {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InventoryComponent {
|
||||
fn default() -> Self {
|
||||
InventoryComponent {
|
||||
|
|
|
@ -18,6 +18,7 @@ pub mod disconnect;
|
|||
mod entity_query;
|
||||
mod events;
|
||||
mod get_mc_dir;
|
||||
pub mod interact;
|
||||
pub mod inventory;
|
||||
mod local_player;
|
||||
mod movement;
|
||||
|
|
|
@ -14,9 +14,15 @@ pub struct ServerboundUseItemOnPacket {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BlockHitResult {
|
||||
/// The block that we clicked.
|
||||
pub block_pos: BlockPos,
|
||||
/// The face of the block that was clicked.
|
||||
pub direction: Direction,
|
||||
/// The exact coordinates of the world where the block was clicked. In the
|
||||
/// network, this is transmitted as the difference between the location and
|
||||
/// block position.
|
||||
pub location: Vec3,
|
||||
/// Whether the player's head is inside of a block.
|
||||
pub inside: bool,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue