From 21a1e0eb6bc0002ff310e8d9b46893443a05c0c8 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 17 Aug 2017 11:08:54 +0100 Subject: [PATCH] Player API typehints --- src/pocketmine/IPlayer.php | 4 +- src/pocketmine/Player.php | 76 ++++++++++++++++++++------------- src/pocketmine/entity/Human.php | 2 +- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/pocketmine/IPlayer.php b/src/pocketmine/IPlayer.php index 65e6cd200..bc0e8bc9d 100644 --- a/src/pocketmine/IPlayer.php +++ b/src/pocketmine/IPlayer.php @@ -63,12 +63,12 @@ interface IPlayer extends ServerOperator{ public function getPlayer(); /** - * @return int|double + * @return int|null */ public function getFirstPlayed(); /** - * @return int|double + * @return int|null */ public function getLastPlayed(); diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index ec20eb369..7fdd96f12 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -267,7 +267,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ /** @var Player[] */ protected $hiddenPlayers = []; - /** @var Vector3 */ + /** @var Vector3|null */ protected $newPosition; /** @var bool */ @@ -388,12 +388,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return $this->flying; } - public function setAutoJump($value){ + public function setAutoJump(bool $value){ $this->autoJump = $value; $this->sendSettings(); } - public function hasAutoJump(){ + public function hasAutoJump() : bool{ return $this->autoJump; } @@ -439,7 +439,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ /** * @param bool $remove */ - public function setRemoveFormat($remove = true){ + public function setRemoveFormat(bool $remove = true){ $this->removeFormat = (bool) $remove; } @@ -636,7 +636,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * @param string $ip * @param int $port */ - public function __construct(SourceInterface $interface, $clientID, $ip, $port){ + public function __construct(SourceInterface $interface, $clientID, string $ip, int $port){ $this->interface = $interface; $this->windows = new \SplObjectStorage(); $this->perm = new PermissibleBase($this); @@ -668,7 +668,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ /** * @param string $achievementId */ - public function removeAchievement($achievementId){ + public function removeAchievement(string $achievementId){ if($this->hasAchievement($achievementId)){ $this->achievements[$achievementId] = false; } @@ -706,14 +706,14 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ /** * @param string $name */ - public function setDisplayName($name){ + public function setDisplayName(string $name){ $this->displayName = $name; if($this->spawned){ $this->server->updatePlayerListData($this->getUniqueId(), $this->getId(), $this->getDisplayName(), $this->getSkinId(), $this->getSkinData()); } } - public function setSkin($str, $skinId){ + public function setSkin(string $str, string $skinId){ parent::setSkin($str, $skinId); if($this->spawned){ $this->server->updatePlayerListData($this->getUniqueId(), $this->getId(), $this->getDisplayName(), $skinId, $str); @@ -741,8 +741,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return $this->port; } - public function getNextPosition(){ - return $this->newPosition !== null ? new Position($this->newPosition->x, $this->newPosition->y, $this->newPosition->z, $this->level) : $this->getPosition(); + /** + * @return Position + */ + public function getNextPosition() : Position{ + return $this->newPosition !== null ? Position::fromObject($this->newPosition, $this->level) : $this->getPosition(); } /** @@ -752,7 +755,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return $this->sleeping !== null; } - public function getInAirTicks(){ + public function getInAirTicks() : int{ return $this->inAirTicks; } @@ -769,7 +772,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - private function unloadChunk($x, $z, Level $level = null){ + private function unloadChunk(int $x, int $z, Level $level = null){ $level = $level ?? $this->level; $index = Level::chunkHash($x, $z); if(isset($this->usedChunks[$index])){ @@ -1045,9 +1048,9 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * @param DataPacket $packet * @param bool $needACK * - * @return int|bool + * @return bool|int */ - public function dataPacket(DataPacket $packet, $needACK = false){ + public function dataPacket(DataPacket $packet, bool $needACK = false){ if(!$this->connected){ return false; } @@ -1085,7 +1088,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * * @return bool|int */ - public function directDataPacket(DataPacket $packet, $needACK = false){ + public function directDataPacket(DataPacket $packet, bool $needACK = false){ if($this->connected === false){ return false; } @@ -1419,7 +1422,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - protected function checkNearEntities($tickDiff){ + protected function checkNearEntities(int $tickDiff){ foreach($this->level->getNearbyEntities($this->boundingBox->grow(1, 0.5, 1), $this) as $entity){ $entity->scheduleUpdate(); @@ -1481,7 +1484,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - protected function processMovement($tickDiff){ + protected function processMovement(int $tickDiff){ if(!$this->isAlive() or !$this->spawned or $this->newPosition === null or $this->isSleeping()){ return; } @@ -1743,7 +1746,16 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - public function canInteract(Vector3 $pos, $maxDistance, $maxDiff = 0.5){ + /** + * Returns whether the player can interact with the specified position. This checks distance and direction. + * + * @param Vector3 $pos + * @param $maxDistance + * @param float $maxDiff + * + * @return bool + */ + public function canInteract(Vector3 $pos, $maxDistance, float $maxDiff = 0.5) : bool{ $eyePos = $this->getPosition()->add(0, $this->getEyeHeight(), 0); if($eyePos->distanceSquared($pos) > $maxDistance ** 2){ return false; @@ -3228,11 +3240,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * Kicks a player from the server * * @param string $reason - * @param bool $isAdmin + * @param bool $isAdmin * * @return bool */ - public function kick($reason = "", bool $isAdmin = true) : bool{ + public function kick(string $reason = "", bool $isAdmin = true) : bool{ $this->server->getPluginManager()->callEvent($ev = new PlayerKickEvent($this, $reason, $this->getLeaveMessage())); if(!$ev->isCancelled()){ if($isAdmin){ @@ -3360,7 +3372,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->dataPacket($pk); } - public function sendTranslation($message, array $parameters = []){ + /** + * @param string $message + * @param string[] $parameters + */ + public function sendTranslation(string $message, array $parameters = []){ $pk = new TextPacket(); if(!$this->server->isLanguageForced()){ $pk->type = TextPacket::TYPE_TRANSLATION; @@ -3376,7 +3392,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->dataPacket($pk); } - public function sendPopup($message, $subtitle = ""){ + public function sendPopup(string $message, string $subtitle = ""){ $pk = new TextPacket(); $pk->type = TextPacket::TYPE_POPUP; $pk->source = $message; @@ -3384,7 +3400,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->dataPacket($pk); } - public function sendTip($message){ + public function sendTip(string $message){ $pk = new TextPacket(); $pk->type = TextPacket::TYPE_TIP; $pk->message = $message; @@ -3395,7 +3411,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * @param string $sender * @param string $message */ - public function sendWhisper($sender, $message){ + public function sendWhisper(string $sender, string $message){ $pk = new TextPacket(); $pk->type = TextPacket::TYPE_WHISPER; $pk->source = $sender; @@ -3407,11 +3423,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * Note for plugin developers: use kick() with the isAdmin * flag set to kick without the "Kicked by admin" part instead of this method. * - * @param string $message Message to be broadcasted - * @param string $reason Reason showed in console - * @param bool $notify + * @param TextContainer|string $message Message to be broadcasted + * @param string $reason Reason showed in console + * @param bool $notify */ - final public function close($message = "", $reason = "generic reason", $notify = true){ + final public function close($message = "", string $reason = "generic reason", bool $notify = true){ if($this->connected and !$this->closed){ try{ @@ -3518,8 +3534,10 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * Handles player data saving * * @param bool $async + * + * @throws \InvalidStateException if the player is closed */ - public function save($async = false){ + public function save(bool $async = false){ if($this->closed){ throw new \InvalidStateException("Tried to save closed player"); } diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 0dee4e7de..b4b87d5f8 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -107,7 +107,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ * @param string $str * @param string $skinId */ - public function setSkin($str, $skinId){ + public function setSkin(string $str, string $skinId){ if(!Player::isValidSkin($str)){ throw new \InvalidStateException("Specified skin is not valid, must be 8KiB or 16KiB"); }