Use scoping to decrease the amount of drops needed
@@ -79,15 +79,15 @@ pub fn handle_block_placement(
) {
let gamemode = *world.get::<Gamemode>(player);
+ let item = {
let inventory = world.get::<Inventory>(player);
-
- let item = match inventory.item_in_main_hand(player, world) {
+ match inventory.item_in_main_hand(player, world) {
Some(item) => item,
+ // Offhand?
None => return, // No block to place
+ }
};
- drop(inventory);
-
let block = match item.ty.to_block() {
Some(block) => block,
None => return, // Item is not a block
@@ -103,31 +103,41 @@ pub fn handle_block_placement(
game.set_block_at(world, pos, block, BlockUpdateCause::Entity(player));
- let held_item = world.get::<HeldItem>(player).0;
- let inventory = world.get::<Inventory>(player);
-
// Update player's inventory if in survival
- if gamemode == Gamemode::Survival {
+ let event = {
+ let event;
+
+ if gamemode != Gamemode::Creative {
if item.amount == 0 {
- drop(inventory);
game.disconnect(
player,
world,
- "attempted to place block with zero-sized item stack",
+ "Attempted to place block with zero-sized item stack.",
);
return;
}
+ let held_item = world.get::<HeldItem>(player).0;
+ let inventory = world.get::<Inventory>(player);
+
let item = ItemStack::new(item.ty, item.amount - 1);
inventory
.set_item_at(Area::Hotbar, held_item, item)
.unwrap();
- let event = InventoryUpdateEvent {
+ event = Some(InventoryUpdateEvent {
slots: smallvec![slot(Area::Hotbar, held_item)],
player,
+ });
+ } else {
+ event = None;
+ }
+
+ event
};
- drop(inventory);
+
+ if let Some(event) = event {
+ // Only send the event to decrement the held stack if the player's gamemode is survival
game.handle(world, event);
}
}
