pocketmine\Server::broadcastPacket PHP Method

broadcastPacket() public static method

Broadcasts a Minecraft packet to a list of players
public static broadcastPacket ( array $players, DataPacket $packet )
$players array
$packet pocketmine\network\protocol\DataPacket
    public static function broadcastPacket(array $players, DataPacket $packet)
    {
        $packet->encode();
        $packet->isEncoded = true;
        if (Network::$BATCH_THRESHOLD >= 0 and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD) {
            Server::getInstance()->batchPackets($players, [$packet->buffer], false);
            return;
        }
        foreach ($players as $player) {
            $player->dataPacket($packet);
        }
        if (isset($packet->__encapsulatedPacket)) {
            unset($packet->__encapsulatedPacket);
        }
    }

Usage Example

示例#1
0
 public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC)
 {
     if ($this->attackTime > 0) {
         $lastCause = $this->getLastDamageCause();
         if ($lastCause instanceof EntityDamageEvent and $lastCause->getDamage() >= $damage) {
             return;
         }
     }
     $pk = new EntityEventPacket();
     $pk->eid = $this->getID();
     $pk->event = 2;
     //Ouch!
     Server::broadcastPacket($this->hasSpawned, $pk);
     $this->setLastDamageCause($source);
     $motion = new Vector3(0, 0, 0);
     if ($source instanceof EntityDamageByEntityEvent) {
         $e = $source->getDamager();
         $deltaX = $this->x - $e->x;
         $deltaZ = $this->z - $e->z;
         $yaw = atan2($deltaX, $deltaZ);
         $motion->x = sin($yaw) * 0.5;
         $motion->z = cos($yaw) * 0.5;
     }
     $this->setMotion($motion);
     $this->setHealth($this->getHealth() - $damage);
     $this->attackTime = 10;
     //0.5 seconds cooldown
 }
All Usage Examples Of pocketmine\Server::broadcastPacket
Server