pocketmine\Player::sendMessage PHP Method

sendMessage() public method

Sends a direct chat message to a player
public sendMessage ( string | TextContainer $message ) : boolean
$message string | pocketmine\event\TextContainer
return boolean
    public function sendMessage($message)
    {
        if ($message instanceof TextContainer) {
            if ($message instanceof TranslationContainer) {
                $this->sendTranslation($message->getText(), $message->getParameters());
                return false;
            }
            $message = $message->getText();
        }
        $mes = explode("\n", $this->server->getLanguage()->translateString($message));
        foreach ($mes as $m) {
            if ($m !== "") {
                $this->server->getPluginManager()->callEvent($ev = new PlayerTextPreSendEvent($this, $m, PlayerTextPreSendEvent::MESSAGE));
                if (!$ev->isCancelled()) {
                    $pk = new TextPacket();
                    $pk->type = TextPacket::TYPE_RAW;
                    $pk->message = $ev->getMessage();
                    $this->dataPacket($pk);
                }
            }
        }
        return true;
    }

Usage Example

Example #1
1
 public function teleport(Player $player)
 {
     if ($this->message !== null) {
         $player->sendMessage($this->message);
     }
     if ($this->position instanceof Position) {
         if ($this->position->isValid()) {
             if ($this->position instanceof WeakPosition) {
                 $this->position->updateProperties();
             }
             //Server::getInstance()->getLogger()->info($this->position->x . " : " . $this->position->y . " : " . $this->position->z);
             $player->teleport($this->position);
         } else {
             $player->sendMessage($this->getApi()->executeTranslationItem("level-not-loaded-warp"));
         }
     } else {
         $plugin = $player->getServer()->getPluginManager()->getPlugin("FastTransfer");
         if ($plugin instanceof PluginBase && $plugin->isEnabled() && $plugin instanceof FastTransfer) {
             $plugin->transferPlayer($player, $this->address, $this->port);
         } else {
             $player->getServer()->getPluginManager()->getPlugin("SimpleWarp")->getLogger()->warning("In order to use warps tp other servers, you must install " . TextFormat::AQUA . "FastTransfer" . TextFormat::RESET . ".");
             $player->sendPopup(TextFormat::RED . "Warp failed!" . TextFormat::RESET);
         }
     }
 }
All Usage Examples Of pocketmine\Player::sendMessage
Player