NBT is no longer needed to create an entity

it's still able to be provided, but shouldn't be needed in the majority of cases (constructor args and/or API methods should be sufficient).
This commit is contained in:
Dylan K. Taylor 2020-06-19 09:49:06 +01:00
parent 0a1bb0041b
commit 4b528aa637
14 changed files with 19 additions and 24 deletions

View File

@ -32,7 +32,6 @@ use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\FlintSteel; use pocketmine\item\FlintSteel;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\utils\Random; use pocketmine\utils\Random;
use function cos; use function cos;
@ -95,7 +94,7 @@ class TNT extends Opaque{
$mot = (new Random())->nextSignedFloat() * M_PI * 2; $mot = (new Random())->nextSignedFloat() * M_PI * 2;
$tnt = new PrimedTNT(Location::fromObject($this->pos->add(0.5, 0, 0.5), $this->pos->getWorldNonNull()), new CompoundTag()); $tnt = new PrimedTNT(Location::fromObject($this->pos->add(0.5, 0, 0.5), $this->pos->getWorldNonNull()));
$tnt->setFuse($fuse); $tnt->setFuse($fuse);
$tnt->setMotion(new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02)); $tnt->setMotion(new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02));

View File

@ -31,7 +31,6 @@ use pocketmine\block\VanillaBlocks;
use pocketmine\entity\Location; use pocketmine\entity\Location;
use pocketmine\entity\object\FallingBlock; use pocketmine\entity\object\FallingBlock;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\Position; use pocketmine\world\Position;
@ -57,7 +56,7 @@ trait FallableTrait{
$block = $this; $block = $this;
if(!($block instanceof Block)) throw new AssumptionFailedError(__TRAIT__ . " should only be used by Blocks"); if(!($block instanceof Block)) throw new AssumptionFailedError(__TRAIT__ . " should only be used by Blocks");
$fall = new FallingBlock(Location::fromObject($pos->add(0.5, 0, 0.5), $pos->getWorldNonNull()), $block, new CompoundTag()); $fall = new FallingBlock(Location::fromObject($pos->add(0.5, 0, 0.5), $pos->getWorldNonNull()), $block);
$fall->spawnToAll(); $fall->spawnToAll();
} }
} }

View File

@ -213,7 +213,7 @@ abstract class Entity{
/** @var int|null */ /** @var int|null */
protected $targetId = null; protected $targetId = null;
public function __construct(Location $location, CompoundTag $nbt){ public function __construct(Location $location, ?CompoundTag $nbt = null){
$this->timings = Timings::getEntityTimings($this); $this->timings = Timings::getEntityTimings($this);
$this->temporalVector = new Vector3(); $this->temporalVector = new Vector3();
@ -241,7 +241,7 @@ abstract class Entity{
} }
$this->motion = new Vector3(0, 0, 0); $this->motion = new Vector3(0, 0, 0);
if($nbt->hasTag("Motion", ListTag::class)){ if($nbt !== null and $nbt->hasTag("Motion", ListTag::class)){
/** @var float[] $motion */ /** @var float[] $motion */
$motion = $nbt->getListTag("Motion")->getAllValues(); $motion = $nbt->getListTag("Motion")->getAllValues();
$this->setMotion($this->temporalVector->setComponents(...$motion)); $this->setMotion($this->temporalVector->setComponents(...$motion));
@ -254,7 +254,7 @@ abstract class Entity{
$this->attributeMap = new AttributeMap(); $this->attributeMap = new AttributeMap();
$this->addAttributes(); $this->addAttributes();
$this->initEntity($nbt); $this->initEntity($nbt ?? new CompoundTag());
$this->chunk->addEntity($this); $this->chunk->addEntity($this);
$this->getWorld()->addEntity($this); $this->getWorld()->addEntity($this);

View File

@ -93,7 +93,7 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
protected $baseOffset = 1.62; protected $baseOffset = 1.62;
public function __construct(Location $location, Skin $skin, CompoundTag $nbt){ public function __construct(Location $location, Skin $skin, ?CompoundTag $nbt = null){
$this->skin = $skin; $this->skin = $skin;
parent::__construct($location, $nbt); parent::__construct($location, $nbt);
} }

View File

@ -55,7 +55,7 @@ class FallingBlock extends Entity{
public $canCollide = false; public $canCollide = false;
public function __construct(Location $location, Block $block, CompoundTag $nbt){ public function __construct(Location $location, Block $block, ?CompoundTag $nbt = null){
$this->block = $block; $this->block = $block;
parent::__construct($location, $nbt); parent::__construct($location, $nbt);
} }

View File

@ -65,7 +65,7 @@ class ItemEntity extends Entity{
/** @var int */ /** @var int */
protected $despawnDelay = self::DEFAULT_DESPAWN_DELAY; protected $despawnDelay = self::DEFAULT_DESPAWN_DELAY;
public function __construct(Location $location, Item $item, CompoundTag $nbt){ public function __construct(Location $location, Item $item, ?CompoundTag $nbt = null){
if($item->isNull()){ if($item->isNull()){
throw new \InvalidArgumentException("Item entity must have a non-air item with a count of at least 1"); throw new \InvalidArgumentException("Item entity must have a non-air item with a count of at least 1");
} }

View File

@ -73,7 +73,7 @@ class Painting extends Entity{
/** @var string */ /** @var string */
protected $motive; protected $motive;
public function __construct(Location $location, Vector3 $blockIn, int $facing, PaintingMotive $motive, CompoundTag $nbt){ public function __construct(Location $location, Vector3 $blockIn, int $facing, PaintingMotive $motive, ?CompoundTag $nbt = null){
$this->motive = $motive->getName(); //TODO: use motive directly $this->motive = $motive->getName(); //TODO: use motive directly
$this->blockIn = $blockIn->asVector3(); $this->blockIn = $blockIn->asVector3();
$this->facing = $facing; $this->facing = $facing;

View File

@ -71,7 +71,7 @@ class Arrow extends Projectile{
/** @var bool */ /** @var bool */
protected $critical = false; protected $critical = false;
public function __construct(Location $location, ?Entity $shootingEntity, bool $critical, CompoundTag $nbt){ public function __construct(Location $location, ?Entity $shootingEntity, bool $critical, ?CompoundTag $nbt = null){
parent::__construct($location, $shootingEntity, $nbt); parent::__construct($location, $shootingEntity, $nbt);
$this->setCritical($critical); $this->setCritical($critical);
} }

View File

@ -57,7 +57,7 @@ abstract class Projectile extends Entity{
/** @var Block|null */ /** @var Block|null */
protected $blockHit; protected $blockHit;
public function __construct(Location $location, ?Entity $shootingEntity, CompoundTag $nbt){ public function __construct(Location $location, ?Entity $shootingEntity, ?CompoundTag $nbt = null){
parent::__construct($location, $nbt); parent::__construct($location, $nbt);
if($shootingEntity !== null){ if($shootingEntity !== null){
$this->setOwningEntity($shootingEntity); $this->setOwningEntity($shootingEntity);

View File

@ -29,7 +29,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\item\enchantment\Enchantment; use pocketmine\item\enchantment\Enchantment;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\world\sound\BowShootSound; use pocketmine\world\sound\BowShootSound;
use function intdiv; use function intdiv;
@ -62,7 +61,7 @@ class Bow extends Tool{
$player->getWorld(), $player->getWorld(),
($location->yaw > 180 ? 360 : 0) - $location->yaw, ($location->yaw > 180 ? 360 : 0) - $location->yaw,
-$location->pitch -$location->pitch
), $player, $baseForce >= 1, new CompoundTag()); ), $player, $baseForce >= 1);
$entity->setMotion($player->getDirectionVector()); $entity->setMotion($player->getDirectionVector());
$infinity = $this->hasEnchantment(Enchantment::INFINITY()); $infinity = $this->hasEnchantment(Enchantment::INFINITY());

View File

@ -319,17 +319,17 @@ class ItemFactory{
//TODO: the meta values should probably be hardcoded; they won't change, but the EntityLegacyIds might //TODO: the meta values should probably be hardcoded; they won't change, but the EntityLegacyIds might
$this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::ZOMBIE, "Zombie Spawn Egg") extends SpawnEgg{ $this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::ZOMBIE, "Zombie Spawn Egg") extends SpawnEgg{
protected function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{ protected function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{
return new Zombie(Location::fromObject($pos, $world, $yaw, $pitch), new CompoundTag()); return new Zombie(Location::fromObject($pos, $world, $yaw, $pitch));
} }
}); });
$this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::SQUID, "Squid Spawn Egg") extends SpawnEgg{ $this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::SQUID, "Squid Spawn Egg") extends SpawnEgg{
public function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{ public function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{
return new Squid(Location::fromObject($pos, $world, $yaw, $pitch), new CompoundTag()); return new Squid(Location::fromObject($pos, $world, $yaw, $pitch));
} }
}); });
$this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::VILLAGER, "Villager Spawn Egg") extends SpawnEgg{ $this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::VILLAGER, "Villager Spawn Egg") extends SpawnEgg{
public function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{ public function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{
return new Villager(Location::fromObject($pos, $world, $yaw, $pitch), new CompoundTag()); return new Villager(Location::fromObject($pos, $world, $yaw, $pitch));
} }
}); });
} }

View File

@ -29,7 +29,6 @@ use pocketmine\entity\object\Painting;
use pocketmine\entity\object\PaintingMotive; use pocketmine\entity\object\PaintingMotive;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\world\sound\PaintingPlaceSound; use pocketmine\world\sound\PaintingPlaceSound;
use function array_rand; use function array_rand;
@ -76,7 +75,7 @@ class PaintingItem extends Item{
$replacePos = $blockReplace->getPos(); $replacePos = $blockReplace->getPos();
$clickedPos = $blockClicked->getPos(); $clickedPos = $blockClicked->getPos();
$entity = new Painting(Location::fromObject($replacePos, $replacePos->getWorldNonNull()), $clickedPos, $face, $motive, new CompoundTag()); $entity = new Painting(Location::fromObject($replacePos, $replacePos->getWorldNonNull()), $clickedPos, $face, $motive);
$this->pop(); $this->pop();
$entity->spawnToAll(); $entity->spawnToAll();

View File

@ -295,7 +295,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$world->registerChunkListener($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4); $world->registerChunkListener($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4);
$this->usedChunks[World::chunkHash($spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4)] = UsedChunkStatus::NEEDED(); $this->usedChunks[World::chunkHash($spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4)] = UsedChunkStatus::NEEDED();
parent::__construct($spawn, $this->playerInfo->getSkin(), $namedtag ?? new CompoundTag()); parent::__construct($spawn, $this->playerInfo->getSkin(), $namedtag);
$this->onGround = $onGround; //TODO: this hack is needed for new players in-air ticks - they don't get detected as on-ground until they move $this->onGround = $onGround; //TODO: this hack is needed for new players in-air ticks - they don't get detected as on-ground until they move
$ev = new PlayerLoginEvent($this, "Plugin reason"); $ev = new PlayerLoginEvent($this, "Plugin reason");

View File

@ -52,7 +52,6 @@ use pocketmine\item\ItemUseResult;
use pocketmine\item\LegacyStringToItemParser; use pocketmine\item\LegacyStringToItemParser;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\BlockActorDataPacket; use pocketmine\network\mcpe\protocol\BlockActorDataPacket;
use pocketmine\network\mcpe\protocol\ClientboundPacket; use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\network\mcpe\protocol\UpdateBlockPacket; use pocketmine\network\mcpe\protocol\UpdateBlockPacket;
@ -1386,7 +1385,7 @@ class World implements ChunkManager{
return null; return null;
} }
$itemEntity = new ItemEntity(Location::fromObject($source, $this, lcg_value() * 360, 0), $item, new CompoundTag()); $itemEntity = new ItemEntity(Location::fromObject($source, $this, lcg_value() * 360, 0), $item);
$itemEntity->setPickupDelay($delay); $itemEntity->setPickupDelay($delay);
$itemEntity->setMotion($motion ?? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1)); $itemEntity->setMotion($motion ?? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1));
@ -1405,7 +1404,7 @@ class World implements ChunkManager{
$orbs = []; $orbs = [];
foreach(ExperienceOrb::splitIntoOrbSizes($amount) as $split){ foreach(ExperienceOrb::splitIntoOrbSizes($amount) as $split){
$orb = new ExperienceOrb(Location::fromObject($pos, $this,lcg_value() * 360, 0), new CompoundTag()); $orb = new ExperienceOrb(Location::fromObject($pos, $this, lcg_value() * 360, 0));
$orb->setXpValue($split); $orb->setXpValue($split);
$orb->setMotion($this->temporalVector->setComponents((lcg_value() * 0.2 - 0.1) * 2, lcg_value() * 0.4, (lcg_value() * 0.2 - 0.1) * 2)); $orb->setMotion($this->temporalVector->setComponents((lcg_value() * 0.2 - 0.1) * 2, lcg_value() * 0.4, (lcg_value() * 0.2 - 0.1) * 2));