From 2254e87ce4c3f943665b696c26f5479ff02659e0 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Tue, 14 May 2013 20:58:53 +0200 Subject: [PATCH] PMF & Multiworld [part 3] --- src/API/BlockAPI.php | 2 +- src/API/LevelAPI.php | 13 ++++++++++--- src/API/PlayerAPI.php | 3 +++ src/API/ServerAPI.php | 5 +---- src/Player.php | 17 ++++++++++------- src/PocketMinecraftServer.php | 2 +- src/network/Packet.php | 3 +-- src/world/Entity.php | 18 +++++++++--------- src/world/Level.php | 2 +- src/world/Position.php | 2 +- 10 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 493c222c9..a583b8272 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -359,7 +359,7 @@ class BlockAPI{ return $this->cancelAction($block, $player); } - if($hand->isTransparent === false and $player->entity->inBlock($block->x, $block->y, $block->z)){ + if($hand->isTransparent === false and $player->entity->inBlock($block)){ return $this->cancelAction($block, $player); //Entity in block } diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index f6a075816..dba1825e7 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -30,7 +30,13 @@ class LevelAPI{ public function __construct(){ $this->server = ServerAPI::request(); $this->levels = array(); - $this->map = $this->server->level; + } + + public function get($name){ + if(isset($this->levels[$name])){ + return $this->levels[$name]; + } + return false; } public function getDefault(){ @@ -54,6 +60,7 @@ class LevelAPI{ $gen->save($path, $this->default); $this->loadLevel($this->default); } + $this->server->spawn = $this->getDefault()->getSpawn(); } public function loadLevel($name){ @@ -85,11 +92,11 @@ class LevelAPI{ )); }elseif($entity["id"] === OBJECT_PAINTING){ //Painting $e = $this->server->api->entity->add($this->levels[$name], ENTITY_OBJECT, $entity["id"], $entity); - $e->setPosition($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2], $entity["Rotation"][0], $entity["Rotation"][1]); + $e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]); $e->setHealth($entity["Health"]); }else{ $e = $this->server->api->entity->add($this->levels[$name], ENTITY_MOB, $entity["id"], $entity); - $e->setPosition($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2], $entity["Rotation"][0], $entity["Rotation"][1]); + $e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]); $e->setHealth($entity["Health"]); } } diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 8c2dd4fb0..6c21cda8c 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -302,6 +302,9 @@ class PlayerAPI{ $player = $this->server->clients[$CID]; $player->data = $this->getOffline($player->username); $player->gamemode = $player->data->get("gamemode"); + if(($player->level = $this->server->api->level->get($player->data->get("position")["level"])) === false){ + $player->level = $this->server->api->level->getDefault(); + } $this->server->query("INSERT OR REPLACE INTO players (clientID, ip, port, name) VALUES (".$player->clientID.", '".$player->ip."', ".$player->port.", '".strtolower($player->username)."');"); } } diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 72d0eba6c..90965a2a0 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -78,7 +78,7 @@ class ServerAPI{ "difficulty" => 1, "generator" => "", "generator-settings" => "", - "level-name" => false, + "level-name" => "world", "server-id" => false, "enable-query" => false, "enable-rcon" => false, @@ -170,9 +170,6 @@ class ServerAPI{ } $this->loadAPI("plugin", "PluginAPI"); //fix :( $this->plugin->init(); - - - $this->server->loadEntities(); } public function async(callable $callable, $params = array(), $remove = false){ diff --git a/src/Player.php b/src/Player.php index 7a2ff661c..e7d44d783 100644 --- a/src/Player.php +++ b/src/Player.php @@ -93,8 +93,11 @@ class Player{ console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); } - public function setSpawn(Position $pos){ - $this->spawnPosition = $pos; + public function setSpawn(Vector3 $pos, $level){ + if(($level = $this->server->api->level->get($level)) === false){ + $level = $this->server->api->level->getDefault(); + } + $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); $this->dataPacket(MC_SET_SPAWN_POSITION, array( "x" => (int) $this->spawnPosition->x, "y" => (int) $this->spawnPosition->y, @@ -546,7 +549,7 @@ class Player{ $this->lastCorrect = $pos; $this->entity->fallY = false; $this->entity->fallStart = false; - $this->entity->setPosition($pos->x, $pos->y, $pos->z, $yaw, $pitch); + $this->entity->setPosition($pos, $yaw, $pitch); $this->entity->resetSpeed(); $this->entity->updateLast(); $this->entity->calculateVelocity(); @@ -838,7 +841,7 @@ class Player{ if(($this->gamemode & 0x01) === 0x01){ $this->equipment = BlockAPI::getItem($this->inventory[7][0], $this->inventory[7][1], $this->inventory[7][2]); } - $this->entity = $this->server->api->entity->add(ENTITY_PLAYER, 0, array("player" => $this)); + $this->entity = $this->server->api->entity->add($this->level, ENTITY_PLAYER, 0, array("player" => $this)); $this->eid = $this->entity->eid; $this->server->query("UPDATE players SET EID = ".$this->eid." WHERE clientID = ".$this->clientID.";"); $this->entity->x = $this->data->get("position")["x"]; @@ -888,8 +891,8 @@ class Player{ $this->sendSettings(); $this->server->schedule(50, array($this, "orderChunks"), array(), true); $this->blocked = false; - $this->teleport(new Vector3($this->data->get("position")["x"], $this->data->get("position")["y"], $this->data->get("position")["z"])); - $this->setSpawn(new Vector3($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"])); + $this->teleport(new Position($this->data->get("position")["x"], $this->data->get("position")["y"], $this->data->get("position")["z"], $this->level)); + $this->setSpawn(new Vector3($this->data->get("spawn")["x"], $this->data->get("spawn")["y"], $this->data->get("spawn")["z"]), $this->data->get("spawn")["level"]); break; case 2://Chunk loaded? break; @@ -908,7 +911,7 @@ class Player{ } console("[WARNING] ".$this->username." moved too quickly!"); }else{ - $this->entity->setPosition($data["x"], $data["y"], $data["z"], $data["yaw"], $data["pitch"]); + $this->entity->setPosition(new Vector3($data["x"], $data["y"], $data["z"]), $data["yaw"], $data["pitch"]); } } break; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 6faa58680..d4c70e3c1 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -64,7 +64,7 @@ class PocketMinecraftServer{ $this->description = ""; $this->whitelist = false; $this->clients = array(); - $this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5); + $this->spawn = false; $this->time = 0; $this->timePerSecond = 20; $this->tickMeasure = array_fill(0, 40, 0); diff --git a/src/network/Packet.php b/src/network/Packet.php index 9a58c3da0..21166ff45 100644 --- a/src/network/Packet.php +++ b/src/network/Packet.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or class Packet{ - private $struct, $sock; + private $struct; protected $pid, $packet; public $data, $raw; @@ -40,7 +40,6 @@ class Packet{ $this->addRaw(chr($pid)); } $this->struct = $struct; - $this->sock = $sock; } public function create($raw = false){ diff --git a/src/world/Entity.php b/src/world/Entity.php index 6c045bfb7..5ea1b0aae 100644 --- a/src/world/Entity.php +++ b/src/world/Entity.php @@ -248,7 +248,7 @@ class Entity extends Position{ switch($b->getID()){ case WATER: case STILL_WATER: //Drowing - if($this->fire > 0 and $this->inBlock($x, $y, $z)){ + if($this->fire > 0 and $this->inBlock(new Vector3($x, $y, $z))){ $this->fire = 0; $this->updateMetadata(); } @@ -262,26 +262,26 @@ class Entity extends Position{ break; case LAVA: //Lava damage case STILL_LAVA: - if($this->inBlock($x, $y, $z)){ + if($this->inBlock(new Vector3($x, $y, $z))){ $this->harm(5, "lava"); $this->fire = 300; $this->updateMetadata(); } break; case FIRE: //Fire block damage - if($this->inBlock($x, $y, $z)){ + if($this->inBlock(new Vector3($x, $y, $z))){ $this->harm(1, "fire"); $this->fire = 300; $this->updateMetadata(); } break; case CACTUS: //Cactus damage - if($this->touchingBlock($x, $y, $z)){ + if($this->touchingBlock(new Vector3($x, $y, $z))){ $this->harm(1, "cactus"); } break; default: - if($this->inBlock($x, $y, $z, 0.7) and $y == $endY and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){ + if($this->inBlock(new Vector3($x, $y, $z), 0.7) and $y == $endY and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){ $this->harm(1, "suffocation"); //Suffocation }elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){ $this->air = 300; //Breathing @@ -362,8 +362,8 @@ class Entity extends Position{ } }elseif($this->fallY !== false){ //Fall damage! if($y < $this->fallY){ - $d = $this->server->api->block->getBlock(new Vector3($x, $y + 1, $z)); - $d2 = $this->server->api->block->getBlock(new Vector3($x, $y + 2, $z)); + $d = $this->level->getBlock(new Vector3($x, $y + 1, $z)); + $d2 = $this->level->getBlock(new Vector3($x, $y + 2, $z)); $dmg = ($this->fallY - $y) - 3; if($dmg > 0 and !($d instanceof LiquidBlock) and $d->getID() !== LADDER and !($d2 instanceof LiquidBlock) and $d2->getID() !== LADDER){ $this->harm($dmg, "fall"); @@ -571,7 +571,7 @@ class Entity extends Position{ public function inBlock(Vector3 $block, $radius = 0.8){ $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); - if(($y == ((int) $this->y) or $y == (((int) $this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ + if(($block->y == ((int) $this->y) or $block->y == (((int) $this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ return true; } return false; @@ -579,7 +579,7 @@ class Entity extends Position{ public function touchingBlock(Vector3 $block, $radius = 0.9){ $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); - if(($y == (((int) $this->y) - 1) or $y == ((int) $this->y) or $y == (((int) $this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ + if(($block->y == (((int) $this->y) - 1) or $block->y == ((int) $this->y) or $block->y == (((int) $this->y) + 1)) and $block->maxPlainDistance($me) < $radius){ return true; } return false; diff --git a/src/world/Level.php b/src/world/Level.php index 693ad9ade..b61000315 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -97,7 +97,7 @@ class Level{ } public function getOrderedMiniChunk($X, $Z, $Y, $MTU){ - $raw = $this->map->getMiniChunk($X, $Z, $Y); + $raw = $this->level->getMiniChunk($X, $Z, $Y); $ordered = array(); $i = 0; $ordered[$i] = ""; diff --git a/src/world/Position.php b/src/world/Position.php index d54240544..9a6e26718 100644 --- a/src/world/Position.php +++ b/src/world/Position.php @@ -25,7 +25,7 @@ the Free Software Foundation, either version 3 of the License, or */ -class Position{ +class Position extends Vector3{ public $level; public function __construct($x = 0, $y = 0, $z = 0, Level $level){