Skip to content

Commit

Permalink
SQLite database, Entities, position, health
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Dec 7, 2012
1 parent 842e01f commit ea1725e
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data/*
*.log
*.bat
server.properties
Expand Down
42 changes: 40 additions & 2 deletions classes/CustomPacketHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ public function __construct($pid, $raw = "", $data = array(), $create = false){
$this->raw .= Utils::writeFloat($this->data["z"]);
}
break;
case MC_ADD_PLAYER:
if($this->c === false){
$this->data["clientID"] = Utils::readLong($this->get(8));
$this->data["username"] = $this->get(Utils::readShort($this->get(2), false));
$this->data["eid"] = Utils::readInt($this->get(4));
$this->data["x"] = Utils::readFloat($this->get(4));
$this->data["y"] = Utils::readFloat($this->get(4));
$this->data["z"] = Utils::readFloat($this->get(4));
$this->data["yaw"] = Utils::readFloat($this->get(4));
$this->data["pitch"] = Utils::readFloat($this->get(4));
$this->data["block"] = Utils::readShort($this->get(2), false);
$this->data["meta"] = Utils::readShort($this->get(2), false);
}else{
$this->raw .= Utils::writeLong($this->data["clientID"]);
$this->raw .= Utils::writeShort(strlen($this->data["username"])).$this->data["username"];
$this->raw .= Utils::writeInt($this->data["eid"]);
$this->raw .= Utils::writeFloat($this->data["x"]);
$this->raw .= Utils::writeFloat($this->data["y"]);
$this->raw .= Utils::writeFloat($this->data["z"]);
$this->raw .= Utils::writeFloat($this->data["yaw"]);
$this->raw .= Utils::writeFloat($this->data["pitch"]);
$this->raw .= Utils::writeShort($this->data["block"]);
$this->raw .= Utils::writeShort($this->data["meta"]);
}
break;
case MC_REMOVE_ENTITY:
if($this->c === false){
$this->data["eid"] = Utils::readInt($this->get(4));
Expand Down Expand Up @@ -233,8 +258,8 @@ public function __construct($pid, $raw = "", $data = array(), $create = false){
case MC_PLAYER_EQUIPMENT:
if($this->c === false){
$this->data["eid"] = Utils::readInt($this->get(4));
$this->data["block"] = Utils::readShort($this->get(2), true);
$this->data["meta"] = Utils::readShort($this->get(2), true);
$this->data["block"] = Utils::readShort($this->get(2), false);
$this->data["meta"] = Utils::readShort($this->get(2), false);
}else{
$this->raw .= Utils::writeInt($this->data["eid"]);
$this->raw .= Utils::writeShort($this->data["block"]);
Expand All @@ -246,6 +271,19 @@ public function __construct($pid, $raw = "", $data = array(), $create = false){
$this->data["health"] = ord($this->get(1));
}else{
$this->raw .= chr($this->data["health"]);
}
break;
case MC_RESPAWN:
if($this->c === false){
$this->data["eid"] = Utils::readInt($this->get(4));
$this->data["x"] = Utils::readFloat($this->get(4));
$this->data["y"] = Utils::readFloat($this->get(4));
$this->data["z"] = Utils::readFloat($this->get(4));
}else{
$this->raw .= Utils::writeInt($this->data["eid"]);
$this->raw .= Utils::writeFloat($this->data["x"]);
$this->raw .= Utils::writeFloat($this->data["y"]);
$this->raw .= Utils::writeFloat($this->data["z"]);
}
break;
case MC_CLIENT_MESSAGE:
Expand Down
157 changes: 157 additions & 0 deletions classes/Entity.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/*
-
/ \
/ \
/ POCKET \
/ MINECRAFT PHP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/


define("ENTITY_PLAYER", 0);
define("ENTITY_MOB", 1);
define("ENTITY_OBJECT", 2);
define("ENTITY_ITEM", 3);
define("ENTITY_PAINTING", 4);

class Entity{
var $eid, $type, $name, $position, $dead, $metadata, $class, $attach;
protected $health, $client;

function __construct($eid, $class, $type = 0, $server){ //$type = 0 ---> player
$this->server = $server;
$this->eid = (int) $eid;
$this->type = (int) $type;
$this->class = (int) $class;
$this->attach = false;
$this->status = 0;
$this->health = 20;
$this->dead = false;
$this->server->query("INSERT OR REPLACE INTO entities (EID, type, class, health) VALUES (".$this->eid.", ".$this->type.", ".$this->class.", ".$this->health.");");
$this->metadata = array();
/*include("misc/entities.php");
switch($this->class){
case ENTITY_PLAYER:
case ENTITY_ITEM:
break;
case ENTITY_MOB:
$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type));
break;
case ENTITY_OBJECT:
$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type));
break;
}*/
}

public function __destruct(){
$this->server->query("DELETE FROM entities WHERE EID = ".$this->eid.";");
$this->server->trigger("onEntityRemove", $this->eid);
}

public function getEID(){
return $this->eid;
}

public function getName(){
return $this->name;
}

public function setName($name){
$this->name = $name;
$this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";");
}

public function look($pos2){
$pos = $this->getPosition();
$angle = Utils::angle3D($pos2, $pos);
$this->position["yaw"] = $angle["yaw"];
$this->position["pitch"] = $angle["pitch"];
$this->server->query("UPDATE entities SET pitch = ".$this->position["pitch"].", yaw = ".$this->position["yaw"]." WHERE EID = ".$this->eid.";");
}

public function setCoords($x, $y, $z){
if(!isset($this->position)){
$this->position = array(
"x" => 0,
"y" => 0,
"z" => 0,
"yaw" => 0,
"pitch" => 0,
"ground" => 0,
);
}
$this->position["x"] = $x;
$this->position["y"] = $y;
$this->position["z"] = $z;
$this->server->query("UPDATE entities SET x = ".$this->position["x"].", y = ".$this->position["y"].", z = ".$this->position["z"]." WHERE EID = ".$this->eid.";");
}

public function move($x, $y, $z, $yaw = 0, $pitch = 0){
if(!isset($this->position)){
$this->position = array(
"x" => 0,
"y" => 0,
"z" => 0,
"yaw" => 0,
"pitch" => 0,
"ground" => 0,
);
}
$this->position["x"] += $x;
$this->position["y"] += $y;
$this->position["z"] += $z;
$this->position["yaw"] += $yaw;
$this->position["yaw"] %= 360;
$this->position["pitch"] += $pitch;
$this->position["pitch"] %= 90;
$this->server->query("UPDATE entities SET x = ".$this->position["x"].", y = ".$this->position["y"].", z = ".$this->position["z"].", pitch = ".$this->position["pitch"].", yaw = ".$this->position["yaw"]." WHERE EID = ".$this->eid.";");
}

public function setPosition($x, $y, $z, $yaw, $pitch){
$this->position = array(
"x" => $x,
"y" => $y,
"z" => $z,
"yaw" => $yaw,
"pitch" => $pitch,
"ground" => $ground,
);
$this->server->query("UPDATE entities SET x = ".$this->position["x"].", y = ".$this->position["y"].", z = ".$this->position["z"].", pitch = ".$this->position["pitch"].", yaw = ".$this->position["yaw"]." WHERE EID = ".$this->eid.";");
return true;
}

public function getPosition($round = false){
return !isset($this->position) ? false:($round === true ? array_map("floor", $this->position):$this->position);
}

public function setHealth($health){
$this->health = (int) $health;
$this->server->query("UPDATE entities SET health = ".$this->health." WHERE EID = ".$this->eid.";");
}

public function getHealth(){
return $this->health;
}

}

?>
Loading

0 comments on commit ea1725e

Please sign in to comment.