diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 70a892a2..a4d77bd0 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -109,8 +109,9 @@ class LevelAPI{ return true; } /** + * Gets a level by name. Returns false if no level with such name exists. * @param string $name - * @return Level|boolean + * @return Level|false */ public function get($name){ if(isset($this->levels[$name])){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 7e4b6f36..95c7302b 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -404,7 +404,8 @@ class PlayerAPI{ "lastIP" => "", "lastID" => 0, "achievements" => [], - "slot-count" => 7 + "slot-count" => 7, + "bed-position" => null, ]; if(!file_exists(DATA_PATH . "players/" . $iname . ".yml")){ diff --git a/src/Player.php b/src/Player.php index b42b529f..881ab6a9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -46,7 +46,6 @@ class Player{ private $receiveCount = -1; private $buffer; private $bufferLen = 0; - private $nextBuffer = 0; private $evid = []; private $lastMovement = 0; private $forceMovement = false; @@ -87,6 +86,7 @@ class Player{ private $ackQueue = []; public $slotCount = 7; + public $bedPosition = null; /** * DataPacket that is used as a buffer for all non-player entity movement packets @@ -197,12 +197,18 @@ class Player{ return $this->spawnPosition; } + public function setBedPosition(?Position $bedPos){ + if($bedPos != null) $this->bedPosition = [$bedPos->level->getName(), (int)$bedPos->x, (int)$bedPos->y, (int)$bedPos->z]; + else $this->bedPosition = null; + $this->data->set("bed-position", $this->bedPosition); + } + /** * @param Vector3 $pos * * @return boolean */ - public function sleepOn(Vector3 $pos){ + public function sleepOn(Position $pos){ foreach($this->server->api->player->getAll($this->level) as $p){ if($p->isSleeping instanceof Vector3){ if($pos->distance($p->isSleeping) <= 0.1){ @@ -210,6 +216,8 @@ class Player{ } } } + + $this->setBedPosition($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); @@ -218,13 +226,6 @@ class Player{ $this->entity->updateMetadata(); } - $spawnPoint = BedBlock::findStandUpPosition($this->level, $pos->x, $pos->y, $pos->z); - if($spawnPoint == null) $spawnPoint = $pos->add(0.5, 1, 0.5); - else{ - $spawnPoint->x += 0.5; - $spawnPoint->z += 0.5; - } - $this->setSpawn($spawnPoint); return true; } @@ -586,7 +587,6 @@ class Player{ $this->bufferLen = 0; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->nextBuffer = microtime(true) + 0.05; } /** @@ -1916,6 +1916,12 @@ class Player{ $this->data->set("slot-count", $this->slotCount); } + if($this->data->exists("bed-position")){ + $this->bedPosition = $this->data->get("bed-position"); + }else{ + $this->setBedPosition(null); + } + $this->entity = $this->server->api->entity->add($this->level, ENTITY_PLAYER, 0, ["player" => $this]); $this->eid = $this->entity->eid; $this->server->query("UPDATE players SET EID = " . $this->eid . " WHERE CID = " . $this->CID . ";"); @@ -2418,7 +2424,24 @@ class Player{ $this->craftingType = CraftingRecipes::TYPE_INVENTORY; $this->checkSpawnPosition(); - $this->teleport($this->spawnPosition, false, false, true, false); + $tpTarget = $this->spawnPosition; + if($this->bedPosition != null){ + [$levelname, $x, $y, $z] = $this->bedPosition; + $level = $this->server->api->level->get($levelname); + if($level === false || $level->level->getBlockID($x, $y, $z) != BED_BLOCK){ + remove_bed: + $this->sendChat("The bed is missing or cannot be accessed."); + $this->setBedPosition(null); + }else{ + $spawnPoint = BedBlock::findStandUpPosition($level, $x, $y, $z); + if($spawnPoint == null) goto remove_bed; + $spawnPoint->x += 0.5; + $spawnPoint->z += 0.5; + $tpTarget = $spawnPoint; + } + } + + $this->teleport($tpTarget, false, false, true, false); $pk = new MovePlayerPacket(); $pk->eid = $this->entity->eid; $pk->x = $this->entity->x; diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index d94bbb08..2d9af20d 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -65,7 +65,7 @@ class BedBlock extends TransparentBlock{ for($zCheck = $minZ; $zCheck <= $maxZ; ++$zCheck){ $idCheck = $level->level->getBlockID($xCheck, $y - 1, $zCheck); if(!StaticBlock::getIsTransparent($idCheck) && $level->level->getBlockID($xCheck, $y, $zCheck) == 0 && $level->level->getBlockID($xCheck, $y + 1, $zCheck) == 0){ - return new Vector3($xCheck, $y, $zCheck); + return new Position($xCheck, $y, $zCheck, $level); } } }