Updated player deaths

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-18 21:51:28 +01:00
parent d9c39ca816
commit 4a46fde483
7 changed files with 57 additions and 45 deletions

3
TODO
View File

@ -3,4 +3,5 @@
- Mob spawning, item pick up
- Fix metadata orientation
- Proper session checks
- Fix incorrect timeout
- Fix incorrect timeout
- Inventory loading and saving

View File

@ -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);

View File

@ -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 <player>");
}

View File

@ -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);

View File

@ -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(){

View File

@ -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;
}

View File

@ -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;
}