Entities despawning, half item pickup

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-04 19:36:51 +01:00
parent 8d9f9ed9ed
commit a64f0c8bbe
10 changed files with 42 additions and 16 deletions

View file

@ -288,6 +288,15 @@ class CustomPacketHandler{
$this->raw .= Utils::writeByte($this->data["roll"]);
}
break;
case MC_TAKE_ITEM_ENTITY:
if($this->c === false){
$this->data["target"] = Utils::readInt($this->get(4));
$this->data["eid"] = Utils::readInt($this->get(4));
}else{
$this->raw .= Utils::writeInt($this->data["target"]);
$this->raw .= Utils::writeInt($this->data["eid"]);
}
break;
case MC_MOVE_ENTITY:
if($this->c === false){
$this->data["eid"] = Utils::readInt($this->get(4));

View file

@ -49,7 +49,7 @@ class Entity extends stdClass{
$this->closed = false;
$this->name = "";
$this->server->query("INSERT OR REPLACE INTO entities (EID, type, class, health) VALUES (".$this->eid.", ".$this->type.", ".$this->class.", ".$this->health.");");
$this->ev = $this->server->event("server.tick", array($this, "update"));
$this->server->schedule(20, array($this, "update"), array(), true);
$this->metadata = array();
$this->x = isset($this->data["x"]) ? $this->data["x"]:0;
$this->y = isset($this->data["y"]) ? $this->data["y"]:0;
@ -76,7 +76,17 @@ class Entity extends stdClass{
}
public function update(){
if($this->class === ENTITY_ITEM){
$player = $this->server->query("SELECT EID FROM entities WHERE class == ".ENTITY_PLAYER." AND abs(x - {$this->x}) <= 1.5 AND abs(y - {$this->y}) <= 1.5 AND abs(z - {$this->z}) <= 1.5 LIMIT 1;", true);
if($player !== true and $player !== false){
if($this->server->api->dhandle("player.item.pick", array(
"eid" => $player["EID"],
"target" => $this->eid
)) !== false){
$this->close();
}
}
}
}
public function getDirection(){
@ -152,7 +162,6 @@ class Entity extends stdClass{
if($this->closed === false){
$this->server->query("DELETE FROM entities WHERE EID = ".$this->eid.";");
$this->server->api->dhandle("entity.remove", $this->eid);
$this->server->deleteEvent($this->ev);
$this->closed = true;
}
}

View file

@ -113,6 +113,9 @@ class Player{
public function eventHandler($data, $event){
switch($event){
case "player.item.pick":
$this->dataPacket(MC_TAKE_ITEM_ENTITY, $data);
break;
case "player.equipment.change":
if($data["eid"] === $this->eid){
break;
@ -140,7 +143,7 @@ class Player{
if($data === $this->eid){
break;
}
$this->dataPacket(MC_ENTITY_REMOVE, array(
$this->dataPacket(MC_REMOVE_ENTITY, array(
"eid" => $data,
));
break;
@ -292,6 +295,7 @@ class Player{
$this->evid[] = $this->server->event("entity.move", array($this, "eventHandler"));
$this->evid[] = $this->server->event("entity.animate", array($this, "eventHandler"));
$this->evid[] = $this->server->event("player.equipment.change", array($this, "eventHandler"));
$this->evid[] = $this->server->event("player.item.pick", array($this, "eventHandler"));
$this->evid[] = $this->server->event("world.block.change", array($this, "eventHandler"));
console("[DEBUG] Player with EID ".$this->eid." \"".$this->username."\" spawned!", true, true, 2);

View file

@ -494,7 +494,7 @@ class PocketMinecraftServer extends stdClass{
$add = ' unset($this->schedule['.$this->scheduleCnt.']);';
}
$this->schedule[$this->scheduleCnt] = array($callback, $data, $eventName);
$this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.'];'.$add.' call_user_func($schedule[0], $schedule[1], $schedule[2]);', (bool) $repeat);
$this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.'];'.$add.'if(!is_callable($schedule[0])){unset($this->schedule['.$this->scheduleCnt.']);return;} call_user_func($schedule[0], $schedule[1], $schedule[2]);', (bool) $repeat);
return $this->scheduleCnt++;
}