Death cause!

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-09 21:16:56 +01:00
parent bc934a8940
commit 68a685a984
3 changed files with 46 additions and 8 deletions

5
README
View File

@ -35,7 +35,8 @@ How to connect to internet servers: http://www.minecraftforum.net/topic/1256915-
Current features of the server:
- Players can connect and move around the world
- Players can connect and move around the world (and see each other)
- PvP, life regeneration and death cause!
- Online list broadcast
- Configurable day/night cycle
- Health and position saving
@ -43,7 +44,5 @@ Current features of the server:
- Whitelist and IP Ban files
- Survival & Creative
- Awesome features in server list!
- Players spawn and see each other moving!
- PvP and life regeneration!
- Multiple worlds and importing!
+ more!

View File

@ -76,6 +76,9 @@ class PocketMinecraftServer{
$this->events = array("disabled" => array());
$this->event("onChat", "eventHandler", true);
$this->event("onPlayerDeath", "eventHandler", true);
$this->event("onPlayerAdd", "eventHandler", true);
$this->event("onHealthChange", "eventHandler", true);
$this->action(100000, '$this->time += ceil($this->timePerSecond / 10);$this->trigger("onTimeChange", $this->time);');
$this->action(5000000, '$this->trigger("onHealthRegeneration", 1);');
@ -87,6 +90,7 @@ class PocketMinecraftServer{
public function startDatabase(){
$this->database = new SQLite3(":memory:");
$this->query("CREATE TABLE clients (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT);");
$this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, type NUMERIC, class NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);");
$this->query("CREATE TABLE metadata (EID INTEGER PRIMARY KEY, name TEXT, value TEXT);");
$this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);");
@ -155,6 +159,34 @@ class PocketMinecraftServer{
public function eventHandler($data, $event){
switch($event){
case "onPlayerDeath":
$message = $data["name"];
if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){
switch($this->entities[$data["cause"]]->class){
case ENTITY_PLAYER:
$message .= " was killed by ".$this->entities[$data["cause"]]->name;
break;
default:
$message .= " was killed";
break;
}
}else{
switch($data["cause"]){
default:
$message .= " was killed";
break;
}
}
$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;
case "onChat":
console("[CHAT] $data");
break;

View File

@ -90,6 +90,11 @@ 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 "onEntityMove":
if($data === $this->eid){
break;
@ -112,7 +117,7 @@ class Session{
break;
case "onHealthRegeneration":
if($this->server->difficulty < 2){
$this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => min(20, $this->data["health"] + $data)));
$this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => min(20, $this->data["health"] + $data), "cause" => "regeneration"));
}
break;
case "onHealthChange":
@ -277,6 +282,7 @@ class Session{
}
$this->evid[] = array("onTimeChange", $this->server->event("onTimeChange", array($this, "eventHandler")));
$this->evid[] = array("onChat", $this->server->event("onChat", array($this, "eventHandler")));
$this->evid[] = array("onDeath", $this->server->event("onDeath", array($this, "eventHandler")));
$this->evid[] = array("onPlayerAdd", $this->server->event("onPlayerAdd", array($this, "eventHandler")));
$this->evid[] = array("onEntityRemove", $this->server->event("onEntityRemove", array($this, "eventHandler")));
$this->evid[] = array("onEntityMove", $this->server->event("onEntityMove", array($this, "eventHandler")));
@ -312,7 +318,7 @@ class Session{
if(is_object($this->entity)){
break;
}
$this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => $this->data["health"]));
$this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => $this->data["health"], "cause" => "respawn"));
console("[DEBUG] Player with EID ".$this->eid." \"".$this->username."\" spawned!", true, true, 2);
$this->entity = new Entity($this->eid, ENTITY_PLAYER, 0, $this->server);
$this->entity->setName($this->username);
@ -392,15 +398,16 @@ class Session{
++$this->counter[0];
break;
case MC_INTERACT:
if($this->server->difficulty > 0 and isset($this->server->entities[$data["target"]]) and Utils::distance($this->entity->position, $this->server->entities[$data["target"]]->position) <= 8){
$this->server->trigger("onHealthChange", array("eid" => $data["target"], "health" => $this->server->entities[$data["target"]]->getHealth() - $this->server->difficulty));
if($this->server->gamemode !== 1 and $this->server->difficulty > 0 and isset($this->server->entities[$data["target"]]) and Utils::distance($this->entity->position, $this->server->entities[$data["target"]]->position) <= 8){
console("[DEBUG] EID ".$this->eid." attacked EID ".$data["target"], true, true, 2);
$this->server->trigger("onHealthChange", array("eid" => $data["target"], "health" => $this->server->entities[$data["target"]]->getHealth() - $this->server->difficulty, "cause" => $this->eid));
}
break;
case MC_ANIMATE:
$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));
$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);
break;