|
1 | 1 | use crate::{ClientId, NetworkId, Server};
|
| 2 | +use base::inventory::{SLOT_HOTBAR_OFFSET, SLOT_OFFHAND}; |
2 | 3 | use common::entities::player::HotbarSlot;
|
3 | 4 | use common::interactable::InteractableRegistry;
|
4 |
| -use common::Game; |
| 5 | +use common::{Game, Window}; |
5 | 6 | use ecs::{Entity, EntityRef, SysResult};
|
6 | 7 | use libcraft_core::{BlockFace as LibcraftBlockFace, Hand};
|
7 | 8 | use libcraft_core::{InteractionType, Vec3f};
|
@@ -110,13 +111,40 @@ pub fn handle_player_block_placement(
|
110 | 111 | /// * Shooting arrows.
|
111 | 112 | /// * Eating.
|
112 | 113 | /// * Swapping items between the main and off hand.
|
113 |
| -pub fn handle_player_digging(game: &mut Game, packet: PlayerDigging, _player: Entity) -> SysResult { |
| 114 | +pub fn handle_player_digging( |
| 115 | + game: &mut Game, |
| 116 | + server: &mut Server, |
| 117 | + packet: PlayerDigging, |
| 118 | + player: Entity, |
| 119 | +) -> SysResult { |
114 | 120 | log::trace!("Got player digging with status {:?}", packet.status);
|
115 | 121 | match packet.status {
|
116 | 122 | PlayerDiggingStatus::StartDigging | PlayerDiggingStatus::CancelDigging => {
|
117 | 123 | game.break_block(packet.position);
|
118 | 124 | Ok(())
|
119 | 125 | }
|
| 126 | + PlayerDiggingStatus::SwapItemInHand => { |
| 127 | + let window = game.ecs.get::<Window>(player)?; |
| 128 | + |
| 129 | + let hotbar_slot = game.ecs.get::<HotbarSlot>(player)?.get(); |
| 130 | + |
| 131 | + let hotbar_index = SLOT_HOTBAR_OFFSET + hotbar_slot; |
| 132 | + let offhand_index = SLOT_OFFHAND; |
| 133 | + |
| 134 | + { |
| 135 | + let mut hotbar_item = window.item(hotbar_index)?; |
| 136 | + let mut offhand_item = window.item(offhand_index)?; |
| 137 | + |
| 138 | + std::mem::swap(&mut *hotbar_item, &mut *offhand_item); |
| 139 | + } |
| 140 | + |
| 141 | + let client_id = *game.ecs.get::<ClientId>(player)?; |
| 142 | + let client = server.clients.get(client_id).unwrap(); |
| 143 | + |
| 144 | + client.send_window_items(&window); |
| 145 | + |
| 146 | + Ok(()) |
| 147 | + } |
120 | 148 | _ => Ok(()),
|
121 | 149 | }
|
122 | 150 | }
|
|
0 commit comments