diff --git a/src/API/TimeAPI.php b/src/API/TimeAPI.php index 0c449cdb4..2d39a069e 100644 --- a/src/API/TimeAPI.php +++ b/src/API/TimeAPI.php @@ -20,7 +20,7 @@ */ class TimeAPI{ - var $phases = array( + public static $phases = array( "day" => 0, "sunset" => 9500, "night" => 10900, @@ -105,7 +105,7 @@ class TimeAPI{ $time = !is_integer($time) ? $this->get(false, $time):$time; if($time < TimeAPI::$phases["sunset"]){ $time = "day"; - }elseif($time < TimeAPI::$phase["night"]){ + }elseif($time < TimeAPI::$phases["night"]){ $time = "sunset"; }elseif($time < TimeAPI::$phases["sunrise"]){ $time = "night"; diff --git a/src/Player.php b/src/Player.php index 80216dc05..d3d04ae5a 100644 --- a/src/Player.php +++ b/src/Player.php @@ -41,6 +41,7 @@ class Player{ private $iusername; private $eid = false; private $startAction = false; + private $isSleeping = false; public $data; public $entity = false; public $auth = false; @@ -290,6 +291,39 @@ class Player{ } } + public function sleepOn(Vector3 $pos){ + $this->isSleeping = $pos; + $this->teleport(new Position($pos->x, $pos->y, $pos->z, $this->level)); + if($this->entity instanceof Entity){ + $this->entity->updateMetadata(); + } + $this->setSpawn($pos); + $this->server->schedule(30, array($this, "checkSleep")); + } + + public function stopSleep(){ + $this->isSleeping = false; + if($this->entity instanceof Entity){ + $this->entity->updateMetadata(); + } + } + + 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(); + } + } + } + } + public function hasSpace($type, $damage, $count){ $inv = $this->inventory; while($count > 0){ @@ -1196,6 +1230,9 @@ class Player{ $this->entity->x = $this->data->get("position")["x"]; $this->entity->y = $this->data->get("position")["y"]; $this->entity->z = $this->data->get("position")["z"]; + if(($level = $this->server->api->level->get($this->data->get("spawn")["level"])) !== false){ + $this->spawnPosition = new Position($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"], $level); + } $this->entity->check = false; $this->entity->setName($this->username); $this->entity->data["CID"] = $this->CID; @@ -1373,10 +1410,11 @@ class Player{ break; } $this->craftingItems = array(); - $this->toCraft = array(); - if($this->entity->inAction === true){ - switch($data["action"]){ - case 5: //Shot arrow + $this->toCraft = array(); + + switch($data["action"]){ + case 5: //Shot arrow + if($this->entity->inAction === true){ if($this->getSlot($this->slot)->getID() === BOW){ if($this->startAction !== false){ $time = microtime(true) - $this->startAction; @@ -1389,12 +1427,14 @@ class Player{ $this->server->api->entity->spawnToAll($e); } } - break; - } + } + $this->startAction = false; + $this->entity->inAction = false; + $this->entity->updateMetadata(); + break; + case 6: //get out of the bed + $this->stopSleep(); } - $this->startAction = false; - $this->entity->inAction = false; - $this->entity->updateMetadata(); break; case MC_REMOVE_BLOCK: if($this->spawned === false or $this->blocked === true or $this->entity->distance(new Vector3($data["x"], $data["y"], $data["z"])) > 8){ diff --git a/src/material/block/misc/Bed.php b/src/material/block/misc/Bed.php index cf0405f2a..b02b95cde 100644 --- a/src/material/block/misc/Bed.php +++ b/src/material/block/misc/Bed.php @@ -27,9 +27,10 @@ class BedBlock extends TransparentBlock{ } public function onActivate(Item $item, Player $player){ - $player->dataPacket(MC_CLIENT_MESSAGE, array( + $player->sleepOn($this); + /*$player->dataPacket(MC_CLIENT_MESSAGE, array( "message" => "This bed has been corrupted by your hands!" - )); + ));*/ return true; } diff --git a/src/world/Entity.php b/src/world/Entity.php index 698765ae3..fdc67c956 100644 --- a/src/world/Entity.php +++ b/src/world/Entity.php @@ -575,6 +575,11 @@ class Entity extends Position{ $this->data["Color"] = mt_rand(0,15); } $d[16]["value"] = (($this->data["Sheared"] == 1 ? 1:0) << 4) | ($this->data["Color"] & 0x0F); + }elseif($this->class === ENTITY_PLAYER){ + if($this->player->isSleeping !== false){ + $d[16]["value"] = 2; + $d[17]["value"] = array($this->player->isSleeping->x, $this->player->isSleeping->y, $this->player->isSleeping->z); + } } return $d; }