From 62ba36b474966fce095510a97ee641794bebbdff Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 19 Mar 2015 20:27:51 +0100 Subject: [PATCH] New TextPacket, second part! --- src/pocketmine/Player.php | 21 +++++++++-- src/pocketmine/block/Bed.php | 15 +++----- src/pocketmine/network/RakLibInterface.php | 6 ++-- .../network/protocol/TextPacket.php | 36 ++++++++++++++++--- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 328a00563..9895205cc 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -95,7 +95,7 @@ use pocketmine\network\protocol\EntityEventPacket; use pocketmine\network\protocol\FullChunkDataPacket; use pocketmine\network\protocol\Info as ProtocolInfo; use pocketmine\network\protocol\PlayStatusPacket; -use pocketmine\network\protocol\MessagePacket; +use pocketmine\network\protocol\TextPacket; use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\SetDifficultyPacket; @@ -2400,14 +2400,29 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $mes = explode("\n", $message); foreach($mes as $m){ if($m !== ""){ - $pk = new MessagePacket(); - $pk->source = ""; //Do not use this ;) + $pk = new TextPacket(); + $pk->type = TextPacket::TYPE_RAW; $pk->message = $m; $this->dataPacket($pk); } } } + public function sendTranslation($message, array $parameters = []){ + if($this->removeFormat !== false){ + $message = TextFormat::clean($message); + foreach($parameters as $k => $v){ + $parameters[$k] = TextFormat::clean($v); + } + } + + $pk = new TextPacket(); + $pk->type = TextPacket::TYPE_TRANSLATION; + $pk->message = $message; + $pk->parameters = $parameters; + $this->dataPacket($pk); + } + /** * @param string $message Message to be broadcasted * @param string $reason Reason showed in console diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index 16cadf7f9..1584afb67 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -24,8 +24,8 @@ namespace pocketmine\block; use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\math\AxisAlignedBB; -use pocketmine\network\protocol\ChatPacket; use pocketmine\Player; +use pocketmine\utils\TextFormat; class Bed extends Transparent{ @@ -65,10 +65,7 @@ class Bed extends Transparent{ $isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE); if($player instanceof Player and !$isNight){ - $pk = new ChatPacket(); - $pk->message = "You can only sleep at night"; - $player->dataPacket($pk); - + $player->sendMessage(TextFormat::GRAY . "You can only sleep at night"); return true; } @@ -89,9 +86,7 @@ class Bed extends Transparent{ $b = $blockWest; }else{ if($player instanceof Player){ - $pk = new ChatPacket(); - $pk->message = "This bed is incomplete"; - $player->dataPacket($pk); + $player->sendMessage(TextFormat::GRAY . "This bed is incomplete"); } return true; @@ -99,9 +94,7 @@ class Bed extends Transparent{ } if($player instanceof Player and $player->sleepOn($b) === false){ - $pk = new ChatPacket(); - $pk->message = "This bed is occupied"; - $player->dataPacket($pk); + $player->sendMessage(TextFormat::GRAY . "This bed is occupied"); } return true; diff --git a/src/pocketmine/network/RakLibInterface.php b/src/pocketmine/network/RakLibInterface.php index 5168f3ec2..96c5c3cf8 100644 --- a/src/pocketmine/network/RakLibInterface.php +++ b/src/pocketmine/network/RakLibInterface.php @@ -32,7 +32,6 @@ use pocketmine\network\protocol\AddPaintingPacket; use pocketmine\network\protocol\AddPlayerPacket; use pocketmine\network\protocol\AdventureSettingsPacket; use pocketmine\network\protocol\AnimatePacket; -use pocketmine\network\protocol\ChatPacket; use pocketmine\network\protocol\ContainerClosePacket; use pocketmine\network\protocol\ContainerOpenPacket; use pocketmine\network\protocol\ContainerSetContentPacket; @@ -50,7 +49,7 @@ use pocketmine\network\protocol\LevelEventPacket; use pocketmine\network\protocol\DisconnectPacket; use pocketmine\network\protocol\LoginPacket; use pocketmine\network\protocol\PlayStatusPacket; -use pocketmine\network\protocol\MessagePacket; +use pocketmine\network\protocol\TextPacket; use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\PlayerActionPacket; @@ -302,7 +301,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{ $this->registerPacket(ProtocolInfo::LOGIN_PACKET, LoginPacket::class); $this->registerPacket(ProtocolInfo::PLAY_STATUS_PACKET, PlayStatusPacket::class); $this->registerPacket(ProtocolInfo::DISCONNECT_PACKET, DisconnectPacket::class); - $this->registerPacket(ProtocolInfo::MESSAGE_PACKET, MessagePacket::class); + $this->registerPacket(ProtocolInfo::TEXT_PACKET, TextPacket::class); $this->registerPacket(ProtocolInfo::SET_TIME_PACKET, SetTimePacket::class); $this->registerPacket(ProtocolInfo::START_GAME_PACKET, StartGamePacket::class); $this->registerPacket(ProtocolInfo::ADD_MOB_PACKET, AddMobPacket::class); @@ -340,7 +339,6 @@ class RakLibInterface implements ServerInstance, SourceInterface{ $this->registerPacket(ProtocolInfo::CONTAINER_SET_SLOT_PACKET, ContainerSetSlotPacket::class); $this->registerPacket(ProtocolInfo::CONTAINER_SET_DATA_PACKET, ContainerSetDataPacket::class); $this->registerPacket(ProtocolInfo::CONTAINER_SET_CONTENT_PACKET, ContainerSetContentPacket::class); - $this->registerPacket(ProtocolInfo::CHAT_PACKET, ChatPacket::class); $this->registerPacket(ProtocolInfo::ADVENTURE_SETTINGS_PACKET, AdventureSettingsPacket::class); $this->registerPacket(ProtocolInfo::TILE_ENTITY_DATA_PACKET, TileEntityDataPacket::class); $this->registerPacket(ProtocolInfo::SET_DIFFICULTY_PACKET, SetDifficultyPacket::class); diff --git a/src/pocketmine/network/protocol/TextPacket.php b/src/pocketmine/network/protocol/TextPacket.php index f55ed4d89..98cf11db1 100644 --- a/src/pocketmine/network/protocol/TextPacket.php +++ b/src/pocketmine/network/protocol/TextPacket.php @@ -28,6 +28,10 @@ class TextPacket extends DataPacket{ public static $pool = []; public static $next = 0; + const TYPE_RAW = 0; + const TYPE_CHAT = 1; + const TYPE_TRANSLATION = 2; + public $type; public $source; public $message; @@ -39,15 +43,39 @@ class TextPacket extends DataPacket{ public function decode(){ $this->type = $this->getByte(); - $this->source = $this->getString(); - $this->message = $this->getString(); + switch($this->type){ + case self::TYPE_CHAT: + $this->source = $this->getString(); + case self::TYPE_RAW: + $this->message = $this->getString(); + break; + + case self::TYPE_TRANSLATION: + $this->message = $this->getString(); + $count = $this->getByte(); + for($i = 0; $i < $count; ++$count){ + $this->parameters[] = $this->getString(); + } + } } public function encode(){ $this->reset(); $this->putByte($this->type); - $this->putString($this->source); - $this->putString($this->message); + switch($this->type){ + case self::TYPE_CHAT: + $this->putString($this->source); + case self::TYPE_RAW: + $this->putString($this->message); + break; + + case self::TYPE_TRANSLATION: + $this->putString($this->message); + $this->putByte(count($this->parameters)); + foreach($this->parameters as $p){ + $this->putString($p); + } + } } }