Fixed Memory Leak

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-11 00:20:13 +01:00
parent 8d3ad0c5ec
commit 6102efc809
4 changed files with 46 additions and 49 deletions

View File

@ -31,36 +31,6 @@ class EntityAPI{
$this->server = $server; $this->server = $server;
} }
public function init(){
$this->server->addHandler("player.death", array($this, "handle"), 1);
}
public function handle($data, $event){
switch($event){
case "player.death":
$message = $data["name"];
if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){
$e = $this->api->entity->get($data["cause"]);
switch($e->class){
case ENTITY_PLAYER:
$message .= " was killed by ".$e->name;
break;
default:
$message .= " was killed";
break;
}
}else{
switch($data["cause"]){
default:
$message .= " was killed";
break;
}
}
$this->server->chat(false, $message);
break;
}
}
public function get($eid){ public function get($eid){
if(isset($this->server->entities[$eid])){ if(isset($this->server->entities[$eid])){
return $this->server->entities[$eid]; return $this->server->entities[$eid];
@ -68,6 +38,10 @@ class EntityAPI{
return false; return false;
} }
public function init(){
}
public function getAll(){ public function getAll(){
return $this->server->entities; return $this->server->entities;
} }

View File

@ -32,7 +32,8 @@ class PlayerAPI{
} }
public function init(){ public function init(){
$this->server->event("server.regeneration", array($this, "handle")); $this->server->addHandler("server.regeneration", array($this, "handle"));
$this->server->addHandler("player.death", array($this, "handle"), 1);
$this->server->api->console->register("list", "Shows connected player list", array($this, "commandHandler")); $this->server->api->console->register("list", "Shows connected player list", array($this, "commandHandler"));
$this->server->api->console->register("kill", "Kills a player", array($this, "commandHandler")); $this->server->api->console->register("kill", "Kills a player", array($this, "commandHandler"));
$this->server->api->console->register("tppos", "Teleports a player to a position", array($this, "commandHandler")); $this->server->api->console->register("tppos", "Teleports a player to a position", array($this, "commandHandler"));
@ -51,6 +52,27 @@ class PlayerAPI{
} }
} }
break; break;
case "player.death":
$message = $data["name"];
if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){
$e = $this->api->entity->get($data["cause"]);
switch($e->class){
case ENTITY_PLAYER:
$message .= " was killed by ".$e->name;
break;
default:
$message .= " was killed";
break;
}
}else{
switch($data["cause"]){
default:
$message .= " was killed";
break;
}
}
$this->server->chat(false, $message);
break;
} }
} }
@ -180,9 +202,7 @@ class PlayerAPI{
public function remove($CID){ public function remove($CID){
if(isset($this->server->clients[$CID])){ if(isset($this->server->clients[$CID])){
$player = $this->server->clients[$CID]; $player = $this->server->clients[$CID];
if(is_object($player->entity)){ $this->server->api->entity->remove($player->entity->eid);
$player->entity->close();
}
$this->saveOffline($player->username, $player->data); $this->saveOffline($player->username, $player->data);
$this->server->query("DELETE FROM players WHERE name = '".$player->username."';"); $this->server->query("DELETE FROM players WHERE name = '".$player->username."';");
unset($this->server->entities[$player->eid]); unset($this->server->entities[$player->eid]);

View File

@ -92,23 +92,25 @@ class Player{
} }
public function close($reason = "", $msg = true){ public function close($reason = "", $msg = true){
$reason = $reason == "" ? "server stop":$reason; if($this->connected === true){
$this->save(); $reason = $reason == "" ? "server stop":$reason;
foreach($this->evid as $ev){ $this->save();
$this->server->deleteEvent($ev); foreach($this->evid as $ev){
} $this->server->deleteEvent($ev);
$this->eventHandler("You have been kicked. Reason: ".$reason, "server.chat"); }
$this->dataPacket(MC_LOGIN_STATUS, array( $this->eventHandler("You have been kicked. Reason: ".$reason, "server.chat");
"status" => 1, $this->dataPacket(MC_LOGIN_STATUS, array(
)); "status" => 1,
$this->dataPacket(MC_DISCONNECT); ));
$this->dataPacket(MC_DISCONNECT);
$this->connected = false; $this->connected = false;
if($msg === true){ if($msg === true){
$this->server->api->dhandle("server.chat", $this->username." left the game"); $this->server->api->dhandle("server.chat", $this->username." left the game");
}
console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason);
$this->server->api->player->remove($this->CID);
} }
console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason);
$this->server->api->player->remove($this->CID);
} }
public function eventHandler($data, $event){ public function eventHandler($data, $event){

View File

@ -210,6 +210,7 @@ class PocketMinecraftServer{
$this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT); $this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT);
$handlers = $this->preparedSQL->selectHandlers->execute(); $handlers = $this->preparedSQL->selectHandlers->execute();
$result = true; $result = true;
console("[INTERNAL] Handling ".$event, true, true, 3);
if($handlers !== false and $handlers !== true){ if($handlers !== false and $handlers !== true){
while(false !== ($hn = $handlers->fetchArray(SQLITE3_ASSOC)) and $result !== false){ while(false !== ($hn = $handlers->fetchArray(SQLITE3_ASSOC)) and $result !== false){
$handler = $this->handlers[(int) $hn["ID"]]; $handler = $this->handlers[(int) $hn["ID"]];