Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #574 "All torches place in a wall state" #618

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
12cee7d
add a vertical attachment type and register a torch block
lwalton101 Mar 9, 2025
129eac0
remove unused imports
lwalton101 Mar 9, 2025
403eb02
added redstone torch to manager
lwalton101 Mar 9, 2025
b344532
register soul torches as a vertical attachment block
lwalton101 Mar 9, 2025
c38ba61
fix redstone torch facing by adding lit block state
lwalton101 Mar 9, 2025
c852053
remove unused import
lwalton101 Mar 9, 2025
2e31ca1
remove debug statement
lwalton101 Mar 9, 2025
b8eea0b
remove unused import again
lwalton101 Mar 9, 2025
23ccd5a
use clippy too many arguments for on_place
lwalton101 Mar 9, 2025
7472a0b
actually allow clippy too many arguments for on_place
lwalton101 Mar 9, 2025
b859565
cargo fmt
lwalton101 Mar 9, 2025
f6ace1d
Merge remote-tracking branch 'origin/master' into torch-direction-fix
lwalton101 Mar 10, 2025
c0bbb13
fix torches, but only torches
lwalton101 Mar 11, 2025
6eb2f67
remove unused imports
lwalton101 Mar 11, 2025
c2007ef
remove old lit component
lwalton101 Mar 16, 2025
a94adb0
all torches work
lwalton101 Mar 16, 2025
e135241
remove unused imports
lwalton101 Mar 16, 2025
c065a7e
cargo fmt
lwalton101 Mar 16, 2025
1de8ad2
Update branch to be up to date with main on Pumpkin-MC/Pumpkin
lwalton101 Mar 16, 2025
ed5b03f
remove leftover file from before property revamp
lwalton101 Mar 16, 2025
6b2be44
Merge remote-tracking branch 'origin/torch-direction-fix' into torch-…
lwalton101 Mar 16, 2025
c41dd94
fix clippy
lwalton101 Mar 16, 2025
c33c53b
fix clippy
lwalton101 Mar 16, 2025
db80634
remove as much duplication as possible
lwalton101 Mar 17, 2025
4adce4c
cargo fmt
lwalton101 Mar 17, 2025
2e2d2e2
remove underscore from _face
lwalton101 Mar 17, 2025
6d696ce
cargo fmt
lwalton101 Mar 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pumpkin/src/block/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) mod jukebox;
pub(crate) mod lever;
pub(crate) mod logs;
pub(crate) mod tnt;
pub(crate) mod torch;

/// The standard destroy with container removes the player forcibly from the container,
/// drops items to the floor, and back to the player's inventory if the item stack is in movement.
Expand Down
177 changes: 177 additions & 0 deletions pumpkin/src/block/blocks/torch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
use async_trait::async_trait;
use log::error;
use pumpkin_data::block::{
Block, BlockProperties, EnumVariants, FurnaceLikeProperties, HorizontalFacing,
WallTorchLikeProperties,
};
use pumpkin_macros::pumpkin_block;
use pumpkin_protocol::server::play::SUseItemOn;
use pumpkin_util::math::position::BlockPos;
use pumpkin_world::block::BlockDirection;

use crate::{block::pumpkin_block::PumpkinBlock, server::Server, world::World};

#[pumpkin_block("minecraft:torch")]
pub struct TorchBlock;
#[pumpkin_block("minecraft:redstone_torch")]
pub struct RedstoneTorchBlock;
#[pumpkin_block("minecraft:soul_torch")]
pub struct SoulTorchBlock;
#[async_trait]
impl PumpkinBlock for TorchBlock {
async fn on_place(
&self,
_server: &Server,
world: &World,
block: &Block,
face: &BlockDirection,
block_pos: &BlockPos,
_use_item_on: &SUseItemOn,
player_direction: &HorizontalFacing,
_other: bool,
) -> u16 {
if face == &BlockDirection::Down {
block.default_state_id
} else {
let props = get_wall_block_props(
world,
block,
block_pos,
face,
player_direction,
WallTorchLikeProperties::default(&Block::WALL_TORCH).to_props(),
)
.await;
WallTorchLikeProperties::from_props(props, &Block::WALL_TORCH)
.to_state_id(&Block::WALL_TORCH)
}
}
}

#[async_trait]
impl PumpkinBlock for RedstoneTorchBlock {
async fn on_place(
&self,
_server: &Server,
world: &World,
block: &Block,
face: &BlockDirection,
block_pos: &BlockPos,
_use_item_on: &SUseItemOn,
player_direction: &HorizontalFacing,
_other: bool,
) -> u16 {
if face == &BlockDirection::Down {
block.default_state_id
} else {
let props = get_wall_block_props(
world,
block,
block_pos,
face,
player_direction,
FurnaceLikeProperties::default(&Block::REDSTONE_WALL_TORCH).to_props(),
)
.await;
FurnaceLikeProperties::from_props(props, &Block::REDSTONE_WALL_TORCH)
.to_state_id(&Block::REDSTONE_WALL_TORCH)
}
}
}

#[async_trait]
impl PumpkinBlock for SoulTorchBlock {
async fn on_place(
&self,
_server: &Server,
world: &World,
block: &Block,
face: &BlockDirection,
block_pos: &BlockPos,
_use_item_on: &SUseItemOn,
player_direction: &HorizontalFacing,
_other: bool,
) -> u16 {
if face == &BlockDirection::Down {
block.default_state_id
} else {
let props = get_wall_block_props(
world,
block,
block_pos,
face,
player_direction,
WallTorchLikeProperties::default(&Block::SOUL_WALL_TORCH).to_props(),
)
.await;
WallTorchLikeProperties::from_props(props, &Block::SOUL_WALL_TORCH)
.to_state_id(&Block::SOUL_WALL_TORCH)
}
}
}
async fn get_wall_block_props(
world: &World,
block: &Block,
block_pos: &BlockPos,
face: &BlockDirection,
player_direction: &HorizontalFacing,
mut block_properties: Vec<(String, String)>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of serializing the block properties, you should mutate the LikeProperties struct itself, which contains enums of valid states.

) -> Vec<(String, String)> {
let contains_facing = block_properties.iter().any(|(key, _)| key == "facing");

if !contains_facing {
error!("Cannot find facing property in block {}", block.name);
return Block::AIR
.properties(Block::AIR.default_state_id)
.unwrap()
.to_props();
}

let facing_index = block_properties
.iter()
.position(|(key, _)| key == "facing")
.unwrap();

match face {
BlockDirection::North
| BlockDirection::South
| BlockDirection::West
| BlockDirection::East => {
block_properties[facing_index].1 = face
.to_cardinal_direction()
.opposite()
.to_value()
.to_string();
return block_properties;
}
BlockDirection::Up => {
let mut possible_directions = vec![];
for bd in BlockDirection::horizontal() {
let block_offset = bd.to_offset();
let base_block_pos = block_pos.offset(block_offset);
let base_block = world.get_block(&base_block_pos).await.unwrap();
if base_block.id != 0 {
possible_directions.push(bd.to_cardinal_direction());
}
}

return if possible_directions.contains(player_direction) {
block_properties[facing_index].1 =
player_direction.opposite().to_value().to_string();
block_properties
} else if possible_directions.is_empty() {
block_properties[facing_index].1 =
possible_directions[0].opposite().to_value().to_string();
block_properties
} else {
Block::AIR
.properties(Block::AIR.default_state_id)
.unwrap()
.to_props()
};
}
BlockDirection::Down => {}
}

block_properties
}
4 changes: 4 additions & 0 deletions pumpkin/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use pumpkin_util::math::vector3::Vector3;
use pumpkin_world::item::ItemStack;
use rand::Rng;

use crate::block::blocks::torch::{RedstoneTorchBlock, SoulTorchBlock, TorchBlock};
use crate::block::registry::BlockRegistry;
use crate::entity::item::ItemEntity;
use crate::world::World;
Expand All @@ -35,6 +36,9 @@ pub fn default_registry() -> Arc<BlockRegistry> {
manager.register(ChestBlock);
manager.register(TNTBlock);
manager.register(LeverBlock);
manager.register(TorchBlock);
manager.register(RedstoneTorchBlock);
manager.register(SoulTorchBlock);

register_door_blocks(&mut manager);
register_fence_blocks(&mut manager);
Expand Down