Player: reduce SetTitlePacket creation boilerplate

it's better to encapsulate all this logic in one place so that third party developers can more easily understand this, and also to reduce the amount of crap we have in Player.
This commit is contained in:
Dylan K. Taylor 2019-05-08 16:43:05 +01:00
parent 9fbf41b9a1
commit a331c5e13f
2 changed files with 46 additions and 28 deletions

View File

@ -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
*

View File

@ -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;
}
}