Use native union types for TranslationContainer|string

This commit is contained in:
Dylan K. Taylor 2021-08-10 15:17:26 +01:00
parent 27a2d06083
commit b4c0ddd155
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
9 changed files with 21 additions and 75 deletions

View File

@ -1194,10 +1194,9 @@ class Server{
} }
/** /**
* @param TranslationContainer|string $message
* @param CommandSender[]|null $recipients * @param CommandSender[]|null $recipients
*/ */
public function broadcastMessage($message, ?array $recipients = null) : int{ public function broadcastMessage(TranslationContainer|string $message, ?array $recipients = null) : int{
$recipients = $recipients ?? $this->getBroadcastChannelSubscribers(self::BROADCAST_CHANNEL_USERS); $recipients = $recipients ?? $this->getBroadcastChannelSubscribers(self::BROADCAST_CHANNEL_USERS);
foreach($recipients as $recipient){ foreach($recipients as $recipient){

View File

@ -231,10 +231,7 @@ abstract class Command{
$this->usageMessage = $usage; $this->usageMessage = $usage;
} }
/** public static function broadcastCommandMessage(CommandSender $source, TranslationContainer|string $message, bool $sendToSource = true) : void{
* @param TranslationContainer|string $message
*/
public static function broadcastCommandMessage(CommandSender $source, $message, bool $sendToSource = true) : void{
$users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE); $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
if($message instanceof TranslationContainer){ if($message instanceof TranslationContainer){
$formatted = "[" . $source->getName() . ": " . ($source->getLanguage()->get($message->getText()) !== $message->getText() ? "%" : "") . $message->getText() . "]"; $formatted = "[" . $source->getName() . ": " . ($source->getLanguage()->get($message->getText()) !== $message->getText() ? "%" : "") . $message->getText() . "]";

View File

@ -32,10 +32,7 @@ interface CommandSender extends Permissible{
public function getLanguage() : Language; public function getLanguage() : Language;
/** public function sendMessage(TranslationContainer|string $message) : void;
* @param TranslationContainer|string $message
*/
public function sendMessage($message) : void;
public function getServer() : Server; public function getServer() : Server;

View File

@ -58,10 +58,7 @@ class ConsoleCommandSender implements CommandSender{
return $this->language; return $this->language;
} }
/** public function sendMessage(TranslationContainer|string $message) : void{
* @param TranslationContainer|string $message
*/
public function sendMessage($message) : void{
$server = $this->getServer(); $server = $this->getServer();
if($message instanceof TranslationContainer){ if($message instanceof TranslationContainer){
$message = $this->getLanguage()->translate($message); $message = $this->getLanguage()->translate($message);

View File

@ -47,7 +47,7 @@ class PlayerDeathEvent extends EntityDeathEvent{
* @param Item[] $drops * @param Item[] $drops
* @param string|TranslationContainer|null $deathMessage Null will cause the default vanilla message to be used * @param string|TranslationContainer|null $deathMessage Null will cause the default vanilla message to be used
*/ */
public function __construct(Player $entity, array $drops, int $xp, $deathMessage){ public function __construct(Player $entity, array $drops, int $xp, TranslationContainer|string|null $deathMessage){
parent::__construct($entity, $drops, $xp); parent::__construct($entity, $drops, $xp);
$this->deathMessage = $deathMessage ?? self::deriveMessage($entity->getDisplayName(), $entity->getLastDamageCause()); $this->deathMessage = $deathMessage ?? self::deriveMessage($entity->getDisplayName(), $entity->getLastDamageCause());
} }
@ -63,17 +63,11 @@ class PlayerDeathEvent extends EntityDeathEvent{
return $this->entity; return $this->entity;
} }
/** public function getDeathMessage() : TranslationContainer|string{
* @return TranslationContainer|string
*/
public function getDeathMessage(){
return $this->deathMessage; return $this->deathMessage;
} }
/** public function setDeathMessage(TranslationContainer|string $deathMessage) : void{
* @param TranslationContainer|string $deathMessage
*/
public function setDeathMessage($deathMessage) : void{
$this->deathMessage = $deathMessage; $this->deathMessage = $deathMessage;
} }

View File

@ -37,27 +37,16 @@ class PlayerJoinEvent extends PlayerEvent{
/** @var string|TranslationContainer */ /** @var string|TranslationContainer */
protected $joinMessage; protected $joinMessage;
/** public function __construct(Player $player, TranslationContainer|string $joinMessage){
* PlayerJoinEvent constructor.
*
* @param TranslationContainer|string $joinMessage
*/
public function __construct(Player $player, $joinMessage){
$this->player = $player; $this->player = $player;
$this->joinMessage = $joinMessage; $this->joinMessage = $joinMessage;
} }
/** public function setJoinMessage(TranslationContainer|string $joinMessage) : void{
* @param string|TranslationContainer $joinMessage
*/
public function setJoinMessage($joinMessage) : void{
$this->joinMessage = $joinMessage; $this->joinMessage = $joinMessage;
} }
/** public function getJoinMessage() : TranslationContainer|string{
* @return string|TranslationContainer
*/
public function getJoinMessage(){
return $this->joinMessage; return $this->joinMessage;
} }
} }

View File

@ -40,12 +40,7 @@ class PlayerKickEvent extends PlayerEvent implements Cancellable{
/** @var string */ /** @var string */
protected $reason; protected $reason;
/** public function __construct(Player $player, string $reason, TranslationContainer|string $quitMessage){
* PlayerKickEvent constructor.
*
* @param TranslationContainer|string $quitMessage
*/
public function __construct(Player $player, string $reason, $quitMessage){
$this->player = $player; $this->player = $player;
$this->quitMessage = $quitMessage; $this->quitMessage = $quitMessage;
$this->reason = $reason; $this->reason = $reason;
@ -59,17 +54,11 @@ class PlayerKickEvent extends PlayerEvent implements Cancellable{
return $this->reason; return $this->reason;
} }
/** public function setQuitMessage(TranslationContainer|string $quitMessage) : void{
* @param TranslationContainer|string $quitMessage
*/
public function setQuitMessage($quitMessage) : void{
$this->quitMessage = $quitMessage; $this->quitMessage = $quitMessage;
} }
/** public function getQuitMessage() : TranslationContainer|string{
* @return TranslationContainer|string
*/
public function getQuitMessage(){
return $this->quitMessage; return $this->quitMessage;
} }
} }

View File

@ -36,26 +36,17 @@ class PlayerQuitEvent extends PlayerEvent{
/** @var string */ /** @var string */
protected $quitReason; protected $quitReason;
/** public function __construct(Player $player, TranslationContainer|string $quitMessage, string $quitReason){
* @param TranslationContainer|string $quitMessage
*/
public function __construct(Player $player, $quitMessage, string $quitReason){
$this->player = $player; $this->player = $player;
$this->quitMessage = $quitMessage; $this->quitMessage = $quitMessage;
$this->quitReason = $quitReason; $this->quitReason = $quitReason;
} }
/** public function setQuitMessage(TranslationContainer|string $quitMessage) : void{
* @param TranslationContainer|string $quitMessage
*/
public function setQuitMessage($quitMessage) : void{
$this->quitMessage = $quitMessage; $this->quitMessage = $quitMessage;
} }
/** public function getQuitMessage() : TranslationContainer|string{
* @return TranslationContainer|string
*/
public function getQuitMessage(){
return $this->quitMessage; return $this->quitMessage;
} }

View File

@ -319,10 +319,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
} }
} }
/** public function getLeaveMessage() : TranslationContainer|string{
* @return TranslationContainer|string
*/
public function getLeaveMessage(){
if($this->spawned){ if($this->spawned){
return new TranslationContainer(TextFormat::YELLOW . "%" . KnownTranslationKeys::MULTIPLAYER_PLAYER_LEFT, [ return new TranslationContainer(TextFormat::YELLOW . "%" . KnownTranslationKeys::MULTIPLAYER_PLAYER_LEFT, [
$this->getDisplayName() $this->getDisplayName()
@ -1782,10 +1779,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
/** /**
* Sends a direct chat message to a player * Sends a direct chat message to a player
*
* @param TranslationContainer|string $message
*/ */
public function sendMessage($message) : void{ public function sendMessage(TranslationContainer|string $message) : void{
if($message instanceof TranslationContainer){ if($message instanceof TranslationContainer){
$this->sendTranslation($message->getText(), $message->getParameters()); $this->sendTranslation($message->getText(), $message->getParameters());
return; return;
@ -1883,10 +1878,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
/** /**
* Kicks a player from the server * Kicks a player from the server
*
* @param TranslationContainer|string|null $quitMessage
*/ */
public function kick(string $reason = "", $quitMessage = null) : bool{ public function kick(string $reason = "", TranslationContainer|string|null $quitMessage = null) : bool{
$ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage()); $ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage());
$ev->call(); $ev->call();
if(!$ev->isCancelled()){ if(!$ev->isCancelled()){
@ -1914,7 +1907,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
* @param string $reason Shown to the player, usually this will appear on their disconnect screen. * @param string $reason Shown to the player, usually this will appear on their disconnect screen.
* @param TranslationContainer|string|null $quitMessage Message to broadcast to online players (null will use default) * @param TranslationContainer|string|null $quitMessage Message to broadcast to online players (null will use default)
*/ */
public function disconnect(string $reason, $quitMessage = null) : void{ public function disconnect(string $reason, TranslationContainer|string|null $quitMessage = null) : void{
if(!$this->isConnected()){ if(!$this->isConnected()){
return; return;
} }
@ -1930,7 +1923,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
* @param string $reason Shown to the player, usually this will appear on their disconnect screen. * @param string $reason Shown to the player, usually this will appear on their disconnect screen.
* @param TranslationContainer|string|null $quitMessage Message to broadcast to online players (null will use default) * @param TranslationContainer|string|null $quitMessage Message to broadcast to online players (null will use default)
*/ */
public function onPostDisconnect(string $reason, $quitMessage) : void{ public function onPostDisconnect(string $reason, TranslationContainer|string|null $quitMessage) : void{
if($this->isConnected()){ if($this->isConnected()){
throw new \InvalidStateException("Player is still connected"); throw new \InvalidStateException("Player is still connected");
} }