From 9e337e21d26d60dc75011d04bdf7d195839035d5 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 27 Aug 2013 00:22:06 +0200 Subject: [PATCH] Implemented Level::getSafeSpawn() --- src/API/LevelAPI.php | 2 +- src/API/PlayerAPI.php | 2 +- src/Player.php | 4 ++-- src/material/block/solid/Chest.php | 4 ++-- src/world/Level.php | 20 ++++++++++++++++++++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index f4aa3124..23349113 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -53,7 +53,7 @@ class LevelAPI{ $this->generateLevel($this->default, $this->server->seed); $this->loadLevel($this->default); } - $this->server->spawn = $this->getDefault()->getSpawn(); + $this->server->spawn = $this->getDefault()->getSafeSpawn(); } public function commandHandler($cmd, $params, $issuer, $alias){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 08e44823..f5eb3972 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -269,7 +269,7 @@ class PlayerAPI{ $origin = $this->get($name); if($origin instanceof Player){ $name = $origin->username; - return $origin->teleport($lv->getSpawn()); + return $origin->teleport($lv->getSafeSpawn()); } }else{ return false; diff --git a/src/Player.php b/src/Player.php index 0434d0ca..3912dbd1 100644 --- a/src/Player.php +++ b/src/Player.php @@ -1651,7 +1651,7 @@ class Player{ } $this->craftingItems = array(); $this->toCraft = array(); - if(isset($this->windows[$data["windowid"]]) and $this->windows[$data["windowid"]]->class === TILE_CHEST){ + /*if(isset($this->windows[$data["windowid"]]) and $this->windows[$data["windowid"]]->class === TILE_CHEST){ $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), MC_TILE_EVENT, array( "x" => $this->windows[$data["windowid"]]->x, "y" => $this->windows[$data["windowid"]]->y, @@ -1659,7 +1659,7 @@ class Player{ "case1" => 1, "case2" => 0, )); - } + }*/ unset($this->windows[$data["windowid"]]); $this->dataPacket(MC_CONTAINER_CLOSE, array( diff --git a/src/material/block/solid/Chest.php b/src/material/block/solid/Chest.php index f5603373..a4a760f6 100644 --- a/src/material/block/solid/Chest.php +++ b/src/material/block/solid/Chest.php @@ -116,13 +116,13 @@ class ChestBlock extends TransparentBlock{ "slots" => CHEST_SLOTS, "title" => "Chest", )); - $server->api->player->broadcastPacket($server->api->player->getAll($this->level), MC_TILE_EVENT, array( + /*$server->api->player->broadcastPacket($server->api->player->getAll($this->level), MC_TILE_EVENT, array( "x" => $this->x, "y" => $this->y, "z" => $this->z, "case1" => 1, "case2" => 2, - )); + ));*/ $slots = array(); for($s = 0; $s < CHEST_SLOTS; ++$s){ $slot = $chest->getSlot($s); diff --git a/src/world/Level.php b/src/world/Level.php index 2ffa6108..8fa7d5e7 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -421,6 +421,26 @@ class Level{ return new Position($this->level->getData("spawnX"), $this->level->getData("spawnY"), $this->level->getData("spawnZ"), $this); } + public function getSafeSpawn(){ + if(($spawn = $this->getSpawn()) !== false){ + $x = (int) round($spawn->x); + $y = (int) round($spawn->y); + $z = (int) round($spawn->z); + for(; $y < 128; ++$y){ + $v = new Vector3($x, $y, $z); + if($this->getBlock($v->getSide(1)) instanceof AirBlock){ + if($this->getBlock($v) instanceof AirBlock){ + return new Position($x, $y, $z, $this); + } + }else{ + ++$y; + } + } + return new Position($x, $y, $z, $this); + } + return false; + } + public function setSpawn(Vector3 $pos){ if(!isset($this->level)){ return false;