diff --git a/src/pocketmine/block/Fallable.php b/src/pocketmine/block/Fallable.php index 4a6e24036..3f00b8ae7 100644 --- a/src/pocketmine/block/Fallable.php +++ b/src/pocketmine/block/Fallable.php @@ -26,12 +26,6 @@ namespace pocketmine\block; use pocketmine\entity\Entity; use pocketmine\level\Level; use pocketmine\math\Vector3; -use pocketmine\nbt\tag\ByteTag; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\DoubleTag; -use pocketmine\nbt\tag\FloatTag; -use pocketmine\nbt\tag\IntTag; -use pocketmine\nbt\tag\ListTag; abstract class Fallable extends Solid{ @@ -40,26 +34,16 @@ abstract class Fallable extends Solid{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() === self::AIR or $down instanceof Liquid or $down instanceof Fire){ $this->level->setBlock($this, BlockFactory::get(Block::AIR), true); - $fall = Entity::createEntity("FallingSand", $this->getLevel(), new CompoundTag("", [ - new ListTag("Pos", [ - new DoubleTag("", $this->x + 0.5), - new DoubleTag("", $this->y), - new DoubleTag("", $this->z + 0.5) - ]), - new ListTag("Motion", [ - new DoubleTag("", 0), - new DoubleTag("", 0), - new DoubleTag("", 0) - ]), - new ListTag("Rotation", [ - new FloatTag("", 0), - new FloatTag("", 0) - ]), - new IntTag("TileID", $this->getId()), - new ByteTag("Data", $this->getDamage()) - ])); - $fall->spawnToAll(); + $nbt = Entity::createBaseNBT($this->add(0.5, 0, 0.5)); + $nbt->setInt("TileID", $this->getId()); + $nbt->setByte("Data", $this->getDamage()); + + $fall = Entity::createEntity("FallingSand", $this->getLevel(), $nbt); + + if($fall !== null){ + $fall->spawnToAll(); + } } } } diff --git a/src/pocketmine/block/TNT.php b/src/pocketmine/block/TNT.php index e1d4340a5..66ef0d563 100644 --- a/src/pocketmine/block/TNT.php +++ b/src/pocketmine/block/TNT.php @@ -25,11 +25,7 @@ namespace pocketmine\block; use pocketmine\entity\Entity; use pocketmine\item\Item; -use pocketmine\nbt\tag\ByteTag; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\DoubleTag; -use pocketmine\nbt\tag\FloatTag; -use pocketmine\nbt\tag\ListTag; +use pocketmine\math\Vector3; use pocketmine\Player; use pocketmine\utils\Random; @@ -63,24 +59,13 @@ class TNT extends Solid{ $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true); $mot = (new Random())->nextSignedFloat() * M_PI * 2; - $tnt = Entity::createEntity("PrimedTNT", $this->getLevel(), new CompoundTag("", [ - new ListTag("Pos", [ - new DoubleTag("", $this->x + 0.5), - new DoubleTag("", $this->y), - new DoubleTag("", $this->z + 0.5) - ]), - new ListTag("Motion", [ - new DoubleTag("", -sin($mot) * 0.02), - new DoubleTag("", 0.2), - new DoubleTag("", -cos($mot) * 0.02) - ]), - new ListTag("Rotation", [ - new FloatTag("", 0), - new FloatTag("", 0) - ]), - new ByteTag("Fuse", $fuse) - ])); + $nbt = Entity::createBaseNBT($this->add(0.5, 0, 0.5), new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02)); + $nbt->setByte("Fuse", $fuse); - $tnt->spawnToAll(); + $tnt = Entity::createEntity("PrimedTNT", $this->getLevel(), $nbt); + + if($tnt !== null){ + $tnt->spawnToAll(); + } } } \ No newline at end of file diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index eb444a28e..c2f5008ef 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -286,6 +286,35 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ return false; } + /** + * Helper function which creates minimal NBT needed to spawn an entity. + * + * @param Vector3 $pos + * @param Vector3|null $motion + * @param float $yaw + * @param float $pitch + * + * @return CompoundTag + */ + public static function createBaseNBT(Vector3 $pos, ?Vector3 $motion = null , float $yaw = 0.0, float $pitch = 0.0) : CompoundTag{ + return new CompoundTag("", [ + new ListTag("Pos", [ + new DoubleTag("", $pos->x), + new DoubleTag("", $pos->y), + new DoubleTag("", $pos->z) + ]), + new ListTag("Motion", [ + new DoubleTag("", $motion ? $motion->x : 0.0), + new DoubleTag("", $motion ? $motion->y : 0.0), + new DoubleTag("", $motion ? $motion->z : 0.0) + ]), + new ListTag("Rotation", [ + new FloatTag("", $yaw), + new FloatTag("", $pitch) + ]) + ]); + } + /** * @var Player[] */ diff --git a/src/pocketmine/item/Bow.php b/src/pocketmine/item/Bow.php index 5942ec887..e18d17846 100644 --- a/src/pocketmine/item/Bow.php +++ b/src/pocketmine/item/Bow.php @@ -28,11 +28,6 @@ use pocketmine\entity\projectile\Projectile; use pocketmine\event\entity\EntityShootBowEvent; use pocketmine\event\entity\ProjectileLaunchEvent; use pocketmine\level\sound\LaunchSound; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\DoubleTag; -use pocketmine\nbt\tag\FloatTag; -use pocketmine\nbt\tag\ListTag; -use pocketmine\nbt\tag\ShortTag; use pocketmine\Player; class Bow extends Tool{ @@ -54,26 +49,13 @@ class Bow extends Tool{ return false; } - $directionVector = $player->getDirectionVector(); - - $nbt = new CompoundTag("", [ - new ListTag("Pos", [ - new DoubleTag("", $player->x), - new DoubleTag("", $player->y + $player->getEyeHeight()), - new DoubleTag("", $player->z) - ]), - new ListTag("Motion", [ - new DoubleTag("", $directionVector->x), - new DoubleTag("", $directionVector->y), - new DoubleTag("", $directionVector->z) - ]), - new ListTag("Rotation", [ - //yaw/pitch for arrows taken crosswise, not along the arrow shaft. - new FloatTag("", ($player->yaw > 180 ? 360 : 0) - $player->yaw), //arrow yaw must range from -180 to +180 - new FloatTag("", -$player->pitch) - ]), - new ShortTag("Fire", $player->isOnFire() ? 45 * 60 : 0) - ]); + $nbt = Entity::createBaseNBT( + $player->add(0, $player->getEyeHeight(), 0), + $player->getDirectionVector(), + ($player->yaw > 180 ? 360 : 0) - $player->yaw, + -$player->pitch + ); + $nbt->setShort("Fire", $player->isOnFire() ? 45 * 60 : 0); $diff = $player->getItemUseDuration(); $p = $diff / 20; diff --git a/src/pocketmine/item/ProjectileItem.php b/src/pocketmine/item/ProjectileItem.php index 63f896a1d..70c31140d 100644 --- a/src/pocketmine/item/ProjectileItem.php +++ b/src/pocketmine/item/ProjectileItem.php @@ -28,10 +28,6 @@ use pocketmine\entity\projectile\Projectile; use pocketmine\event\entity\ProjectileLaunchEvent; use pocketmine\level\sound\LaunchSound; use pocketmine\math\Vector3; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\DoubleTag; -use pocketmine\nbt\tag\FloatTag; -use pocketmine\nbt\tag\ListTag; use pocketmine\Player; abstract class ProjectileItem extends Item{ @@ -41,25 +37,12 @@ abstract class ProjectileItem extends Item{ abstract public function getThrowForce() : float; public function onClickAir(Player $player, Vector3 $directionVector) : bool{ - $nbt = new CompoundTag("", [ - new ListTag("Pos", [ - new DoubleTag("", $player->x), - new DoubleTag("", $player->y + $player->getEyeHeight()), - new DoubleTag("", $player->z) - ]), - new ListTag("Motion", [ - new DoubleTag("", $directionVector->x), - new DoubleTag("", $directionVector->y), - new DoubleTag("", $directionVector->z) - ]), - new ListTag("Rotation", [ - new FloatTag("", $player->yaw), - new FloatTag("", $player->pitch) - ]), - ]); + $nbt = Entity::createBaseNBT($player->add(0, $player->getEyeHeight(), 0), $directionVector, $player->yaw, $player->pitch); $projectile = Entity::createEntity($this->getProjectileEntityType(), $player->getLevel(), $nbt, $player); - $projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce())); + if($projectile !== null){ + $projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce())); + } $this->count--; diff --git a/src/pocketmine/item/SpawnEgg.php b/src/pocketmine/item/SpawnEgg.php index 76240f4ee..094ee9d12 100644 --- a/src/pocketmine/item/SpawnEgg.php +++ b/src/pocketmine/item/SpawnEgg.php @@ -27,11 +27,6 @@ use pocketmine\block\Block; use pocketmine\entity\Entity; use pocketmine\level\Level; use pocketmine\math\Vector3; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\DoubleTag; -use pocketmine\nbt\tag\FloatTag; -use pocketmine\nbt\tag\ListTag; -use pocketmine\nbt\tag\StringTag; use pocketmine\Player; class SpawnEgg extends Item{ @@ -40,25 +35,10 @@ class SpawnEgg extends Item{ } public function onActivate(Level $level, Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $facePos) : bool{ - $nbt = new CompoundTag("", [ - new ListTag("Pos", [ - new DoubleTag("", $blockReplace->getX() + 0.5), - new DoubleTag("", $blockReplace->getY()), - new DoubleTag("", $blockReplace->getZ() + 0.5) - ]), - new ListTag("Motion", [ - new DoubleTag("", 0), - new DoubleTag("", 0), - new DoubleTag("", 0) - ]), - new ListTag("Rotation", [ - new FloatTag("", lcg_value() * 360), - new FloatTag("", 0) - ]), - ]); + $nbt = Entity::createBaseNBT($blockReplace->add(0.5, 0, 0.5), null, lcg_value() * 360, 0); if($this->hasCustomName()){ - $nbt->CustomName = new StringTag("CustomName", $this->getCustomName()); + $nbt->setString("CustomName", $this->getCustomName()); } $entity = Entity::createEntity($this->meta, $level, $nbt); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 0a50230d1..536263538 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -67,11 +67,7 @@ use pocketmine\math\Vector3; use pocketmine\metadata\BlockMetadataStore; use pocketmine\metadata\Metadatable; use pocketmine\metadata\MetadataValue; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\DoubleTag; -use pocketmine\nbt\tag\FloatTag; use pocketmine\nbt\tag\ListTag; -use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\StringTag; use pocketmine\network\mcpe\protocol\BatchPacket; use pocketmine\network\mcpe\protocol\DataPacket; @@ -1544,28 +1540,17 @@ class Level implements ChunkManager, Metadatable{ $itemTag->setName("Item"); if(!$item->isNull()){ - $itemEntity = Entity::createEntity("Item", $this, new CompoundTag("", [ - new ListTag("Pos", [ - new DoubleTag("", $source->getX()), - new DoubleTag("", $source->getY()), - new DoubleTag("", $source->getZ()) - ]), - new ListTag("Motion", [ - new DoubleTag("", $motion->x), - new DoubleTag("", $motion->y), - new DoubleTag("", $motion->z) - ]), - new ListTag("Rotation", [ - new FloatTag("", lcg_value() * 360), - new FloatTag("", 0) - ]), - new ShortTag("Health", 5), - $itemTag, - new ShortTag("PickupDelay", $delay) - ])); + $nbt = Entity::createBaseNBT($source, $motion, lcg_value() * 360, 0); + $nbt->setShort("Health", 5); + $nbt->setShort("PickupDelay", $delay); + $nbt->setTag($itemTag); + $itemEntity = Entity::createEntity("Item", $this, $nbt); - $itemEntity->spawnToAll(); - return $itemEntity; + if($itemEntity instanceof DroppedItem){ + $itemEntity->spawnToAll(); + + return $itemEntity; + } } return null; }