diff --git a/src/Player.php b/src/Player.php index 23f25fa4..bf9c2ef1 100755 --- a/src/Player.php +++ b/src/Player.php @@ -50,10 +50,11 @@ class Player{ private $port; private $counter = [0, 0, 0, 0]; private $username; - private $iusername; + public $iusername; private $eid = false; private $startAction = false; - private $isSleeping = false; + public $isSleeping = false; + public $sleepingTime = 0; private $chunksOrder = []; private $lastMeasure = 0; private $bandwidthRaw = 0; @@ -125,12 +126,12 @@ class Player{ } } $this->isSleeping = $pos; + $this->sleepingTime = 0; $this->teleport(new Position($pos->x + 0.5, $pos->y + 1, $pos->z + 0.5, $this->level), false, false, false, false); if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } $this->setSpawn($pos); - $this->server->schedule(60, [$this, "checkSleep"]); return true; } @@ -605,23 +606,6 @@ class Player{ $pk->hotbar = $hotbar; $this->dataPacket($pk); } - - public function checkSleep(){ - if($this->isSleeping !== false){ - if($this->server->api->time->getPhase($this->level) === "night"){ - foreach($this->server->api->player->getAll($this->level) as $p){ - if($p->isSleeping === false){ - return false; - } - } - $this->server->api->time->set("day", $this->level); - foreach($this->server->api->player->getAll($this->level) as $p){ - $p->stopSleep(); - } - } - } - } - /** * @param $type * @param $damage @@ -1261,7 +1245,9 @@ class Player{ $this->send($pk); return [$pk->seqNumber]; } - + public function entityTick(){ + if($this->isSleeping) ++$this->sleepingTime; + } public function handleDataPacket(RakNetDataPacket $packet){ if($this->connected === false){ return; @@ -2241,6 +2227,7 @@ class Player{ public function stopSleep(){ $this->isSleeping = false; + $this->sleepingTime = 0; if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 71446a86..73ff0c01 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -188,6 +188,7 @@ class Entity extends Position $this->speedY += $vY; $this->speedZ += $vZ; } + public function isMovingHorizontally() { return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); @@ -703,10 +704,13 @@ class Entity extends Position $this->updateMovement(); } + if($this->isPlayer()){ + $this->player->entityTick(); + } + $this->needsUpdate = $hasUpdate; $this->lastUpdate = $now; } - public function isOnLadder(){ $x = (int)$this->x; $y = (int)$this->boundingBox->minY; diff --git a/src/world/Level.php b/src/world/Level.php index a5de2b47..ef73bfae 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -205,6 +205,21 @@ class Level{ } + public function checkSleep(){ //TODO events? + if(count($this->players) == 0) return false; + if($this->server->api->time->getPhase($this->level) === "night"){ //TODO vanilla + foreach($this->players as $p){ + if($p->isSleeping == false || $p->sleepingTime < 100){ + return false; + } + } + $this->server->api->time->set("day", $this->level); + } + foreach($this->players as $p){ + $p->stopSleep(); + } + } + public function checkThings(){ if(!isset($this->level)){ return false; @@ -360,6 +375,9 @@ class Level{ $e->update(); } } + + $this->checkSleep(); + if($server->ticks % 40 === 0){ //40 ticks delay $this->mobSpawner->handle(); }