From 4a46fde483d164511b13f75c0c8e09b40fd2f430 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Tue, 18 Dec 2012 21:51:28 +0100 Subject: [PATCH] Updated player deaths --- TODO | 3 ++- classes/API/EntityAPI.php | 12 +++++++++++ classes/API/PlayerAPI.php | 16 +++++++++++++- classes/ChunkParser.class.php | 10 ++++----- classes/Entity.class.php | 16 +++++++++++++- classes/PocketMinecraftServer.class.php | 17 ++++++--------- classes/Session.class.php | 28 ++----------------------- 7 files changed, 57 insertions(+), 45 deletions(-) diff --git a/TODO b/TODO index b6690a1f5..600d9a458 100644 --- a/TODO +++ b/TODO @@ -3,4 +3,5 @@ - Mob spawning, item pick up - Fix metadata orientation - Proper session checks -- Fix incorrect timeout \ No newline at end of file +- Fix incorrect timeout +- Inventory loading and saving \ No newline at end of file diff --git a/classes/API/EntityAPI.php b/classes/API/EntityAPI.php index fd6983d3e..9e8efd86f 100644 --- a/classes/API/EntityAPI.php +++ b/classes/API/EntityAPI.php @@ -53,6 +53,18 @@ class EntityAPI{ return $this->server->entities; } + public function heal($eid, $heal = 1, $cause){ + $this->harm($eid, -$heal, $cause); + } + + public function harm($eid, $attack = 1, $cause){ + $e = $this->get($eid); + if($e === false or $e->dead === true){ + return false; + } + $e->setHealth($e->getHealth()-$attack, $cause); + } + public function add($class, $type = 0, $data = array()){ $eid = $this->server->eidCnt++; $this->server->entities[$eid] = new Entity($this->server, $eid, $class, $type, $data); diff --git a/classes/API/PlayerAPI.php b/classes/API/PlayerAPI.php index 2e699865e..d49abc5e4 100644 --- a/classes/API/PlayerAPI.php +++ b/classes/API/PlayerAPI.php @@ -29,6 +29,7 @@ class PlayerAPI{ private $server; function __construct($server){ $this->server = $server; + $this->server->event("onHealthRegeneration", array($this, "handle")); } public function init(){ @@ -38,6 +39,19 @@ class PlayerAPI{ $this->server->api->console->register("tp", "Teleports a player to another player", array($this, "commandHandler")); } + public function handle($data, $event){ + switch($event){ + case "onHealthRegeneration": + $result = $this->server->query("SELECT ip,port FROM players WHERE EID = (SELECT EID FROM entities WHERE health < 20);", true); + if($result !== true and $result !== false){ + while($player = $result->fetchArray()){ + $player->entity->setHealth(min(20, $player->entity->getHealth() + $data), "regeneration"); + } + } + break; + } + } + public function commandHandler($cmd, $params){ switch($cmd){ case "tp": @@ -71,7 +85,7 @@ class PlayerAPI{ case "kill": $player = $this->get(implode(" ", $params)); if($player !== false){ - $this->server->trigger("onHealthChange", array("eid" => $player->eid, "health" => -1, "cause" => "console")); + $this->server->api->entity->harm($player->eid, 20, "console"); }else{ console("[INFO] Usage: /kill "); } diff --git a/classes/ChunkParser.class.php b/classes/ChunkParser.class.php index 19002f6fa..3413698ac 100644 --- a/classes/ChunkParser.class.php +++ b/classes/ChunkParser.class.php @@ -27,8 +27,8 @@ the Free Software Foundation, either version 3 of the License, or class ChunkParser{ private $location, $raw = b"", $file; - var $sectorLenght = 4096; //16 * 16 * 16 - var $chunkLenght = 86016; //21 * $sectorLenght + var $sectorLength = 4096; //16 * 16 * 16 + var $chunkLength = 86016; //21 * $sectorLength var $map; function __construct(){ @@ -64,7 +64,7 @@ class ChunkParser{ } $this->file = $file; $this->raw = file_get_contents($file); - $this->chunkLenght = $this->sectorLenght * ord($this->raw{0}); + $this->chunkLength = $this->sectorLength * ord($this->raw{0}); return true; } @@ -80,7 +80,7 @@ class ChunkParser{ public function getChunk($X, $Z){ $X = (int) $X; $Z = (int) $Z; - return substr($this->raw, $this->getOffset($X, $Z), $this->chunkLenght); + return substr($this->raw, $this->getOffset($X, $Z), $this->chunkLength); } public function writeChunk($X, $Z){ @@ -169,7 +169,7 @@ class ChunkParser{ foreach($this->map as $x => $d){ foreach($d as $z => $chunk){ fseek($fp, $this->getOffset($x, $z)); - fwrite($fp, $this->writeChunk($x, $z), $this->chunkLenght); + fwrite($fp, $this->writeChunk($x, $z), $this->chunkLength); } } flock($fp, LOCK_UN); diff --git a/classes/Entity.class.php b/classes/Entity.class.php index f04237c0f..00a89fecb 100644 --- a/classes/Entity.class.php +++ b/classes/Entity.class.php @@ -183,9 +183,23 @@ class Entity extends stdClass{ return !isset($this->position) ? false:($round === true ? array_map("floor", $this->position):$this->position); } - public function setHealth($health){ + public function setHealth($health, $cause = ""){ $this->health = (int) $health; $this->server->query("UPDATE entities SET health = ".$this->health." WHERE EID = ".$this->eid.";"); + $this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => $health, "cause" => $cause)); + if($this->player !== false){ + $this->player->dataPacket(MC_SET_HEALTH, array( + "health" => $this->health, + )); + } + if($this->health <= 0){ + $this->dead = true; + if($this->player !== false){ + $this->server->trigger("onPlayerDeath", array("name" => $this->name, "cause" => $cause)); + } + }elseif($this->health > 0){ + $this->dead = false; + } } public function getHealth(){ diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index c0f0be166..6a00b7a71 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -91,7 +91,7 @@ class PocketMinecraftServer extends stdClass{ $this->event("onHealthChange", "eventHandler", true); $this->action(500000, '$this->time += (int) ($this->timePerSecond / 2);$this->trigger("onTimeChange", $this->time);'); - $this->action(5000000, '$this->trigger("onHealthRegeneration", 1);'); + $this->action(5000000, 'if($this->difficulty < 2){$this->trigger("onHealthRegeneration", 1);}'); $this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60 * 10, '$this->custom = array();'); if($this->api !== false){ @@ -167,7 +167,7 @@ class PocketMinecraftServer extends stdClass{ $message = "<".$owner."> "; } $message .= $text; - $this->trigger("onChat", $text); + $this->trigger("onChat", $message); } public function setType($type = "normal"){ @@ -212,9 +212,10 @@ class PocketMinecraftServer extends stdClass{ case "onPlayerDeath": $message = $data["name"]; if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){ - switch($this->entities[$data["cause"]]->class){ + $e = $this->api->entity->get($data["cause"]); + switch($e->class){ case ENTITY_PLAYER: - $message .= " was killed by ".$this->entities[$data["cause"]]->name; + $message .= " was killed by ".$e->name; break; default: $message .= " was killed"; @@ -229,11 +230,6 @@ class PocketMinecraftServer extends stdClass{ } $this->chat(false, $message); break; - case "onHealthChange": - if($data["health"] <= 0){ - $this->trigger("onDeath", array("eid" => $data["eid"], "cause" => $data["cause"])); - } - break; case "onPlayerAdd": console("[DEBUG] Player \"".$data["username"]."\" EID ".$data["eid"]." spawned at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); break; @@ -425,8 +421,7 @@ class PocketMinecraftServer extends stdClass{ } while($evn = $events->fetchArray(SQLITE3_ASSOC)){ $evid = (int) $evn["ID"]; - $this->responses[$evid] = call_user_func($this->events[$evid], $data, $event, $this); - + $this->responses[$evid] = call_user_func($this->events[$evid], $data, $event, $this); } return true; } diff --git a/classes/Session.class.php b/classes/Session.class.php index 973d0777d..5346e978e 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -112,11 +112,6 @@ class Session{ public function eventHandler($data, $event){ switch($event){ - case "onDeath": - if($data["eid"] === $this->eid){ - $this->server->trigger("onPlayerDeath", array("name" => $this->username, "cause" => $data["cause"])); - } - break; case "onBlockUpdate": $this->dataPacket(MC_UPDATE_BLOCK, $data); break; @@ -147,22 +142,6 @@ class Session{ "pitch" => $entity->pitch, )); break; - case "onHealthRegeneration": - if($this->server->difficulty < 2){ - $this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => min(20, $this->data["health"] + $data), "cause" => "regeneration")); - } - break; - case "onHealthChange": - if($data["eid"] === $this->eid){ - $this->dataPacket(MC_SET_HEALTH, array( - "health" => $data["health"], - )); - $this->data["health"] = $data["health"]; - /*if(is_object($this->entity)){ - $this->entity->setHealth($data["health"]); - }*/ - } - break; case "onEntityRemove": if($data === $this->eid){ break; @@ -296,11 +275,8 @@ class Session{ $this->server->api->entity->spawnToAll($this->eid); $this->evid[] = $this->server->event("onTimeChange", array($this, "eventHandler")); $this->evid[] = $this->server->event("onChat", array($this, "eventHandler")); - $this->evid[] = $this->server->event("onDeath", array($this, "eventHandler")); $this->evid[] = $this->server->event("onEntityRemove", array($this, "eventHandler")); $this->evid[] = $this->server->event("onEntityMove", array($this, "eventHandler")); - $this->evid[] = $this->server->event("onHealthChange", array($this, "eventHandler")); - $this->evid[] = $this->server->event("onHealthRegeneration", array($this, "eventHandler")); $this->evid[] = $this->server->event("onAnimate", array($this, "eventHandler")); $this->evid[] = $this->server->event("onTeleport", array($this, "eventHandler")); $this->evid[] = $this->server->event("onBlockUpdate", array($this, "eventHandler")); @@ -354,8 +330,8 @@ class Session{ $this->server->trigger("onAnimate", array("eid" => $this->eid, "action" => $data["action"])); break; case MC_RESPAWN: - $this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => 20, "cause" => "respawn")); - $this->entity->setPosition($data["x"], $data["y"], $data["z"], $data["x"], 0, 0); + $this->entity->setHealth(20, "respawn"); + $this->entity->setPosition($data["x"], $data["y"], $data["z"], 0, 0); break; }