Added Player inventory (WIP)

This commit is contained in:
Snowiiii
2024-08-17 18:52:13 +02:00
parent d6fb4ed121
commit 391d5cbcfc
13 changed files with 79 additions and 10 deletions

1
Cargo.lock generated
View File

@@ -1239,6 +1239,7 @@ version = "0.1.0"
dependencies = [
"num-derive",
"num-traits",
"pumpkin-world",
]
[[package]]

View File

@@ -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"}

View File

@@ -1,5 +1,7 @@
use num_derive::ToPrimitive;
pub mod player;
/// https://wiki.vg/Inventory
#[derive(Debug, ToPrimitive)]
pub enum WindowType {

View File

@@ -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;
}
}

View File

@@ -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::*;

View File

@@ -1,5 +1,4 @@
use pumpkin_macros::packet;
use serde::Deserialize;
use crate::{ServerPacket, VarInt};

View File

@@ -0,0 +1,8 @@
use pumpkin_macros::packet;
use serde::Deserialize;
#[derive(Deserialize)]
#[packet(0x2F)]
pub struct SSetHeldItem {
pub slot: i16,
}

View File

@@ -9,3 +9,6 @@ pub enum Raritiy {
Rare,
Epic,
}
#[derive(Clone, Copy)]
pub struct Item {}

View File

@@ -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;

View File

@@ -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},
},
@@ -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),
}
}

View File

@@ -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;
@@ -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);
}
}

View File

@@ -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};
@@ -15,6 +16,7 @@ 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,
@@ -22,6 +24,7 @@ pub struct Player {
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
@@ -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,
}
}