From f4d36554d7e5ac0c926afbf9c2354494ebc9be11 Mon Sep 17 00:00:00 2001 From: Ryan Johnson Date: Fri, 21 Oct 2022 04:55:15 -0700 Subject: [PATCH] Make replaceable blocks place correctly in the building example. (#129) Closes #114 Note that this is a band-aid solution to a more general problem. In the future, there should be a function in the `block` module to handle all these placement rules. --- examples/building.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/building.rs b/examples/building.rs index 037c88a..a576af8 100644 --- a/examples/building.rs +++ b/examples/building.rs @@ -200,13 +200,22 @@ impl Config for Game { .. } => { if hand == Hand::Main { - let place_at = location.get_in_direction(face); if let Some(stack) = client.held_item() { - if let Some(block_kind) = stack.item.to_block_kind() { - world.chunks.set_block_state( - place_at, - BlockState::from_kind(block_kind), - ); + if let Some(held_block_kind) = stack.item.to_block_kind() { + let block_to_place = BlockState::from_kind(held_block_kind); + + if world + .chunks + .block_state(location) + .map(|s| s.is_replaceable()) + .unwrap_or(false) + { + world.chunks.set_block_state(location, block_to_place); + } else { + let place_at = location.get_in_direction(face); + world.chunks.set_block_state(place_at, block_to_place); + } + if client.game_mode() != GameMode::Creative { client.consume_one_held_item(); }