Map saving, breaking blocks. Huge rewrite

This commit is contained in:
Shoghi Cervantes Pueyo
2012-12-16 23:33:18 +01:00
parent 72c4cc7a30
commit 3a2010696f
15 changed files with 423 additions and 484 deletions

View File

@ -31,11 +31,14 @@ class ConsoleAPI{
$this->help = array();
$this->server = $server;
$this->input = fopen(FILE_PATH."console.in", "w+b");
}
public function init(){
$this->event = $this->server->event("onTick", array($this, "handle"));
}
function __destroy(){
$this->server->deleteEvent("onTick", $this->event);
$this->server->deleteEvent($this->event);
fclose($this->input);
}

94
classes/API/EntityAPI.php Normal file
View File

@ -0,0 +1,94 @@
<?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.
*/
class EntityAPI{
private $server;
function __construct($server){
$this->server = $server;
}
public function init(){
$this->server->api->console->register("give", "Give items to a player", array($this, "commandHandler"));
}
public function commandHandler($cmd, $params){
switch($cmd){
case "give":
break;
}
}
public function get($eid){
if(isset($this->server->entities[$eid])){
return $this->server->entities[$eid];
}
return false;
}
public function getAll(){
return $this->server->entities;
}
public function add($class, $type = 0, $data = array()){
$eid = $this->server->eidCnt++;
$this->server->entities[$eid] = new Entity($this->server, $eid, $class, $type, $data);
return $this->server->entities[$eid];
}
public function spawnTo($eid, $player){
$e = $this->get($eid);
if($e === false){
return false;
}
$e->spawn($player);
}
public function spawnToAll($eid){
$e = $this->get($eid);
if($e === false){
return false;
}
foreach($this->server->api->player->getAll() as $player){
if($player->eid !== false){
$e->spawn($player);
}
}
}
public function spawnAll($player){
foreach($this->getAll() as $e){
$e->spawn($player);
}
}
public function remove($eid){
if(isset($this->server->entities[$eid])){
$this->server->entities[$eid]->close();
unset($this->server->entities[$eid]);
}
}
}

View File

@ -35,6 +35,32 @@ class LevelAPI{
}
}
public function init(){
//$this->server->event("onBlockBreak", array($this, "handle"));
}
public function handle($data, $event){
switch($event){
case "onBlockBreak":
$block = $this->getBlock($data["x"], $data["y"], $data["z"]);
console("[DEBUG] EID ".$data["eid"]." broke block ".$block[0].":".$block[1]." at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2);
if($block[0] === 0){
break;
}
$this->setBlock($data["x"], $data["y"], $data["z"], 0, 0);
$data["block"] = $block[0];
$data["meta"] = $block[1];
$data["stack"] = 1;
$data["x"] += mt_rand(2, 8) / 10;
$data["y"] += mt_rand(2, 8) / 10;
$data["z"] += mt_rand(2, 8) / 10;
$e = $this->server->api->entity->add(ENTITY_ITEM, $block[0], $data);
$this->server->api->entity->spawnToAll($e->eid);
break;
}
}
private function check(){
if($this->active === false and $this->server->map === false){
return false;
@ -57,6 +83,19 @@ class LevelAPI{
return array(0,0);
}
public function setBlock($x, $y, $z, $block, $meta = 0){
if($this->check()){
$this->map->setBlock($x, $y, $z, $block, $meta);
}
$this->server->trigger("onBlockUpdate", array(
"x" => $x,
"y" => $y,
"z" => $z,
"block" => $block,
"meta" => $meta,
));
}
public function getOrderedChunk($X, $Z, $columnsPerPacket = 2){
$columnsPerPacket = max(1, (int) $columnsPerPacket);
$c = $this->getChunk($X, $Z);

View File

@ -73,7 +73,7 @@ class PlayerAPI{
case "list":
console("[INFO] Player list:");
foreach($this->server->clients as $c){
console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->username->entity->position["x"], 2).", ".round($c->username->entity->position["y"], 2).", ".round($c->username->entity->position["z"], 2).")");
console("[INFO] ".$c->username." (".$c->ip.":".$c->port."), ClientID ".$c->clientID.", (".round($c->username->entity->x, 2).", ".round($c->username->entity->y, 2).", ".round($c->username->entity->z, 2).")");
}
break;
}
@ -82,7 +82,7 @@ class PlayerAPI{
public function teleport($name, $target){
$target = $this->get($target);
if($target !== false){
$this->tppos($name, $target->entity->position["x"], $target->entity->position["y"], $target->entity->position["z"]);
$this->tppos($name, $target->entity->x, $target->entity->y, $target->entity->z);
}
}
@ -102,6 +102,10 @@ class PlayerAPI{
return false;
}
public function getAll(){
return $this->server->clients;
}
public function getByEID($eid){
$eid = (int) $eid;
$CID = $this->server->query("SELECT ip,port FROM players WHERE EID = '".$eid."';", true);
@ -112,6 +116,16 @@ class PlayerAPI{
return false;
}
public function getByClientID($clientID){
$clientID = (int) $clientID;
$CID = $this->server->query("SELECT ip,port FROM players WHERE clientID = '".$clientID."';", true);
$CID = $this->server->clientID($CID["ip"], $CID["port"]);
if(isset($this->server->clients[$CID])){
return $this->server->clients[$CID];
}
return false;
}
public function online(){
$o = array();
foreach($this->server->clients as $p){
@ -127,7 +141,7 @@ class PlayerAPI{
$player = $this->server->clients[$CID];
console("[INFO] Player \"".$player->username."\" connected from ".$player->ip.":".$player->port);
$player->data = $this->getOffline($player->username);
$this->server->query("INSERT OR REPLACE INTO players (clientID, EID, ip, port, name) VALUES (".$player->clientID.", ".$player->eid.", '".$player->ip."', ".$player->port.", '".$player->username."');");
$this->server->query("INSERT OR REPLACE INTO players (clientID, ip, port, name) VALUES (".$player->clientID.", '".$player->ip."', ".$player->port.", '".$player->username."');");
}
}

View File

@ -99,6 +99,8 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
$this->$a->init();
}
}
$this->server->loadEntities();
}
public function getList(){