Scrap TextContainer, make TranslationContainer immutable

TextContainer provided zero real value as a base of TranslationContainer, given that it required its own logic to be handled wherever accepted. As such, it's no better than a simple string.
Removing it also allows fixing an ambiguity when embedding translations inside other translations, allowing it to be made immutable.
This commit is contained in:
Dylan K. Taylor 2020-02-08 13:38:02 +00:00
parent 2375e9519d
commit 4c51f1dda3
11 changed files with 53 additions and 104 deletions

View File

@ -45,7 +45,7 @@ use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\ItemFactory;
use pocketmine\lang\Language;
use pocketmine\lang\LanguageNotFoundException;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\CompoundTag;
@ -1138,8 +1138,8 @@ class Server{
}
/**
* @param TextContainer|string $message
* @param CommandSender[]|null $recipients
* @param TranslationContainer|string $message
* @param CommandSender[]|null $recipients
*/
public function broadcastMessage($message, ?array $recipients = null) : int{
if(!is_array($recipients)){
@ -1210,7 +1210,7 @@ class Server{
}
/**
* @param TextContainer|string $message
* @param TranslationContainer|string $message
*/
public function broadcast($message, string $permissions) : int{
/** @var CommandSender[] $recipients */

View File

@ -27,7 +27,6 @@ declare(strict_types=1);
namespace pocketmine\command;
use pocketmine\command\utils\CommandException;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\permission\PermissionManager;
use pocketmine\Server;
@ -224,20 +223,15 @@ abstract class Command{
}
/**
* @param TextContainer|string $message
* @param TranslationContainer|string $message
*/
public static function broadcastCommandMessage(CommandSender $source, $message, bool $sendToSource = true) : void{
$users = PermissionManager::getInstance()->getPermissionSubscriptions(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
if($message instanceof TextContainer){
$m = clone $message;
$result = "[" . $source->getName() . ": " . ($source->getServer()->getLanguage()->get($m->getText()) !== $m->getText() ? "%" : "") . $m->getText() . "]";
if($message instanceof TranslationContainer){
$formatted = "[" . $source->getName() . ": %" . $message->getText() . "]";
$colored = TextFormat::GRAY . TextFormat::ITALIC . $result;
$m->setText($result);
$result = clone $m;
$m->setText($colored);
$colored = clone $m;
$result = new TranslationContainer($formatted, $message->getParameters());
$colored = new TranslationContainer(TextFormat::GRAY . TextFormat::ITALIC . $formatted, $message->getParameters());
}else{
$result = new TranslationContainer("chat.type.admin", [$source->getName(), $message]);
$colored = new TranslationContainer(TextFormat::GRAY . TextFormat::ITALIC . "%chat.type.admin", [$source->getName(), $message]);

View File

@ -23,14 +23,14 @@ declare(strict_types=1);
namespace pocketmine\command;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\permission\Permissible;
use pocketmine\Server;
interface CommandSender extends Permissible{
/**
* @param TextContainer|string $message
* @param TranslationContainer|string $message
*/
public function sendMessage($message) : void;

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\command;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissibleDelegateTrait;
use pocketmine\Server;
@ -46,11 +46,11 @@ class ConsoleCommandSender implements CommandSender{
}
/**
* @param TextContainer|string $message
* @param TranslationContainer|string $message
*/
public function sendMessage($message) : void{
$server = $this->getServer();
if($message instanceof TextContainer){
if($message instanceof TranslationContainer){
$message = $server->getLanguage()->translate($message);
}else{
$message = $server->getLanguage()->translateString($message);

View File

@ -30,7 +30,6 @@ use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\item\Item;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\player\Player;
@ -38,14 +37,14 @@ class PlayerDeathEvent extends EntityDeathEvent{
/** @var Player */
protected $entity;
/** @var TextContainer|string */
/** @var TranslationContainer|string */
private $deathMessage;
/** @var bool */
private $keepInventory = false;
/**
* @param Item[] $drops
* @param string|TextContainer|null $deathMessage Null will cause the default vanilla message to be used
* @param Item[] $drops
* @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){
parent::__construct($entity, $drops, $xp);
@ -64,14 +63,14 @@ class PlayerDeathEvent extends EntityDeathEvent{
}
/**
* @return TextContainer|string
* @return TranslationContainer|string
*/
public function getDeathMessage(){
return $this->deathMessage;
}
/**
* @param TextContainer|string $deathMessage
* @param TranslationContainer|string $deathMessage
*/
public function setDeathMessage($deathMessage) : void{
$this->deathMessage = $deathMessage;

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\event\player;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\player\Player;
/**
@ -34,13 +34,13 @@ use pocketmine\player\Player;
* @see PlayerLoginEvent
*/
class PlayerJoinEvent extends PlayerEvent{
/** @var string|TextContainer */
/** @var string|TranslationContainer */
protected $joinMessage;
/**
* PlayerJoinEvent constructor.
*
* @param TextContainer|string $joinMessage
* @param TranslationContainer|string $joinMessage
*/
public function __construct(Player $player, $joinMessage){
$this->player = $player;
@ -48,14 +48,14 @@ class PlayerJoinEvent extends PlayerEvent{
}
/**
* @param string|TextContainer $joinMessage
* @param string|TranslationContainer $joinMessage
*/
public function setJoinMessage($joinMessage) : void{
$this->joinMessage = $joinMessage;
}
/**
* @return string|TextContainer
* @return string|TranslationContainer
*/
public function getJoinMessage(){
return $this->joinMessage;

View File

@ -25,7 +25,7 @@ namespace pocketmine\event\player;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\player\Player;
/**
@ -34,7 +34,7 @@ use pocketmine\player\Player;
class PlayerKickEvent extends PlayerEvent implements Cancellable{
use CancellableTrait;
/** @var TextContainer|string */
/** @var TranslationContainer|string */
protected $quitMessage;
/** @var string */
@ -43,7 +43,7 @@ class PlayerKickEvent extends PlayerEvent implements Cancellable{
/**
* PlayerKickEvent constructor.
*
* @param TextContainer|string $quitMessage
* @param TranslationContainer|string $quitMessage
*/
public function __construct(Player $player, string $reason, $quitMessage){
$this->player = $player;
@ -60,14 +60,14 @@ class PlayerKickEvent extends PlayerEvent implements Cancellable{
}
/**
* @param TextContainer|string $quitMessage
* @param TranslationContainer|string $quitMessage
*/
public function setQuitMessage($quitMessage) : void{
$this->quitMessage = $quitMessage;
}
/**
* @return TextContainer|string
* @return TranslationContainer|string
*/
public function getQuitMessage(){
return $this->quitMessage;

View File

@ -142,16 +142,12 @@ class Language{
return $baseText;
}
public function translate(TextContainer $c) : string{
if($c instanceof TranslationContainer){
$baseText = $this->internalGet($c->getText());
$baseText = $this->parseTranslation($baseText ?? $c->getText());
public function translate(TranslationContainer $c) : string{
$baseText = $this->internalGet($c->getText());
$baseText = $this->parseTranslation($baseText ?? $c->getText());
foreach($c->getParameters() as $i => $p){
$baseText = str_replace("{%$i}", $this->parseTranslation($p), $baseText);
}
}else{
$baseText = $this->parseTranslation($c->getText());
foreach($c->getParameters() as $i => $p){
$baseText = str_replace("{%$i}", $this->parseTranslation($p), $baseText);
}
return $baseText;

View File

@ -1,46 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\lang;
class TextContainer{
/** @var string $text */
protected $text;
public function __construct(string $text){
$this->text = $text;
}
public function setText(string $text) : void{
$this->text = $text;
}
public function getText() : string{
return $this->text;
}
public function __toString() : string{
return $this->getText();
}
}

View File

@ -23,8 +23,10 @@ declare(strict_types=1);
namespace pocketmine\lang;
class TranslationContainer extends TextContainer{
final class TranslationContainer{
/** @var string $text */
protected $text;
/** @var string[] $params */
protected $params = [];
@ -32,7 +34,7 @@ class TranslationContainer extends TextContainer{
* @param (float|int|string)[] $params
*/
public function __construct(string $text, array $params = []){
parent::__construct($text);
$this->text = $text;
$i = 0;
foreach($params as $str){
@ -42,6 +44,10 @@ class TranslationContainer extends TextContainer{
}
}
public function getText() : string{
return $this->text;
}
/**
* @return string[]
*/
@ -52,4 +58,8 @@ class TranslationContainer extends TextContainer{
public function getParameter(int $i) : ?string{
return $this->params[$i] ?? null;
}
public function __toString() : string{
return $this->getText();
}
}

View File

@ -73,7 +73,6 @@ use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\MeleeWeaponEnchantment;
use pocketmine\item\Item;
use pocketmine\item\ItemUseResult;
use pocketmine\lang\TextContainer;
use pocketmine\lang\TranslationContainer;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
@ -1813,15 +1812,12 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
/**
* Sends a direct chat message to a player
*
* @param TextContainer|string $message
* @param TranslationContainer|string $message
*/
public function sendMessage($message) : void{
if($message instanceof TextContainer){
if($message instanceof TranslationContainer){
$this->sendTranslation($message->getText(), $message->getParameters());
return;
}
$message = $message->getText();
if($message instanceof TranslationContainer){
$this->sendTranslation($message->getText(), $message->getParameters());
return;
}
$this->networkSession->onRawChatMessage($this->server->getLanguage()->translateString($message));
@ -1910,7 +1906,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
/**
* Kicks a player from the server
*
* @param TextContainer|string $quitMessage
* @param TranslationContainer|string $quitMessage
*/
public function kick(string $reason = "", bool $isAdmin = true, $quitMessage = null) : bool{
$ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage());
@ -1942,8 +1938,8 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
* Note for plugin developers: Prefer kick() with the isAdmin flag set to kick without the "Kicked by admin" part
* instead of this method. This way other plugins can have a say in whether the player is removed or not.
*
* @param string $reason Shown to the player, usually this will appear on their disconnect screen.
* @param TextContainer|string $quitMessage Message to broadcast to online players (null will use default)
* @param string $reason Shown to the player, usually this will appear on their disconnect screen.
* @param TranslationContainer|string $quitMessage Message to broadcast to online players (null will use default)
*/
public function disconnect(string $reason, $quitMessage = null, bool $notify = true) : void{
if(!$this->isConnected()){