|
| 1 | +use crate::block::pumpkin_block::{ |
| 2 | + CanPlaceAtArgs, GetStateForNeighborUpdateArgs, OnPlaceArgs, OnScheduledTickArgs, PumpkinBlock, |
| 3 | +}; |
| 4 | +use crate::world::World; |
| 5 | +use async_trait::async_trait; |
| 6 | +use pumpkin_data::BlockDirection; |
| 7 | +use pumpkin_data::block_properties::HorizontalFacing; |
| 8 | +use pumpkin_data::block_properties::{BlockProperties, LadderLikeProperties}; |
| 9 | +use pumpkin_macros::pumpkin_block; |
| 10 | +use pumpkin_util::math::position::BlockPos; |
| 11 | +use pumpkin_world::BlockStateId; |
| 12 | +use pumpkin_world::tick::TickPriority; |
| 13 | +use pumpkin_world::world::BlockFlags; |
| 14 | + |
| 15 | +#[pumpkin_block("minecraft:ladder")] |
| 16 | +pub struct LadderBlock; |
| 17 | + |
| 18 | +#[async_trait] |
| 19 | +impl PumpkinBlock for LadderBlock { |
| 20 | + async fn on_place(&self, args: OnPlaceArgs<'_>) -> BlockStateId { |
| 21 | + let mut props = LadderLikeProperties::default(args.block); |
| 22 | + props.facing = args.direction.opposite().to_cardinal_direction(); |
| 23 | + props.to_state_id(args.block) |
| 24 | + } |
| 25 | + async fn can_place_at(&self, args: CanPlaceAtArgs<'_>) -> bool { |
| 26 | + args.block_accessor |
| 27 | + .get_block_state(&args.use_item_on.unwrap().position) |
| 28 | + .await |
| 29 | + .is_side_solid(args.direction.opposite()) |
| 30 | + && args.direction.is_horizontal() |
| 31 | + } |
| 32 | + |
| 33 | + async fn get_state_for_neighbor_update( |
| 34 | + &self, |
| 35 | + args: GetStateForNeighborUpdateArgs<'_>, |
| 36 | + ) -> BlockStateId { |
| 37 | + if !can_place_at(args.world, args.position).await { |
| 38 | + args.world |
| 39 | + .schedule_block_tick(args.block, *args.position, 1, TickPriority::Normal) |
| 40 | + .await; |
| 41 | + } |
| 42 | + args.state_id |
| 43 | + } |
| 44 | + |
| 45 | + async fn on_scheduled_tick(&self, args: OnScheduledTickArgs<'_>) { |
| 46 | + if !can_place_at(args.world, args.position).await { |
| 47 | + args.world |
| 48 | + .break_block(args.position, None, BlockFlags::empty()) |
| 49 | + .await; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async fn can_place_at(world: &World, position: &BlockPos) -> bool { |
| 55 | + let props = LadderLikeProperties::from_state_id( |
| 56 | + world.get_block_state_id(position).await, |
| 57 | + world.get_block(position).await, |
| 58 | + ); |
| 59 | + let pos; |
| 60 | + let direction; |
| 61 | + match props.r#facing { |
| 62 | + HorizontalFacing::North => { |
| 63 | + pos = position.add(0, 0, 1); |
| 64 | + direction = BlockDirection::North; |
| 65 | + } |
| 66 | + HorizontalFacing::South => { |
| 67 | + pos = position.add(0, 0, -1); |
| 68 | + direction = BlockDirection::South; |
| 69 | + } |
| 70 | + HorizontalFacing::West => { |
| 71 | + pos = position.add(1, 0, 0); |
| 72 | + direction = BlockDirection::West; |
| 73 | + } |
| 74 | + HorizontalFacing::East => { |
| 75 | + pos = position.add(-1, 0, 0); |
| 76 | + direction = BlockDirection::East; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + world |
| 81 | + .get_block_state(&pos) |
| 82 | + .await |
| 83 | + .is_side_solid(direction.opposite()) |
| 84 | + && direction.is_horizontal() |
| 85 | +} |
0 commit comments