PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
FloorCoralFan.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\utils\StaticSupportTrait;
35use function atan2;
36use function rad2deg;
37
38final class FloorCoralFan extends BaseCoral{
39 use StaticSupportTrait;
40
41 private int $axis = Axis::X;
42
43 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
44 $w->horizontalAxis($this->axis);
45 }
46
47 public function getAxis() : int{ return $this->axis; }
48
50 public function setAxis(int $axis) : self{
51 if($axis !== Axis::X && $axis !== Axis::Z){
52 throw new \InvalidArgumentException("Axis must be X or Z only");
53 }
54 $this->axis = $axis;
55 return $this;
56 }
57
58 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
59 if($player !== null){
60 $playerBlockPos = $player->getPosition()->floor();
61 $directionVector = $blockReplace->position->subtractVector($playerBlockPos)->normalize();
62 $angle = rad2deg(atan2($directionVector->getZ(), $directionVector->getX()));
63
64 if($angle <= 45 || 315 <= $angle || (135 <= $angle && $angle <= 225)){
65 //TODO: This produces Z axis 75% of the time, because any negative angle will produce Z axis.
66 //This is a bug in vanilla. https://bugs.mojang.com/browse/MCPE-125311
67 $this->axis = Axis::Z;
68 }
69 }
70
71 $this->dead = !$this->isCoveredWithWater();
72
73 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
74 }
75
76 private function canBeSupportedAt(Block $block) : bool{
77 return $block->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport();
78 }
79
80 public function asItem() : Item{
81 return VanillaItems::CORAL_FAN()->setCoralType($this->coralType)->setDead($this->dead);
82 }
83}
describeBlockOnlyState(RuntimeDataDescriber $w)
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)