PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
MobHead.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\MobHead as TileMobHead;
27use pocketmine\block\utils\MobHeadType;
35use function assert;
36use function floor;
37
38class MobHead extends Flowable{
39 public const MIN_ROTATION = 0;
40 public const MAX_ROTATION = 15;
41
42 protected MobHeadType $mobHeadType = MobHeadType::SKELETON;
43
44 protected int $facing = Facing::NORTH;
45 protected int $rotation = self::MIN_ROTATION; //TODO: split this into floor skull and wall skull handling
46
47 public function describeBlockItemState(RuntimeDataDescriber $w) : void{
48 $w->enum($this->mobHeadType);
49 }
50
51 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
52 $w->facingExcept($this->facing, Facing::DOWN);
53 }
54
55 public function readStateFromWorld() : Block{
56 parent::readStateFromWorld();
57 $tile = $this->position->getWorld()->getTile($this->position);
58 if($tile instanceof TileMobHead){
59 $this->mobHeadType = $tile->getMobHeadType();
60 $this->rotation = $tile->getRotation();
61 }
62
63 return $this;
64 }
65
66 public function writeStateToWorld() : void{
67 parent::writeStateToWorld();
68 //extra block properties storage hack
69 $tile = $this->position->getWorld()->getTile($this->position);
70 assert($tile instanceof TileMobHead);
71 $tile->setRotation($this->rotation);
72 $tile->setMobHeadType($this->mobHeadType);
73 }
74
75 public function getMobHeadType() : MobHeadType{
76 return $this->mobHeadType;
77 }
78
80 public function setMobHeadType(MobHeadType $mobHeadType) : self{
81 $this->mobHeadType = $mobHeadType;
82 return $this;
83 }
84
85 public function getFacing() : int{ return $this->facing; }
86
88 public function setFacing(int $facing) : self{
89 if($facing === Facing::DOWN){
90 throw new \InvalidArgumentException("Skull may not face DOWN");
91 }
92 $this->facing = $facing;
93 return $this;
94 }
95
96 public function getRotation() : int{ return $this->rotation; }
97
99 public function setRotation(int $rotation) : self{
100 if($rotation < self::MIN_ROTATION || $rotation > self::MAX_ROTATION){
101 throw new \InvalidArgumentException("Rotation must be in range " . self::MIN_ROTATION . " ... " . self::MAX_ROTATION);
102 }
103 $this->rotation = $rotation;
104 return $this;
105 }
106
110 protected function recalculateCollisionBoxes() : array{
111 $collisionBox = AxisAlignedBB::one()
112 ->contract(0.25, 0, 0.25)
113 ->trim(Facing::UP, 0.5);
114 if($this->facing !== Facing::UP){
115 $collisionBox = $collisionBox
116 ->offsetTowards(Facing::opposite($this->facing), 0.25)
117 ->offsetTowards(Facing::UP, 0.25);
118 }
119 return [$collisionBox];
120 }
121
122 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
123 if($face === Facing::DOWN){
124 return false;
125 }
126
127 $this->facing = $face;
128 if($player !== null && $face === Facing::UP){
129 $this->rotation = ((int) floor(($player->getLocation()->getYaw() * 16 / 360) + 0.5)) & 0xf;
130 }
131 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
132 }
133}
setFacing(int $facing)
Definition: MobHead.php:88
setRotation(int $rotation)
Definition: MobHead.php:99
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: MobHead.php:122
describeBlockItemState(RuntimeDataDescriber $w)
Definition: MobHead.php:47
setMobHeadType(MobHeadType $mobHeadType)
Definition: MobHead.php:80
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: MobHead.php:51
offsetTowards(int $face, float $distance)