PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
Bell.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
26use pocketmine\block\tile\Bell as TileBell;
27use pocketmine\block\utils\BellAttachmentType;
28use pocketmine\block\utils\HorizontalFacingTrait;
29use pocketmine\block\utils\SupportType;
40
41final class Bell extends Transparent{
42 use HorizontalFacingTrait;
43
44 private BellAttachmentType $attachmentType = BellAttachmentType::FLOOR;
45
46 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
47 $w->enum($this->attachmentType);
48 $w->horizontalFacing($this->facing);
49 }
50
51 protected function recalculateCollisionBoxes() : array{
52 if($this->attachmentType === BellAttachmentType::FLOOR){
53 return [
54 AxisAlignedBB::one()->squash(Facing::axis($this->facing), 1 / 4)->trim(Facing::UP, 3 / 16)
55 ];
56 }
57 if($this->attachmentType === BellAttachmentType::CEILING){
58 return [
59 AxisAlignedBB::one()->contract(1 / 4, 0, 1 / 4)->trim(Facing::DOWN, 1 / 4)
60 ];
61 }
62
63 $box = AxisAlignedBB::one()
64 ->squash(Facing::axis(Facing::rotateY($this->facing, true)), 1 / 4)
65 ->trim(Facing::UP, 1 / 16)
66 ->trim(Facing::DOWN, 1 / 4);
67
68 return [
69 $this->attachmentType === BellAttachmentType::ONE_WALL ? $box->trim($this->facing, 3 / 16) : $box
70 ];
71 }
72
73 public function getSupportType(int $facing) : SupportType{
74 return SupportType::NONE;
75 }
76
77 public function getAttachmentType() : BellAttachmentType{ return $this->attachmentType; }
78
80 public function setAttachmentType(BellAttachmentType $attachmentType) : self{
81 $this->attachmentType = $attachmentType;
82 return $this;
83 }
84
85 private function canBeSupportedAt(Block $block, int $face) : bool{
86 return $block->getAdjacentSupportType($face) !== SupportType::NONE;
87 }
88
89 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
90 if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){
91 return false;
92 }
93 if($face === Facing::UP){
94 if($player !== null){
95 $this->setFacing(Facing::opposite($player->getHorizontalFacing()));
96 }
97 $this->setAttachmentType(BellAttachmentType::FLOOR);
98 }elseif($face === Facing::DOWN){
99 $this->setAttachmentType(BellAttachmentType::CEILING);
100 }else{
101 $this->setFacing($face);
102 $this->setAttachmentType(
103 $this->canBeSupportedAt($blockReplace, $face) ?
104 BellAttachmentType::TWO_WALLS :
105 BellAttachmentType::ONE_WALL
106 );
107 }
108 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
109 }
110
111 public function onNearbyBlockChange() : void{
112 foreach(match($this->attachmentType){
113 BellAttachmentType::CEILING => [Facing::UP],
114 BellAttachmentType::FLOOR => [Facing::DOWN],
115 BellAttachmentType::ONE_WALL => [Facing::opposite($this->facing)],
116 BellAttachmentType::TWO_WALLS => [$this->facing, Facing::opposite($this->facing)]
117 } as $supportBlockDirection){
118 if(!$this->canBeSupportedAt($this, $supportBlockDirection)){
119 $this->position->getWorld()->useBreakOn($this->position);
120 break;
121 }
122 }
123 }
124
125 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
126 if($player !== null){
127 $faceHit = Facing::opposite($player->getHorizontalFacing());
128 if($this->isValidFaceToRing($faceHit)){
129 $this->ring($faceHit);
130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 public function onProjectileHit(Projectile $projectile, RayTraceResult $hitResult) : void{
138 $faceHit = Facing::opposite($projectile->getHorizontalFacing());
139 if($this->isValidFaceToRing($faceHit)){
140 $this->ring($faceHit);
141 }
142 }
143
144 public function ring(int $faceHit) : void{
145 $world = $this->position->getWorld();
146 $world->addSound($this->position, new BellRingSound());
147 $tile = $world->getTile($this->position);
148 if($tile instanceof TileBell){
149 $world->broadcastPacketToViewers($this->position, $tile->createFakeUpdatePacket($faceHit));
150 }
151 }
152
153 public function getDropsForIncompatibleTool(Item $item) : array{
154 return [$this->asItem()];
155 }
156
157 private function isValidFaceToRing(int $faceHit) : bool{
158 return match($this->attachmentType){
159 BellAttachmentType::CEILING => true,
160 BellAttachmentType::FLOOR => Facing::axis($faceHit) === Facing::axis($this->facing),
161 BellAttachmentType::ONE_WALL, BellAttachmentType::TWO_WALLS => $faceHit === Facing::rotateY($this->facing, false) || $faceHit === Facing::rotateY($this->facing, true),
162 };
163 }
164}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Bell.php:46
setAttachmentType(BellAttachmentType $attachmentType)
Definition: Bell.php:80
getSupportType(int $facing)
Definition: Bell.php:73
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: Bell.php:125
getDropsForIncompatibleTool(Item $item)
Definition: Bell.php:153
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: Bell.php:89
onProjectileHit(Projectile $projectile, RayTraceResult $hitResult)
Definition: Bell.php:137
recalculateCollisionBoxes()
Definition: Bell.php:51
static axis(int $direction)
Definition: Facing.php:92
static rotateY(int $direction, bool $clockwise)
Definition: Facing.php:132