aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-client/src')
-rw-r--r--azalea-client/src/plugins/interact/pick.rs2
-rw-r--r--azalea-client/src/plugins/inventory.rs53
-rw-r--r--azalea-client/src/plugins/mining.rs5
3 files changed, 51 insertions, 9 deletions
diff --git a/azalea-client/src/plugins/interact/pick.rs b/azalea-client/src/plugins/interact/pick.rs
index a0a75910..c32ac5c7 100644
--- a/azalea-client/src/plugins/interact/pick.rs
+++ b/azalea-client/src/plugins/interact/pick.rs
@@ -201,6 +201,8 @@ fn filter_hit_result(hit_result: HitResult, eye_position: Vec3, range: f64) -> H
/// Get the block that a player would be looking at if their eyes were at the
/// given direction and position.
///
+/// This does not consider entities.
+///
/// Also see [`pick`].
pub fn pick_block(
look_direction: LookDirection,
diff --git a/azalea-client/src/plugins/inventory.rs b/azalea-client/src/plugins/inventory.rs
index 8037f8fc..7dfe42c4 100644
--- a/azalea-client/src/plugins/inventory.rs
+++ b/azalea-client/src/plugins/inventory.rs
@@ -1,6 +1,7 @@
use std::{cmp, collections::HashSet};
use azalea_chat::FormattedText;
+use azalea_core::tick::GameTick;
use azalea_entity::PlayerAbilities;
pub use azalea_inventory::*;
use azalea_inventory::{
@@ -46,6 +47,10 @@ impl Plugin for InventoryPlugin {
.chain()
.in_set(InventorySet)
.before(perform_respawn),
+ )
+ .add_systems(
+ GameTick,
+ ensure_has_sent_carried_item.after(super::mining::handle_mining_queued),
);
}
}
@@ -927,15 +932,17 @@ fn handle_set_container_content_event(
}
}
+/// An ECS event to switch our hand to a different hotbar slot.
+///
+/// This is equivalent to using the scroll wheel or number keys in Minecraft.
#[derive(Event)]
pub struct SetSelectedHotbarSlotEvent {
pub entity: Entity,
/// The hotbar slot to select. This should be in the range 0..=8.
pub slot: u8,
}
-fn handle_set_selected_hotbar_slot_event(
+pub fn handle_set_selected_hotbar_slot_event(
mut events: EventReader<SetSelectedHotbarSlotEvent>,
- mut commands: Commands,
mut query: Query<&mut Inventory>,
) {
for event in events.read() {
@@ -947,12 +954,42 @@ fn handle_set_selected_hotbar_slot_event(
}
inventory.selected_hotbar_slot = event.slot;
- commands.trigger(SendPacketEvent::new(
- event.entity,
- ServerboundSetCarriedItem {
- slot: event.slot as u16,
- },
- ));
+ }
+}
+
+/// The item slot that the server thinks we have selected.
+///
+/// See [`ensure_has_sent_carried_item`].
+#[derive(Component)]
+pub struct LastSentSelectedHotbarSlot {
+ pub slot: u8,
+}
+/// A system that makes sure that [`LastSentSelectedHotbarSlot`] is in sync with
+/// [`Inventory::selected_hotbar_slot`].
+///
+/// This is necessary to make sure that [`ServerboundSetCarriedItem`] is sent in
+/// the right order, since it's not allowed to happen outside of a tick.
+pub fn ensure_has_sent_carried_item(
+ mut commands: Commands,
+ query: Query<(Entity, &Inventory, Option<&LastSentSelectedHotbarSlot>)>,
+) {
+ for (entity, inventory, last_sent) in query.iter() {
+ if let Some(last_sent) = last_sent {
+ if last_sent.slot == inventory.selected_hotbar_slot {
+ continue;
+ }
+
+ commands.trigger(SendPacketEvent::new(
+ entity,
+ ServerboundSetCarriedItem {
+ slot: inventory.selected_hotbar_slot as u16,
+ },
+ ));
+ }
+
+ commands.entity(entity).insert(LastSentSelectedHotbarSlot {
+ slot: inventory.selected_hotbar_slot,
+ });
}
}
diff --git a/azalea-client/src/plugins/mining.rs b/azalea-client/src/plugins/mining.rs
index b6ac113a..f9bd50f6 100644
--- a/azalea-client/src/plugins/mining.rs
+++ b/azalea-client/src/plugins/mining.rs
@@ -181,6 +181,9 @@ fn handle_start_mining_block_event(
// we're looking at the block
(block_hit_result.direction, false)
} else {
+ debug!(
+ "Got StartMiningBlockEvent but we're not looking at the block. Picking an arbitrary direction instead."
+ );
// we're not looking at the block, arbitrary direction
(Direction::Down, true)
};
@@ -201,7 +204,7 @@ pub struct MiningQueued {
pub force: bool,
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
-fn handle_mining_queued(
+pub fn handle_mining_queued(
mut commands: Commands,
mut attack_block_events: EventWriter<AttackBlockEvent>,
mut mine_block_progress_events: EventWriter<MineBlockProgressEvent>,