From b2430ce3d44cfd6ffb56576768b7768424fc4f49 Mon Sep 17 00:00:00 2001 From: Edvin Bryntesson Date: Tue, 20 Aug 2024 11:23:21 +0200 Subject: [PATCH] make block place the item that is held. --- pumpkin-inventory/src/player.rs | 5 +++++ .../src/server/play/s_set_creative_slot.rs | 4 ++-- pumpkin-protocol/src/slot.rs | 22 ++++++++++++++++++- pumpkin-world/src/item/mod.rs | 4 ++-- pumpkin/src/client/player_packet.rs | 13 ++++++++--- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/pumpkin-inventory/src/player.rs b/pumpkin-inventory/src/player.rs index 5b8aa2e40..9133a7909 100644 --- a/pumpkin-inventory/src/player.rs +++ b/pumpkin-inventory/src/player.rs @@ -105,4 +105,9 @@ impl PlayerInventory { assert!((0..9).contains(&slot)); self.selected = slot; } + + pub fn held_item(&self) -> Option<&Item> { + debug_assert!((0..9).contains(&self.selected)); + self.items[self.selected+36-8].as_ref() + } } diff --git a/pumpkin-protocol/src/server/play/s_set_creative_slot.rs b/pumpkin-protocol/src/server/play/s_set_creative_slot.rs index 7a2cc4adc..ce0b7490d 100644 --- a/pumpkin-protocol/src/server/play/s_set_creative_slot.rs +++ b/pumpkin-protocol/src/server/play/s_set_creative_slot.rs @@ -5,6 +5,6 @@ use crate::slot::Slot; #[derive(serde::Deserialize, Debug)] #[packet(0x32)] pub struct SSetCreativeSlot { - slot: i16, - clicked_item: Slot, + pub slot: i16, + pub clicked_item: Slot, } diff --git a/pumpkin-protocol/src/slot.rs b/pumpkin-protocol/src/slot.rs index f6b3d8370..c79a211de 100644 --- a/pumpkin-protocol/src/slot.rs +++ b/pumpkin-protocol/src/slot.rs @@ -2,7 +2,7 @@ use serde::{ de::{self, SeqAccess, Visitor}, Deserialize, }; - +use pumpkin_world::item::Item; use crate::VarInt; #[derive(Debug, Clone)] @@ -15,6 +15,9 @@ pub struct Slot { components_to_remove: Option>, } + + + impl<'de> Deserialize<'de> for Slot { fn deserialize(deserializer: D) -> Result where @@ -74,3 +77,20 @@ impl<'de> Deserialize<'de> for Slot { deserializer.deserialize_seq(VarIntVisitor) } } +impl Slot { + pub fn to_item(self) -> Option { + let item_id = self.item_id?.0.try_into().unwrap(); + Some(Item { + item_id, + item_count: self.item_count.0.try_into().unwrap(), + }) + } +} +impl From for Item { + fn from(slot: Slot) -> Self { + Item { + item_count: slot.item_count.0.try_into().unwrap(), + item_id: slot.item_id.unwrap().0.try_into().unwrap() + } + } +} \ No newline at end of file diff --git a/pumpkin-world/src/item/mod.rs b/pumpkin-world/src/item/mod.rs index 7bf441202..c0e119074 100644 --- a/pumpkin-world/src/item/mod.rs +++ b/pumpkin-world/src/item/mod.rs @@ -13,8 +13,8 @@ pub enum Rarity { #[derive(Clone, Copy)] pub struct Item { - item_count: u32, + pub item_count: u32, // This ID is the numerical protocol ID, not the usual minecraft::block ID. - item_id: u32, + pub item_id: u32, // TODO: Add Item Components } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 0904e1b9d..78fd3b7f0 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -322,7 +322,11 @@ impl Client { let location = use_item_on.location; let face = BlockFace::from_i32(use_item_on.face.0).unwrap(); let location = WorldPosition(location.0 + face.to_offset()); - server.broadcast_packet(self, &CBlockUpdate::new(location, 11.into())); + // TODO: + // - Add checking for if used item is a block + if let Some(item) = self.player.as_ref().unwrap().inventory.held_item() { + server.broadcast_packet(self, &CBlockUpdate::new(location, item.item_id.into())); + } } pub fn handle_set_held_item(&mut self, _server: &mut Server, held: SSetHeldItem) { @@ -335,7 +339,10 @@ impl Client { } pub fn handle_set_creative_slot(&mut self, _server: &mut Server, packet: SSetCreativeSlot) { - // TODO: handle this - dbg!(&packet); + let inventory = &mut self.player.as_mut() + .unwrap() + .inventory; + + inventory.set_slot(packet.slot as usize, packet.clicked_item.to_item(), false); } }