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
30 changes: 29 additions & 1 deletion feather/common/src/entities/player.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
use anyhow::bail;
use base::EntityKind;
use ecs::EntityBuilder;
use ecs::{EntityBuilder, SysResult};
use quill_common::entities::Player;

pub fn build_default(builder: &mut EntityBuilder) {
super::build_default(builder);
builder.add(Player).add(EntityKind::Player);
}

/// The hotbar slot a player's cursor is currently on
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct HotbarSlot(usize);

impl HotbarSlot {
pub fn new(id: usize) -> Self {
Self(id)
}

pub fn default() -> Self {
Self(0)
}

pub fn get(&self) -> usize {
self.0
}

pub fn set(&mut self, id: usize) -> SysResult {
if id > 8 {
bail!("invalid hotbar slot id");
}

self.0 = id;
Ok(())
}
}
7 changes: 5 additions & 2 deletions feather/server/src/packet_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use base::{Position, Text};
use common::{chat::ChatKind, Game};
use ecs::{Entity, EntityRef, SysResult};
use interaction::{handle_interact_entity, handle_player_block_placement, handle_player_digging};
use interaction::{
handle_held_item_change, handle_interact_entity, handle_player_block_placement,
handle_player_digging,
};
use protocol::{
packets::{
client,
Expand Down Expand Up @@ -56,6 +59,7 @@ pub fn handle_packet(
handle_player_block_placement(game, server, packet, player_id)
}

ClientPlayPacket::HeldItemChange(packet) => handle_held_item_change(player, packet),
ClientPlayPacket::InteractEntity(packet) => {
handle_interact_entity(game, server, packet, player_id)
}
Expand Down Expand Up @@ -89,7 +93,6 @@ pub fn handle_packet(
| ClientPlayPacket::AdvancementTab(_)
| ClientPlayPacket::SelectTrade(_)
| ClientPlayPacket::SetBeaconEffect(_)
| ClientPlayPacket::HeldItemChange(_)
| ClientPlayPacket::UpdateCommandBlock(_)
| ClientPlayPacket::UpdateCommandBlockMinecart(_)
| ClientPlayPacket::UpdateJigsawBlock(_)
Expand Down
47 changes: 42 additions & 5 deletions feather/server/src/packet_handlers/interaction.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use crate::{ClientId, NetworkId, Server};
use common::entities::player::HotbarSlot;
use common::events::InteractEntityEvent;
use common::Game;
use common::{
events::{BlockInteractEvent, BlockPlacementEvent, InteractEntityEvent},
events::{BlockInteractEvent, BlockPlacementEvent},
interactable::InteractableRegistry,
};
use ecs::{Entity, SysResult};
use libcraft_core::{BlockFace as LibcraftBlockFace, Hand, InteractionType, Vec3f};
use ecs::{Entity, EntityRef, SysResult};
use libcraft_core::{BlockFace as LibcraftBlockFace, Hand};
use libcraft_core::{InteractionType, Vec3f};
use protocol::packets::client::{
BlockFace, InteractEntity, InteractEntityKind, PlayerBlockPlacement, PlayerDigging,
PlayerDiggingStatus,
BlockFace, HeldItemChange, InteractEntity, InteractEntityKind, PlayerBlockPlacement,
PlayerDigging, PlayerDiggingStatus,
};

/// Handles the player block placement packet. Currently just removes the block client side for the player.
Expand Down Expand Up @@ -193,3 +196,37 @@ pub fn handle_interact_entity(

Ok(())
}

pub fn handle_held_item_change(player: EntityRef, packet: HeldItemChange) -> SysResult {
let new_id = packet.slot as usize;
let mut slot = player.get_mut::<HotbarSlot>()?;

log::trace!("Got player slot change from {} to {}", slot.get(), new_id);

slot.set(new_id)?;
Ok(())
}

#[cfg(test)]
mod tests {
use common::Game;
use protocol::packets::client::HeldItemChange;

use super::*;

#[test]
fn held_item_change() {
let mut game = Game::new();
let entity = game.ecs.spawn((HotbarSlot::new(0),));
let player = game.ecs.entity(entity).unwrap();

let packet = HeldItemChange { slot: 8 };

handle_held_item_change(player, packet).unwrap();

assert_eq!(
*game.ecs.get::<HotbarSlot>(entity).unwrap(),
HotbarSlot::new(8)
);
}
}
4 changes: 3 additions & 1 deletion feather/server/src/systems/player_join.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use base::{Inventory, Position, Text};
use common::{
chat::{ChatKind, ChatPreference},
entities::player::HotbarSlot,
view::View,
window::BackingWindow,
ChatBox, Game, Window,
Expand Down Expand Up @@ -50,7 +51,8 @@ fn accept_new_player(game: &mut Game, server: &mut Server, client_id: ClientId)
.add(client.profile().to_vec())
.add(ChatBox::new(ChatPreference::All))
.add(inventory)
.add(window);
.add(window)
.add(HotbarSlot::default());

game.spawn_entity(builder);

Expand Down