Added a helper function Entity->createBaseNBT() to cut down on boilerplate code

This commit is contained in:
Dylan K. Taylor 2017-10-19 17:36:51 +01:00
parent 67c6fca0ed
commit 50be26958a
7 changed files with 69 additions and 141 deletions

View File

@ -26,12 +26,6 @@ namespace pocketmine\block;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\math\Vector3; 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{ abstract class Fallable extends Solid{
@ -40,29 +34,19 @@ abstract class Fallable extends Solid{
$down = $this->getSide(Vector3::SIDE_DOWN); $down = $this->getSide(Vector3::SIDE_DOWN);
if($down->getId() === self::AIR or $down instanceof Liquid or $down instanceof Fire){ if($down->getId() === self::AIR or $down instanceof Liquid or $down instanceof Fire){
$this->level->setBlock($this, BlockFactory::get(Block::AIR), true); $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())
]));
$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(); $fall->spawnToAll();
} }
} }
} }
}
/** /**
* @return null|Block * @return null|Block

View File

@ -25,11 +25,7 @@ namespace pocketmine\block;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\nbt\tag\ByteTag; 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; use pocketmine\Player;
use pocketmine\utils\Random; use pocketmine\utils\Random;
@ -63,24 +59,13 @@ class TNT extends Solid{
$this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true); $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true);
$mot = (new Random())->nextSignedFloat() * M_PI * 2; $mot = (new Random())->nextSignedFloat() * M_PI * 2;
$tnt = Entity::createEntity("PrimedTNT", $this->getLevel(), new CompoundTag("", [ $nbt = Entity::createBaseNBT($this->add(0.5, 0, 0.5), new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02));
new ListTag("Pos", [ $nbt->setByte("Fuse", $fuse);
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)
]));
$tnt = Entity::createEntity("PrimedTNT", $this->getLevel(), $nbt);
if($tnt !== null){
$tnt->spawnToAll(); $tnt->spawnToAll();
} }
}
} }

View File

@ -286,6 +286,35 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
return false; 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[] * @var Player[]
*/ */

View File

@ -28,11 +28,6 @@ use pocketmine\entity\projectile\Projectile;
use pocketmine\event\entity\EntityShootBowEvent; use pocketmine\event\entity\EntityShootBowEvent;
use pocketmine\event\entity\ProjectileLaunchEvent; use pocketmine\event\entity\ProjectileLaunchEvent;
use pocketmine\level\sound\LaunchSound; 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; use pocketmine\Player;
class Bow extends Tool{ class Bow extends Tool{
@ -54,26 +49,13 @@ class Bow extends Tool{
return false; return false;
} }
$directionVector = $player->getDirectionVector(); $nbt = Entity::createBaseNBT(
$player->add(0, $player->getEyeHeight(), 0),
$nbt = new CompoundTag("", [ $player->getDirectionVector(),
new ListTag("Pos", [ ($player->yaw > 180 ? 360 : 0) - $player->yaw,
new DoubleTag("", $player->x), -$player->pitch
new DoubleTag("", $player->y + $player->getEyeHeight()), );
new DoubleTag("", $player->z) $nbt->setShort("Fire", $player->isOnFire() ? 45 * 60 : 0);
]),
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)
]);
$diff = $player->getItemUseDuration(); $diff = $player->getItemUseDuration();
$p = $diff / 20; $p = $diff / 20;

View File

@ -28,10 +28,6 @@ use pocketmine\entity\projectile\Projectile;
use pocketmine\event\entity\ProjectileLaunchEvent; use pocketmine\event\entity\ProjectileLaunchEvent;
use pocketmine\level\sound\LaunchSound; use pocketmine\level\sound\LaunchSound;
use pocketmine\math\Vector3; 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; use pocketmine\Player;
abstract class ProjectileItem extends Item{ abstract class ProjectileItem extends Item{
@ -41,25 +37,12 @@ abstract class ProjectileItem extends Item{
abstract public function getThrowForce() : float; abstract public function getThrowForce() : float;
public function onClickAir(Player $player, Vector3 $directionVector) : bool{ public function onClickAir(Player $player, Vector3 $directionVector) : bool{
$nbt = new CompoundTag("", [ $nbt = Entity::createBaseNBT($player->add(0, $player->getEyeHeight(), 0), $directionVector, $player->yaw, $player->pitch);
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)
]),
]);
$projectile = Entity::createEntity($this->getProjectileEntityType(), $player->getLevel(), $nbt, $player); $projectile = Entity::createEntity($this->getProjectileEntityType(), $player->getLevel(), $nbt, $player);
if($projectile !== null){
$projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce())); $projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
}
$this->count--; $this->count--;

View File

@ -27,11 +27,6 @@ use pocketmine\block\Block;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\math\Vector3; 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; use pocketmine\Player;
class SpawnEgg extends Item{ 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{ public function onActivate(Level $level, Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $facePos) : bool{
$nbt = new CompoundTag("", [ $nbt = Entity::createBaseNBT($blockReplace->add(0.5, 0, 0.5), null, lcg_value() * 360, 0);
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)
]),
]);
if($this->hasCustomName()){ if($this->hasCustomName()){
$nbt->CustomName = new StringTag("CustomName", $this->getCustomName()); $nbt->setString("CustomName", $this->getCustomName());
} }
$entity = Entity::createEntity($this->meta, $level, $nbt); $entity = Entity::createEntity($this->meta, $level, $nbt);

View File

@ -67,11 +67,7 @@ use pocketmine\math\Vector3;
use pocketmine\metadata\BlockMetadataStore; use pocketmine\metadata\BlockMetadataStore;
use pocketmine\metadata\Metadatable; use pocketmine\metadata\Metadatable;
use pocketmine\metadata\MetadataValue; 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\ListTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\protocol\BatchPacket; use pocketmine\network\mcpe\protocol\BatchPacket;
use pocketmine\network\mcpe\protocol\DataPacket; use pocketmine\network\mcpe\protocol\DataPacket;
@ -1544,29 +1540,18 @@ class Level implements ChunkManager, Metadatable{
$itemTag->setName("Item"); $itemTag->setName("Item");
if(!$item->isNull()){ if(!$item->isNull()){
$itemEntity = Entity::createEntity("Item", $this, new CompoundTag("", [ $nbt = Entity::createBaseNBT($source, $motion, lcg_value() * 360, 0);
new ListTag("Pos", [ $nbt->setShort("Health", 5);
new DoubleTag("", $source->getX()), $nbt->setShort("PickupDelay", $delay);
new DoubleTag("", $source->getY()), $nbt->setTag($itemTag);
new DoubleTag("", $source->getZ()) $itemEntity = Entity::createEntity("Item", $this, $nbt);
]),
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)
]));
if($itemEntity instanceof DroppedItem){
$itemEntity->spawnToAll(); $itemEntity->spawnToAll();
return $itemEntity; return $itemEntity;
} }
}
return null; return null;
} }