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