Revamp Entity construction

This is a similar refactor to the one I recently did for tiles.

- Entity::createEntity() is removed. In its place are Entity::create() (runtime creation, use where you'd use a constructor, accepts a ::class parameter, throws exceptions on unknown entities) and Entity::createFromData() (internal, used to restore entities from chunks, swallows unknown entities and returns null).
- Entity::registerEntity() is renamed to Entity::register().
- Added Entity::override() to allow overriding factory classes without touching save IDs. This allows more cleanly extending & overriding entities. This method only allows overriding registered Entity classes with children of that class, which makes code using the factory much more sane and allows to provide safety guarantees which make the code less nasty.
- Entity::getKnownEntityTypes() is renamed to Entity::getKnownTypes().
- ProjectileItem::getProjectileEntityType() now returns a ::class constant instead of a stringy ID.
- Cleaned up a bunch of nasty code, particularly in Bow.
This commit is contained in:
Dylan K. Taylor
2019-01-06 23:33:36 +00:00
parent 3ae722867c
commit b1cef8509a
16 changed files with 224 additions and 169 deletions

View File

@ -1634,13 +1634,11 @@ class Level implements ChunkManager, Metadatable{
$nbt->setShort("Health", 5);
$nbt->setShort("PickupDelay", $delay);
$nbt->setTag($itemTag);
$itemEntity = Entity::createEntity("Item", $this, $nbt);
if($itemEntity instanceof ItemEntity){
$itemEntity->spawnToAll();
return $itemEntity;
}
/** @var ItemEntity $itemEntity */
$itemEntity = Entity::create(ItemEntity::class, $this, $nbt);
$itemEntity->spawnToAll();
return $itemEntity;
}
return null;
}
@ -1666,15 +1664,10 @@ class Level implements ChunkManager, Metadatable{
);
$nbt->setShort(ExperienceOrb::TAG_VALUE_PC, $split);
$orb = Entity::createEntity("XPOrb", $this, $nbt);
if($orb === null){
continue;
}
/** @var ExperienceOrb $orb */
$orb = Entity::create(ExperienceOrb::class, $this, $nbt);
$orb->spawnToAll();
if($orb instanceof ExperienceOrb){
$orbs[] = $orb;
}
$orbs[] = $orb;
}
return $orbs;