mirror of
https://github.com/Pumpkin-MC/Pumpkin.git
synced 2026-08-30 20:14:23 +00:00
Added Player inventory (WIP)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1239,6 +1239,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"pumpkin-world",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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"}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use num_derive::ToPrimitive;
|
||||
|
||||
pub mod player;
|
||||
|
||||
/// https://wiki.vg/Inventory
|
||||
#[derive(Debug, ToPrimitive)]
|
||||
pub enum WindowType {
|
||||
|
||||
35
pumpkin-inventory/src/player.rs
Normal file
35
pumpkin-inventory/src/player.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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::*;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use pumpkin_macros::packet;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{ServerPacket, VarInt};
|
||||
|
||||
8
pumpkin-protocol/src/server/play/s_set_held_item.rs
Normal file
8
pumpkin-protocol/src/server/play/s_set_held_item.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use pumpkin_macros::packet;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[packet(0x2F)]
|
||||
pub struct SSetHeldItem {
|
||||
pub slot: i16,
|
||||
}
|
||||
@@ -9,3 +9,6 @@ pub enum Raritiy {
|
||||
Rare,
|
||||
Epic,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Item {}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user