diff --git a/.travis.yml b/.travis.yml index 6c4cc803..c3cf74e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,10 +5,11 @@ php: before_script: - pecl install channel://pecl.php.net/pthreads-0.0.44 + - git clone --depth=100 --quiet --branch=tests git://github.com/PocketMine/PocketMine-MP.git $(pwd)/tests/ script: - - phpunit src/tests/ + - phpunit tests/ notifications: email: false - webhooks: http://n.tkte.ch/h/214/wsNvmG43-ncxUVRrFPwSM-r0 \ No newline at end of file + webhooks: http://n.tkte.ch/h/214/wsNvmG43-ncxUVRrFPwSM-r0 diff --git a/PocketMine-MP.php b/PocketMine-MP.php index bdf1cfd1..5d44186d 100644 --- a/PocketMine-MP.php +++ b/PocketMine-MP.php @@ -1,22 +1,28 @@ $owner, - "message" => $text, - ); + $message = ""; if($owner !== false){ if($owner instanceof Player){ - if($whitelist === false){ - console("[INFO] <".$owner->username."> ".$text); - } + $message = "<".$owner->username."> "; }else{ - if($whitelist === false){ - console("[INFO] <".$owner."> ".$text); - } + $message = "<".$owner."> "; } - }else{ - if($whitelist === false){ - console("[INFO] $text"); - } - $message["player"] = ""; } - + $message .= $text; + if($whitelist === false){ + console("[INFO] ".$message); + } $this->server->handle("server.chat", new Container($message, $whitelist, $blacklist)); } } \ No newline at end of file diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index e74a68da..1cb55d9f 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -1,22 +1,28 @@ server->schedule(2, array($this, "handle"), array(), true); - if(!defined("NO_THREADS")){ - $this->loop = new ConsoleLoop(); - } + $this->loop = new ConsoleLoop(); $this->register("help", "[page|command name]", array($this, "defaultCommands")); $this->register("status", "", array($this, "defaultCommands")); $this->register("difficulty", "<0|1|2|3>", array($this, "defaultCommands")); @@ -44,11 +48,9 @@ class ConsoleAPI{ function __destruct(){ $this->server->deleteEvent($this->event); - if(!defined("NO_THREADS")){ - $this->loop->stop(); - $this->loop->notify(); - //$this->loop->join(); - } + $this->loop->stop(); + $this->loop->notify(); + //$this->loop->join(); } public function defaultCommands($cmd, $params, $issuer, $alias){ @@ -252,9 +254,6 @@ class ConsoleAPI{ } public function handle($time){ - if(defined("NO_THREADS")){ - return; - } if($this->loop->line !== false){ $line = trim($this->loop->line); $this->loop->line = false; diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 5f317ad6..a1407c46 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -1,22 +1,28 @@ generateLevel($this->default, $this->server->seed); $this->loadLevel($this->default); } - $this->server->spawn = $this->getDefault()->getSafeSpawn(); + $this->server->spawn = $this->getDefault()->getSpawn(); } public function commandHandler($cmd, $params, $issuer, $alias){ diff --git a/src/API/MobAPI.php b/src/API/MobAPI.php index 305aceba..48c6ea2e 100644 --- a/src/API/MobAPI.php +++ b/src/API/MobAPI.php @@ -1,22 +1,28 @@ get($name); if($origin instanceof Player){ $name = $origin->username; - return $origin->teleport($lv->getSafeSpawn()); + return $origin->teleport($lv->getSpawn()); } }else{ return false; diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 552ca1a9..54d465ca 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -1,22 +1,28 @@ getProperty("auto-save") === true){ $this->server->schedule(18000, array($this, "autoSave"), array(), true); } - if(!defined("NO_THREADS") and $this->getProperty("enable-rcon") === true){ + if($this->getProperty("enable-rcon") === true){ $this->rcon = new RCON($this->getProperty("rcon.password", ""), $this->getProperty("rcon.port", $this->getProperty("server-port")), ($ip = $this->getProperty("server-ip")) != "" ? $ip:"0.0.0.0", $this->getProperty("rcon.threads", 1), $this->getProperty("rcon.clients-per-thread", 50)); } diff --git a/src/API/TileAPI.php b/src/API/TileAPI.php index c0313ae8..810263cd 100644 --- a/src/API/TileAPI.php +++ b/src/API/TileAPI.php @@ -1,22 +1,28 @@ chunkCount[$count]); } } - - if(is_array($this->lastChunk)){ - $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '".$this->level->getName()."' AND x >= ".($this->lastChunk[0] - 1)." AND x < ".($this->lastChunk[0] + 17)." AND z >= ".($this->lastChunk[1] - 1)." AND z < ".($this->lastChunk[1] + 17).";"); - $this->lastChunk = false; - if($tiles !== false and $tiles !== true){ - while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ - $tile = $this->server->api->tile->getByID($tile["ID"]); - if($tile instanceof Tile){ - $tile->spawn($this); - } - } - } - } $c = key($this->chunksOrder); $d = @$this->chunksOrder[$c]; @@ -206,8 +198,21 @@ class Player{ foreach($cnt as $i => $count){ $this->chunkCount[$count] = true; } + /*$this->chunkCount = $this->dataPacket(MC_CHUNK_DATA, array( + "x" => $X, + "z" => $Z, + "data" => $this->level->getOrderedMiniChunk($X, $Z, $Y), + ));*/ - $this->lastChunk = array($x, $z); + $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '".$this->level->getName()."' AND x >= ".($x - 1)." AND x < ".($x + 17)." AND z >= ".($z - 1)." AND z < ".($z + 17).";"); + if($tiles !== false and $tiles !== true){ + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ + $tile = $this->server->api->tile->getByID($tile["ID"]); + if($tile instanceof Tile){ + $tile->spawn($this); + } + } + } $this->server->schedule(MAX_CHUNK_RATE, array($this, "getNextChunk")); } @@ -457,6 +462,8 @@ class Player{ )); } } + }elseif($data->class === TILE_SIGN){ + $data->spawn($this); } } break; @@ -466,7 +473,7 @@ class Player{ if($w === $data["tile"]){ $this->dataPacket(MC_CONTAINER_SET_SLOT, array( "windowid" => $id, - "slot" => $data["slot"] + (isset($data["offset"]) ? $data["offset"]:0), + "slot" => $data["slot"], "block" => $data["slotdata"]->getID(), "stack" => $data["slotdata"]->count, "meta" => $data["slotdata"]->getMetadata(), @@ -555,17 +562,16 @@ class Player{ return; }else{ $message = $data->get(); - $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", $message["message"]), $message["player"]); //Remove ANSI codes from chat } }else{ $message = (string) $data; - $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", (string) $data)); //Remove ANSI codes from chat } + $this->sendChat(preg_replace('/\x1b\[[0-9;]*m/', "", $message)); //Remove ANSI codes from chat break; } } - public function sendChat($message, $author = ""){ + public function sendChat($message){ $mes = explode("\n", $message); foreach($mes as $m){ if(preg_match_all('#@([@A-Za-z_]{1,})#', $m, $matches, PREG_OFFSET_CAPTURE) > 0){ @@ -587,8 +593,7 @@ class Player{ } if($m !== ""){ - $this->dataPacket(MC_CHAT, array( - "player" => ($author instanceof Player) ? $author->username:$author, + $this->dataPacket(MC_CHAT, array( "message" => $m, )); } @@ -893,11 +898,8 @@ class Player{ public function getBandwidth(){ return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats)); } - + public function clearQueue(){ - if($this->connected === false){ - return false; - } ksort($this->received); if(($cnt = count($this->received)) > PLAYER_MAX_QUEUE){ foreach($this->received as $c => $t){ @@ -1181,7 +1183,7 @@ class Player{ "x" => $this->data->get("position")["x"], "y" => $this->data->get("position")["y"], "z" => $this->data->get("position")["z"], - "generator" => 0, + "unknown1" => 0, "gamemode" => ($this->gamemode & 0x01), "eid" => 0, )); @@ -1625,14 +1627,29 @@ class Player{ $this->entity->updateMetadata(); } break; + case MC_SIGN_UPDATE: + if($this->spawned === false or $this->blocked === true){ + break; + } + $this->craftingItems = array(); + $this->toCraft = array(); + $t = $this->server->api->tile->get(new Position($data["x"], $data["y"], $data["z"], $this->level)); + if(($t instanceof Tile) and $t->class === TILE_SIGN){ + if($t->data["creator"] !== $this->username){ + $t->spawn($this); + }else{ + $t->setText($data["line0"], $data["line1"], $data["line2"], $data["line3"]); + } + } + break; case MC_CHAT: if($this->spawned === false){ break; } $this->craftingItems = array(); $this->toCraft = array(); - if(trim($data["message"]) != "" and strlen($data["message"]) <= 255 and preg_match('#[^\\x20-\\xff]#', $data["message"]) == 0){ - $message = $data["message"]; + $message = preg_replace('#^<.*> #', "", $data["message"]); + if(trim($data["message"]) != "" and strlen($data["message"]) <= 100 and preg_match('#[^\\x20-\\xff]#', $message) == 0){ if($message{0} === "/"){ //Command $this->server->api->console->run(substr($message, 1), $this); }else{ @@ -1648,27 +1665,14 @@ class Player{ } $this->craftingItems = array(); $this->toCraft = array(); - if(isset($this->windows[$data["windowid"]])){ - if(is_array($this->windows[$data["windowid"]])){ - $all = $this->server->api->player->getAll($this->level); - foreach($this->windows[$data["windowid"]] as $ob){ - $this->server->api->player->broadcastPacket($all, MC_TILE_EVENT, array( - "x" => $ob->x, - "y" => $ob->y, - "z" => $ob->z, - "case1" => 1, - "case2" => 0, - )); - } - }elseif($this->windows[$data["windowid"]]->class === TILE_CHEST){ - $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), MC_TILE_EVENT, array( - "x" => $this->windows[$data["windowid"]]->x, - "y" => $this->windows[$data["windowid"]]->y, - "z" => $this->windows[$data["windowid"]]->z, - "case1" => 1, - "case2" => 0, - )); - } + if(isset($this->windows[$data["windowid"]]) and $this->windows[$data["windowid"]]->class === TILE_CHEST){ + $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), MC_TILE_EVENT, array( + "x" => $this->windows[$data["windowid"]]->x, + "y" => $this->windows[$data["windowid"]]->y, + "z" => $this->windows[$data["windowid"]]->z, + "case1" => 1, + "case2" => 0, + )); } unset($this->windows[$data["windowid"]]); @@ -1735,123 +1739,52 @@ class Player{ if(!isset($this->windows[$data["windowid"]])){ break; } - if(is_array($this->windows[$data["windowid"]])){ - $tiles = $this->windows[$data["windowid"]]; - if($data["slot"] > 0 and $data["slot"] < CHEST_SLOTS){ - $tile = $tiles[0]; - $slotn = $data["slot"]; - $offset = 0; - }elseif($data["slot"] >= CHEST_SLOTS and $data["slot"] <= (CHEST_SLOTS << 1)){ - $tile = $tiles[1]; - $slotn = $data["slot"] - CHEST_SLOTS; - $offset = CHEST_SLOTS; - }else{ - break; - } - - $item = BlockAPI::getItem($data["block"], $data["meta"], $data["stack"]); - - $slot = $tile->getSlot($slotn); - if($this->server->api->dhandle("player.container.slot", array( - "tile" => $tile, - "slot" => $data["slot"], - "offset" => $offset, - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this, - )) === false){ - $this->dataPacket(MC_CONTAINER_SET_SLOT, array( - "windowid" => $data["windowid"], - "slot" => $data["slot"], - "block" => $slot->getID(), - "stack" => $slot->count, - "meta" => $slot->getMetadata(), - )); - break; - } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ - break; - } - }elseif($slot->count > $item->count){ - $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); - } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ - break; - } - $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); - } - $tile->setSlot($slotn, $item, true, $offset); - }else{ - $tile = $this->windows[$data["windowid"]]; - if(($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $data["slot"] < 0 or ($tile->class === TILE_CHEST and $data["slot"] >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $data["slot"] >= FURNACE_SLOTS)){ - break; - } - $item = BlockAPI::getItem($data["block"], $data["meta"], $data["stack"]); - - $slot = $tile->getSlot($data["slot"]); - if($this->server->api->dhandle("player.container.slot", array( - "tile" => $tile, - "slot" => $data["slot"], - "slotdata" => $slot, - "itemdata" => $item, - "player" => $this, - )) === false){ - $this->dataPacket(MC_CONTAINER_SET_SLOT, array( - "windowid" => $data["windowid"], - "slot" => $data["slot"], - "block" => $slot->getID(), - "stack" => $slot->count, - "meta" => $slot->getMetadata(), - )); - break; - } - if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ - break; - } - }elseif($slot->count > $item->count){ - $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); - } - }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ - break; - } - $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); - } - $tile->setSlot($data["slot"], $item); + $tile = $this->windows[$data["windowid"]]; + if(($tile->class !== TILE_CHEST and $tile->class !== TILE_FURNACE) or $data["slot"] < 0 or ($tile->class === TILE_CHEST and $data["slot"] >= CHEST_SLOTS) or ($tile->class === TILE_FURNACE and $data["slot"] >= FURNACE_SLOTS)){ + break; } + $done = false; + $item = BlockAPI::getItem($data["block"], $data["meta"], $data["stack"]); + + $slot = $tile->getSlot($data["slot"]); + $done = true; + if($this->server->api->dhandle("player.container.slot", array( + "tile" => $tile, + "slot" => $data["slot"], + "slotdata" => $slot, + "itemdata" => $item, + "player" => $this, + )) === false){ + $this->dataPacket(MC_CONTAINER_SET_SLOT, array( + "windowid" => $data["windowid"], + "slot" => $data["slot"], + "block" => $slot->getID(), + "stack" => $slot->count, + "meta" => $slot->getMetadata(), + )); + break; + } + if($item->getID() !== AIR and $slot->getID() == $item->getID()){ + if($slot->count < $item->count){ + if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + break; + } + }elseif($slot->count > $item->count){ + $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); + } + }else{ + if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + break; + } + $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); + } + $tile->setSlot($data["slot"], $item); break; case MC_SEND_INVENTORY: //TODO, Mojang, enable this ยด^_^` if($this->spawned === false){ break; } break; - case MC_ENTITY_DATA: - if($this->spawned === false or $this->blocked === true){ - break; - } - $this->craftingItems = array(); - $this->toCraft = array(); - $t = $this->server->api->tile->get(new Position($data["x"], $data["y"], $data["z"], $this->level)); - if(($t instanceof Tile) and $t->class === TILE_SIGN){ - if($t->data["creator"] !== $this->username){ - $t->spawn($this); - }else{ - $nbt = new NBT(); - $nbt->load($data["namedtag"]); - $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ - $t->spawn($this); - }else{ - $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); - } - } - } - break; default: console("[DEBUG] Unhandled 0x".dechex($pid)." Data Packet for Client ID ".$this->clientID.": ".print_r($data, true), true, true, 2); break; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index a2919498..981f592e 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -1,22 +1,28 @@ gamemode & 0x01) === 0x01){ + if($furnace->class !== TILE_FURNACE or ($player->gamemode & 0x01) === 0x01){ return true; } - - $furnace->openInventory($player); + $player->windowCnt++; + $player->windowCnt = $id = max(2, $player->windowCnt % 16); + $player->windows[$id] = $furnace; + $player->dataPacket(MC_CONTAINER_OPEN, array( + "windowid" => $id, + "type" => WINDOW_FURNACE, + "slots" => FURNACE_SLOTS, + "title" => "Furnace", + )); + $slots = array(); + for($s = 0; $s < FURNACE_SLOTS; ++$s){ + $slot = $furnace->getSlot($s); + if($slot->getID() > 0 and $slot->count > 0){ + $slots[] = $slot; + }else{ + $slots[] = BlockAPI::getItem(AIR, 0, 0); + } + } + $player->dataPacket(MC_CONTAINER_SET_CONTENT, array( + "windowid" => $id, + "count" => count($slots), + "slots" => $slots + )); return true; } diff --git a/src/material/block/solid/Chest.php b/src/material/block/solid/Chest.php index ff725304..f5603373 100644 --- a/src/material/block/solid/Chest.php +++ b/src/material/block/solid/Chest.php @@ -1,22 +1,28 @@ isActivable = true; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $server = ServerAPI::request(); + $faces = array( 0 => 4, 1 => 2, 2 => 5, 3 => 3, ); - + $facesc = array( + 2 => 4, + 3 => 2, + 4 => 5, + 5 => 3, + ); $chest = false; - $this->meta = $faces[$player->entity->getDirection()]; - for($side = 2; $side <= 5; ++$side){ - if(($this->meta === 4 or $this->meta === 5) and ($side === 4 or $side === 5)){ - continue; - }elseif(($this->meta === 3 or $this->meta === 2) and ($side === 2 or $side === 3)){ - continue; - } $c = $this->getSide($side); - if(($c instanceof ChestBlock) and $c->getMetadata() === $this->meta){ - if((($tile = $server->api->tile->get($c)) instanceof Tile) and !$tile->isPaired()){ - $chest = $tile; - break; - } + if($c instanceof ChestBlock){ + /*if($chest !== false){ //No chests in the middle + return false; + }*/ + $chest = array($side, $c); + break; } } - + + if($chest !== false and ($chest[1]->getSide($chest[0]) instanceof ChestBlock)){ //Already double chest + return false; + } + + if($chest !== false){ + $this->meta = $facesc[$chest[0]]; + $this->level->setBlock($chest[1], new ChestBlock($this->meta)); + }else{ + $this->meta = $faces[$player->entity->getDirection()]; + } $this->level->setBlock($block, $this); - $tile = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( + $server = ServerAPI::request(); + $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( "Items" => array(), "id" => TILE_CHEST, "x" => $this->x, "y" => $this->y, "z" => $this->z )); - - /* 0.7.4 bug, waiting for Mojang... - if($chest instanceof Tile){ - $chest->pairWith($tile); - $tile->pairWith($chest); - }*/ return true; } public function onBreak(Item $item, Player $player){ - $t = ServerAPI::request()->api->tile->get($this); - if($t !== false){ - $t->unpair(); - } - $this->level->setBlock($this, new AirBlock(), true, true); - return true; + $this->level->setBlock($this, new AirBlock(), true, true); + return true; } public function onActivate(Item $item, Player $player){ @@ -98,13 +104,40 @@ class ChestBlock extends TransparentBlock{ )); } - - - if(($player->gamemode & 0x01) === 0x01){ + if($chest->class !== TILE_CHEST or ($player->gamemode & 0x01) === 0x01){ return true; } + $player->windowCnt++; + $player->windowCnt = $id = max(2, $player->windowCnt % 16); + $player->windows[$id] = $chest; + $player->dataPacket(MC_CONTAINER_OPEN, array( + "windowid" => $id, + "type" => WINDOW_CHEST, + "slots" => CHEST_SLOTS, + "title" => "Chest", + )); + $server->api->player->broadcastPacket($server->api->player->getAll($this->level), MC_TILE_EVENT, array( + "x" => $this->x, + "y" => $this->y, + "z" => $this->z, + "case1" => 1, + "case2" => 2, + )); + $slots = array(); + for($s = 0; $s < CHEST_SLOTS; ++$s){ + $slot = $chest->getSlot($s); + if($slot->getID() > AIR and $slot->count > 0){ + $slots[] = $slot; + }else{ + $slots[] = BlockAPI::getItem(AIR, 0, 0); + } + } + $player->dataPacket(MC_CONTAINER_SET_CONTENT, array( + "windowid" => $id, + "count" => count($slots), + "slots" => $slots + )); - $chest->openInventory($player); return true; } diff --git a/src/material/block/solid/Clay.php b/src/material/block/solid/Clay.php index 425f649f..1e8a336b 100644 --- a/src/material/block/solid/Clay.php +++ b/src/material/block/solid/Clay.php @@ -1,22 +1,28 @@ useOn($block) and ($player->gamemode & 0x01) === 0 and $this->getMetadata() >= $this->getMaxDurability()){ - $player->setSlot($player->slot, new Item(AIR, 0, 0), false); - } - - if($block->getID() === AIR and ($target instanceof SolidBlock)){ + if($block->getID() === AIR){ $level->setBlock($block, new FireBlock(), true, false, true); $block->level->scheduleBlockUpdate(new Position($block, 0, 0, $block->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); return true; } + if($this->useOn($block) and ($player->gamemode & 0x01) === 0 and $this->getMetadata() >= $this->getMaxDurability()){ + $player->setSlot($player->slot, new Item(AIR, 0, 0), false); + } return false; } } \ No newline at end of file diff --git a/src/material/item/tool/IronAxe.php b/src/material/item/tool/IronAxe.php index 573097d6..e3692c56 100644 --- a/src/material/item/tool/IronAxe.php +++ b/src/material/item/tool/IronAxe.php @@ -1,22 +1,28 @@ c === false){ - $this->data["player"] = $this->get(Utils::readShort($this->get(2), false)); $this->data["message"] = $this->get(Utils::readShort($this->get(2), false)); }else{ - $this->raw .= Utils::writeShort(strlen($this->data["player"])).$this->data["player"]; $this->raw .= Utils::writeShort(strlen($this->data["message"])).$this->data["message"]; } break; @@ -196,7 +200,7 @@ class CustomPacketHandler{ case MC_START_GAME: if($this->c === false){ $this->data["seed"] = Utils::readInt($this->get(4)); - $this->data["generator"] = Utils::readInt($this->get(4)); + $this->data["unknown1"] = Utils::readInt($this->get(4)); $this->data["gamemode"] = Utils::readInt($this->get(4)); $this->data["eid"] = Utils::readInt($this->get(4)); $this->data["x"] = Utils::readFloat($this->get(4)); @@ -204,7 +208,7 @@ class CustomPacketHandler{ $this->data["z"] = Utils::readFloat($this->get(4)); }else{ $this->raw .= Utils::writeInt($this->data["seed"]); - $this->raw .= Utils::writeInt($this->data["generator"]); + $this->raw .= Utils::writeInt($this->data["unknown1"]); $this->raw .= Utils::writeInt($this->data["gamemode"]); $this->raw .= Utils::writeInt($this->data["eid"]); $this->raw .= Utils::writeFloat($this->data["x"]); @@ -739,12 +743,12 @@ class CustomPacketHandler{ if($this->c === false){ $this->data["windowid"] = ord($this->get(1)); $this->data["type"] = ord($this->get(1)); - $this->data["slots"] = ord($this->get(1)); + $this->data["slots"] = Utils::readShort($this->get(2), false); $this->data["title"] = $this->get(Utils::readShort($this->get(2), false)); }else{ $this->raw .= chr($this->data["windowid"]); $this->raw .= chr($this->data["type"]); - $this->raw .= chr($this->data["slots"]); + $this->raw .= Utils::writeShort($this->data["slots"]); $this->raw .= Utils::writeShort(strlen($this->data["title"])).$this->data["title"]; } break; @@ -804,6 +808,23 @@ class CustomPacketHandler{ $this->raw .= Utils::writeShort(strlen($this->data["message"])).$this->data["message"]; } break; + case MC_SIGN_UPDATE: + if($this->c === false){ + $this->data["x"] = Utils::readShort($this->get(2)); + $this->data["y"] = ord($this->get(1)); + $this->data["z"] = Utils::readShort($this->get(2)); + for($i = 0; $i < 4; ++$i){ + $this->data["line$i"] = $this->get(Utils::readLShort($this->get(2), false)); + } + }else{ + $this->raw .= Utils::writeShort($this->data["x"]); + $this->raw .= chr($this->data["y"]); + $this->raw .= Utils::writeShort($this->data["z"]); + for($i = 0; $i < 4; ++$i){ + $this->raw .= Utils::writeLShort(strlen($this->data["line$i"])).$this->data["line$i"]; + } + } + break; case MC_ADVENTURE_SETTINGS: if($this->c === false){ $this->data["flags"] = Utils::readInt($this->get(4)); @@ -811,19 +832,6 @@ class CustomPacketHandler{ $this->raw .= Utils::writeInt($this->data["flags"]); } break; - case MC_ENTITY_DATA: - if($this->c === false){ - $this->data["x"] = Utils::readShort($this->get(2)); - $this->data["y"] = ord($this->get(1)); - $this->data["z"] = Utils::readShort($this->get(2)); - $this->data["namedtag"] = $this->get(true); - }else{ - $this->raw .= Utils::writeShort($this->data["x"]); - $this->raw .= chr($this->data["y"]); - $this->raw .= Utils::writeShort($this->data["z"]); - $this->raw .= $this->data["namedtag"]; - } - break; default: if($this->c === false){ console("[DEBUG] Received unknown Data Packet ID 0x".dechex($pid), true, true, 2); diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index ae582dd3..64bcae6c 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -1,22 +1,28 @@ "Set Container Slot", MC_CLIENT_MESSAGE => "Client Message", + MC_SIGN_UPDATE => "Sign Update", MC_ADVENTURE_SETTINGS => "Adventure Settings", - MC_ENTITY_DATA => "Entity Data", ); public static $packetName = array( diff --git a/src/network/Query.php b/src/network/Query.php index 3ec2e522..532e9dab 100644 --- a/src/network/Query.php +++ b/src/network/Query.php @@ -1,22 +1,28 @@ assertTrue(true); - $server = ServerAPI::request(); - - - //Everything done! - $server->close(); - } - - public function testRead(){ - define("NO_THREADS", true); - require_once(dirname(__FILE__)."/../dependencies.php"); - require_once(FILE_PATH."/src/functions.php"); - require_once(FILE_PATH."/src/dependencies.php"); - - //binary things - $this->assertTrue(Utils::readTriad("\x02\x01\x03") === 131331, "Utils::readTriad"); - $this->assertTrue(Utils::readInt("\xff\x02\x01\x03") === -16645885, "Utils::readInt"); - $this->assertTrue(abs(Utils::readFloat("\x49\x02\x01\x03") - 532496.1875) < 0.0001, "Utils::readFloat"); - $this->assertTrue(abs(Utils::readDouble("\x41\x02\x03\x04\x05\x06\x07\x08") - 147552.5024529) < 0.0001, "Utils::readDouble"); - $this->assertTrue(Utils::readLong("\x41\x02\x03\x04\x05\x06\x07\x08") === "4684309878217770760", "Utils::readLong"); - - //PocketMine-MP server startup - $this->server = new ServerAPI(); - $this->server->load(); - $this->assertTrue(is_integer($this->server->event("server.start", array($this, "hook")))); - $this->server->init(); - exit(0); - } - } - -} \ No newline at end of file diff --git a/src/tests/phpunit.xml b/src/tests/phpunit.xml deleted file mode 100644 index 2fa40761..00000000 --- a/src/tests/phpunit.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - ./ - - \ No newline at end of file diff --git a/src/utils/Config.php b/src/utils/Config.php index 31babceb..27541f91 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -1,22 +1,28 @@ + * @version 1.0 + * MODIFIED BY @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. - * - * @author PocketMine Team - * @link http://www.pocketmine.net/ - * - * -*/ + * Dependencies: + * PHP 4.3+ (5.3+ recommended) + */ -class NBT{ - public $tree = array(); - public $binary = b""; - private $offset = 0; +class NBT { + public $root = array(); const TAG_END = 0; const TAG_BYTE = 1; @@ -35,173 +24,79 @@ class NBT{ const TAG_STRING = 8; const TAG_LIST = 9; const TAG_COMPOUND = 10; - - public function read($n){ - if($n <= 0){ - return ""; + + public function loadFile($filename) { + if(is_file($filename)) { + $fp = fopen($filename, "rb"); + }else{ + trigger_error("First parameter must be a filename", E_USER_WARNING); + return false; } - $this->offset += $n; - return substr($this->binary, $this->offset - $n, $n); - } - - public function write($bin){ - $this->binary .= $bin; - } - - public function load($str){ - $this->offset = 0; - $this->binary = (string) $str; - $this->parseTree($this->tree); - } - - public function readTAG_BYTE(){ - return Utils::readByte($this->read(1)); - } - - public function readTAG_SHORT(){ - return Utils::readLShort($this->read(2)); - } - - public function readTAG_INT(){ - return Utils::readLInt($this->read(4)); - } - - public function readTAG_LONG(){ - return Utils::readLLong($this->read(8)); - } - - public function readTAG_FLOAT(){ - return Utils::readLFloat($this->read(4)); - } - - public function readTAG_DOUBLE(){ - return Utils::readLDouble($this->read(8)); - } - - public function readTAG_BYTE_ARRAY(){ - return $this->read($this->readTAG_INT()); - } - - public function readTAG_STRING(){ - return $this->read(Utils::readLShort($this->read(2), false)); - } - - public function writeTAG_BYTE($v){ - $this->binary .= chr($v); - } - - public function writeTAG_SHORT($v){ - $this->binary .= Utils::writeLShort($v); - } - - public function writeTAG_INT($v){ - $this->binary .= Utils::writeLInt($v); - } - - public function writeTAG_LONG($v){ - $this->binary .= Utils::writeLLong($v); - } - - public function writeTAG_FLOAT($v){ - $this->binary .= Utils::writeLFloar($v); - } - - public function writeTAG_DOUBLE($v){ - $this->binary .= Utils::writeLDouble($v); - } - - public function writeTAG_BYTE_ARRAY($v){ - $this->binary .= $this->writeTAG_INT(strlen($v)).$v; - } - - public function writeTAG_STRING($v){ - $this->binary .= $this->writeTAG_SHORT(strlen($v)).$v; + switch(basename(strtolower($filename), ".dat")){ + case "level": + $version = Utils::readLInt(fread($fp, 4)); + $lenght = Utils::readLInt(fread($fp, 4)); + break; + case "entities": + fread($fp, 12); + break; + } + $this->traverseTag($fp, $this->root); + return end($this->root); } - private function parseList(&$node, $tag, $cnt){ - for($i = 0; $i < $cnt; ++$i){ - switch($tag){ - case self::TAG_BYTE: - $value = $this->readTAG_BYTE(); - break; - case self::TAG_SHORT: - $value = $this->readTAG_SHORT(); - break; - case self::TAG_INT: - $value = $this->readTAG_INT(); - break; - case self::TAG_LONG: - $value = $this->readTAG_LONG(); - break; - case self::TAG_FLOAT: - $value = $this->readTAG_FLOAT(); - break; - case self::TAG_DOUBLE: - $value = $this->readTAG_DOUBLE(); - break; - case self::TAG_BYTE_ARRAY: - $value = $this->readTAG_BYTE_ARRAY(); - break; - case self::TAG_STRING: - $value = $this->readTAG_STRING(); - break; - case self::TAG_LIST: - $value = array(); - $this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4))); - break; - case self::TAG_COMPOUND: - $value = array(); - $this->parseTree($value); - break; - default: - die("Invalid NBT Tag $tag"); - break; - } - $node[] = $value; + public function traverseTag($fp, &$tree) { + if(feof($fp)) { + return false; + } + $tagType = $this->readType($fp, self::TAG_BYTE); // Read type byte. + if($tagType == self::TAG_END) { + return false; + } else { + $tagName = $this->readType($fp, self::TAG_STRING); + $tagData = $this->readType($fp, $tagType); + $tree[] = array("type"=>$tagType, "name"=>$tagName, "value"=>$tagData); + return true; } } - - private function parseTree(&$node){ - while(($tag = ord($this->read(1))) !== self::TAG_END){ - $name = $this->readTAG_STRING(); - switch($tag){ - case self::TAG_BYTE: - $value = $this->readTAG_BYTE(); - break; - case self::TAG_SHORT: - $value = $this->readTAG_SHORT(); - break; - case self::TAG_INT: - $value = $this->readTAG_INT(); - break; - case self::TAG_LONG: - $value = $this->readTAG_LONG(); - break; - case self::TAG_FLOAT: - $value = $this->readTAG_FLOAT(); - break; - case self::TAG_DOUBLE: - $value = $this->readTAG_DOUBLE(); - break; - case self::TAG_BYTE_ARRAY: - $value = $this->readTAG_BYTE_ARRAY(); - break; - case self::TAG_STRING: - $value = $this->readTAG_STRING(); - break; - case self::TAG_LIST: - $value = array(); - $this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4))); - break; - case self::TAG_COMPOUND: - $value = array(); - $this->parseTree($value); - break; - default: - die("Invalid NBT Tag $tag"); - break; - } - $node[$name] = $value; + + public function readType($fp, $tagType) { + switch($tagType) { + case self::TAG_BYTE: // Signed byte (8 bit) + return Utils::readByte(fread($fp, 1)); + case self::TAG_SHORT: // Signed short (16 bit, big endian) + return Utils::readLShort(fread($fp, 2)); + case self::TAG_INT: // Signed integer (32 bit, big endian) + return Utils::readLInt(fread($fp, 4)); + case self::TAG_LONG: // Signed long (64 bit, big endian) + return Utils::readLLong(fread($fp, 8)); + case self::TAG_FLOAT: // Floating point value (32 bit, big endian, IEEE 754-2008) + return Utils::readLFloat(fread($fp, 4)); + case self::TAG_DOUBLE: // Double value (64 bit, big endian, IEEE 754-2008) + return Utils::readLDouble(fread($fp, 8)); + case self::TAG_BYTE_ARRAY: // Byte array + $arrayLength = $this->readType($fp, self::TAG_INT); + $array = array(); + for($i = 0; $i < $arrayLength; $i++) $array[] = $this->readType($fp, self::TAG_BYTE); + return $array; + case self::TAG_STRING: // String + if(!$stringLength = $this->readType($fp, self::TAG_SHORT)) return ""; + $string = fread($fp, $stringLength); // Read in number of bytes specified by string length, and decode from utf8. + return $string; + case self::TAG_LIST: // List + $tagID = $this->readType($fp, self::TAG_BYTE); + $listLength = $this->readType($fp, self::TAG_INT); + $list = array("type"=>$tagID, "value"=>array()); + for($i = 0; $i < $listLength; $i++) { + if(feof($fp)) break; + $list["value"][] = $this->readType($fp, $tagID); + } + return $list; + case self::TAG_COMPOUND: // Compound + $tree = array(); + while($this->traverseTag($fp, $tree)); + return $tree; } } -} \ No newline at end of file +} +?> \ No newline at end of file diff --git a/src/utils/Random.php b/src/utils/Random.php index 7e2e7ea6..8dde506d 100644 --- a/src/utils/Random.php +++ b/src/utils/Random.php @@ -1,22 +1,28 @@ level->getData("spawnX"), $this->level->getData("spawnY"), $this->level->getData("spawnZ"), $this); } - public function getSafeSpawn(){ - if(($spawn = $this->getSpawn()) !== false){ - $x = (int) round($spawn->x); - $y = (int) round($spawn->y); - $z = (int) round($spawn->z); - for(; $y < 128; ++$y){ - $v = new Vector3($x, $y, $z); - if($this->getBlock($v->getSide(1)) instanceof AirBlock){ - if($this->getBlock($v) instanceof AirBlock){ - return new Position($x, $y, $z, $this); - } - }else{ - ++$y; - } - } - return new Position($x, $y, $z, $this); - } - return false; - } - public function setSpawn(Vector3 $pos){ if(!isset($this->level)){ return false; diff --git a/src/world/LevelImport.php b/src/world/LevelImport.php index 39676d2f..69aed2d9 100644 --- a/src/world/LevelImport.php +++ b/src/world/LevelImport.php @@ -1,22 +1,28 @@ save(); }elseif(file_exists($this->path."chunks.dat") and file_exists($this->path."level.dat")){ //Pocket $nbt = new NBT(); - $nbt->load(substr(file_get_contents($this->path."level.dat"), 8)); - $level = array_shift($nbt->tree); + $level = parseNBTData($nbt->loadFile($this->path."level.dat")); if($level["LevelName"] == ""){ $level["LevelName"] = "world".time(); } console("[INFO] Importing Pocket level \"".$level["LevelName"]."\" to PMF format"); unset($level["Player"]); - $nbt->load(substr(file_get_contents($this->path."entities.dat"), 12)); - $entities = array_shift($nbt->tree); + $entities = parseNBTData($nbt->loadFile($this->path."entities.dat")); if(!isset($entities["TileEntities"])){ $entities["TileEntities"] = array(); } diff --git a/src/world/PocketChunkParser.php b/src/world/PocketChunkParser.php index 589ece0e..70250b8f 100644 --- a/src/world/PocketChunkParser.php +++ b/src/world/PocketChunkParser.php @@ -1,22 +1,28 @@ z = (int) $z; $this->server->query("INSERT OR REPLACE INTO tiles (ID, level, class, x, y, z) VALUES (".$this->id.", '".$this->level->getName()."', '".$this->class."', ".$this->x.", ".$this->y.", ".$this->z.");"); switch($this->class){ - case TILE_CHEST: case TILE_SIGN: $this->server->query("UPDATE tiles SET spawnable = 1 WHERE ID = ".$this->id.";"); break; @@ -72,149 +77,6 @@ class Tile extends Position{ break; } } - - public function isPaired(){ - if($this->class !== TILE_CHEST){ - return false; - } - if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])){ - return false; - } - return true; - } - - public function getPair(){ - if($this->isPaired()){ - return $this->server->api->tile->get(new Position((int) $this->data["pairx"], $this->y, (int) $this->data["pairz"], $this->level)); - } - return false; - } - - public function pairWith(Tile $tile){ - if($this->isPaired()or $tile->isPaired()){ - return false; - } - - $this->data["pairx"] = $tile->x; - $this->data["pairz"] = $tile->z; - - $tile->data["pairx"] = $this->x; - $tile->data["pairz"] = $this->z; - - $this->server->api->tile->spawnToAll($this); - $this->server->api->tile->spawnToAll($tile); - } - - public function unpair(){ - if(!$this->isPaired()){ - return false; - } - - $tile = $this->getPair(); - unset($this->data["pairx"], $this->data["pairz"], $tile->data["pairx"], $tile->data["pairz"]); - - $this->server->api->tile->spawnToAll($this); - if($tile instanceof Tile){ - $this->server->api->tile->spawnToAll($tile); - } - } - - public function openInventory(Player $player){ - if($this->class === TILE_CHEST){ - $player->windowCnt++; - $player->windowCnt = $id = max(2, $player->windowCnt % 99); - if(($pair = $this->getPair()) !== false){ - if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))){ //Order them correctly - $player->windows[$id] = array( - $pair, - $this - ); - }else{ - $player->windows[$id] = array( - $this, - $pair - ); - } - }else{ - $player->windows[$id] = $this; - } - $player->dataPacket(MC_CONTAINER_OPEN, array( - "windowid" => $id, - "type" => WINDOW_CHEST, - "slots" => is_array($player->windows[$id]) ? CHEST_SLOTS << 1:CHEST_SLOTS, - "title" => "Chest", - )); - $slots = array(); - - if(is_array($player->windows[$id])){ - $all = $this->server->api->player->getAll($this->level); - foreach($player->windows[$id] as $ob){ - $this->server->api->player->broadcastPacket($all, MC_TILE_EVENT, array( - "x" => $ob->x, - "y" => $ob->y, - "z" => $ob->z, - "case1" => 1, - "case2" => 2, - )); - for($s = 0; $s < CHEST_SLOTS; ++$s){ - $slot = $ob->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $slots[] = $slot; - }else{ - $slots[] = BlockAPI::getItem(AIR, 0, 0); - } - } - } - }else{ - $this->server->api->player->broadcastPacket($this->server->api->player->getAll($this->level), MC_TILE_EVENT, array( - "x" => $this->x, - "y" => $this->y, - "z" => $this->z, - "case1" => 1, - "case2" => 2, - )); - for($s = 0; $s < CHEST_SLOTS; ++$s){ - $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $slots[] = $slot; - }else{ - $slots[] = BlockAPI::getItem(AIR, 0, 0); - } - } - } - $player->dataPacket(MC_CONTAINER_SET_CONTENT, array( - "windowid" => $id, - "count" => count($slots), - "slots" => $slots - )); - return true; - }elseif($this->class === TILE_FURNACE){ - $player->windowCnt++; - $player->windowCnt = $id = max(2, $player->windowCnt % 99); - $player->windows[$id] = $this; - $player->dataPacket(MC_CONTAINER_OPEN, array( - "windowid" => $id, - "type" => WINDOW_FURNACE, - "slots" => FURNACE_SLOTS, - "title" => "Furnace", - )); - $slots = array(); - for($s = 0; $s < FURNACE_SLOTS; ++$s){ - $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $slots[] = $slot; - }else{ - $slots[] = BlockAPI::getItem(AIR, 0, 0); - } - } - $player->dataPacket(MC_CONTAINER_SET_CONTENT, array( - "windowid" => $id, - "count" => count($slots), - "slots" => $slots - )); - return true; - } - } public function update(){ if($this->closed === true){ @@ -303,7 +165,7 @@ class Tile extends Position{ } } - public function setSlot($s, Item $item, $update = true, $offset = 0){ + public function setSlot($s, Item $item, $update = true){ $i = $this->getSlotIndex($s); $d = array( "Count" => $item->count, @@ -325,7 +187,6 @@ class Tile extends Position{ $this->server->api->dhandle("tile.container.slot", array( "tile" => $this, "slot" => $s, - "offset" => $offset, "slotdata" => $item, )); @@ -343,88 +204,16 @@ class Tile extends Position{ $player = $this->server->api->player->get($player); } switch($this->class){ - case TILE_CHEST: - $nbt = new NBT(); - $nbt->write(chr(NBT::TAG_COMPOUND)."\x00\x00"); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("id"); - $nbt->writeTAG_String($this->class); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("x"); - $nbt->writeTAG_Int((int) $this->x); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("y"); - $nbt->writeTAG_Int((int) $this->y); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("z"); - $nbt->writeTAG_Int((int) $this->z); - - if($this->isPaired()){ - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("pairx"); - $nbt->writeTAG_Int((int) $this->data["pairx"]); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("pairz"); - $nbt->writeTAG_Int((int) $this->data["pairz"]); - } - - $nbt->write(chr(NBT::TAG_END)); - - $player->dataPacket(MC_ENTITY_DATA, array( - "x" => $this->x, - "y" => $this->y, - "z" => $this->z, - "namedtag" => $nbt->binary, - )); - break; case TILE_SIGN: - $nbt = new NBT(); - $nbt->write(chr(NBT::TAG_COMPOUND)."\x00\x00"); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text1"); - $nbt->writeTAG_String($this->data["Text1"]); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text2"); - $nbt->writeTAG_String($this->data["Text2"]); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text3"); - $nbt->writeTAG_String($this->data["Text3"]); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text4"); - $nbt->writeTAG_String($this->data["Text4"]); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("id"); - $nbt->writeTAG_String($this->class); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("x"); - $nbt->writeTAG_Int((int) $this->x); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("y"); - $nbt->writeTAG_Int((int) $this->y); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("z"); - $nbt->writeTAG_Int((int) $this->z); - - $nbt->write(chr(NBT::TAG_END)); - - $player->dataPacket(MC_ENTITY_DATA, array( + $player->dataPacket(MC_SIGN_UPDATE, array( + "level" => $this->level, "x" => $this->x, "y" => $this->y, "z" => $this->z, - "namedtag" => $nbt->binary, + "line0" => $this->data["Text1"], + "line1" => $this->data["Text2"], + "line2" => $this->data["Text3"], + "line3" => $this->data["Text4"], )); break; } diff --git a/src/world/generator/LevelGenerator.php b/src/world/generator/LevelGenerator.php index 5da98645..821d3e83 100644 --- a/src/world/generator/LevelGenerator.php +++ b/src/world/generator/LevelGenerator.php @@ -1,22 +1,28 @@