More changes! Alsp added Anvil block and BinaryStream

This commit is contained in:
Shoghi Cervantes
2015-08-03 18:04:13 +02:00
parent 522932d7c0
commit 7fd053fb09
13 changed files with 422 additions and 202 deletions

View File

@ -28,6 +28,7 @@ use pocketmine\inventory\FurnaceRecipe;
use pocketmine\inventory\ShapedRecipe;
use pocketmine\inventory\ShapelessRecipe;
use pocketmine\utils\Binary;
use pocketmine\utils\BinaryStream;
class CraftingDataPacket extends DataPacket{
const NETWORK_ID = Info::CRAFTING_DATA_PACKET;
@ -42,47 +43,53 @@ class CraftingDataPacket extends DataPacket{
public $entries = [];
public $cleanRecipes = false;
public function writeEntry($entry){
private static function writeEntry($entry, BinaryStream $stream){
if($entry instanceof ShapelessRecipe){
$this->writeShapelessRecipe($entry);
return self::writeShapelessRecipe($entry, $stream);
}elseif($entry instanceof ShapedRecipe){
$this->writeShapedRecipe($entry);
return self::writeShapedRecipe($entry, $stream);
}elseif($entry instanceof FurnaceRecipe){
$this->writeFurnaceRecipe($entry);
return self::writeFurnaceRecipe($entry, $stream);
}
return -1;
}
private function writeShapelessRecipe(ShapelessRecipe $recipe){
$this->putInt(CraftingDataPacket::ENTRY_SHAPELESS);
$this->putInt($recipe->getIngredientCount());
private static function writeShapelessRecipe(ShapelessRecipe $recipe, BinaryStream $stream){
$stream->putInt($recipe->getIngredientCount());
foreach($recipe->getIngredientList() as $item){
$this->putSlot($item);
$stream->putSlot($item);
}
$this->putInt(1);
$this->putSlot($recipe->getResult());
$stream->putInt(1);
$stream->putSlot($recipe->getResult());
return CraftingDataPacket::ENTRY_SHAPELESS;
}
private function writeShapedRecipe(ShapedRecipe $recipe){
$this->putInt(CraftingDataPacket::ENTRY_SHAPED);
}
private function writeFurnaceRecipe(FurnaceRecipe $recipe){
if($recipe->getInput()->getDamage() !== 0){ //Data recipe
$this->putInt(CraftingDataPacket::ENTRY_FURNACE_DATA);
$this->putInt(($recipe->getInput()->getId() << 16) | ($recipe->getInput()->getDamage()));
$this->putSlot($recipe->getResult());
}else{
$this->putInt(CraftingDataPacket::ENTRY_FURNACE);
$this->putInt($recipe->getInput()->getId());
$this->putSlot($recipe->getResult());
}
}
private function writeEnchant(){
$entry = Binary::writeInt(CraftingDataPacket::ENTRY_ENCHANT);
private static function writeShapedRecipe(ShapedRecipe $recipe, BinaryStream $stream){
//TODO
return CraftingDataPacket::ENTRY_SHAPED;
}
private static function writeFurnaceRecipe(FurnaceRecipe $recipe, BinaryStream $stream){
if($recipe->getInput()->getDamage() !== 0){ //Data recipe
$stream->putInt(($recipe->getInput()->getId() << 16) | ($recipe->getInput()->getDamage()));
$stream->putSlot($recipe->getResult());
return CraftingDataPacket::ENTRY_FURNACE_DATA;
}else{
$stream->putInt($recipe->getInput()->getId());
$stream->putSlot($recipe->getResult());
return CraftingDataPacket::ENTRY_FURNACE;
}
}
private static function writeEnchant(){
//TODO
return CraftingDataPacket::ENTRY_ENCHANT;
}
public function addShapelessRecipe(ShapelessRecipe $recipe){
@ -113,8 +120,20 @@ class CraftingDataPacket extends DataPacket{
public function encode(){
$this->reset();
$this->putInt(count($this->entries));
$writer = new BinaryStream();
foreach($this->entries as $d){
$this->writeEntry($d);
$entryType = self::writeEntry($d, $writer);
if($entryType >= 0){
$this->putInt($entryType);
$this->putInt(strlen($writer->getBuffer()));
$this->put($writer->getBuffer());
}else{
$this->putInt(-1);
$this->putInt(0);
}
$writer->reset();
}
$this->putByte($this->cleanRecipes ? 1 : 0);

View File

@ -28,16 +28,15 @@ use pocketmine\utils\Binary;
#endif
use pocketmine\item\Item;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\Utils;
use pocketmine\utils\UUID;
abstract class DataPacket extends \stdClass{
abstract class DataPacket extends BinaryStream{
const NETWORK_ID = 0;
public $offset = 0;
public $buffer = "";
public $isEncoded = false;
private $channel = 0;
@ -63,170 +62,6 @@ abstract class DataPacket extends \stdClass{
return $this->channel;
}
public function setBuffer($buffer = null, $offset = 0){
$this->buffer = $buffer;
$this->offset = (int) $offset;
}
public function getOffset(){
return $this->offset;
}
public function getBuffer(){
return $this->buffer;
}
protected function get($len){
if($len < 0){
$this->offset = strlen($this->buffer) - 1;
return "";
}elseif($len === true){
return substr($this->buffer, $this->offset);
}
return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len);
}
protected function put($str){
$this->buffer .= $str;
}
protected function getLong(){
return Binary::readLong($this->get(8));
}
protected function putLong($v){
$this->buffer .= Binary::writeLong($v);
}
protected function getInt(){
return Binary::readInt($this->get(4));
}
protected function putInt($v){
$this->buffer .= Binary::writeInt($v);
}
protected function getShort($signed = true){
return $signed ? Binary::readSignedShort($this->get(2)) : Binary::readShort($this->get(2));
}
protected function putShort($v){
$this->buffer .= Binary::writeShort($v);
}
protected function getFloat(){
return Binary::readFloat($this->get(4));
}
protected function putFloat($v){
$this->buffer .= Binary::writeFloat($v);
}
protected function getTriad(){
return Binary::readTriad($this->get(3));
}
protected function putTriad($v){
$this->buffer .= Binary::writeTriad($v);
}
protected function getLTriad(){
return Binary::readLTriad($this->get(3));
}
protected function putLTriad($v){
$this->buffer .= Binary::writeLTriad($v);
}
protected function getByte(){
return ord($this->buffer{$this->offset++});
}
protected function putByte($v){
$this->buffer .= chr($v);
}
protected function getDataArray($len = 10){
$data = [];
for($i = 1; $i <= $len and !$this->feof(); ++$i){
$data[] = $this->get($this->getTriad());
}
return $data;
}
protected function putDataArray(array $data = []){
foreach($data as $v){
$this->putTriad(strlen($v));
$this->put($v);
}
}
protected function getUUID(){
return UUID::fromBinary($this->get(16));
}
protected function putUUID(UUID $uuid){
$this->put($uuid->toBinary());
}
protected function getSlot(){
$id = $this->getShort(true);
if($id <= 0){
return Item::get(0, 0, 0);
}
$cnt = $this->getByte();
$data = $this->getShort();
$nbtLen = $this->getShort();
$nbt = "";
if($nbtLen > 0){
$nbt = $this->get($nbtLen);
}
return Item::get(
$id,
$data,
$cnt,
$nbt
);
}
protected function putSlot(Item $item){
if($item->getId() === 0){
$this->putShort(0);
return;
}
$this->putShort($item->getId());
$this->putByte($item->getCount());
$this->putShort($item->getDamage());
$nbt = $item->getCompoundTag();
$this->putShort(strlen($nbt));
$this->put($nbt);
}
protected function getString(){
return $this->get($this->getShort());
}
protected function putString($v){
$this->putShort(strlen($v));
$this->put($v);
}
protected function feof(){
return !isset($this->buffer{$this->offset});
}
public function clean(){
$this->buffer = null;
$this->isEncoded = false;

View File

@ -30,7 +30,7 @@ interface Info{
/**
* Actual Minecraft: PE protocol version
*/
const CURRENT_PROTOCOL = 30;
const CURRENT_PROTOCOL = 31;
const LOGIN_PACKET = 0x87;
const PLAY_STATUS_PACKET = 0x88;