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

@ -2807,7 +2807,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$t = $this->level->getTile($pos); $t = $this->level->getTile($pos);
if($t instanceof Sign){ if($t instanceof Sign){
$nbt = new NBT(NBT::LITTLE_ENDIAN); $nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->read($packet->namedtag); $nbt->read($packet->namedtag, false, true);
$nbt = $nbt->getData(); $nbt = $nbt->getData();
if($nbt["id"] !== Tile::SIGN){ if($nbt["id"] !== Tile::SIGN){
$t->spawnTo($this); $t->spawnTo($this);

View File

@ -86,7 +86,7 @@ class Anvil extends McRegion{
} }
} }
$nbt->setData($list); $nbt->setData($list);
$tiles = $nbt->write(); $tiles = $nbt->write(true);
} }
$extraData = new BinaryStream(); $extraData = new BinaryStream();

View File

@ -49,7 +49,7 @@ class ChunkRequestTask extends AsyncTask{
foreach($chunk->getTiles() as $tile){ foreach($chunk->getTiles() as $tile){
if($tile instanceof Spawnable){ if($tile instanceof Spawnable){
$nbt->setData($tile->getSpawnCompound()); $nbt->setData($tile->getSpawnCompound());
$tiles .= $nbt->write(); $tiles .= $nbt->write(true);
} }
} }

View File

@ -129,7 +129,7 @@ class McRegion extends BaseLevelProvider{
} }
} }
$nbt->setData($list); $nbt->setData($list);
$tiles = $nbt->write(); $tiles = $nbt->write(true);
} }
$extraData = new BinaryStream(); $extraData = new BinaryStream();

View File

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

View File

@ -31,12 +31,12 @@ class ByteArrayTag extends NamedTag{
return NBT::TAG_ByteArray; return NBT::TAG_ByteArray;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->get($nbt->getInt()); $this->value = $nbt->get($nbt->getInt($network));
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putInt(strlen($this->value)); $nbt->putInt(strlen($this->value), $network);
$nbt->put($this->value); $nbt->put($this->value);
} }
} }

View File

@ -31,11 +31,11 @@ class ByteTag extends NamedTag{
return NBT::TAG_Byte; return NBT::TAG_Byte;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getByte(); $this->value = $nbt->getByte();
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putByte($this->value); $nbt->putByte($this->value);
} }
} }

View File

@ -83,23 +83,23 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
return NBT::TAG_Compound; return NBT::TAG_Compound;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = []; $this->value = [];
do{ do{
$tag = $nbt->readTag(); $tag = $nbt->readTag($network);
if($tag instanceof NamedTag and $tag->getName() !== ""){ if($tag instanceof NamedTag and $tag->getName() !== ""){
$this->{$tag->getName()} = $tag; $this->{$tag->getName()} = $tag;
} }
}while(!($tag instanceof EndTag) and !$nbt->feof()); }while(!($tag instanceof EndTag) and !$nbt->feof());
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
foreach($this as $tag){ foreach($this as $tag){
if($tag instanceof Tag and !($tag instanceof EndTag)){ if($tag instanceof Tag and !($tag instanceof EndTag)){
$nbt->writeTag($tag); $nbt->writeTag($tag, $network);
} }
} }
$nbt->writeTag(new EndTag); $nbt->writeTag(new EndTag, $network);
} }
public function __toString(){ public function __toString(){

View File

@ -31,11 +31,11 @@ class DoubleTag extends NamedTag{
return NBT::TAG_Double; return NBT::TAG_Double;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getDouble(); $this->value = $nbt->getDouble();
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putDouble($this->value); $nbt->putDouble($this->value);
} }
} }

View File

@ -29,11 +29,11 @@ class EndTag extends Tag{
return NBT::TAG_End; return NBT::TAG_End;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
} }
} }

View File

@ -31,11 +31,11 @@ class FloatTag extends NamedTag{
return NBT::TAG_Float; return NBT::TAG_Float;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getFloat(); $this->value = $nbt->getFloat();
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putFloat($this->value); $nbt->putFloat($this->value);
} }
} }

View File

@ -31,13 +31,13 @@ class IntArrayTag extends NamedTag{
return NBT::TAG_IntArray; return NBT::TAG_IntArray;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$size = $nbt->getInt(); $size = $nbt->getInt($network);
$this->value = array_values(unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4))); $this->value = array_values(unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4)));
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putInt(count($this->value)); $nbt->putInt(count($this->value), $network);
$nbt->put(pack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", ...$this->value)); $nbt->put(pack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", ...$this->value));
} }

View File

@ -31,11 +31,11 @@ class IntTag extends NamedTag{
return NBT::TAG_Int; return NBT::TAG_Int;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getInt(); $this->value = $nbt->getInt($network);
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putInt($this->value); $nbt->putInt($this->value, $network);
} }
} }

View File

@ -114,72 +114,72 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
return $this->tagType; return $this->tagType;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = []; $this->value = [];
$this->tagType = $nbt->getByte(); $this->tagType = $nbt->getByte();
$size = $nbt->getInt(); $size = $nbt->getInt($network);
for($i = 0; $i < $size and !$nbt->feof(); ++$i){ for($i = 0; $i < $size and !$nbt->feof(); ++$i){
switch($this->tagType){ switch($this->tagType){
case NBT::TAG_Byte: case NBT::TAG_Byte:
$tag = new ByteTag(""); $tag = new ByteTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_Short: case NBT::TAG_Short:
$tag = new ShortTag(""); $tag = new ShortTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_Int: case NBT::TAG_Int:
$tag = new IntTag(""); $tag = new IntTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_Long: case NBT::TAG_Long:
$tag = new LongTag(""); $tag = new LongTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_Float: case NBT::TAG_Float:
$tag = new FloatTag(""); $tag = new FloatTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_Double: case NBT::TAG_Double:
$tag = new DoubleTag(""); $tag = new DoubleTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_ByteArray: case NBT::TAG_ByteArray:
$tag = new ByteArrayTag(""); $tag = new ByteArrayTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_String: case NBT::TAG_String:
$tag = new StringTag(""); $tag = new StringTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_List: case NBT::TAG_List:
$tag = new TagEnum(""); $tag = new TagEnum("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_Compound: case NBT::TAG_Compound:
$tag = new CompoundTag(""); $tag = new CompoundTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
case NBT::TAG_IntArray: case NBT::TAG_IntArray:
$tag = new IntArrayTag(""); $tag = new IntArrayTag("");
$tag->read($nbt); $tag->read($nbt, $network);
$this->{$i} = $tag; $this->{$i} = $tag;
break; break;
} }
} }
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
if(!isset($this->tagType)){ if(!isset($this->tagType)){
$id = null; $id = null;
foreach($this as $tag){ foreach($this as $tag){
@ -205,7 +205,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
} }
$nbt->putInt(count($tags)); $nbt->putInt(count($tags));
foreach($tags as $tag){ foreach($tags as $tag){
$tag->write($nbt); $tag->write($nbt, $network);
} }
} }

View File

@ -31,11 +31,13 @@ class LongTag extends NamedTag{
return NBT::TAG_Long; return NBT::TAG_Long;
} }
public function read(NBT $nbt){ //TODO: check if this also changed to varint
public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getLong(); $this->value = $nbt->getLong();
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putLong($this->value); $nbt->putLong($this->value);
} }
} }

View File

@ -31,11 +31,11 @@ class ShortTag extends NamedTag{
return NBT::TAG_Short; return NBT::TAG_Short;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getShort(); $this->value = $nbt->getShort();
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putShort($this->value); $nbt->putShort($this->value);
} }
} }

View File

@ -31,12 +31,11 @@ class StringTag extends NamedTag{
return NBT::TAG_String; return NBT::TAG_String;
} }
public function read(NBT $nbt){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->get($nbt->getShort()); $this->value = $nbt->getString($network);
} }
public function write(NBT $nbt){ public function write(NBT $nbt, bool $network = false){
$nbt->putShort(strlen($this->value)); $nbt->putString($this->value, $network);
$nbt->put($this->value);
} }
} }

View File

@ -40,9 +40,9 @@ abstract class Tag extends \stdClass{
$this->value = $value; $this->value = $value;
} }
abstract public function write(NBT $nbt); abstract public function write(NBT $nbt, bool $network = false);
abstract public function read(NBT $nbt); abstract public function read(NBT $nbt, bool $network = false);
public function __toString(){ public function __toString(){
return (string) $this->value; return (string) $this->value;

View File

@ -40,7 +40,7 @@ abstract class Spawnable extends Tile{
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
$pk->z = $this->z; $pk->z = $this->z;
$pk->namedtag = $nbt->write(); $pk->namedtag = $nbt->write(true);
$player->dataPacket($pk); $player->dataPacket($pk);
return true; return true;

View File

@ -58,7 +58,9 @@ class BinaryStream extends \stdClass{
$this->offset = strlen($this->buffer) - 1; $this->offset = strlen($this->buffer) - 1;
return ""; return "";
}elseif($len === true){ }elseif($len === true){
return substr($this->buffer, $this->offset); $str = substr($this->buffer, $this->offset);
$this->offset = strlen($this->buffer);
return $str;
} }
return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len); return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len);