make block place the item that is held.

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

View File

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

View File

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

View File

@@ -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<Vec<VarInt>>,
}
impl<'de> Deserialize<'de> for Slot {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@@ -74,3 +77,20 @@ impl<'de> Deserialize<'de> for Slot {
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()
}
}
}

View File

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

View File

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