Skip to content

Commit

Permalink
Added Player inventory (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 17, 2024
1 parent d6fb4ed commit 391d5cb
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pumpkin-inventory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ edition.workspace = true

[dependencies]
num-traits = "0.2"
num-derive = "0.4"
num-derive = "0.4"

# For items
pumpkin-world = { path = "../pumpkin-world"}
2 changes: 2 additions & 0 deletions pumpkin-inventory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use num_derive::ToPrimitive;

pub mod player;

/// https://wiki.vg/Inventory
#[derive(Debug, ToPrimitive)]
pub enum WindowType {
Expand Down
35 changes: 35 additions & 0 deletions pumpkin-inventory/src/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use pumpkin_world::item::Item;

pub struct PlayerInventory {
// Main Inventory + Hotbar
items: [Option<Item>; 36],
armor: [Option<Item>; 4],
offhand: Option<Item>,
// current selected slot in hortbar
selected: i16,
}

impl Default for PlayerInventory {
fn default() -> Self {
Self::new()
}
}

impl PlayerInventory {
pub fn new() -> Self {
Self {
items: [None; 36],
armor: [None; 4],
offhand: None,
// TODO: What when player spawns in with an diffrent index ?
selected: 0,
}
}

pub fn set_slot(slot: u32, item: Item) {}

pub fn set_selected(&mut self, slot: i16) {
assert!((0..9).contains(&slot));
self.selected = slot;
}
}
10 changes: 6 additions & 4 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
mod c_client_information;
mod c_interact;
mod s_chat_command;
mod s_chat_message;
mod s_client_information;
mod s_confirm_teleport;
mod s_interact;
mod s_player_action;
mod s_player_command;
mod s_player_position;
mod s_player_position_rotation;
mod s_player_rotation;
mod s_set_held_item;
mod s_swing_arm;
mod s_use_item_on;

pub use c_client_information::*;
pub use c_interact::*;
pub use s_chat_command::*;
pub use s_chat_message::*;
pub use s_client_information::*;
pub use s_confirm_teleport::*;
pub use s_interact::*;
pub use s_player_action::*;
pub use s_player_command::*;
pub use s_player_position::*;
pub use s_player_position_rotation::*;
pub use s_player_rotation::*;
pub use s_set_held_item::*;
pub use s_swing_arm::*;
pub use s_use_item_on::*;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use pumpkin_macros::packet;
use serde::Deserialize;

use crate::{ServerPacket, VarInt};

Expand Down
8 changes: 8 additions & 0 deletions pumpkin-protocol/src/server/play/s_set_held_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pumpkin_macros::packet;
use serde::Deserialize;

#[derive(Deserialize)]
#[packet(0x2F)]
pub struct SSetHeldItem {
pub slot: i16,
}
3 changes: 3 additions & 0 deletions pumpkin-world/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ pub enum Raritiy {
Rare,
Epic,
}

#[derive(Clone, Copy)]
pub struct Item {}
4 changes: 2 additions & 2 deletions pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod dimension;
pub const WORLD_HEIGHT: usize = 384;
pub const WORLD_Y_START_AT: i32 = -64;
pub const DIRECT_PALETTE_BITS: u32 = 15;
mod block;
pub mod block;
mod global_registry;
mod item;
pub mod item;
pub mod radial_chunk_iterator;
mod world;
5 changes: 4 additions & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use pumpkin_protocol::{
play::{
SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract,
SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation, SSwingArm, SUseItemOn,
SPlayerRotation, SSetHeldItem, SSwingArm, SUseItemOn,
},
status::{SPingRequest, SStatusRequest},
},
Expand Down Expand Up @@ -301,6 +301,9 @@ impl Client {
SUseItemOn::PACKET_ID => {
self.handle_use_item_on(server, SUseItemOn::read(bytebuf).unwrap())
}
SSetHeldItem::PACKET_ID => {
self.handle_set_held_item(server, SSetHeldItem::read(bytebuf).unwrap())
}
_ => log::error!("Failed to handle player packet id {}", packet.id.0),
}
}
Expand Down
11 changes: 10 additions & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pumpkin_protocol::{
server::play::{
Action, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract,
SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation,
SSwingArm, SUseItemOn,
SSetHeldItem, SSwingArm, SUseItemOn,
},
};
use pumpkin_text::TextComponent;
Expand Down Expand Up @@ -320,4 +320,13 @@ impl Client {
location.y += 2;
server.broadcast_packet(self, &CBlockUpdate::new(location, 11.into()));
}

pub fn handle_set_held_item(&mut self, _server: &mut Server, held: SSetHeldItem) {
let slot = held.slot;
if !(0..=8).contains(&slot) {
self.kick("Invalid held slot")
}
let player = self.player.as_mut().unwrap();
player.inventory.set_selected(slot);
}
}
4 changes: 4 additions & 0 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::str::FromStr;

use num_derive::{FromPrimitive, ToPrimitive};
use pumpkin_entity::{entity_type::EntityType, Entity, EntityId};
use pumpkin_inventory::player::PlayerInventory;
use pumpkin_protocol::VarInt;
use serde::{Deserialize, Serialize};

Expand All @@ -15,13 +16,15 @@ pub struct Player {
pub health: f32,
pub food: i32,
pub food_saturation: f32,
pub inventory: PlayerInventory,

// Client side value, Should be not trusted
pub on_ground: bool,

pub sneaking: bool,
pub sprinting: bool,

// TODO: prbly should put this into an Living Entitiy or something
pub velocity: Vec3,

// Current awaiting teleport id, None if did not teleport
Expand All @@ -41,6 +44,7 @@ impl Player {
food: 20,
food_saturation: 20.0,
velocity: Vec3::new(0.0, 0.0, 0.0),
inventory: PlayerInventory::new(),
gamemode,
}
}
Expand Down

0 comments on commit 391d5cb

Please sign in to comment.