diff --git a/README b/README index 3a089a9d..d36d4289 100644 --- a/README +++ b/README @@ -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! \ No newline at end of file diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 9a2c61c9..3defbe28 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -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; diff --git a/classes/Session.class.php b/classes/Session.class.php index a6ad7d75..8cae2a43 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -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;