Block Updates

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-24 16:34:09 +01:00
parent 849cfcb01b
commit 332202c4e8
2 changed files with 73 additions and 12 deletions

View File

@ -25,6 +25,13 @@ the Free Software Foundation, either version 3 of the License, or
*/
define("BLOCK_UPDATE_NORMAL", 0);
define("BLOCK_UPDATE_RANDOM", 1);
define("BLOCK_UPDATE_SCHEDULED", 2);
define("BLOCK_UPDATE_WEAK", 3);
class BlockAPI{
private $server;
function __construct($server){
@ -114,6 +121,7 @@ class BlockAPI{
$data2 = $data;
--$data2["y"];
$this->server->trigger("player.block.break", $data2);
$this->updateBlocksAround($data2["x"], $data2["y"], $data2["z"], BLOCK_UPDATE_NORMAL);
}
}else{
$up = $this->server->api->level->getBlock($data["x"], $data["y"] + 1, $data["z"]);
@ -121,6 +129,7 @@ class BlockAPI{
$data2 = $data;
++$data2["y"];
$this->server->trigger("player.block.break", $data2);
$this->updateBlocksAround($data2["x"], $data2["y"], $data2["z"], BLOCK_UPDATE_NORMAL);
}
}
break;
@ -129,7 +138,7 @@ class BlockAPI{
$this->drop($data["x"], $data["y"], $data["z"], $drop[0], $drop[1] & 0x0F, $drop[2] & 0xFF);
}
$this->server->trigger("player.block.break", $data);
$this->updateBlocksAround($data["x"], $data["x"], $data["z"]);
$this->updateBlocksAround($data["x"], $data["y"], $data["z"], BLOCK_UPDATE_NORMAL);
}
public function drop($x, $y, $z, $block, $meta, $stack = 1){
@ -300,7 +309,7 @@ class BlockAPI{
$data["meta"] = $direction & 0x03;
++$data2["y"];
$this->server->trigger("player.block.place", $data2);
$this->updateBlocksAround($data2["x"], $data2["y"], $data2["z"]);
$this->updateBlocksAround($data2["x"], $data2["y"], $data2["z"], BLOCK_UPDATE_NORMAL);
}
break;
case 65: //Ladder
@ -338,10 +347,51 @@ class BlockAPI{
break;
}
$this->server->trigger("player.block.place", $data);
$this->updateBlocksAround($data["x"], $data["y"], $data["z"]);
$this->updateBlocksAround($data["x"], $data["y"], $data["z"], BLOCK_UPDATE_NORMAL);
}
public function updateBlocksAround($x, $y, $z){
public function blockScheduler($data){
$this->updateBlock($data["x"], $data["y"], $data["z"], BLOCK_UPDATE_SCHEDULED);
}
public function updateBlock($x, $y, $z, $type){
$block = $this->server->api->level->getBlock($x, $y, $z);
$changed = false;
switch($block[0]){
case 74:
if($type === BLOCK_UPDATE_SCHEDULED){
$changed = true;
$this->server->api->level->setBlock($x, $y, $z, 73, $block[1]);
}
break;
case 73:
if($type === BLOCK_UPDATE_NORMAL){
$changed = true;
$this->server->api->level->setBlock($x, $y, $z, 74, $block[1]);
$this->server->schedule(mt_rand(40, 100), array($this, "blockScheduler"), array(
"x" => $x,
"y" => $y,
"z" => $z,
));
$type = BLOCK_UPDATE_WEAK;
}
break;
}
if($type === BLOCK_TYPE_SCHEDULED){
$type = BLOCK_UPDATE_WEAK;
}
if($changed === true){
$this->updateBlocksAround($x, $y, $z, $type);
}
}
public function updateBlocksAround($x, $y, $z, $type){
$this->updateBlock($x + 1, $y, $z, $type);
$this->updateBlock($x, $y + 1, $z, $type);
$this->updateBlock($x, $y, $z + 1, $type);
$this->updateBlock($x - 1, $y, $z, $type);
$this->updateBlock($x, $y - 1, $z, $type);
$this->updateBlock($x, $y, $z - 1, $type);
}
}

View File

@ -55,6 +55,8 @@ class PocketMinecraftServer extends stdClass{
$this->handCnt = 0;
$this->eidCnt = 1;
$this->maxClients = 20;
$this->schedule = array();
$this->scheduleCnt = 0;
$this->description = "";
$this->whitelist = false;
$this->bannedIPs = array();
@ -100,16 +102,16 @@ class PocketMinecraftServer extends stdClass{
public function startDatabase(){
$this->preparedSQL = new stdClass();
$this->database = new SQLite3(":memory:");
//$this->query("PRAGMA journal_mode = OFF;");
//$this->query("PRAGMA encoding = \"UTF-8\";");
//$this->query("PRAGMA secure_delete = OFF;");
$this->query("PRAGMA journal_mode = OFF;");
$this->query("PRAGMA encoding = \"UTF-8\";");
$this->query("PRAGMA secure_delete = OFF;");
$this->query("CREATE TABLE players (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT UNIQUE);");
$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);");
$this->query("CREATE TABLE events (ID INTEGER PRIMARY KEY, name TEXT);");
$this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);");
//$this->query("PRAGMA synchronous = OFF;");
$this->query("PRAGMA synchronous = OFF;");
$this->preparedSQL->selectHandlers = $this->database->prepare("SELECT ID FROM handlers WHERE name = :name ORDER BY priority DESC;");
$this->preparedSQL->selectEvents = $this->database->prepare("SELECT ID FROM events WHERE name = :name;");
$this->preparedSQL->selectActions = $this->database->prepare("SELECT ID,code,repeat FROM actions WHERE last <= (:time - interval);");
@ -450,7 +452,7 @@ class PocketMinecraftServer extends stdClass{
}
while(false !== ($evn = $events->fetchArray(SQLITE3_ASSOC))){
$evid = (int) $evn["ID"];
$this->responses[$evid] = call_user_func($this->events[$evid], $data, $event, $this);
$this->responses[$evid] = call_user_func($this->events[$evid], $data, $event);
}
$events->finalize();
return true;
@ -463,10 +465,19 @@ class PocketMinecraftServer extends stdClass{
return $res;
}
return false;
}
}
public function schedule($ticks, $callback, $data = array(), $eventName = "server.schedule"){
if(!is_callable($callback)){
return false;
}
$this->schedule[$this->scheduleCnt] = array($callback, $data, $eventName);
$this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.']; unset($this->schedule['.$this->scheduleCnt.']); call_user_func($schedule[0], $schedule[1], $schedule[2]);', false);
return $this->scheduleCnt++;
}
public function action($microseconds, $code, $repeat = true){
$this->query("INSERT INTO actions (interval, last, code, repeat) VALUES(".($microseconds / 1000000).", ".microtime(true).", '".str_replace("'", "\\'", $code)."', ".($repeat === true ? 1:0).");");
$this->query("INSERT INTO actions (interval, last, code, repeat) VALUES(".($microseconds / 1000000).", ".microtime(true).", '".base64_encode($code)."', ".($repeat === true ? 1:0).");");
console("[INTERNAL] Attached to action ".$microseconds, true, true, 3);
}
@ -481,7 +492,7 @@ class PocketMinecraftServer extends stdClass{
return;
}
while(false !== ($action = $actions->fetchArray(SQLITE3_ASSOC))){
eval($action["code"]);
eval(base64_decode($action["code"]));
if($action["repeat"] === 0){
$this->query("DELETE FROM actions WHERE ID = ".$action["ID"].";");
}