PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
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;
29use pocketmine\block\utils\HorizontalFacingTrait;
30use pocketmine\block\utils\SupportType;
41
42final class Bell extends Transparent implements HorizontalFacing{
43 use HorizontalFacingTrait;
44
45 private BellAttachmentType $attachmentType = BellAttachmentType::FLOOR;
46
47 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
48 $w->enum($this->attachmentType);
49 $w->horizontalFacing($this->facing);
50 }
51
52 protected function recalculateCollisionBoxes() : array{
53 if($this->attachmentType === BellAttachmentType::FLOOR){
54 return [
55 AxisAlignedBB::one()->squash(Facing::axis($this->facing), 1 / 4)->trim(Facing::UP, 3 / 16)
56 ];
57 }
58 if($this->attachmentType === BellAttachmentType::CEILING){
59 return [
60 AxisAlignedBB::one()->contract(1 / 4, 0, 1 / 4)->trim(Facing::DOWN, 1 / 4)
61 ];
62 }
63
64 $box = AxisAlignedBB::one()
65 ->squash(Facing::axis(Facing::rotateY($this->facing, true)), 1 / 4)
66 ->trim(Facing::UP, 1 / 16)
67 ->trim(Facing::DOWN, 1 / 4);
68
69 return [
70 $this->attachmentType === BellAttachmentType::ONE_WALL ? $box->trim($this->facing, 3 / 16) : $box
71 ];
72 }
73
74 public function getSupportType(int $facing) : SupportType{
75 return SupportType::NONE;
76 }
77
78 public function getAttachmentType() : BellAttachmentType{ return $this->attachmentType; }
79
81 public function setAttachmentType(BellAttachmentType $attachmentType) : self{
82 $this->attachmentType = $attachmentType;
83 return $this;
84 }
85
86 private function canBeSupportedAt(Block $block, int $face) : bool{
87 return $block->getAdjacentSupportType($face) !== SupportType::NONE;
88 }
89
90 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
91 if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){
92 return false;
93 }
94 if($face === Facing::UP){
95 if($player !== null){
96 $this->setFacing(Facing::opposite($player->getHorizontalFacing()));
97 }
98 $this->setAttachmentType(BellAttachmentType::FLOOR);
99 }elseif($face === Facing::DOWN){
100 $this->setAttachmentType(BellAttachmentType::CEILING);
101 }else{
102 $this->setFacing($face);
103 $this->setAttachmentType(
104 $this->canBeSupportedAt($blockReplace, $face) ?
105 BellAttachmentType::TWO_WALLS :
106 BellAttachmentType::ONE_WALL
107 );
108 }
109 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
110 }
111
112 public function onNearbyBlockChange() : void{
113 foreach(match($this->attachmentType){
114 BellAttachmentType::CEILING => [Facing::UP],
115 BellAttachmentType::FLOOR => [Facing::DOWN],
116 BellAttachmentType::ONE_WALL => [Facing::opposite($this->facing)],
117 BellAttachmentType::TWO_WALLS => [$this->facing, Facing::opposite($this->facing)]
118 } as $supportBlockDirection){
119 if(!$this->canBeSupportedAt($this, $supportBlockDirection)){
120 $this->position->getWorld()->useBreakOn($this->position);
121 break;
122 }
123 }
124 }
125
126 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
127 if($player !== null){
128 $faceHit = Facing::opposite($player->getHorizontalFacing());
129 if($this->isValidFaceToRing($faceHit)){
130 $this->ring($faceHit);
131 return true;
132 }
133 }
134
135 return false;
136 }
137
138 public function onProjectileHit(Projectile $projectile, RayTraceResult $hitResult) : void{
139 $faceHit = Facing::opposite($projectile->getHorizontalFacing());
140 if($this->isValidFaceToRing($faceHit)){
141 $this->ring($faceHit);
142 }
143 }
144
145 public function ring(int $faceHit) : void{
146 $world = $this->position->getWorld();
147 $world->addSound($this->position, new BellRingSound());
148 $tile = $world->getTile($this->position);
149 if($tile instanceof TileBell){
150 $world->broadcastPacketToViewers($this->position, $tile->createFakeUpdatePacket($faceHit));
151 }
152 }
153
154 public function getDropsForIncompatibleTool(Item $item) : array{
155 return [$this->asItem()];
156 }
157
158 private function isValidFaceToRing(int $faceHit) : bool{
159 return match($this->attachmentType){
160 BellAttachmentType::CEILING => true,
161 BellAttachmentType::FLOOR => Facing::axis($faceHit) === Facing::axis($this->facing),
162 BellAttachmentType::ONE_WALL, BellAttachmentType::TWO_WALLS => $faceHit === Facing::rotateY($this->facing, false) || $faceHit === Facing::rotateY($this->facing, true),
163 };
164 }
165}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Bell.php:47
setAttachmentType(BellAttachmentType $attachmentType)
Definition Bell.php:81
getSupportType(int $facing)
Definition Bell.php:74
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Bell.php:126
getDropsForIncompatibleTool(Item $item)
Definition Bell.php:154
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition Bell.php:90
onProjectileHit(Projectile $projectile, RayTraceResult $hitResult)
Definition Bell.php:138