sleeping rewritten(+ fix 73)

This commit is contained in:
GameHerobrine 2023-10-08 20:03:37 +03:00
parent b969b5d8f1
commit 37b3c02396
3 changed files with 31 additions and 22 deletions

View file

@ -50,10 +50,11 @@ class Player{
private $port; private $port;
private $counter = [0, 0, 0, 0]; private $counter = [0, 0, 0, 0];
private $username; private $username;
private $iusername; public $iusername;
private $eid = false; private $eid = false;
private $startAction = false; private $startAction = false;
private $isSleeping = false; public $isSleeping = false;
public $sleepingTime = 0;
private $chunksOrder = []; private $chunksOrder = [];
private $lastMeasure = 0; private $lastMeasure = 0;
private $bandwidthRaw = 0; private $bandwidthRaw = 0;
@ -125,12 +126,12 @@ class Player{
} }
} }
$this->isSleeping = $pos; $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); $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){ if($this->entity instanceof Entity){
$this->entity->updateMetadata(); $this->entity->updateMetadata();
} }
$this->setSpawn($pos); $this->setSpawn($pos);
$this->server->schedule(60, [$this, "checkSleep"]);
return true; return true;
} }
@ -605,23 +606,6 @@ class Player{
$pk->hotbar = $hotbar; $pk->hotbar = $hotbar;
$this->dataPacket($pk); $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 $type
* @param $damage * @param $damage
@ -1261,7 +1245,9 @@ class Player{
$this->send($pk); $this->send($pk);
return [$pk->seqNumber]; return [$pk->seqNumber];
} }
public function entityTick(){
if($this->isSleeping) ++$this->sleepingTime;
}
public function handleDataPacket(RakNetDataPacket $packet){ public function handleDataPacket(RakNetDataPacket $packet){
if($this->connected === false){ if($this->connected === false){
return; return;
@ -2241,6 +2227,7 @@ class Player{
public function stopSleep(){ public function stopSleep(){
$this->isSleeping = false; $this->isSleeping = false;
$this->sleepingTime = 0;
if($this->entity instanceof Entity){ if($this->entity instanceof Entity){
$this->entity->updateMetadata(); $this->entity->updateMetadata();
} }

View file

@ -188,6 +188,7 @@ class Entity extends Position
$this->speedY += $vY; $this->speedY += $vY;
$this->speedZ += $vZ; $this->speedZ += $vZ;
} }
public function isMovingHorizontally() public function isMovingHorizontally()
{ {
return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); 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(); $this->updateMovement();
} }
if($this->isPlayer()){
$this->player->entityTick();
}
$this->needsUpdate = $hasUpdate; $this->needsUpdate = $hasUpdate;
$this->lastUpdate = $now; $this->lastUpdate = $now;
} }
public function isOnLadder(){ public function isOnLadder(){
$x = (int)$this->x; $x = (int)$this->x;
$y = (int)$this->boundingBox->minY; $y = (int)$this->boundingBox->minY;

View file

@ -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(){ public function checkThings(){
if(!isset($this->level)){ if(!isset($this->level)){
return false; return false;
@ -360,6 +375,9 @@ class Level{
$e->update(); $e->update();
} }
} }
$this->checkSleep();
if($server->ticks % 40 === 0){ //40 ticks delay if($server->ticks % 40 === 0){ //40 ticks delay
$this->mobSpawner->handle(); $this->mobSpawner->handle();
} }