Typehinted up entity API

Did you guys think ALPHA7 changes were done?!
Sone stuff still needs some work, most notably data-properties can't be typed yet because they are just mushed into a couple of methods.
This commit is contained in:
Dylan K. Taylor 2017-08-16 19:03:39 +01:00
parent ea414ea72d
commit 741394dab1
23 changed files with 197 additions and 149 deletions

View File

@ -35,7 +35,7 @@ interface IPlayer extends ServerOperator{
/** /**
* @return string * @return string
*/ */
public function getName(); public function getName() : string;
/** /**
* @return bool * @return bool

View File

@ -55,7 +55,7 @@ class OfflinePlayer implements IPlayer, Metadatable{
return $this->getPlayer() !== null; return $this->getPlayer() !== null;
} }
public function getName(){ public function getName() : string{
return $this->name; return $this->name;
} }

View File

@ -487,7 +487,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity) : bool{
return false; return false;
} }
@ -756,7 +756,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return $this->inAirTicks; return $this->inAirTicks;
} }
protected function switchLevel(Level $targetLevel){ protected function switchLevel(Level $targetLevel) : bool{
$oldLevel = $this->level; $oldLevel = $this->level;
if(parent::switchLevel($targetLevel)){ if(parent::switchLevel($targetLevel)){
foreach($this->usedChunks as $index => $d){ foreach($this->usedChunks as $index => $d){
@ -1398,7 +1398,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return []; return [];
} }
protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){ protected function checkGroundState(float $movX, float $movY, float $movZ, float $dx, float $dy, float $dz){
if(!$this->onGround or $movY != 0){ if(!$this->onGround or $movY != 0){
$bb = clone $this->boundingBox; $bb = clone $this->boundingBox;
$bb->minY = $this->y - 0.01; $bb->minY = $this->y - 0.01;
@ -1643,7 +1643,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if(!$this->loggedIn){ if(!$this->loggedIn){
return false; return false;
} }
@ -3549,10 +3549,9 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
/** /**
* Gets the username * Gets the username
*
* @return string * @return string
*/ */
public function getName(){ public function getName() : string{
return $this->username; return $this->username;
} }

View File

@ -42,7 +42,7 @@ interface CommandSender extends Permissible{
/** /**
* @return string * @return string
*/ */
public function getName(); public function getName() : string;
/** /**
* Returns the line height of the command-sender's screen. Used for determining sizes for command output pagination * Returns the line height of the command-sender's screen. Used for determining sizes for command output pagination

View File

@ -124,7 +124,7 @@ class ConsoleCommandSender implements CommandSender{
/** /**
* @return string * @return string
*/ */
public function getName(){ public function getName() : string{
return "CONSOLE"; return "CONSOLE";
} }

View File

@ -44,7 +44,7 @@ class RemoteConsoleCommandSender extends ConsoleCommandSender{
return $this->messages; return $this->messages;
} }
public function getName(){ public function getName() : string{
return "Rcon"; return "Rcon";
} }

View File

@ -25,5 +25,5 @@ namespace pocketmine\entity;
interface Ageable{ interface Ageable{
public function isBaby(); public function isBaby() : bool;
} }

View File

@ -26,7 +26,7 @@ namespace pocketmine\entity;
abstract class Animal extends Creature implements Ageable{ abstract class Animal extends Creature implements Ageable{
public function isBaby(){ public function isBaby() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY);
} }
} }

View File

@ -62,7 +62,7 @@ class Arrow extends Projectile{
} }
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;
} }

View File

@ -82,6 +82,10 @@ class Effect{
} }
} }
/**
* @param string $internalName
* @param Effect $effect
*/
public static function registerEffect(string $internalName, Effect $effect){ public static function registerEffect(string $internalName, Effect $effect){
self::$effects[$effect->getId()] = $effect; self::$effects[$effect->getId()] = $effect;
self::$effects[$internalName] = $effect; self::$effects[$internalName] = $effect;
@ -92,9 +96,9 @@ class Effect{
* *
* @return Effect|null * @return Effect|null
*/ */
public static function getEffect($id){ public static function getEffect(int $id){
if(isset(self::$effects[$id])){ if(isset(self::$effects[$id])){
return clone self::$effects[(int) $id]; return clone self::$effects[$id];
} }
return null; return null;
} }
@ -104,7 +108,7 @@ class Effect{
* *
* @return Effect|null * @return Effect|null
*/ */
public static function getEffectByName($name){ public static function getEffectByName(string $name){
if(isset(self::$effects[$name])){ if(isset(self::$effects[$name])){
return clone self::$effects[$name]; return clone self::$effects[$name];
} }
@ -113,23 +117,23 @@ class Effect{
/** @var int */ /** @var int */
protected $id; protected $id;
/** @var string */
protected $name; protected $name;
/** @var int */
protected $duration; protected $duration;
/** @var int */
protected $amplifier = 0; protected $amplifier = 0;
/** @var int[] */
protected $color; protected $color;
/** @var bool */
protected $show = true; protected $visible = true;
/** @var bool */
protected $ambient = false; protected $ambient = false;
/** @var bool */
protected $bad; protected $bad;
/** @var int */
protected $defaultDuration = 300 * 20; protected $defaultDuration = 300 * 20;
/** @var bool */
protected $hasBubbles = true; protected $hasBubbles = true;
/** /**
@ -142,10 +146,10 @@ class Effect{
* @param int $defaultDuration Duration in ticks the effect will last for by default if applied without a duration. * @param int $defaultDuration Duration in ticks the effect will last for by default if applied without a duration.
* @param bool $hasBubbles Whether the effect has potion bubbles. Some do not (e.g. Instant Damage has its own particles instead of bubbles) * @param bool $hasBubbles Whether the effect has potion bubbles. Some do not (e.g. Instant Damage has its own particles instead of bubbles)
*/ */
public function __construct($id, $name, $r, $g, $b, $isBad = false, int $defaultDuration = 300 * 20, bool $hasBubbles = true){ public function __construct(int $id, string $name, int $r, int $g, int $b, bool $isBad = false, int $defaultDuration = 300 * 20, bool $hasBubbles = true){
$this->id = $id; $this->id = $id;
$this->name = $name; $this->name = $name;
$this->bad = (bool) $isBad; $this->bad = $isBad;
$this->setColor($r, $g, $b); $this->setColor($r, $g, $b);
$this->defaultDuration = $defaultDuration; $this->defaultDuration = $defaultDuration;
$this->duration = $defaultDuration; $this->duration = $defaultDuration;
@ -156,7 +160,7 @@ class Effect{
* Returns the translation key used to translate this effect's name. * Returns the translation key used to translate this effect's name.
* @return string * @return string
*/ */
public function getName(){ public function getName() : string{
return $this->name; return $this->name;
} }
@ -164,7 +168,7 @@ class Effect{
* Returns the effect ID as per Minecraft PE * Returns the effect ID as per Minecraft PE
* @return int * @return int
*/ */
public function getId(){ public function getId() : int{
return $this->id; return $this->id;
} }
@ -186,7 +190,7 @@ class Effect{
* Returns the duration remaining of the effect in ticks. * Returns the duration remaining of the effect in ticks.
* @return int * @return int
*/ */
public function getDuration(){ public function getDuration() : int{
return $this->duration; return $this->duration;
} }
@ -212,18 +216,19 @@ class Effect{
* *
* @return bool * @return bool
*/ */
public function isVisible(){ public function isVisible() : bool{
return $this->show; return $this->visible;
} }
/** /**
* Changes the visibility of the effect. * Changes the visibility of the effect.
*
* @param bool $bool * @param bool $bool
* *
* @return $this * @return $this
*/ */
public function setVisible($bool){ public function setVisible(bool $bool){
$this->show = (bool) $bool; $this->visible = $bool;
return $this; return $this;
} }
@ -238,10 +243,9 @@ class Effect{
/** /**
* Returns the amplifier of this effect. * Returns the amplifier of this effect.
*
* @return int * @return int
*/ */
public function getAmplifier(){ public function getAmplifier() : int{
return $this->amplifier; return $this->amplifier;
} }
@ -259,18 +263,19 @@ class Effect{
* Returns whether the effect is ambient. * Returns whether the effect is ambient.
* @return bool * @return bool
*/ */
public function isAmbient(){ public function isAmbient() : bool{
return $this->ambient; return $this->ambient;
} }
/** /**
* Sets the ambiency of this effect. * Sets the ambiency of this effect.
*
* @param bool $ambient * @param bool $ambient
* *
* @return $this * @return $this
*/ */
public function setAmbient($ambient = true){ public function setAmbient(bool $ambient = true){
$this->ambient = (bool) $ambient; $this->ambient = $ambient;
return $this; return $this;
} }
@ -280,7 +285,7 @@ class Effect{
* *
* @return bool * @return bool
*/ */
public function isBad(){ public function isBad() : bool{
return $this->bad; return $this->bad;
} }
@ -289,7 +294,7 @@ class Effect{
* *
* @return bool * @return bool
*/ */
public function canTick(){ public function canTick() : bool{
switch($this->id){ switch($this->id){
case Effect::POISON: case Effect::POISON:
if(($interval = (25 >> $this->amplifier)) > 0){ if(($interval = (25 >> $this->amplifier)) > 0){
@ -370,9 +375,9 @@ class Effect{
/** /**
* Returns an RGB color array of this effect's color. * Returns an RGB color array of this effect's color.
* @return array * @return int[]
*/ */
public function getColor(){ public function getColor() : array{
return [$this->color >> 16, ($this->color >> 8) & 0xff, $this->color & 0xff]; return [$this->color >> 16, ($this->color >> 8) & 0xff, $this->color & 0xff];
} }
@ -382,8 +387,10 @@ class Effect{
* @param int $r * @param int $r
* @param int $g * @param int $g
* @param int $b * @param int $b
*
* @return int
*/ */
public function setColor($r, $g, $b){ public function setColor(int $r, int $g, int $b) {
$this->color = (($r & 0xff) << 16) + (($g & 0xff) << 8) + ($b & 0xff); $this->color = (($r & 0xff) << 16) + (($g & 0xff) << 8) + ($b & 0xff);
} }
@ -394,7 +401,7 @@ class Effect{
* @param bool $modify * @param bool $modify
* @param Effect|null $oldEffect * @param Effect|null $oldEffect
*/ */
public function add(Entity $entity, $modify = false, Effect $oldEffect = null){ public function add(Entity $entity, bool $modify = false, Effect $oldEffect = null){
$entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $modify, $oldEffect)); $entity->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityEffectAddEvent($entity, $this, $modify, $oldEffect));
if($ev->isCancelled()){ if($ev->isCancelled()){
return; return;

View File

@ -222,6 +222,7 @@ abstract class Entity extends Location implements Metadatable{
*/ */
protected $hasSpawned = []; protected $hasSpawned = [];
/** @var int */
protected $id; protected $id;
protected $dataProperties = [ protected $dataProperties = [
@ -241,85 +242,121 @@ abstract class Entity extends Location implements Metadatable{
/** @var Chunk */ /** @var Chunk */
public $chunk; public $chunk;
/** @var EntityDamageEvent|null */
protected $lastDamageCause = null; protected $lastDamageCause = null;
/** @var Block[] */ /** @var Block[] */
private $blocksAround = []; private $blocksAround = [];
/** @var float|null */
public $lastX = null; public $lastX = null;
/** @var float|null */
public $lastY = null; public $lastY = null;
/** @var float|null */
public $lastZ = null; public $lastZ = null;
/** @var float */
public $motionX; public $motionX;
/** @var float */
public $motionY; public $motionY;
/** @var float */
public $motionZ; public $motionZ;
/** @var Vector3 */ /** @var Vector3 */
public $temporalVector; public $temporalVector;
/** @var float */
public $lastMotionX; public $lastMotionX;
/** @var float */
public $lastMotionY; public $lastMotionY;
/** @var float */
public $lastMotionZ; public $lastMotionZ;
/** @var float */
public $lastYaw; public $lastYaw;
/** @var float */
public $lastPitch; public $lastPitch;
/** @var AxisAlignedBB */ /** @var AxisAlignedBB */
public $boundingBox; public $boundingBox;
/** @var bool */
public $onGround; public $onGround;
public $inBlock = false; /** @var int */
public $positionChanged;
public $motionChanged;
public $deadTicks = 0; public $deadTicks = 0;
/** @var int */
protected $age = 0; protected $age = 0;
public $height; /** @var float */
public $eyeHeight = null; public $eyeHeight = null;
/** @var float */
public $height;
/** @var float */
public $width; public $width;
/** @var float */
public $length; public $length;
/** @var float */
protected $baseOffset = 0.0; protected $baseOffset = 0.0;
/** @var int */ /** @var int */
private $health = 20; private $health = 20;
private $maxHealth = 20; private $maxHealth = 20;
protected $ySize = 0; /** @var float */
protected $stepHeight = 0; protected $ySize = 0.0;
/** @var float */
protected $stepHeight = 0.0;
/** @var bool */
public $keepMovement = false; public $keepMovement = false;
/** @var float */ /** @var float */
public $fallDistance = 0.0; public $fallDistance = 0.0;
/** @var int */
public $ticksLived = 0; public $ticksLived = 0;
/** @var int */
public $lastUpdate; public $lastUpdate;
/** @var int */
public $maxFireTicks; public $maxFireTicks;
/** @var int */
public $fireTicks = 0; public $fireTicks = 0;
/** @var CompoundTag */
public $namedtag; public $namedtag;
/** @var bool */
public $canCollide = true; public $canCollide = true;
/** @var bool */
protected $isStatic = false; protected $isStatic = false;
/** @var bool */
public $isCollided = false; public $isCollided = false;
/** @var bool */
public $isCollidedHorizontally = false; public $isCollidedHorizontally = false;
/** @var bool */
public $isCollidedVertically = false; public $isCollidedVertically = false;
/** @var int */
public $noDamageTicks; public $noDamageTicks;
protected $justCreated; /** @var bool */
protected $justCreated = true;
/** @var bool */
private $invulnerable; private $invulnerable;
/** @var AttributeMap */ /** @var AttributeMap */
protected $attributeMap; protected $attributeMap;
/** @var float */
protected $gravity; protected $gravity;
/** @var float */
protected $drag; protected $drag;
/** @var Server */ /** @var Server */
protected $server; protected $server;
/** @var bool */
public $closed = false; public $closed = false;
/** @var TimingsHandler */ /** @var TimingsHandler */
protected $timings; protected $timings;
/** @var bool */
protected $isPlayer = false; protected $isPlayer = false;
@ -335,7 +372,6 @@ abstract class Entity extends Location implements Metadatable{
} }
$this->id = Entity::$entityCount++; $this->id = Entity::$entityCount++;
$this->justCreated = true;
$this->namedtag = $nbt; $this->namedtag = $nbt;
$this->chunk = $level->getChunk($this->namedtag["Pos"][0] >> 4, $this->namedtag["Pos"][2] >> 4, true); $this->chunk = $level->getChunk($this->namedtag["Pos"][0] >> 4, $this->namedtag["Pos"][2] >> 4, true);
@ -372,7 +408,7 @@ abstract class Entity extends Location implements Metadatable{
if(!isset($this->namedtag->Fire)){ if(!isset($this->namedtag->Fire)){
$this->namedtag->Fire = new ShortTag("Fire", 0); $this->namedtag->Fire = new ShortTag("Fire", 0);
} }
$this->fireTicks = $this->namedtag["Fire"]; $this->fireTicks = (int) $this->namedtag["Fire"];
if(!isset($this->namedtag->Air)){ if(!isset($this->namedtag->Air)){
$this->namedtag->Air = new ShortTag("Air", 300); $this->namedtag->Air = new ShortTag("Air", 300);
@ -412,14 +448,14 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* @return bool * @return bool
*/ */
public function isNameTagVisible(){ public function isNameTagVisible() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_SHOW_NAMETAG); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_SHOW_NAMETAG);
} }
/** /**
* @return bool * @return bool
*/ */
public function isNameTagAlwaysVisible(){ public function isNameTagAlwaysVisible() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ALWAYS_SHOW_NAMETAG); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ALWAYS_SHOW_NAMETAG);
} }
@ -434,14 +470,14 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* @param bool $value * @param bool $value
*/ */
public function setNameTagVisible($value = true){ public function setNameTagVisible(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_SHOW_NAMETAG, $value); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_SHOW_NAMETAG, $value);
} }
/** /**
* @param bool $value * @param bool $value
*/ */
public function setNameTagAlwaysVisible($value = true){ public function setNameTagAlwaysVisible(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ALWAYS_SHOW_NAMETAG, $value); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ALWAYS_SHOW_NAMETAG, $value);
} }
@ -480,21 +516,21 @@ abstract class Entity extends Location implements Metadatable{
} }
public function isSneaking(){ public function isSneaking() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SNEAKING); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SNEAKING);
} }
public function setSneaking($value = true){ public function setSneaking(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SNEAKING, (bool) $value); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SNEAKING, $value);
} }
public function isSprinting(){ public function isSprinting() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING);
} }
public function setSprinting($value = true){ public function setSprinting(bool $value = true){
if($value !== $this->isSprinting()){ if($value !== $this->isSprinting()){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING, (bool) $value); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING, $value);
$attr = $this->attributeMap->getAttribute(Attribute::MOVEMENT_SPEED); $attr = $this->attributeMap->getAttribute(Attribute::MOVEMENT_SPEED);
$attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true); $attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true);
} }
@ -504,7 +540,7 @@ abstract class Entity extends Location implements Metadatable{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IMMOBILE); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IMMOBILE);
} }
public function setImmobile($value = true){ public function setImmobile(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IMMOBILE, $value); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IMMOBILE, $value);
} }
@ -544,7 +580,7 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* Returns the entity ID of the owning entity, or null if the entity doesn't have an owner. * Returns the entity ID of the owning entity, or null if the entity doesn't have an owner.
* @return int|string|null * @return int|null
*/ */
public function getOwningEntityId(){ public function getOwningEntityId(){
return $this->getDataProperty(self::DATA_OWNER_EID); return $this->getDataProperty(self::DATA_OWNER_EID);
@ -580,7 +616,7 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* Returns the entity ID of the entity's target, or null if it doesn't have a target. * Returns the entity ID of the entity's target, or null if it doesn't have a target.
* @return int|string|null * @return int|null
*/ */
public function getTargetEntityId(){ public function getTargetEntityId(){
return $this->getDataProperty(self::DATA_TARGET_EID); return $this->getDataProperty(self::DATA_TARGET_EID);
@ -689,7 +725,7 @@ abstract class Entity extends Location implements Metadatable{
return null; return null;
} }
public static function registerEntity($className, $force = false){ public static function registerEntity($className, bool $force = false) : bool{
$class = new \ReflectionClass($className); $class = new \ReflectionClass($className);
if(is_a($className, Entity::class, true) and !$class->isAbstract()){ if(is_a($className, Entity::class, true) and !$class->isAbstract()){
if($className::NETWORK_ID !== -1){ if($className::NETWORK_ID !== -1){
@ -771,7 +807,7 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* @return Player[] * @return Player[]
*/ */
public function getViewers(){ public function getViewers() : array{
return $this->hasSpawned; return $this->hasSpawned;
} }
@ -884,7 +920,7 @@ abstract class Entity extends Location implements Metadatable{
return $this->health; return $this->health;
} }
public function isAlive(){ public function isAlive() : bool{
return $this->health > 0; return $this->health > 0;
} }
@ -950,11 +986,11 @@ abstract class Entity extends Location implements Metadatable{
$this->maxHealth = (int) $amount; $this->maxHealth = (int) $amount;
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity) : bool{
return !$this->justCreated and $entity !== $this; return !$this->justCreated and $entity !== $this;
} }
protected function checkObstruction($x, $y, $z){ protected function checkObstruction(float $x, float $y, float $z) : bool{
if(count($this->level->getCollisionCubes($this, $this->getBoundingBox(), false)) === 0){ if(count($this->level->getCollisionCubes($this, $this->getBoundingBox(), false)) === 0){
return false; return false;
} }
@ -1049,7 +1085,7 @@ abstract class Entity extends Location implements Metadatable{
return false; return false;
} }
public function entityBaseTick($tickDiff = 1){ public function entityBaseTick(int $tickDiff = 1) : bool{
//TODO: check vehicles //TODO: check vehicles
$this->blocksAround = null; $this->blocksAround = null;
@ -1156,7 +1192,7 @@ abstract class Entity extends Location implements Metadatable{
/** /**
* @return Vector3 * @return Vector3
*/ */
public function getDirectionVector(){ public function getDirectionVector() : Vector3{
$y = -sin(deg2rad($this->pitch)); $y = -sin(deg2rad($this->pitch));
$xz = cos(deg2rad($this->pitch)); $xz = cos(deg2rad($this->pitch));
$x = -$xz * sin(deg2rad($this->yaw)); $x = -$xz * sin(deg2rad($this->yaw));
@ -1165,11 +1201,11 @@ abstract class Entity extends Location implements Metadatable{
return $this->temporalVector->setComponents($x, $y, $z)->normalize(); return $this->temporalVector->setComponents($x, $y, $z)->normalize();
} }
public function getDirectionPlane(){ public function getDirectionPlane() : Vector2{
return (new Vector2(-cos(deg2rad($this->yaw) - M_PI_2), -sin(deg2rad($this->yaw) - M_PI_2)))->normalize(); return (new Vector2(-cos(deg2rad($this->yaw) - M_PI_2), -sin(deg2rad($this->yaw) - M_PI_2)))->normalize();
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;
} }
@ -1212,11 +1248,11 @@ abstract class Entity extends Location implements Metadatable{
$this->level->updateEntities[$this->id] = $this; $this->level->updateEntities[$this->id] = $this;
} }
public function isOnFire(){ public function isOnFire() : bool{
return $this->fireTicks > 0; return $this->fireTicks > 0;
} }
public function setOnFire($seconds){ public function setOnFire(int $seconds){
$ticks = $seconds * 20; $ticks = $seconds * 20;
if($ticks > $this->fireTicks){ if($ticks > $this->fireTicks){
$this->fireTicks = $ticks; $this->fireTicks = $ticks;
@ -1234,6 +1270,9 @@ abstract class Entity extends Location implements Metadatable{
return false; return false;
} }
/**
* @return int|null
*/
public function getDirection(){ public function getDirection(){
$rotation = ($this->yaw - 90) % 360; $rotation = ($this->yaw - 90) % 360;
if($rotation < 0){ if($rotation < 0){
@ -1252,9 +1291,7 @@ abstract class Entity extends Location implements Metadatable{
} }
} }
public function canTriggerWalking() : bool{
public function canTriggerWalking(){
return true; return true;
} }
@ -1267,7 +1304,7 @@ abstract class Entity extends Location implements Metadatable{
* @param bool $onGround * @param bool $onGround
*/ */
protected function updateFallState(float $distanceThisTick, bool $onGround){ protected function updateFallState(float $distanceThisTick, bool $onGround){
if($onGround === true){ if($onGround){
if($this->fallDistance > 0){ if($this->fallDistance > 0){
$this->fall($this->fallDistance); $this->fall($this->fallDistance);
$this->resetFallDistance(); $this->resetFallDistance();
@ -1290,7 +1327,7 @@ abstract class Entity extends Location implements Metadatable{
} }
public function getEyeHeight(){ public function getEyeHeight() : float{
return $this->eyeHeight; return $this->eyeHeight;
} }
@ -1302,7 +1339,7 @@ abstract class Entity extends Location implements Metadatable{
} }
protected function switchLevel(Level $targetLevel){ protected function switchLevel(Level $targetLevel) : bool{
if($this->closed){ if($this->closed){
return false; return false;
} }
@ -1327,15 +1364,15 @@ abstract class Entity extends Location implements Metadatable{
return true; return true;
} }
public function getPosition(){ public function getPosition() : Position{
return new Position($this->x, $this->y, $this->z, $this->level); return $this->asPosition();
} }
public function getLocation(){ public function getLocation() : Location{
return new Location($this->x, $this->y, $this->z, $this->yaw, $this->pitch, $this->level); return $this->asLocation();
} }
public function isInsideOfWater(){ public function isInsideOfWater() : bool{
$block = $this->level->getBlock($this->temporalVector->setComponents(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z))); $block = $this->level->getBlock($this->temporalVector->setComponents(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z)));
if($block instanceof Water){ if($block instanceof Water){
@ -1346,7 +1383,7 @@ abstract class Entity extends Location implements Metadatable{
return false; return false;
} }
public function isInsideOfSolid(){ public function isInsideOfSolid() : bool{
$block = $this->level->getBlock($this->temporalVector->setComponents(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z))); $block = $this->level->getBlock($this->temporalVector->setComponents(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z)));
$bb = $block->getBoundingBox(); $bb = $block->getBoundingBox();
@ -1357,7 +1394,7 @@ abstract class Entity extends Location implements Metadatable{
return false; return false;
} }
public function fastMove($dx, $dy, $dz){ public function fastMove(float $dx, float $dy, float $dz) : bool{
if($dx == 0 and $dz == 0 and $dy == 0){ if($dx == 0 and $dz == 0 and $dy == 0){
return true; return true;
} }
@ -1396,7 +1433,7 @@ abstract class Entity extends Location implements Metadatable{
return true; return true;
} }
public function move($dx, $dy, $dz){ public function move(float $dx, float $dy, float $dz) : bool{
if($dx == 0 and $dz == 0 and $dy == 0){ if($dx == 0 and $dz == 0 and $dy == 0){
return true; return true;
@ -1557,14 +1594,17 @@ abstract class Entity extends Location implements Metadatable{
} }
} }
protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){ protected function checkGroundState(float $movX, float $movY, float $movZ, float $dx, float $dy, float $dz){
$this->isCollidedVertically = $movY != $dy; $this->isCollidedVertically = $movY != $dy;
$this->isCollidedHorizontally = ($movX != $dx or $movZ != $dz); $this->isCollidedHorizontally = ($movX != $dx or $movZ != $dz);
$this->isCollided = ($this->isCollidedHorizontally or $this->isCollidedVertically); $this->isCollided = ($this->isCollidedHorizontally or $this->isCollidedVertically);
$this->onGround = ($movY != $dy and $movY < 0); $this->onGround = ($movY != $dy and $movY < 0);
} }
public function getBlocksAround(){ /**
* @return Block[]
*/
public function getBlocksAround() : array{
if($this->blocksAround === null){ if($this->blocksAround === null){
$minX = Math::floorFloat($this->boundingBox->minX); $minX = Math::floorFloat($this->boundingBox->minX);
$minY = Math::floorFloat($this->boundingBox->minY); $minY = Math::floorFloat($this->boundingBox->minY);
@ -1607,7 +1647,7 @@ abstract class Entity extends Location implements Metadatable{
} }
} }
public function setPositionAndRotation(Vector3 $pos, $yaw, $pitch){ public function setPositionAndRotation(Vector3 $pos, float $yaw, float $pitch) : bool{
if($this->setPosition($pos) === true){ if($this->setPosition($pos) === true){
$this->setRotation($yaw, $pitch); $this->setRotation($yaw, $pitch);
@ -1617,7 +1657,7 @@ abstract class Entity extends Location implements Metadatable{
return false; return false;
} }
public function setRotation($yaw, $pitch){ public function setRotation(float $yaw, float $pitch){
$this->yaw = $yaw; $this->yaw = $yaw;
$this->pitch = $pitch; $this->pitch = $pitch;
$this->scheduleUpdate(); $this->scheduleUpdate();
@ -1681,7 +1721,7 @@ abstract class Entity extends Location implements Metadatable{
list($this->lastMotionX, $this->lastMotionY, $this->lastMotionZ) = [$this->motionX, $this->motionY, $this->motionZ]; list($this->lastMotionX, $this->lastMotionY, $this->lastMotionZ) = [$this->motionX, $this->motionY, $this->motionZ];
} }
public function getMotion(){ public function getMotion() : Vector3{
return new Vector3($this->motionX, $this->motionY, $this->motionZ); return new Vector3($this->motionX, $this->motionY, $this->motionZ);
} }
@ -1704,7 +1744,7 @@ abstract class Entity extends Location implements Metadatable{
return true; return true;
} }
public function isOnGround(){ public function isOnGround() : bool{
return $this->onGround === true; return $this->onGround === true;
} }
@ -1754,7 +1794,7 @@ abstract class Entity extends Location implements Metadatable{
return false; return false;
} }
public function getId(){ public function getId() : int{
return $this->id; return $this->id;
} }
@ -1813,7 +1853,7 @@ abstract class Entity extends Location implements Metadatable{
* *
* @return bool * @return bool
*/ */
public function setDataProperty($id, $type, $value, bool $send = true){ public function setDataProperty(int $id, int $type, $value, bool $send = true) : bool{
if($this->getDataProperty($id) !== $value){ if($this->getDataProperty($id) !== $value){
$this->dataProperties[$id] = [$type, $value]; $this->dataProperties[$id] = [$type, $value];
if($send){ if($send){
@ -1831,7 +1871,7 @@ abstract class Entity extends Location implements Metadatable{
* *
* @return mixed * @return mixed
*/ */
public function getDataProperty($id){ public function getDataProperty(int $id){
return isset($this->dataProperties[$id]) ? $this->dataProperties[$id][1] : null; return isset($this->dataProperties[$id]) ? $this->dataProperties[$id][1] : null;
} }
@ -1840,32 +1880,32 @@ abstract class Entity extends Location implements Metadatable{
* *
* @return int|null * @return int|null
*/ */
public function getDataPropertyType($id){ public function getDataPropertyType(int $id){
return isset($this->dataProperties[$id]) ? $this->dataProperties[$id][0] : null; return isset($this->dataProperties[$id]) ? $this->dataProperties[$id][0] : null;
} }
/** /**
* @param int $propertyId * @param int $propertyId
* @param int $id * @param int $flagId
* @param bool $value * @param bool $value
* @param int $type * @param int $propertyType
*/ */
public function setDataFlag($propertyId, $id, $value = true, $type = self::DATA_TYPE_LONG){ public function setDataFlag(int $propertyId, int $flagId, bool $value = true, int $propertyType = self::DATA_TYPE_LONG){
if($this->getDataFlag($propertyId, $id) !== $value){ if($this->getDataFlag($propertyId, $flagId) !== $value){
$flags = (int) $this->getDataProperty($propertyId); $flags = (int) $this->getDataProperty($propertyId);
$flags ^= 1 << $id; $flags ^= 1 << $flagId;
$this->setDataProperty($propertyId, $type, $flags); $this->setDataProperty($propertyId, $propertyType, $flags);
} }
} }
/** /**
* @param int $propertyId * @param int $propertyId
* @param int $id * @param int $flagId
* *
* @return bool * @return bool
*/ */
public function getDataFlag($propertyId, $id){ public function getDataFlag(int $propertyId, int $flagId) : bool{
return (((int) $this->getDataProperty($propertyId)) & (1 << $id)) > 0; return (((int) $this->getDataProperty($propertyId)) & (1 << $flagId)) > 0;
} }
public function __destruct(){ public function __destruct(){

View File

@ -70,7 +70,7 @@ class FallingSand extends Entity{
$this->setDataProperty(self::DATA_VARIANT, self::DATA_TYPE_INT, $this->getBlock() | ($this->getDamage() << 8)); $this->setDataProperty(self::DATA_VARIANT, self::DATA_TYPE_INT, $this->getBlock() | ($this->getDamage() << 8));
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity) : bool{
return false; return false;
} }
@ -80,7 +80,7 @@ class FallingSand extends Entity{
} }
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;

View File

@ -378,7 +378,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::EXPERIENCE)); $this->attributeMap->addAttribute(Attribute::getAttribute(Attribute::EXPERIENCE));
} }
public function entityBaseTick($tickDiff = 1){ public function entityBaseTick(int $tickDiff = 1) : bool{
$hasUpdate = parent::entityBaseTick($tickDiff); $hasUpdate = parent::entityBaseTick($tickDiff);
$this->doFoodTick($tickDiff); $this->doFoodTick($tickDiff);
@ -427,7 +427,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
} }
} }
public function getName(){ public function getName() : string{
return $this->getNameTag(); return $this->getNameTag();
} }

View File

@ -98,7 +98,7 @@ class Item extends Entity{
} }
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;
} }
@ -181,53 +181,53 @@ class Item extends Entity{
/** /**
* @return ItemItem * @return ItemItem
*/ */
public function getItem(){ public function getItem() : ItemItem{
return $this->item; return $this->item;
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity) : bool{
return false; return false;
} }
/** /**
* @return int * @return int
*/ */
public function getPickupDelay(){ public function getPickupDelay() : int{
return $this->pickupDelay; return $this->pickupDelay;
} }
/** /**
* @param int $delay * @param int $delay
*/ */
public function setPickupDelay($delay){ public function setPickupDelay(int $delay){
$this->pickupDelay = $delay; $this->pickupDelay = $delay;
} }
/** /**
* @return string * @return string
*/ */
public function getOwner(){ public function getOwner() : string{
return $this->owner; return $this->owner;
} }
/** /**
* @param string $owner * @param string $owner
*/ */
public function setOwner($owner){ public function setOwner(string $owner){
$this->owner = $owner; $this->owner = $owner;
} }
/** /**
* @return string * @return string
*/ */
public function getThrower(){ public function getThrower() : string{
return $this->thrower; return $this->thrower;
} }
/** /**
* @param string $thrower * @param string $thrower
*/ */
public function setThrower($thrower){ public function setThrower(string $thrower){
$this->thrower = $thrower; $this->thrower = $thrower;
} }

View File

@ -57,7 +57,7 @@ abstract class Living extends Entity implements Damageable{
/** @var Effect[] */ /** @var Effect[] */
protected $effects = []; protected $effects = [];
abstract public function getName(); abstract public function getName() : string;
protected function initEntity(){ protected function initEntity(){
parent::initEntity(); parent::initEntity();
@ -151,7 +151,7 @@ abstract class Living extends Entity implements Damageable{
} }
public function hasLineOfSight(Entity $entity){ public function hasLineOfSight(Entity $entity) : bool{
//TODO: head height //TODO: head height
return true; return true;
//return $this->getLevel()->rayTraceBlocks(Vector3::createVector($this->x, $this->y + $this->height, $this->z), Vector3::createVector($entity->x, $entity->y + $entity->height, $entity->z)) === null; //return $this->getLevel()->rayTraceBlocks(Vector3::createVector($this->x, $this->y + $this->height, $this->z), Vector3::createVector($entity->x, $entity->y + $entity->height, $entity->z)) === null;
@ -377,7 +377,7 @@ abstract class Living extends Entity implements Damageable{
$this->attackTime = 10; //0.5 seconds cooldown $this->attackTime = 10; //0.5 seconds cooldown
} }
public function knockBack(Entity $attacker, $damage, $x, $z, $base = 0.4){ public function knockBack(Entity $attacker, float $damage, float $x, float $z, float $base = 0.4){
$f = sqrt($x * $x + $z * $z); $f = sqrt($x * $x + $z * $z);
if($f <= 0){ if($f <= 0){
return; return;
@ -416,7 +416,7 @@ abstract class Living extends Entity implements Damageable{
} }
} }
public function entityBaseTick($tickDiff = 1){ public function entityBaseTick(int $tickDiff = 1) : bool{
Timings::$timerLivingEntityBaseTick->startTiming(); Timings::$timerLivingEntityBaseTick->startTiming();
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BREATHING, !$this->isInsideOfWater()); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BREATHING, !$this->isInsideOfWater());
@ -505,7 +505,7 @@ abstract class Living extends Entity implements Damageable{
* *
* @return Block[] * @return Block[]
*/ */
public function getLineOfSight($maxDistance, $maxLength = 0, array $transparent = []){ public function getLineOfSight(int $maxDistance, int $maxLength = 0, array $transparent = []) : array{
if($maxDistance > 120){ if($maxDistance > 120){
$maxDistance = 120; $maxDistance = 120;
} }
@ -551,7 +551,7 @@ abstract class Living extends Entity implements Damageable{
* *
* @return Block|null * @return Block|null
*/ */
public function getTargetBlock($maxDistance, array $transparent = []){ public function getTargetBlock(int $maxDistance, array $transparent = []){
try{ try{
$block = $this->getLineOfSight($maxDistance, 1, $transparent)[0]; $block = $this->getLineOfSight($maxDistance, 1, $transparent)[0];
if($block instanceof Block){ if($block instanceof Block){

View File

@ -70,7 +70,7 @@ class PrimedTNT extends Entity implements Explosive{
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity) : bool{
return false; return false;
} }
@ -79,7 +79,7 @@ class PrimedTNT extends Entity implements Explosive{
$this->namedtag->Fuse = new ByteTag("Fuse", $this->fuse); $this->namedtag->Fuse = new ByteTag("Fuse", $this->fuse);
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;

View File

@ -65,7 +65,7 @@ abstract class Projectile extends Entity{
} }
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity) : bool{
return $entity instanceof Living and !$this->onGround; return $entity instanceof Living and !$this->onGround;
} }
@ -108,7 +108,7 @@ abstract class Projectile extends Entity{
$this->namedtag->Age = new ShortTag("Age", $this->age); $this->namedtag->Age = new ShortTag("Age", $this->age);
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;
} }

View File

@ -42,7 +42,7 @@ class Snowball extends Projectile{
parent::__construct($level, $nbt, $shootingEntity); parent::__construct($level, $nbt, $shootingEntity);
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed){ if($this->closed){
return false; return false;
} }

View File

@ -49,7 +49,7 @@ class Squid extends WaterAnimal{
parent::initEntity(); parent::initEntity();
} }
public function getName(){ public function getName() : string{
return "Squid"; return "Squid";
} }
@ -73,12 +73,12 @@ class Squid extends WaterAnimal{
} }
} }
private function generateRandomDirection(){ private function generateRandomDirection() : Vector3{
return new Vector3(mt_rand(-1000, 1000) / 1000, mt_rand(-500, 500) / 1000, mt_rand(-1000, 1000) / 1000); return new Vector3(mt_rand(-1000, 1000) / 1000, mt_rand(-500, 500) / 1000, mt_rand(-1000, 1000) / 1000);
} }
public function onUpdate($currentTick){ public function onUpdate(int $currentTick) : bool{
if($this->closed !== false){ if($this->closed !== false){
return false; return false;
} }

View File

@ -41,7 +41,7 @@ class Villager extends Creature implements NPC, Ageable{
public $length = 0.6; public $length = 0.6;
public $height = 1.8; public $height = 1.8;
public function getName(){ public function getName() : string{
return "Villager"; return "Villager";
} }
@ -73,17 +73,17 @@ class Villager extends Creature implements NPC, Ageable{
/** /**
* Sets the villager profession * Sets the villager profession
* *
* @param $profession * @param int $profession
*/ */
public function setProfession($profession){ public function setProfession(int $profession){
$this->namedtag->Profession = new IntTag("Profession", $profession); $this->namedtag->Profession = new IntTag("Profession", $profession);
} }
public function getProfession(){ public function getProfession() : int{
return $this->namedtag["Profession"]; return (int) $this->namedtag["Profession"];
} }
public function isBaby(){ public function isBaby() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY);
} }
} }

View File

@ -25,7 +25,7 @@ namespace pocketmine\entity;
abstract class WaterAnimal extends Creature implements Ageable{ abstract class WaterAnimal extends Creature implements Ageable{
public function isBaby(){ public function isBaby() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY); return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY);
} }
} }

View File

@ -35,7 +35,7 @@ class Zombie extends Monster{
public $length = 0.6; public $length = 0.6;
public $height = 1.8; public $height = 1.8;
public function getName(){ public function getName() : string{
return "Zombie"; return "Zombie";
} }

View File

@ -27,7 +27,9 @@ use pocketmine\math\Vector3;
class Location extends Position{ class Location extends Position{
/** @var float */
public $yaw; public $yaw;
/** @var float */
public $pitch; public $pitch;
/** /**