Use typed properties in src/entity package

[bc break]
This commit is contained in:
Dylan K. Taylor 2022-06-01 21:19:02 +01:00
parent 2029e3be20
commit 7b8eeb42f6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
16 changed files with 160 additions and 284 deletions

View File

@ -90,120 +90,79 @@ abstract class Entity{
} }
/** @var Player[] */ /** @var Player[] */
protected $hasSpawned = []; protected array $hasSpawned = [];
/** @var int */ protected int $id;
protected $id;
private EntityMetadataCollection $networkProperties; private EntityMetadataCollection $networkProperties;
/** @var EntityDamageEvent|null */ protected ?EntityDamageEvent $lastDamageCause = null;
protected $lastDamageCause = null;
/** @var Block[]|null */ /** @var Block[]|null */
protected $blocksAround; protected ?array $blocksAround = null;
/** @var Location */ protected Location $location;
protected $location; protected Location $lastLocation;
/** @var Location */ protected Vector3 $motion;
protected $lastLocation; protected Vector3 $lastMotion;
/** @var Vector3 */ protected bool $forceMovementUpdate = false;
protected $motion;
/** @var Vector3 */
protected $lastMotion;
/** @var bool */
protected $forceMovementUpdate = false;
/** @var AxisAlignedBB */ public AxisAlignedBB $boundingBox;
public $boundingBox; public bool $onGround = false;
/** @var bool */
public $onGround = false;
/** @var EntitySizeInfo */ public EntitySizeInfo $size;
public $size;
private float $health = 20.0; private float $health = 20.0;
private int $maxHealth = 20; private int $maxHealth = 20;
/** @var float */ protected float $ySize = 0.0;
protected $ySize = 0.0; protected float $stepHeight = 0.0;
/** @var float */ public bool $keepMovement = false;
protected $stepHeight = 0.0;
/** @var bool */
public $keepMovement = false;
/** @var float */ public float $fallDistance = 0.0;
public $fallDistance = 0.0; public int $ticksLived = 0;
/** @var int */ public int $lastUpdate;
public $ticksLived = 0; protected int $fireTicks = 0;
/** @var int */
public $lastUpdate;
/** @var int */
protected $fireTicks = 0;
private bool $savedWithChunk = true; private bool $savedWithChunk = true;
/** @var bool */ public bool $isCollided = false;
public $isCollided = false; public bool $isCollidedHorizontally = false;
/** @var bool */ public bool $isCollidedVertically = false;
public $isCollidedHorizontally = false;
/** @var bool */
public $isCollidedVertically = false;
/** @var int */ public int $noDamageTicks = 0;
public $noDamageTicks = 0; protected bool $justCreated = true;
/** @var bool */
protected $justCreated = true;
/** @var AttributeMap */ protected AttributeMap $attributeMap;
protected $attributeMap;
/** @var float */ protected float $gravity;
protected $gravity; protected float $drag;
/** @var float */ protected bool $gravityEnabled = true;
protected $drag;
/** @var bool */
protected $gravityEnabled = true;
/** @var Server */ protected Server $server;
protected $server;
/** @var bool */ protected bool $closed = false;
protected $closed = false;
private bool $closeInFlight = false; private bool $closeInFlight = false;
private bool $needsDespawn = false; private bool $needsDespawn = false;
/** @var TimingsHandler */ protected TimingsHandler $timings;
protected $timings;
protected bool $networkPropertiesDirty = false; protected bool $networkPropertiesDirty = false;
/** @var string */ protected string $nameTag = "";
protected $nameTag = ""; protected bool $nameTagVisible = true;
/** @var bool */ protected bool $alwaysShowNameTag = false;
protected $nameTagVisible = true; protected string $scoreTag = "";
/** @var bool */ protected float $scale = 1.0;
protected $alwaysShowNameTag = false;
/** @var string */
protected $scoreTag = "";
/** @var float */
protected $scale = 1.0;
/** @var bool */ protected bool $canClimb = false;
protected $canClimb = false; protected bool $canClimbWalls = false;
/** @var bool */ protected bool $immobile = false;
protected $canClimbWalls = false; protected bool $invisible = false;
/** @var bool */ protected bool $silent = false;
protected $immobile = false;
/** @var bool */
protected $invisible = false;
/** @var bool */
protected $silent = false;
/** @var int|null */ protected ?int $ownerId = null;
protected $ownerId = null; protected ?int $targetId = null;
/** @var int|null */
protected $targetId = null;
private bool $constructorCalled = false; private bool $constructorCalled = false;

View File

@ -74,28 +74,18 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
public static function getNetworkTypeId() : string{ return EntityIds::PLAYER; } public static function getNetworkTypeId() : string{ return EntityIds::PLAYER; }
/** @var PlayerInventory */ protected PlayerInventory $inventory;
protected $inventory; protected PlayerOffHandInventory $offHandInventory;
protected PlayerEnderInventory $enderInventory;
/** @var PlayerOffHandInventory */ protected UuidInterface $uuid;
protected $offHandInventory;
/** @var PlayerEnderInventory */ protected Skin $skin;
protected $enderInventory;
/** @var UuidInterface */ protected HungerManager $hungerManager;
protected $uuid; protected ExperienceManager $xpManager;
/** @var Skin */ protected int $xpSeed;
protected $skin;
/** @var HungerManager */
protected $hungerManager;
/** @var ExperienceManager */
protected $xpManager;
/** @var int */
protected $xpSeed;
public function __construct(Location $location, Skin $skin, ?CompoundTag $nbt = null){ public function __construct(Location $location, Skin $skin, ?CompoundTag $nbt = null){
$this->skin = $skin; $this->skin = $skin;
@ -383,9 +373,9 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
public function getDrops() : array{ public function getDrops() : array{
return array_filter(array_merge( return array_filter(array_merge(
$this->inventory !== null ? array_values($this->inventory->getContents()) : [], array_values($this->inventory->getContents()),
$this->armorInventory !== null ? array_values($this->armorInventory->getContents()) : [], array_values($this->armorInventory->getContents()),
$this->offHandInventory !== null ? array_values($this->offHandInventory->getContents()) : [], array_values($this->offHandInventory->getContents()),
), function(Item $item) : bool{ return !$item->hasEnchantment(VanillaEnchantments::VANISHING()); }); ), function(Item $item) : bool{ return !$item->hasEnchantment(VanillaEnchantments::VANISHING()); });
} }
@ -404,55 +394,51 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
$inventoryTag = new ListTag([], NBT::TAG_Compound); $inventoryTag = new ListTag([], NBT::TAG_Compound);
$nbt->setTag("Inventory", $inventoryTag); $nbt->setTag("Inventory", $inventoryTag);
if($this->inventory !== null){
//Normal inventory
$slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
$item = $this->inventory->getItem($slot - 9);
if(!$item->isNull()){
$inventoryTag->push($item->nbtSerialize($slot));
}
}
//Armor //Normal inventory
for($slot = 100; $slot < 104; ++$slot){ $slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
$item = $this->armorInventory->getItem($slot - 100); for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
if(!$item->isNull()){ $item = $this->inventory->getItem($slot - 9);
$inventoryTag->push($item->nbtSerialize($slot)); if(!$item->isNull()){
} $inventoryTag->push($item->nbtSerialize($slot));
} }
$nbt->setInt("SelectedInventorySlot", $this->inventory->getHeldItemIndex());
} }
//Armor
for($slot = 100; $slot < 104; ++$slot){
$item = $this->armorInventory->getItem($slot - 100);
if(!$item->isNull()){
$inventoryTag->push($item->nbtSerialize($slot));
}
}
$nbt->setInt("SelectedInventorySlot", $this->inventory->getHeldItemIndex());
$offHandItem = $this->offHandInventory->getItem(0); $offHandItem = $this->offHandInventory->getItem(0);
if(!$offHandItem->isNull()){ if(!$offHandItem->isNull()){
$nbt->setTag("OffHandItem", $offHandItem->nbtSerialize()); $nbt->setTag("OffHandItem", $offHandItem->nbtSerialize());
} }
if($this->enderInventory !== null){ /** @var CompoundTag[] $items */
/** @var CompoundTag[] $items */ $items = [];
$items = [];
$slotCount = $this->enderInventory->getSize(); $slotCount = $this->enderInventory->getSize();
for($slot = 0; $slot < $slotCount; ++$slot){ for($slot = 0; $slot < $slotCount; ++$slot){
$item = $this->enderInventory->getItem($slot); $item = $this->enderInventory->getItem($slot);
if(!$item->isNull()){ if(!$item->isNull()){
$items[] = $item->nbtSerialize($slot); $items[] = $item->nbtSerialize($slot);
}
} }
$nbt->setTag("EnderChestInventory", new ListTag($items, NBT::TAG_Compound));
} }
if($this->skin !== null){ $nbt->setTag("EnderChestInventory", new ListTag($items, NBT::TAG_Compound));
$nbt->setTag("Skin", CompoundTag::create()
->setString("Name", $this->skin->getSkinId()) $nbt->setTag("Skin", CompoundTag::create()
->setByteArray("Data", $this->skin->getSkinData()) ->setString("Name", $this->skin->getSkinId())
->setByteArray("CapeData", $this->skin->getCapeData()) ->setByteArray("Data", $this->skin->getSkinData())
->setString("GeometryName", $this->skin->getGeometryName()) ->setByteArray("CapeData", $this->skin->getCapeData())
->setByteArray("GeometryData", $this->skin->getGeometryData()) ->setString("GeometryName", $this->skin->getGeometryName())
); ->setByteArray("GeometryData", $this->skin->getGeometryData())
} );
return $nbt; return $nbt;
} }
@ -512,11 +498,13 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
} }
protected function destroyCycles() : void{ protected function destroyCycles() : void{
$this->inventory = null; unset(
$this->offHandInventory = null; $this->inventory,
$this->enderInventory = null; $this->offHandInventory,
$this->hungerManager = null; $this->enderInventory,
$this->xpManager = null; $this->hungerManager,
$this->xpManager
);
parent::destroyCycles(); parent::destroyCycles();
} }
} }

View File

@ -76,47 +76,30 @@ use const M_PI;
abstract class Living extends Entity{ abstract class Living extends Entity{
protected const DEFAULT_BREATH_TICKS = 300; protected const DEFAULT_BREATH_TICKS = 300;
/** @var int */ protected int $attackTime = 0;
protected $attackTime = 0;
/** @var int */ public int $deadTicks = 0;
public $deadTicks = 0; protected int $maxDeadTicks = 25;
/** @var int */
protected $maxDeadTicks = 25;
/** @var float */ protected float $jumpVelocity = 0.42;
protected $jumpVelocity = 0.42;
/** @var EffectManager */ protected EffectManager $effectManager;
protected $effectManager;
/** @var ArmorInventory */ protected ArmorInventory $armorInventory;
protected $armorInventory;
/** @var bool */ protected bool $breathing = true;
protected $breathing = true; protected int $breathTicks = self::DEFAULT_BREATH_TICKS;
/** @var int */ protected int $maxBreathTicks = self::DEFAULT_BREATH_TICKS;
protected $breathTicks = self::DEFAULT_BREATH_TICKS;
/** @var int */
protected $maxBreathTicks = self::DEFAULT_BREATH_TICKS;
/** @var Attribute */ protected Attribute $healthAttr;
protected $healthAttr; protected Attribute $absorptionAttr;
/** @var Attribute */ protected Attribute $knockbackResistanceAttr;
protected $absorptionAttr; protected Attribute $moveSpeedAttr;
/** @var Attribute */
protected $knockbackResistanceAttr;
/** @var Attribute */
protected $moveSpeedAttr;
/** @var bool */ protected bool $sprinting = false;
protected $sprinting = false; protected bool $sneaking = false;
/** @var bool */ protected bool $gliding = false;
protected $sneaking = false; protected bool $swimming = false;
/** @var bool */
protected $gliding = false;
/** @var bool */
protected $swimming = false;
protected function getInitialDragMultiplier() : float{ return 0.02; } protected function getInitialDragMultiplier() : float{ return 0.02; }
@ -851,8 +834,10 @@ abstract class Living extends Entity{
} }
protected function destroyCycles() : void{ protected function destroyCycles() : void{
$this->armorInventory = null; unset(
$this->effectManager = null; $this->armorInventory,
$this->effectManager
);
parent::destroyCycles(); parent::destroyCycles();
} }
} }

View File

@ -39,10 +39,8 @@ class Squid extends WaterAnimal{
public static function getNetworkTypeId() : string{ return EntityIds::SQUID; } public static function getNetworkTypeId() : string{ return EntityIds::SQUID; }
/** @var Vector3|null */ public ?Vector3 $swimDirection = null;
public $swimDirection = null; public float $swimSpeed = 0.1;
/** @var float */
public $swimSpeed = 0.1;
private int $switchDirectionTicker = 0; private int $switchDirectionTicker = 0;

View File

@ -28,8 +28,7 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
abstract class WaterAnimal extends Living implements Ageable{ abstract class WaterAnimal extends Living implements Ageable{
/** @var bool */ protected bool $baby = false;
protected $baby = false;
public function isBaby() : bool{ public function isBaby() : bool{
return $this->baby; return $this->baby;

View File

@ -33,25 +33,22 @@ use function count;
use function spl_object_id; use function spl_object_id;
class EffectManager{ class EffectManager{
/** @var EffectInstance[] */ /** @var EffectInstance[] */
protected $effects = []; protected array $effects = [];
/** @var Color */ protected Color $bubbleColor;
protected $bubbleColor; protected bool $onlyAmbientEffects = false;
/** @var bool */
protected $onlyAmbientEffects = false;
/** /**
* @var \Closure[]|ObjectSet * @var \Closure[]|ObjectSet
* @phpstan-var ObjectSet<\Closure(EffectInstance, bool $replacesOldEffect) : void> * @phpstan-var ObjectSet<\Closure(EffectInstance, bool $replacesOldEffect) : void>
*/ */
protected $effectAddHooks; protected ObjectSet $effectAddHooks;
/** /**
* @var \Closure[]|ObjectSet * @var \Closure[]|ObjectSet
* @phpstan-var ObjectSet<\Closure(EffectInstance) : void> * @phpstan-var ObjectSet<\Closure(EffectInstance) : void>
*/ */
protected $effectRemoveHooks; protected ObjectSet $effectRemoveHooks;
public function __construct( public function __construct(
private Living $entity private Living $entity

View File

@ -78,23 +78,15 @@ class ExperienceOrb extends Entity{
return $result; return $result;
} }
/** @var int */ protected int $age = 0;
protected $age = 0;
/** /** Ticker used for determining interval in which to look for new target players. */
* @var int protected int $lookForTargetTime = 0;
* Ticker used for determining interval in which to look for new target players.
*/
protected $lookForTargetTime = 0;
/** /** Runtime entity ID of the player this XP orb is targeting. */
* @var int|null protected ?int $targetPlayerRuntimeId = null;
* Runtime entity ID of the player this XP orb is targeting.
*/
protected $targetPlayerRuntimeId = null;
/** @var int */ protected int $xpValue;
protected $xpValue;
public function __construct(Location $location, int $xpValue, ?CompoundTag $nbt = null){ public function __construct(Location $location, int $xpValue, ?CompoundTag $nbt = null){
$this->xpValue = $xpValue; $this->xpValue = $xpValue;

View File

@ -46,8 +46,7 @@ class FallingBlock extends Entity{
public static function getNetworkTypeId() : string{ return EntityIds::FALLING_BLOCK; } public static function getNetworkTypeId() : string{ return EntityIds::FALLING_BLOCK; }
/** @var Block */ protected Block $block;
protected $block;
public function __construct(Location $location, Block $block, ?CompoundTag $nbt = null){ public function __construct(Location $location, Block $block, ?CompoundTag $nbt = null){
$this->block = $block; $this->block = $block;

View File

@ -50,17 +50,11 @@ class ItemEntity extends Entity{
public const NEVER_DESPAWN = -1; public const NEVER_DESPAWN = -1;
public const MAX_DESPAWN_DELAY = 32767 + self::DEFAULT_DESPAWN_DELAY; //max value storable by mojang NBT :( public const MAX_DESPAWN_DELAY = 32767 + self::DEFAULT_DESPAWN_DELAY; //max value storable by mojang NBT :(
/** @var string */ protected string $owner = "";
protected $owner = ""; protected string $thrower = "";
/** @var string */ protected int $pickupDelay = 0;
protected $thrower = ""; protected int $despawnDelay = self::DEFAULT_DESPAWN_DELAY;
/** @var int */ protected Item $item;
protected $pickupDelay = 0;
/** @var Item */
protected $item;
/** @var int */
protected $despawnDelay = self::DEFAULT_DESPAWN_DELAY;
public function __construct(Location $location, Item $item, ?CompoundTag $nbt = null){ public function __construct(Location $location, Item $item, ?CompoundTag $nbt = null){
if($item->isNull()){ if($item->isNull()){
@ -196,12 +190,8 @@ class ItemEntity extends Entity{
} }
$nbt->setShort("Age", $age); $nbt->setShort("Age", $age);
$nbt->setShort("PickupDelay", $this->pickupDelay); $nbt->setShort("PickupDelay", $this->pickupDelay);
if($this->owner !== null){ $nbt->setString("Owner", $this->owner);
$nbt->setString("Owner", $this->owner); $nbt->setString("Thrower", $this->thrower);
}
if($this->thrower !== null){
$nbt->setString("Thrower", $this->thrower);
}
return $nbt; return $nbt;
} }

View File

@ -56,12 +56,9 @@ class Painting extends Entity{
Facing::EAST => 3 Facing::EAST => 3
]; ];
/** @var Vector3 */ protected Vector3 $blockIn;
protected $blockIn; protected int $facing;
/** @var int */ protected PaintingMotive $motive;
protected $facing = Facing::NORTH;
/** @var PaintingMotive */
protected $motive;
public function __construct(Location $location, Vector3 $blockIn, int $facing, PaintingMotive $motive, ?CompoundTag $nbt = null){ public function __construct(Location $location, Vector3 $blockIn, int $facing, PaintingMotive $motive, ?CompoundTag $nbt = null){
$this->motive = $motive; $this->motive = $motive;

View File

@ -84,18 +84,11 @@ class PaintingMotive{
return self::$motives; return self::$motives;
} }
/** @var string */ public function __construct(
protected $name; protected int $width,
/** @var int */ protected int $height,
protected $width; protected string $name
/** @var int */ ){}
protected $height;
public function __construct(int $width, int $height, string $name){
$this->name = $name;
$this->width = $width;
$this->height = $height;
}
public function getName() : string{ public function getName() : string{
return $this->name; return $this->name;

View File

@ -41,9 +41,7 @@ class PrimedTNT extends Entity implements Explosive{
public static function getNetworkTypeId() : string{ return EntityIds::TNT; } public static function getNetworkTypeId() : string{ return EntityIds::TNT; }
/** @var int */ protected int $fuse;
protected $fuse;
protected bool $worksUnderwater = false; protected bool $worksUnderwater = false;
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.98, 0.98); } protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.98, 0.98); }

View File

@ -52,20 +52,11 @@ class Arrow extends Projectile{
private const TAG_PICKUP = "pickup"; //TAG_Byte private const TAG_PICKUP = "pickup"; //TAG_Byte
public const TAG_CRIT = "crit"; //TAG_Byte public const TAG_CRIT = "crit"; //TAG_Byte
/** @var float */ protected float $damage = 2.0;
protected $damage = 2.0; protected int $pickupMode = self::PICKUP_ANY;
protected float $punchKnockback = 0.0;
/** @var int */ protected int $collideTicks = 0;
protected $pickupMode = self::PICKUP_ANY; protected bool $critical = false;
/** @var float */
protected $punchKnockback = 0.0;
/** @var int */
protected $collideTicks = 0;
/** @var bool */
protected $critical = false;
public function __construct(Location $location, ?Entity $shootingEntity, bool $critical, ?CompoundTag $nbt = null){ public function __construct(Location $location, ?Entity $shootingEntity, bool $critical, ?CompoundTag $nbt = null){
parent::__construct($location, $shootingEntity, $nbt); parent::__construct($location, $shootingEntity, $nbt);

View File

@ -51,11 +51,8 @@ use const PHP_INT_MAX;
abstract class Projectile extends Entity{ abstract class Projectile extends Entity{
/** @var float */ protected float $damage = 0.0;
protected $damage = 0.0; protected ?Block $blockHit = null;
/** @var Block|null */
protected $blockHit;
public function __construct(Location $location, ?Entity $shootingEntity, ?CompoundTag $nbt = null){ public function __construct(Location $location, ?Entity $shootingEntity, ?CompoundTag $nbt = null){
parent::__construct($location, $nbt); parent::__construct($location, $nbt);

View File

@ -52,8 +52,7 @@ class SplashPotion extends Throwable{
public static function getNetworkTypeId() : string{ return EntityIds::SPLASH_POTION; } public static function getNetworkTypeId() : string{ return EntityIds::SPLASH_POTION; }
/** @var bool */ protected bool $linger = false;
protected $linger = false;
protected PotionType $potionType; protected PotionType $potionType;
public function __construct(Location $location, ?Entity $shootingEntity, PotionType $potionType, ?CompoundTag $nbt = null){ public function __construct(Location $location, ?Entity $shootingEntity, PotionType $potionType, ?CompoundTag $nbt = null){

View File

@ -241,8 +241,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
protected ?float $lastMovementProcess = null; protected ?float $lastMovementProcess = null;
protected int $inAirTicks = 0; protected int $inAirTicks = 0;
/** @var float */
protected $stepHeight = 0.6; protected float $stepHeight = 0.6;
protected ?Vector3 $sleeping = null; protected ?Vector3 $sleeping = null;
private ?Position $spawnPosition = null; private ?Position $spawnPosition = null;
@ -2217,16 +2217,10 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->getWorld()->dropItem($this->location, $item); $this->getWorld()->dropItem($this->location, $item);
} }
if($this->inventory !== null){ $this->inventory->setHeldItemIndex(0);
$this->inventory->setHeldItemIndex(0); $this->inventory->clearAll();
$this->inventory->clearAll(); $this->armorInventory->clearAll();
} $this->offHandInventory->clearAll();
if($this->armorInventory !== null){
$this->armorInventory->clearAll();
}
if($this->offHandInventory !== null){
$this->offHandInventory->clearAll();
}
} }
if(!$ev->getKeepXp()){ if(!$ev->getKeepXp()){