Decrease Block count when Placed

This commit is contained in:
Snowiiii 2024-11-09 11:33:14 +01:00
parent 0ab4b5daed
commit 65cb0b52b4
2 changed files with 21 additions and 1 deletions

View File

@ -120,6 +120,16 @@ impl PlayerInventory {
self.items[self.selected + 36 - 9].as_ref()
}
pub fn held_item_mut(&mut self) -> Option<&mut ItemStack> {
debug_assert!((0..9).contains(&self.selected));
self.items[self.selected + 36 - 9].as_mut()
}
pub fn empty_held_item(&mut self) {
debug_assert!((0..9).contains(&self.selected));
self.items[self.selected + 36 - 9] = None
}
pub fn slots(&self) -> Vec<Option<&ItemStack>> {
let mut slots = vec![self.crafting_output.as_ref()];
slots.extend(self.crafting.iter().map(|c| c.as_ref()));

View File

@ -560,13 +560,23 @@ impl Player {
}
if let Some(face) = BlockFace::from_i32(use_item_on.face.0) {
if let Some(item) = self.inventory.lock().await.held_item() {
let mut inventory = self.inventory.lock().await;
if let Some(item) = inventory.held_item_mut() {
let block = get_block_by_item(item.item_id);
// check if item is a block, Because Not every item can be placed :D
if let Some(block) = block {
let entity = &self.living_entity.entity;
let world = &entity.world;
// TODO: Config
// Decrease Block count
if self.gamemode.load() != GameMode::Creative {
item.item_count -= 1;
if item.item_count == 0 {
inventory.empty_held_item();
}
}
world
.set_block(
WorldPosition(location.0 + face.to_offset()),