pocketmine\Player::sleepOn PHP Method

sleepOn() public method

public sleepOn ( Vector3 $pos ) : boolean
$pos pocketmine\math\Vector3
return boolean
    public function sleepOn(Vector3 $pos)
    {
        if (!$this->isOnline()) {
            return false;
        }
        foreach ($this->level->getNearbyEntities($this->boundingBox->grow(2, 1, 2), $this) as $p) {
            if ($p instanceof Player) {
                if ($p->sleeping !== null and $pos->distance($p->sleeping) <= 0.1) {
                    return false;
                }
            }
        }
        $this->server->getPluginManager()->callEvent($ev = new PlayerBedEnterEvent($this, $this->level->getBlock($pos)));
        if ($ev->isCancelled()) {
            return false;
        }
        $this->sleeping = clone $pos;
        $this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [$pos->x, $pos->y, $pos->z]);
        $this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, true);
        $this->setSpawn($pos);
        $this->level->sleepTicks = 60;
        return true;
    }

Usage Example

Example #1
0
 public function onActivate(Item $item, Player $player = null)
 {
     $time = $this->getLevel()->getTime() % Level::TIME_FULL;
     $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);
         return true;
     }
     $blockNorth = $this->getSide(2);
     //Gets the blocks around them
     $blockSouth = $this->getSide(3);
     $blockEast = $this->getSide(5);
     $blockWest = $this->getSide(4);
     if (($this->meta & 0x8) === 0x8) {
         //This is the Top part of bed
         $b = $this;
     } else {
         //Bottom Part of Bed
         if ($blockNorth->getID() === $this->id and ($blockNorth->meta & 0x8) === 0x8) {
             $b = $blockNorth;
         } elseif ($blockSouth->getID() === $this->id and ($blockSouth->meta & 0x8) === 0x8) {
             $b = $blockSouth;
         } elseif ($blockEast->getID() === $this->id and ($blockEast->meta & 0x8) === 0x8) {
             $b = $blockEast;
         } elseif ($blockWest->getID() === $this->id and ($blockWest->meta & 0x8) === 0x8) {
             $b = $blockWest;
         } else {
             if ($player instanceof Player) {
                 $pk = new ChatPacket();
                 $pk->message = "This bed is incomplete";
                 $player->dataPacket($pk);
             }
             return true;
         }
     }
     if ($player instanceof Player and $player->sleepOn($b) === false) {
         $pk = new ChatPacket();
         $pk->message = "This bed is occupied";
         $player->dataPacket($pk);
     }
     return true;
 }
All Usage Examples Of pocketmine\Player::sleepOn
Player