diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index eb92d86797..01864d68cb 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2290,7 +2290,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, if($subtitle !== ""){ $this->sendSubTitle($subtitle); } - $this->sendTitleText($title, SetTitlePacket::TYPE_SET_TITLE); + $this->sendDataPacket(SetTitlePacket::title($title)); } /** @@ -2309,7 +2309,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, * @param string $subtitle */ public function sendSubTitle(string $subtitle) : void{ - $this->sendTitleText($subtitle, SetTitlePacket::TYPE_SET_SUBTITLE); + $this->sendDataPacket(SetTitlePacket::subtitle($subtitle)); } /** @@ -2328,25 +2328,21 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, * @param string $message */ public function sendActionBarMessage(string $message) : void{ - $this->sendTitleText($message, SetTitlePacket::TYPE_SET_ACTIONBAR_MESSAGE); + $this->sendDataPacket(SetTitlePacket::actionBarMessage($message)); } /** * Removes the title from the client's screen. */ public function removeTitles(){ - $pk = new SetTitlePacket(); - $pk->type = SetTitlePacket::TYPE_CLEAR_TITLE; - $this->sendDataPacket($pk); + $this->sendDataPacket(SetTitlePacket::clearTitle()); } /** * Resets the title duration settings. */ public function resetTitles(){ - $pk = new SetTitlePacket(); - $pk->type = SetTitlePacket::TYPE_RESET_TITLE; - $this->sendDataPacket($pk); + $this->sendDataPacket(SetTitlePacket::resetTitleOptions()); } /** @@ -2358,28 +2354,10 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, */ public function setTitleDuration(int $fadeIn, int $stay, int $fadeOut){ if($fadeIn >= 0 and $stay >= 0 and $fadeOut >= 0){ - $pk = new SetTitlePacket(); - $pk->type = SetTitlePacket::TYPE_SET_ANIMATION_TIMES; - $pk->fadeInTime = $fadeIn; - $pk->stayTime = $stay; - $pk->fadeOutTime = $fadeOut; - $this->sendDataPacket($pk); + $this->sendDataPacket(SetTitlePacket::setAnimationTimes($fadeIn, $stay, $fadeOut)); } } - /** - * Internal function used for sending titles. - * - * @param string $title - * @param int $type - */ - protected function sendTitleText(string $title, int $type){ - $pk = new SetTitlePacket(); - $pk->type = $type; - $pk->text = $title; - $this->sendDataPacket($pk); - } - /** * Sends a direct chat message to a player * diff --git a/src/pocketmine/network/mcpe/protocol/SetTitlePacket.php b/src/pocketmine/network/mcpe/protocol/SetTitlePacket.php index 207a9f86aa..d9227b8efe 100644 --- a/src/pocketmine/network/mcpe/protocol/SetTitlePacket.php +++ b/src/pocketmine/network/mcpe/protocol/SetTitlePacket.php @@ -69,4 +69,44 @@ class SetTitlePacket extends DataPacket implements ClientboundPacket{ public function handle(SessionHandler $handler) : bool{ return $handler->handleSetTitle($this); } + + private static function type(int $type) : self{ + $result = new self; + $result->type = $type; + return $result; + } + + private static function text(int $type, string $text) : self{ + $result = self::type($type); + $result->text = $text; + return $result; + } + + public static function title(string $text) : self{ + return self::text(self::TYPE_SET_TITLE, $text); + } + + public static function subtitle(string $text) : self{ + return self::text(self::TYPE_SET_SUBTITLE, $text); + } + + public static function actionBarMessage(string $text) : self{ + return self::text(self::TYPE_SET_ACTIONBAR_MESSAGE, $text); + } + + public static function clearTitle() : self{ + return self::type(self::TYPE_CLEAR_TITLE); + } + + public static function resetTitleOptions() : self{ + return self::type(self::TYPE_RESET_TITLE); + } + + public static function setAnimationTimes(int $fadeIn, int $stay, int $fadeOut) : self{ + $result = self::type(self::TYPE_SET_ANIMATION_TIMES); + $result->fadeInTime = $fadeIn; + $result->stayTime = $stay; + $result->fadeOutTime = $fadeOut; + return $result; + } }