Skip to content

Commit 567262c

Browse files
authored
Implement swap item in hand (#491)
* Implement swap item in hand * Remove trailing spaces * Use `std::mem::swap` to swap items * Use scope instead of explicit drop
1 parent dd529e2 commit 567262c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

feather/server/src/packet_handlers.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub fn handle_packet(
4747

4848
ClientPlayPacket::ChatMessage(packet) => handle_chat_message(game, player, packet),
4949

50-
ClientPlayPacket::PlayerDigging(packet) => handle_player_digging(game, packet, player_id),
50+
ClientPlayPacket::PlayerDigging(packet) => {
51+
handle_player_digging(game, server, packet, player_id)
52+
}
5153

5254
ClientPlayPacket::CreativeInventoryAction(packet) => {
5355
inventory::handle_creative_inventory_action(player, packet, server)

feather/server/src/packet_handlers/interaction.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{ClientId, NetworkId, Server};
2+
use base::inventory::{SLOT_HOTBAR_OFFSET, SLOT_OFFHAND};
23
use common::entities::player::HotbarSlot;
34
use common::interactable::InteractableRegistry;
4-
use common::Game;
5+
use common::{Game, Window};
56
use ecs::{Entity, EntityRef, SysResult};
67
use libcraft_core::{BlockFace as LibcraftBlockFace, Hand};
78
use libcraft_core::{InteractionType, Vec3f};
@@ -110,13 +111,40 @@ pub fn handle_player_block_placement(
110111
/// * Shooting arrows.
111112
/// * Eating.
112113
/// * 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 {
114120
log::trace!("Got player digging with status {:?}", packet.status);
115121
match packet.status {
116122
PlayerDiggingStatus::StartDigging | PlayerDiggingStatus::CancelDigging => {
117123
game.break_block(packet.position);
118124
Ok(())
119125
}
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+
}
120148
_ => Ok(()),
121149
}
122150
}

0 commit comments

Comments
 (0)