PocketMine-MP 5.14.2 git-50e2c469a547a16a23b2dc691e70a51d34e29395
Painting.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\entity\object;
25
41use function ceil;
42
43class Painting extends Entity{
44 public const TAG_TILE_X = "TileX"; //TAG_Int
45 public const TAG_TILE_Y = "TileY"; //TAG_Int
46 public const TAG_TILE_Z = "TileZ"; //TAG_Int
47 public const TAG_FACING_JE = "Facing"; //TAG_Byte
48 public const TAG_DIRECTION_BE = "Direction"; //TAG_Byte
49 public const TAG_MOTIVE = "Motive"; //TAG_String
50
51 public static function getNetworkTypeId() : string{ return EntityIds::PAINTING; }
52
53 public const DATA_TO_FACING = [
54 0 => Facing::SOUTH,
55 1 => Facing::WEST,
56 2 => Facing::NORTH,
57 3 => Facing::EAST
58 ];
59 private const FACING_TO_DATA = [
60 Facing::SOUTH => 0,
61 Facing::WEST => 1,
62 Facing::NORTH => 2,
63 Facing::EAST => 3
64 ];
65
66 protected Vector3 $blockIn;
67 protected int $facing;
68 protected PaintingMotive $motive;
69
70 public function __construct(Location $location, Vector3 $blockIn, int $facing, PaintingMotive $motive, ?CompoundTag $nbt = null){
71 $this->motive = $motive;
72 $this->blockIn = $blockIn->asVector3();
73 $this->facing = $facing;
74 parent::__construct($location, $nbt);
75 }
76
77 protected function getInitialSizeInfo() : EntitySizeInfo{
78 //these aren't accurate, but it doesn't matter since they aren't used (vanilla PC does something similar)
79 return new EntitySizeInfo(0.5, 0.5);
80 }
81
82 protected function getInitialDragMultiplier() : float{ return 1.0; }
83
84 protected function getInitialGravity() : float{ return 0.0; }
85
86 protected function initEntity(CompoundTag $nbt) : void{
87 $this->setMaxHealth(1);
88 $this->setHealth(1);
89 parent::initEntity($nbt);
90 }
91
92 public function saveNBT() : CompoundTag{
93 $nbt = parent::saveNBT();
94 $nbt->setInt(self::TAG_TILE_X, (int) $this->blockIn->x);
95 $nbt->setInt(self::TAG_TILE_Y, (int) $this->blockIn->y);
96 $nbt->setInt(self::TAG_TILE_Z, (int) $this->blockIn->z);
97
98 $nbt->setByte(self::TAG_FACING_JE, self::FACING_TO_DATA[$this->facing]);
99 $nbt->setByte(self::TAG_DIRECTION_BE, self::FACING_TO_DATA[$this->facing]); //Save both for full compatibility
100
101 $nbt->setString(self::TAG_MOTIVE, $this->motive->getName());
102
103 return $nbt;
104 }
105
106 protected function onDeath() : void{
107 parent::onDeath();
108
109 $drops = true;
110
111 if($this->lastDamageCause instanceof EntityDamageByEntityEvent){
112 $killer = $this->lastDamageCause->getDamager();
113 if($killer instanceof Player && !$killer->hasFiniteResources()){
114 $drops = false;
115 }
116 }
117
118 if($drops){
119 //non-living entities don't have a way to create drops generically yet
120 $this->getWorld()->dropItem($this->location, VanillaItems::PAINTING());
121 }
122 $this->getWorld()->addParticle($this->location->add(0.5, 0.5, 0.5), new BlockBreakParticle(VanillaBlocks::OAK_PLANKS()));
123 }
124
125 protected function recalculateBoundingBox() : void{
126 $side = $this->blockIn->getSide($this->facing);
127 $this->boundingBox = self::getPaintingBB($this->facing, $this->getMotive())->offset($side->x, $side->y, $side->z);
128 }
129
130 public function onNearbyBlockChange() : void{
131 parent::onNearbyBlockChange();
132
133 if(!self::canFit($this->getWorld(), $this->blockIn->getSide($this->facing), $this->facing, false, $this->getMotive())){
134 $this->kill();
135 }
136 }
137
138 public function onRandomUpdate() : void{
139 //NOOP
140 }
141
142 public function hasMovementUpdate() : bool{
143 return false;
144 }
145
146 protected function updateMovement(bool $teleport = false) : void{
147
148 }
149
150 public function canBeCollidedWith() : bool{
151 return false;
152 }
153
154 protected function sendSpawnPacket(Player $player) : void{
155 $player->getNetworkSession()->sendDataPacket(AddPaintingPacket::create(
156 $this->getId(), //TODO: entity unique ID
157 $this->getId(),
158 new Vector3(
159 ($this->boundingBox->minX + $this->boundingBox->maxX) / 2,
160 ($this->boundingBox->minY + $this->boundingBox->maxY) / 2,
161 ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2
162 ),
163 self::FACING_TO_DATA[$this->facing],
164 $this->motive->getName()
165 ));
166 }
167
171 public function getMotive() : PaintingMotive{
172 return $this->motive;
173 }
174
175 public function getFacing() : int{
176 return $this->facing;
177 }
178
182 private static function getPaintingBB(int $facing, PaintingMotive $motive) : AxisAlignedBB{
183 $width = $motive->getWidth();
184 $height = $motive->getHeight();
185
186 $horizontalStart = (int) (ceil($width / 2) - 1);
187 $verticalStart = (int) (ceil($height / 2) - 1);
188
189 return AxisAlignedBB::one()
190 ->trim($facing, 15 / 16)
191 ->extend(Facing::rotateY($facing, true), $horizontalStart)
192 ->extend(Facing::rotateY($facing, false), -$horizontalStart + $width - 1)
193 ->extend(Facing::DOWN, $verticalStart)
194 ->extend(Facing::UP, -$verticalStart + $height - 1);
195 }
196
200 public static function canFit(World $world, Vector3 $blockIn, int $facing, bool $checkOverlap, PaintingMotive $motive) : bool{
201 $width = $motive->getWidth();
202 $height = $motive->getHeight();
203
204 $horizontalStart = (int) (ceil($width / 2) - 1);
205 $verticalStart = (int) (ceil($height / 2) - 1);
206
207 $rotatedFace = Facing::rotateY($facing, false);
208
209 $oppositeSide = Facing::opposite($facing);
210
211 $startPos = $blockIn->asVector3()->getSide(Facing::opposite($rotatedFace), $horizontalStart)->getSide(Facing::DOWN, $verticalStart);
212
213 for($w = 0; $w < $width; ++$w){
214 for($h = 0; $h < $height; ++$h){
215 $pos = $startPos->getSide($rotatedFace, $w)->getSide(Facing::UP, $h);
216
217 $block = $world->getBlockAt($pos->x, $pos->y, $pos->z);
218 if($block->isSolid() || !$block->getSide($oppositeSide)->isSolid()){
219 return false;
220 }
221 }
222 }
223
224 if($checkOverlap){
225 $bb = self::getPaintingBB($facing, $motive)->offset($blockIn->x, $blockIn->y, $blockIn->z);
226
227 foreach($world->getNearbyEntities($bb) as $entity){
228 if($entity instanceof self){
229 return false;
230 }
231 }
232 }
233
234 return true;
235 }
236}
static canFit(World $world, Vector3 $blockIn, int $facing, bool $checkOverlap, PaintingMotive $motive)
Definition: Painting.php:200
setInt(string $name, int $value)
getBlockAt(int $x, int $y, int $z, bool $cached=true, bool $addToCache=true)
Definition: World.php:1883
getNearbyEntities(AxisAlignedBB $bb, ?Entity $entity=null)
Definition: World.php:2320