Introduce EntityFactory

This contains all of the static stuff that was previously embedded in the Entity static root. This solves a bunch of problems like circular dependencies between parent and child classes, encapsulating logic and reducing the size of the enormous Entity.php.
This commit is contained in:
Dylan K. Taylor
2019-01-06 23:54:29 +00:00
parent b1cef8509a
commit 7d827a1c65
13 changed files with 298 additions and 247 deletions

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\projectile\Arrow as ArrowEntity;
use pocketmine\entity\projectile\Projectile;
use pocketmine\event\entity\EntityShootBowEvent;
@ -53,7 +53,7 @@ class Bow extends Tool{
return false;
}
$nbt = Entity::createBaseNBT(
$nbt = EntityFactory::createBaseNBT(
$player->add(0, $player->getEyeHeight(), 0),
$player->getDirectionVector(),
($player->yaw > 180 ? 360 : 0) - $player->yaw,
@ -66,7 +66,7 @@ class Bow extends Tool{
$force = min((($p ** 2) + $p * 2) / 3, 1) * 2;
/** @var ArrowEntity $entity */
$entity = Entity::create(ArrowEntity::class, $player->getLevel(), $nbt, $player, $force == 2);
$entity = EntityFactory::create(ArrowEntity::class, $player->getLevel(), $nbt, $player, $force == 2);
$infinity = $this->hasEnchantment(Enchantment::INFINITY);
if($infinity){

View File

@ -25,7 +25,7 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Living;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\tile\Skull;
@ -194,7 +194,7 @@ class ItemFactory{
//TODO: ENDER_EYE
self::registerItem(new Item(Item::GLISTERING_MELON, 0, "Glistering Melon"));
foreach(Entity::getKnownTypes() as $className){
foreach(EntityFactory::getKnownTypes() as $className){
/** @var Living|string $className */
if(is_a($className, Living::class, true) and $className::NETWORK_ID !== -1){
self::registerItem(new SpawnEgg(Item::SPAWN_EGG, $className::NETWORK_ID, $className, "Spawn Egg"));

View File

@ -24,7 +24,7 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\object\Painting;
use pocketmine\entity\object\PaintingMotive;
use pocketmine\math\Facing;
@ -86,7 +86,7 @@ class PaintingItem extends Item{
return false;
}
$nbt = Entity::createBaseNBT($blockReplace, null, $direction * 90, 0);
$nbt = EntityFactory::createBaseNBT($blockReplace, null, $direction * 90, 0);
$nbt->setByte("Direction", $direction);
$nbt->setString("Motive", $motive->getName());
$nbt->setInt("TileX", $blockClicked->getFloorX());
@ -94,7 +94,7 @@ class PaintingItem extends Item{
$nbt->setInt("TileZ", $blockClicked->getFloorZ());
/** @var Painting $entity */
$entity = Entity::create(Painting::class, $blockReplace->getLevel(), $nbt);
$entity = EntityFactory::create(Painting::class, $blockReplace->getLevel(), $nbt);
$this->pop();
$entity->spawnToAll();

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\EntityIds;
use pocketmine\entity\projectile\Throwable;
use pocketmine\event\entity\ProjectileLaunchEvent;
@ -54,14 +54,14 @@ abstract class ProjectileItem extends Item{
}
public function onClickAir(Player $player, Vector3 $directionVector) : bool{
$nbt = Entity::createBaseNBT($player->add(0, $player->getEyeHeight(), 0), $directionVector, $player->yaw, $player->pitch);
$nbt = EntityFactory::createBaseNBT($player->add(0, $player->getEyeHeight(), 0), $directionVector, $player->yaw, $player->pitch);
$this->addExtraTags($nbt);
$class = $this->getProjectileEntityClass();
Utils::testValidInstance($class, Throwable::class);
/** @var Throwable $projectile */
$projectile = Entity::create($class, $player->getLevel(), $nbt, $player);
$projectile = EntityFactory::create($class, $player->getLevel(), $nbt, $player);
$projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
$projectileEv = new ProjectileLaunchEvent($projectile);

View File

@ -25,6 +25,7 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\math\Vector3;
use pocketmine\Player;
use pocketmine\utils\Utils;
@ -50,13 +51,13 @@ class SpawnEgg extends Item{
}
public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector) : bool{
$nbt = Entity::createBaseNBT($blockReplace->add(0.5, 0, 0.5), null, lcg_value() * 360, 0);
$nbt = EntityFactory::createBaseNBT($blockReplace->add(0.5, 0, 0.5), null, lcg_value() * 360, 0);
if($this->hasCustomName()){
$nbt->setString("CustomName", $this->getCustomName());
}
$entity = Entity::create($this->entityClass, $player->getLevel(), $nbt);
$entity = EntityFactory::create($this->entityClass, $player->getLevel(), $nbt);
$this->pop();
$entity->spawnToAll();
return true;