NBT updates and tile spawns fixed

This commit is contained in:
Dylan K. Taylor
2016-10-04 18:45:03 +01:00
parent 8f9574dec5
commit ff40c0a070
20 changed files with 130 additions and 97 deletions

View File

@ -463,14 +463,14 @@ class NBT{
$this->endianness = $endianness & 0x01;
}
public function read($buffer, $doMultiple = false){
public function read($buffer, $doMultiple = false, bool $network = false){
$this->offset = 0;
$this->buffer = $buffer;
$this->data = $this->readTag();
$this->data = $this->readTag($network);
if($doMultiple and $this->offset < strlen($this->buffer)){
$this->data = [$this->data];
do{
$this->data[] = $this->readTag();
$this->data[] = $this->readTag($network);
}while($this->offset < strlen($this->buffer));
}
$this->buffer = "";
@ -480,20 +480,25 @@ class NBT{
$this->read(zlib_decode($buffer));
}
public function readNetworkCompressed($buffer, $compression = ZLIB_ENCODING_GZIP){
$this->read(zlib_decode($buffer), false, true);
}
/**
* @return string|bool
*/
public function write(){
public function write(bool $network = false){
$this->offset = 0;
$this->buffer = "";
if($this->data instanceof CompoundTag){
$this->writeTag($this->data);
$this->writeTag($this->data, $network);
return $this->buffer;
}elseif(is_array($this->data)){
foreach($this->data as $tag){
$this->writeTag($tag);
$this->writeTag($tag, $network);
}
return $this->buffer;
}
@ -509,51 +514,64 @@ class NBT{
return false;
}
public function readTag(){
switch($this->getByte()){
public function writeNetworkCompressed($compression = ZLIB_ENCODING_GZIP, $level = 7){
if(($write = $this->write(true)) !== false){
return zlib_encode($write, $compression, $level);
}
return false;
}
public function readTag(bool $network = false){
if($this->feof()){
$tagType = -1; //prevent crashes for empty tags
}else{
$tagType = $this->getByte();
}
switch($tagType){
case NBT::TAG_Byte:
$tag = new ByteTag($this->getString());
$tag->read($this);
$tag = new ByteTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_Short:
$tag = new ShortTag($this->getString());
$tag->read($this);
$tag = new ShortTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_Int:
$tag = new IntTag($this->getString());
$tag->read($this);
$tag = new IntTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_Long:
$tag = new LongTag($this->getString());
$tag->read($this);
$tag = new LongTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_Float:
$tag = new FloatTag($this->getString());
$tag->read($this);
$tag = new FloatTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_Double:
$tag = new DoubleTag($this->getString());
$tag->read($this);
$tag = new DoubleTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_ByteArray:
$tag = new ByteArrayTag($this->getString());
$tag->read($this);
$tag = new ByteArrayTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_String:
$tag = new StringTag($this->getString());
$tag->read($this);
$tag = new StringTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_List:
$tag = new ListTag($this->getString());
$tag->read($this);
$tag = new ListTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_Compound:
$tag = new CompoundTag($this->getString());
$tag->read($this);
$tag = new CompoundTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_IntArray:
$tag = new IntArrayTag($this->getString());
$tag->read($this);
$tag = new IntArrayTag($this->getString($network));
$tag->read($this, $network);
break;
case NBT::TAG_End: //No named tag
@ -564,12 +582,12 @@ class NBT{
return $tag;
}
public function writeTag(Tag $tag){
public function writeTag(Tag $tag, bool $network = false){
$this->putByte($tag->getType());
if($tag instanceof NamedTAG){
$this->putString($tag->getName());
$this->putString($tag->getName(), $network);
}
$tag->write($this);
$tag->write($this, $network);
}
public function getByte(){
@ -588,12 +606,19 @@ class NBT{
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Binary::writeShort($v) : Binary::writeLShort($v);
}
public function getInt(){
public function getInt(bool $network = false){
if($network === true){
return Binary::readVarInt($this);
}
return $this->endianness === self::BIG_ENDIAN ? Binary::readInt($this->get(4)) : Binary::readLInt($this->get(4));
}
public function putInt($v){
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Binary::writeInt($v) : Binary::writeLInt($v);
public function putInt($v, bool $network = false){
if($network === true){
$this->buffer .= Binary::writeVarInt($v);
}else{
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Binary::writeInt($v) : Binary::writeLInt($v);
}
}
public function getLong(){
@ -620,12 +645,17 @@ class NBT{
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Binary::writeDouble($v) : Binary::writeLDouble($v);
}
public function getString(){
return $this->get($this->getShort());
public function getString(bool $network = false){
$len = $network ? $this->getByte() : $this->getShort();
return $this->get($len);
}
public function putString($v){
$this->putShort(strlen($v));
public function putString($v, bool $network = false){
if($network === true){
$this->putByte(strlen($v));
}else{
$this->putShort(strlen($v));
}
$this->buffer .= $v;
}