Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion feather/server/src/packet_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub fn handle_packet(

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

ClientPlayPacket::PlayerDigging(packet) => handle_player_digging(game, packet, player_id),
ClientPlayPacket::PlayerDigging(packet) => {
handle_player_digging(game, server, packet, player_id)
}

ClientPlayPacket::CreativeInventoryAction(packet) => {
inventory::handle_creative_inventory_action(player, packet, server)
Expand Down
32 changes: 30 additions & 2 deletions feather/server/src/packet_handlers/interaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{ClientId, NetworkId, Server};
use base::inventory::{SLOT_HOTBAR_OFFSET, SLOT_OFFHAND};
use common::entities::player::HotbarSlot;
use common::interactable::InteractableRegistry;
use common::Game;
use common::{Game, Window};
use ecs::{Entity, EntityRef, SysResult};
use libcraft_core::{BlockFace as LibcraftBlockFace, Hand};
use libcraft_core::{InteractionType, Vec3f};
Expand Down Expand Up @@ -110,13 +111,40 @@ pub fn handle_player_block_placement(
/// * Shooting arrows.
/// * Eating.
/// * Swapping items between the main and off hand.
pub fn handle_player_digging(game: &mut Game, packet: PlayerDigging, _player: Entity) -> SysResult {
pub fn handle_player_digging(
game: &mut Game,
server: &mut Server,
packet: PlayerDigging,
player: Entity,
) -> SysResult {
log::trace!("Got player digging with status {:?}", packet.status);
match packet.status {
PlayerDiggingStatus::StartDigging | PlayerDiggingStatus::CancelDigging => {
game.break_block(packet.position);
Ok(())
}
PlayerDiggingStatus::SwapItemInHand => {
let window = game.ecs.get::<Window>(player)?;

let hotbar_slot = game.ecs.get::<HotbarSlot>(player)?.get();

let hotbar_index = SLOT_HOTBAR_OFFSET + hotbar_slot;
let offhand_index = SLOT_OFFHAND;

{
let mut hotbar_item = window.item(hotbar_index)?;
let mut offhand_item = window.item(offhand_index)?;

std::mem::swap(&mut *hotbar_item, &mut *offhand_item);
}

let client_id = *game.ecs.get::<ClientId>(player)?;
let client = server.clients.get(client_id).unwrap();

client.send_window_items(&window);

Ok(())
}
_ => Ok(()),
}
}
Expand Down