Player data now uses YAML

This commit is contained in:
Shoghi Cervantes Pueyo 2013-03-16 23:26:46 +01:00
parent bb968451ba
commit c536a35beb
2 changed files with 48 additions and 41 deletions

View File

@ -258,7 +258,9 @@ class PlayerAPI{
$this->server->clients[$CID] = null; $this->server->clients[$CID] = null;
unset($this->server->clients[$CID]); unset($this->server->clients[$CID]);
$player->close(); $player->close();
$this->saveOffline($player->username, $player->data); if($player->username != ""){
$this->saveOffline($player->data);
}
$this->server->query("DELETE FROM players WHERE name = '".$player->username."';"); $this->server->query("DELETE FROM players WHERE name = '".$player->username."';");
if($player->entity instanceof Entity){ if($player->entity instanceof Entity){
$player->entity->player = null; $player->entity->player = null;
@ -271,33 +273,33 @@ class PlayerAPI{
} }
public function getOffline($name){ public function getOffline($name){
if(!file_exists(DATA_PATH."players/".$name.".dat")){ $iname = strtolower($name);
console("[NOTICE] Player data not found for \"".$name."\", creating new profile"); $default = array(
$data = array( "position" => array(
"spawn" => array( "x" => $this->server->spawn["x"],
"x" => $this->server->spawn["x"], "y" => $this->server->spawn["y"],
"y" => $this->server->spawn["y"], "z" => $this->server->spawn["z"],
"z" => $this->server->spawn["z"], ),
), "inventory" => array_fill(0, 36, array(AIR, 0, 0)),
"inventory" => array_fill(0, 36, array(AIR, 0, 0)), "armor" => array_fill(0, 4, array(AIR, 0, 0)),
"armor" => array_fill(0, 4, array(AIR, 0, 0)), "health" => 20,
"health" => 20, "lastIP" => "",
"lastIP" => "", "lastID" => 0,
"lastID" => 0, );
); $data = new Config(DATA_PATH."players/".$iname.".yml", CONFIG_YAML, $default);
$this->saveOffline($name, $data); if(!file_exists(DATA_PATH."players/".$iname.".yml")){
}else{ console("[NOTICE] Player data not found for \"".$iname."\", creating new profile");
$data = unserialize(file_get_contents(DATA_PATH."players/".$name.".dat")); $data->save();
} }
if($this->server->gamemode === 1){ if($this->server->gamemode === 1){
$data["health"] = 20; $data->set("health", 20);
} }
$this->server->handle("api.player.offline.get", $data); $this->server->handle("api.player.offline.get", $data);
return $data; return $data;
} }
public function saveOffline($name, $data){ public function saveOffline(Config $data){
$this->server->handle("api.player.offline.save", $data); $this->server->handle("api.player.offline.save", $data);
file_put_contents(DATA_PATH."players/".str_replace("/", "", $name).".dat", serialize($data)); $data->save();
} }
} }

View File

@ -42,7 +42,7 @@ class Player{
public $username; public $username;
public $iusername; public $iusername;
public $eid = false; public $eid = false;
public $data = array(); public $data;
public $entity = false; public $entity = false;
public $auth = false; public $auth = false;
public $CID; public $CID;
@ -181,11 +181,13 @@ class Player{
public function save(){ public function save(){
if($this->entity instanceof Entity){ if($this->entity instanceof Entity){
$this->data["spawn"] = array( $this->data->set("position", array(
"x" => $this->entity->x, "x" => $this->entity->x,
"y" => $this->entity->y, "y" => $this->entity->y,
"z" => $this->entity->z, "z" => $this->entity->z,
); ));
$this->data->set("inventory", $this->inventory);
$this->data->set("armor", $this->armor);
} }
} }
@ -194,9 +196,11 @@ class Player{
foreach($this->evid as $ev){ foreach($this->evid as $ev){
$this->server->deleteEvent($ev); $this->server->deleteEvent($ev);
} }
$this->server->api->handle("player.quit", $this); if($this->username != ""){
$this->server->api->handle("player.quit", $this);
$this->save();
}
$reason = $reason == "" ? "server stop":$reason; $reason = $reason == "" ? "server stop":$reason;
$this->save();
$this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat"); $this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat");
$this->dataPacket(MC_LOGIN_STATUS, array( $this->dataPacket(MC_LOGIN_STATUS, array(
"status" => 1, "status" => 1,
@ -622,23 +626,24 @@ class Player{
$this->server->api->player->add($this->CID); $this->server->api->player->add($this->CID);
$this->auth = true; $this->auth = true;
if(!isset($this->data["inventory"]) or $this->gamemode === CREATIVE){ if(!$this->data->exists("inventory") or $this->gamemode === CREATIVE){
$this->data["inventory"] = $this->inventory; $this->data->set("inventory", $this->inventory);
} }
$this->inventory = &$this->data["inventory"]; $this->inventory = $this->data->get("inventory");
$this->armor = &$this->data["armor"]; $this->armor = $this->data->get("armor");
$this->data["lastIP"] = $this->ip; $this->data->set("lastIP", $this->ip);
$this->data["lastID"] = $this->clientID; $this->data->set("lastID", $this->clientID);
$this->server->api->player->saveOffline($this->username, $this->data);
$this->server->api->player->saveOffline($this->data);
$this->dataPacket(MC_LOGIN_STATUS, array( $this->dataPacket(MC_LOGIN_STATUS, array(
"status" => 0, "status" => 0,
)); ));
$this->dataPacket(MC_START_GAME, array( $this->dataPacket(MC_START_GAME, array(
"seed" => $this->server->seed, "seed" => $this->server->seed,
"x" => $this->data["spawn"]["x"], "x" => $this->data->get("position")["x"],
"y" => $this->data["spawn"]["y"], "y" => $this->data->get("position")["y"],
"z" => $this->data["spawn"]["z"], "z" => $this->data->get("position")["z"],
"unknown1" => 0, "unknown1" => 0,
"gamemode" => $this->gamemode, "gamemode" => $this->gamemode,
"eid" => 0, "eid" => 0,
@ -657,9 +662,9 @@ class Player{
$this->entity = $this->server->api->entity->add(ENTITY_PLAYER, 0, array("player" => $this)); $this->entity = $this->server->api->entity->add(ENTITY_PLAYER, 0, array("player" => $this));
$this->eid = $this->entity->eid; $this->eid = $this->entity->eid;
$this->server->query("UPDATE players SET EID = ".$this->eid." WHERE clientID = ".$this->clientID.";"); $this->server->query("UPDATE players SET EID = ".$this->eid." WHERE clientID = ".$this->clientID.";");
$this->entity->x = $this->data["spawn"]["x"]; $this->entity->x = $this->data->get("position")["x"];
$this->entity->y = $this->data["spawn"]["y"]; $this->entity->y = $this->data->get("position")["y"];
$this->entity->z = $this->data["spawn"]["z"]; $this->entity->z = $this->data->get("position")["z"];
$this->entity->setName($this->username); $this->entity->setName($this->username);
$this->entity->data["clientID"] = $this->clientID; $this->entity->data["clientID"] = $this->clientID;
$this->server->api->entity->spawnAll($this); $this->server->api->entity->spawnAll($this);
@ -689,7 +694,7 @@ class Player{
$this->eventHandler("You're using the default username. Please change it on the Minecraft PE settings.", "server.chat"); $this->eventHandler("You're using the default username. Please change it on the Minecraft PE settings.", "server.chat");
} }
$this->sendInventory(); $this->sendInventory();
$this->teleport(new Vector3($this->data["spawn"]["x"], $this->data["spawn"]["y"], $this->data["spawn"]["z"])); $this->teleport(new Vector3($this->data->get("position")["x"], $this->data->get("position")["y"], $this->data->get("position")["z"]));
$this->sendSettings(); $this->sendSettings();
$this->getNextChunk(); $this->getNextChunk();
break; break;