mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 10:49:10 +00:00
More changes! Alsp added Anvil block and BinaryStream
This commit is contained in:
parent
522932d7c0
commit
7fd053fb09
@ -1910,8 +1910,11 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
|
||||
$packet->slot -= 9; //Get real block slot
|
||||
}
|
||||
|
||||
/** @var Item $item */
|
||||
$item = null;
|
||||
|
||||
if($this->isCreative()){ //Creative mode match
|
||||
$item = Item::get($packet->item, $packet->meta, 1);
|
||||
$item = $packet->item;
|
||||
$slot = Item::getCreativeItemIndex($item);
|
||||
}else{
|
||||
$item = $this->inventory->getItem($packet->slot);
|
||||
@ -1942,7 +1945,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
|
||||
break;
|
||||
}
|
||||
}
|
||||
}elseif(!isset($item) or $slot === -1 or $item->getId() !== $packet->item or $item->getDamage() !== $packet->meta){ // packet error or not implemented
|
||||
}elseif($item === null or $slot === -1 or $item->equals($packet->item, true)){ // packet error or not implemented
|
||||
$this->inventory->sendContents($this);
|
||||
break;
|
||||
}elseif($this->isCreative()){
|
||||
|
86
src/pocketmine/block/Anvil.php
Normal file
86
src/pocketmine/block/Anvil.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* 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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\block;
|
||||
|
||||
use pocketmine\inventory\AnvilInventory;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\Tool;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\nbt\NBT;
|
||||
use pocketmine\nbt\tag\Compound;
|
||||
use pocketmine\nbt\tag\Enum;
|
||||
use pocketmine\nbt\tag\Int;
|
||||
use pocketmine\nbt\tag\String;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\tile\Chest as TileChest;
|
||||
use pocketmine\tile\Tile;
|
||||
|
||||
class Anvil extends Fallable{
|
||||
|
||||
protected $id = self::ANVIL;
|
||||
|
||||
public function isSolid(){
|
||||
return false;
|
||||
}
|
||||
|
||||
public function __construct($meta = 0){
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
public function canBeActivated(){
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getHardness(){
|
||||
return 5;
|
||||
}
|
||||
|
||||
public function getResistance(){
|
||||
return 6000;
|
||||
}
|
||||
|
||||
public function getName(){
|
||||
return "Anvil";
|
||||
}
|
||||
|
||||
public function getToolType(){
|
||||
return Tool::TYPE_PICKAXE;
|
||||
}
|
||||
|
||||
public function onActivate(Item $item, Player $player = null){
|
||||
if($player instanceof Player){
|
||||
if($player->isCreative()){
|
||||
return true;
|
||||
}
|
||||
|
||||
$player->addWindow(new AnvilInventory($this));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDrops(Item $item){
|
||||
return [
|
||||
[$this->id, 0, 1],
|
||||
];
|
||||
}
|
||||
}
|
@ -206,6 +206,8 @@ class Block extends Position implements Metadatable{
|
||||
const CARROT_BLOCK = 141;
|
||||
const POTATO_BLOCK = 142;
|
||||
|
||||
const ANVIL = 145;
|
||||
|
||||
const REDSTONE_BLOCK = 152;
|
||||
|
||||
const QUARTZ_BLOCK = 155;
|
||||
@ -424,6 +426,7 @@ class Block extends Position implements Metadatable{
|
||||
|
||||
self::$list[self::CARROT_BLOCK] = Carrot::class;
|
||||
self::$list[self::POTATO_BLOCK] = Potato::class;
|
||||
self::$list[self::ANVIL] = Anvil::class;
|
||||
|
||||
self::$list[self::REDSTONE_BLOCK] = Redstone::class;
|
||||
|
||||
|
48
src/pocketmine/inventory/AnvilInventory.php
Normal file
48
src/pocketmine/inventory/AnvilInventory.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* 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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\level\Position;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\network\Network;
|
||||
use pocketmine\network\protocol\TileEventPacket;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\Server;
|
||||
use pocketmine\tile\Chest;
|
||||
|
||||
class AnvilInventory extends ContainerInventory implements InventoryHolder{
|
||||
public function __construct(Position $pos){
|
||||
parent::__construct($this, InventoryType::get(InventoryType::ANVIL));
|
||||
}
|
||||
|
||||
public function getInventory(){
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AnvilInventory
|
||||
*/
|
||||
public function getHolder(){
|
||||
return $this->holder;
|
||||
}
|
||||
}
|
@ -32,6 +32,9 @@ class InventoryType{
|
||||
const CRAFTING = 4;
|
||||
const WORKBENCH = 5;
|
||||
const STONECUTTER = 6;
|
||||
const BREWING_STAND = 7;
|
||||
const ANVIL = 8;
|
||||
const ENCHANT_TABLE = 9;
|
||||
|
||||
private static $default = [];
|
||||
|
||||
@ -59,7 +62,10 @@ class InventoryType{
|
||||
static::$default[static::FURNACE] = new InventoryType(3, "Furnace", 2);
|
||||
static::$default[static::CRAFTING] = new InventoryType(5, "Crafting", 1); //4 CRAFTING slots, 1 RESULT
|
||||
static::$default[static::WORKBENCH] = new InventoryType(10, "Crafting", 1); //9 CRAFTING slots, 1 RESULT
|
||||
static::$default[static::STONECUTTER] = new InventoryType(10, "Crafting", 3); //9 CRAFTING slots, 1 RESULT
|
||||
static::$default[static::STONECUTTER] = new InventoryType(10, "Crafting", 1); //9 CRAFTING slots, 1 RESULT
|
||||
static::$default[static::ENCHANT_TABLE] = new InventoryType(1, "Enchant", 4); //1 INPUT/OUTPUT
|
||||
static::$default[static::BREWING_STAND] = new InventoryType(4, "Brewing", 5); //1 INPUT, 3 POTION
|
||||
static::$default[static::ANVIL] = new InventoryType(3, "Brewing", 6); //2 INPUT, 1 OUTPUT
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -200,6 +200,8 @@ class Item{
|
||||
const CARROT_BLOCK = 141;
|
||||
const POTATO_BLOCK = 142;
|
||||
|
||||
const ANVIL = 145;
|
||||
|
||||
const REDSTONE_BLOCK = 152;
|
||||
|
||||
const QUARTZ_BLOCK = 155;
|
||||
@ -727,6 +729,11 @@ class Item{
|
||||
self::addCreativeItem(Item::get(Item::CARPET, 9));
|
||||
self::addCreativeItem(Item::get(Item::CARPET, 8));
|
||||
|
||||
|
||||
self::addCreativeItem(Item::get(Item::ANVIL, 0));
|
||||
self::addCreativeItem(Item::get(Item::ANVIL, 4));
|
||||
self::addCreativeItem(Item::get(Item::ANVIL, 8));
|
||||
|
||||
//Tools
|
||||
//TODO self::addCreativeItem(Item::get(Item::RAILS, 0));
|
||||
//TODO self::addCreativeItem(Item::get(Item::POWERED_RAILS, 0));
|
||||
|
@ -31,6 +31,7 @@ use pocketmine\nbt\tag\Compound;
|
||||
use pocketmine\network\protocol\FullChunkDataPacket;
|
||||
use pocketmine\tile\Spawnable;
|
||||
use pocketmine\utils\ChunkException;
|
||||
use raklib\Binary;
|
||||
|
||||
class Anvil extends McRegion{
|
||||
|
||||
@ -94,6 +95,8 @@ class Anvil extends McRegion{
|
||||
$chunk->getBlockLightArray() .
|
||||
pack("C*", ...$chunk->getHeightMapArray()) .
|
||||
pack("N*", ...$chunk->getBiomeColorArray()) .
|
||||
//TODO extra data
|
||||
Binary::writeInt(0) .
|
||||
$tiles;
|
||||
|
||||
$this->getLevel()->chunkRequestCallback($x, $z, $ordered, FullChunkDataPacket::ORDER_LAYERED);
|
||||
|
@ -162,6 +162,8 @@ class LevelDB extends BaseLevelProvider{
|
||||
$chunk->getBlockLightArray() .
|
||||
$heightmap .
|
||||
$biomeColors .
|
||||
//TODO extra data
|
||||
Binary::writeInt(0) .
|
||||
$tiles;
|
||||
|
||||
$this->getLevel()->chunkRequestCallback($x, $z, $ordered);
|
||||
|
@ -31,7 +31,6 @@ use pocketmine\nbt\tag\Compound;
|
||||
use pocketmine\nbt\tag\Int;
|
||||
use pocketmine\nbt\tag\Long;
|
||||
use pocketmine\nbt\tag\String;
|
||||
use pocketmine\network\protocol\FullChunkDataPacket;
|
||||
use pocketmine\tile\Spawnable;
|
||||
use pocketmine\utils\Binary;
|
||||
use pocketmine\utils\ChunkException;
|
||||
@ -139,6 +138,8 @@ class McRegion extends BaseLevelProvider{
|
||||
$chunk->getBlockLightArray() .
|
||||
pack("C*", ...$chunk->getHeightMapArray()) .
|
||||
pack("N*", ...$chunk->getBiomeColorArray()) .
|
||||
//TODO extra data
|
||||
Binary::writeInt(0) .
|
||||
$tiles;
|
||||
|
||||
$this->getLevel()->chunkRequestCallback($x, $z, $ordered);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
207
src/pocketmine/utils/BinaryStream.php
Normal file
207
src/pocketmine/utils/BinaryStream.php
Normal file
@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* 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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
#include <rules/DataPacket.h>
|
||||
|
||||
#ifndef COMPILE
|
||||
use pocketmine\utils\Binary;
|
||||
#endif
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\utils\UUID;
|
||||
|
||||
|
||||
class BinaryStream extends \stdClass{
|
||||
|
||||
public $offset = 0;
|
||||
public $buffer = "";
|
||||
|
||||
protected function reset(){
|
||||
$this->buffer = "";
|
||||
$this->offset = 0;
|
||||
}
|
||||
|
||||
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() === null ? -1 : $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});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user