LevelAPI and pre-map r

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-13 02:49:04 +01:00
parent 801c2736ba
commit 063f44c330
8 changed files with 115 additions and 24 deletions

73
classes/API/LevelAPI.php Normal file
View File

@ -0,0 +1,73 @@
<?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 LevelAPI{
private $server, $map, $active = false;
function __construct($server){
$this->server = $server;
$this->map = $this->server->map;
if($this->map !== false){
$this->active = true;
}
}
private function check(){
if($this->active === false and $this->server->map === false){
return false;
}
$this->active = true;
return true;
}
public function getChunk($X, $Z){
/*if($this->check() and isset($this->map->map[$X][$Z])){
return $this->map->map[$X][$Z];
}*/
return false;
}
public function getOrderedChunk($X, $Z, $columnsPerPacket = 2){
$columnsPerPacket = max(1, (int) $columnsPerPacket);
$c = $this->getChunk($X, $Z);
if($c === false){
return array(str_repeat("\x00", 256));
}
$ordered = array();
for($i = 0;$i < 0xff; ){
$ordered[$i] = str_repeat("\x00", $i);
for($j = 0; $j < $columnsPerPacket; ++$j){
$ordered[$i] .= "\xff";
for($k = 0; $k < 8; ++$k){
$ordered[$i] .= substr($c[$i][0], $k << 4, 16); //Block data
$ordered[$i] .= substr($c[$i][1], $k << 3, 8); //Meta data
}
++$i;
}
}
return $ordered;
}
}

View File

@ -80,6 +80,8 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
$this->server->mapName = $this->getProperty("level-name");
$this->server->mapDir = FILE_PATH."data/maps/".$this->getProperty("level-name")."/";
$this->loadProperties();
$this->server->loadMap();
//Autoload all default APIs
console("[INFO] Loading default APIs");
$dir = dir(FILE_PATH."classes/API/");

View File

@ -53,8 +53,9 @@ class ChunkParser{
}
private function getOffset($X, $Z){
$info = $this->getOffsetPosition($X, $Z);
return 4096 + (($info[1] * $info[0]) << 12) + (($info[2] * $data[0]) << 16);
//$info = $this->getOffsetPosition($X, $Z);
//return 4096 + (($info[1] * $info[0]) << 12) + (($info[2] * $data[0]) << 16);
return 4096 + (($X * 21) << 12) + (($Z * 21) << 16);
}
public function getChunk($X, $Z, $header = true){

View File

@ -121,7 +121,7 @@ class CustomPacketHandler{
$this->raw .= Utils::writeLong($this->data["session"]);
}
break;
case MC_CLIENT_DISCONNECT:
case MC_DISCONNECT:
//null
break;
case 0x18:
@ -379,17 +379,17 @@ class CustomPacketHandler{
$this->data["z"] = Utils::readInt($this->get(4));
}else{
$this->raw .= Utils::writeInt($this->data["x"]);
$this->raw .= Utils::writeInt($this->data["y"]);
$this->raw .= Utils::writeInt($this->data["z"]);
}
break;
case MC_CHUNK_DATA:
if($this->c === false){
$this->data["x"] = Utils::readInt($this->get(4));
$this->data["z"] = Utils::readInt($this->get(4));
$this->data["data"] = $this->get(256);
$this->data["data"] = $this->get(true);
}else{
$this->raw .= Utils::writeInt($this->data["x"]);
$this->raw .= Utils::writeInt($this->data["y"]);
$this->raw .= Utils::writeInt($this->data["z"]);
$this->raw .= $this->data["data"];
}
break;

View File

@ -196,11 +196,12 @@ class PocketMinecraftServer extends stdClass{
}
}
private function loadMap(){
public function loadMap(){
if($this->mapName !== false and trim($this->mapName) !== ""){
$this->level = unserialize(file_get_contents($this->mapDir."level.dat"));
console("[INFO] Map: ".$this->level["LevelName"]);
$this->time = (int) $this->level["Time"];
$this->seed = (int) $this->level["RandomSeed"];
$this->level["Time"] = &$this->time;
console("[INFO] Time: ".$this->time);
console("[INFO] Seed: ".$this->seed);
@ -246,7 +247,6 @@ class PocketMinecraftServer extends stdClass{
$this->event("onTick", array($this, "tickerFunction"));
$this->event("onReceivedPacket", "packetHandler", true);
register_shutdown_function(array($this, "close"));
$this->loadMap();
console("[INFO] Server started!");
$this->process();
}

View File

@ -81,6 +81,14 @@ class Session{
$this->server->trigger("onChat", $this->username." left the game");
}
console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason);
$this->send(0x84, array(
$this->counter[0],
0x00,
array(
"id" => MC_DISCONNECT,
),
));
++$this->counter[0];
$this->server->api->player->remove($this->CID);
}
@ -247,7 +255,7 @@ class Session{
$this->send(0xc0, array(1, true, $data[0]));
}
switch($data["id"]){
case MC_CLIENT_DISCONNECT:
case MC_DISCONNECT:
$this->close("client disconnect");
break;
case MC_CLIENT_CONNECT:
@ -367,23 +375,30 @@ class Session{
//$this->server->trigger("onChat", $this->username." joined the game");
break;
case MC_MOVE_PLAYER:
$this->entity->setPosition($data["x"], $data["y"], $data["z"], $data["yaw"], $data["pitch"]);
$this->server->trigger("onEntityMove", $this->eid);
if(is_object($this->entity)){
$this->entity->setPosition($data["x"], $data["y"], $data["z"], $data["yaw"], $data["pitch"]);
$this->server->trigger("onEntityMove", $this->eid);
}
break;
case MC_PLAYER_EQUIPMENT:
console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2);
break;
case MC_REQUEST_CHUNK:
$this->send(0x84, array(
$this->counter[0],
0x00,
array(
"id" => MC_CHUNK_DATA,
"x" => $data["x"],
"z" => $data["z"],
"data" => str_repeat("\x00", 256),
),
));
case MC_REQUEST_CHUNK:
$chunk = $this->server->api->level->getOrderedChunk($data["x"], $data["z"]);
foreach($chunk as $d){
$this->send(0x84, array(
$this->counter[0],
0x00,
array(
"id" => MC_CHUNK_DATA,
"x" => $data["x"],
"z" => $data["z"],
"data" => $d,
),
));
}
++$this->counter[0];
console("[DEBUG] Chunk X ".$data["x"]." Z ".$data["z"]." requested", true, true, 2);
break;

View File

@ -35,7 +35,7 @@ define("MC_SERVER_HANDSHAKE", 0x10);
define("MC_CLIENT_HANDSHAKE", 0x13);
define("MC_CLIENT_DISCONNECT", 0x15);
define("MC_DISCONNECT", 0x15);
define("MC_LOGIN", 0x82);
define("MC_LOGIN_STATUS", 0x83);

View File

@ -33,7 +33,7 @@ $dataName = array(
MC_CLIENT_HANDSHAKE => "ClientHandshake",
MC_CLIENT_DISCONNECT => "ClientDisconnect",
MC_DISCONNECT => "Disconnect",
0x18 => "Unknown",