84 lines
2.6 KiB
Rust
84 lines
2.6 KiB
Rust
use crate::block::registry::BlockActionResult;
|
|
use crate::block::{BlockBehaviour, GetScreenHandlerFactoryArgs, NormalUseArgs, OnPlaceArgs};
|
|
use crate::entity::EntityBase;
|
|
|
|
use pumpkin_data::block_properties::WallTorchLikeProperties;
|
|
use pumpkin_data::translation;
|
|
use pumpkin_data::{BlockStateId, FacingExt};
|
|
use pumpkin_inventory::loom_screen_handler::LoomScreenHandler;
|
|
use pumpkin_inventory::player::player_inventory::PlayerInventory;
|
|
use pumpkin_inventory::screen_handler::{
|
|
InventoryPlayer, ScreenHandlerFactory, SharedScreenHandler,
|
|
};
|
|
use pumpkin_macros::pumpkin_block;
|
|
use pumpkin_util::text::TextComponent;
|
|
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
|
|
#[pumpkin_block("minecraft:loom")]
|
|
pub struct LoomBlock;
|
|
|
|
impl BlockBehaviour for LoomBlock {
|
|
fn on_place(&self, args: OnPlaceArgs<'_>) -> BlockStateId {
|
|
let mut props = WallTorchLikeProperties::default(args.block);
|
|
if let Some(facing) = args
|
|
.player
|
|
.get_entity()
|
|
.get_facing()
|
|
.opposite()
|
|
.to_horizontal_facing()
|
|
{
|
|
props.facing = facing;
|
|
}
|
|
props.to_state_id(args.block)
|
|
}
|
|
|
|
fn normal_use(&self, args: NormalUseArgs<'_>) -> BlockActionResult {
|
|
if let Some(factory) = self.get_screen_handler_factory(GetScreenHandlerFactoryArgs {
|
|
server: args.server,
|
|
world: args.world,
|
|
block: args.block,
|
|
position: args.position,
|
|
player: args.player,
|
|
}) {
|
|
args.player.increment_stat(
|
|
pumpkin_data::statistic::StatisticCategory::Custom,
|
|
pumpkin_data::statistic::CustomStatistic::InteractWithLoom as i32,
|
|
1,
|
|
);
|
|
args.player
|
|
.open_handled_screen(factory.as_ref(), Some(*args.position));
|
|
}
|
|
|
|
BlockActionResult::Success
|
|
}
|
|
|
|
fn get_screen_handler_factory(
|
|
&self,
|
|
_args: GetScreenHandlerFactoryArgs<'_>,
|
|
) -> Option<Box<dyn ScreenHandlerFactory>> {
|
|
Some(Box::new(LoomScreenFactory))
|
|
}
|
|
}
|
|
|
|
struct LoomScreenFactory;
|
|
|
|
impl ScreenHandlerFactory for LoomScreenFactory {
|
|
fn create_screen_handler(
|
|
&self,
|
|
sync_id: u8,
|
|
player_inventory: &Arc<PlayerInventory>,
|
|
_player: &dyn InventoryPlayer,
|
|
) -> Option<SharedScreenHandler> {
|
|
let handler: SharedScreenHandler = Arc::new(Mutex::new(LoomScreenHandler::new(
|
|
sync_id,
|
|
player_inventory,
|
|
)));
|
|
Some(handler)
|
|
}
|
|
|
|
fn get_display_name(&self) -> TextComponent {
|
|
TextComponent::translate(translation::java::CONTAINER_LOOM, [])
|
|
}
|
|
}
|