make block place the item that is held.

This commit is contained in:
Edvin Bryntesson committed 2024-08-20 11:23:21 +02:00
1 parent 3d21a991a0
commit b2430ce3d4
5 files changed
+40 -8

No files matched your search

+5
View File
@@ -105,4 +105,9 @@ impl PlayerInventory {
assert!((0..9).contains(&slot)); assert!((0..9).contains(&slot));
self.selected = 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()
}
} }
@@ -5,6 +5,6 @@ use crate::slot::Slot;
#[derive(serde::Deserialize, Debug)] #[derive(serde::Deserialize, Debug)]
#[packet(0x32)] #[packet(0x32)]
pub struct SSetCreativeSlot { pub struct SSetCreativeSlot {
slot: i16, pub slot: i16,
clicked_item: Slot, pub clicked_item: Slot,
} }
+21 -1
View File
@@ -2,7 +2,7 @@ use serde::{
de::{self, SeqAccess, Visitor}, de::{self, SeqAccess, Visitor},
Deserialize, Deserialize,
}; };
use pumpkin_world::item::Item;
use crate::VarInt; use crate::VarInt;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -15,6 +15,9 @@ pub struct Slot {
components_to_remove: Option<Vec<VarInt>>, components_to_remove: Option<Vec<VarInt>>,
} }
impl<'de> Deserialize<'de> for Slot { impl<'de> Deserialize<'de> for Slot {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@@ -74,3 +77,20 @@ impl<'de> Deserialize<'de> for Slot {
deserializer.deserialize_seq(VarIntVisitor) deserializer.deserialize_seq(VarIntVisitor)
} }
} }
impl Slot {
pub fn to_item(self) -> Option<Item> {
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<Slot> 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()
}
}
}
+2 -2
View File
@@ -13,8 +13,8 @@ pub enum Rarity {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Item { pub struct Item {
item_count: u32, pub item_count: u32,
// This ID is the numerical protocol ID, not the usual minecraft::block ID. // This ID is the numerical protocol ID, not the usual minecraft::block ID.
item_id: u32, pub item_id: u32,
// TODO: Add Item Components // TODO: Add Item Components
} }
+10 -3
View File
@@ -322,7 +322,11 @@ impl Client {
let location = use_item_on.location; let location = use_item_on.location;
let face = BlockFace::from_i32(use_item_on.face.0).unwrap(); let face = BlockFace::from_i32(use_item_on.face.0).unwrap();
let location = WorldPosition(location.0 + face.to_offset()); 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) { 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) { pub fn handle_set_creative_slot(&mut self, _server: &mut Server, packet: SSetCreativeSlot) {
// TODO: handle this let inventory = &mut self.player.as_mut()
dbg!(&packet); .unwrap()
.inventory;
inventory.set_slot(packet.slot as usize, packet.clicked_item.to_item(), false);
} }
} }