From f88aed12082e9116a0a10293e2e9eae588404cee Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 20 Apr 2015 13:57:16 +0200 Subject: [PATCH] Fixed NBT parsing on false properties --- src/pocketmine/Server.php | 8 +++---- src/pocketmine/block/BurningFurnace.php | 4 ++-- src/pocketmine/block/Chest.php | 4 ++-- src/pocketmine/entity/Human.php | 8 +++---- src/pocketmine/level/Level.php | 2 +- src/pocketmine/level/format/anvil/Anvil.php | 2 +- .../format/generic/BaseLevelProvider.php | 2 +- .../level/format/leveldb/LevelDB.php | 2 +- .../level/format/mcregion/McRegion.php | 2 +- src/pocketmine/nbt/NBT.php | 14 +++++------- src/pocketmine/nbt/tag/Enum.php | 22 +++++++++---------- src/pocketmine/nbt/tag/NamedTag.php | 2 +- src/pocketmine/tile/Chest.php | 2 +- src/pocketmine/tile/Furnace.php | 2 +- 14 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 74f4c34da..e2f82bdc6 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -706,7 +706,7 @@ class Server{ $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerNotFound", [$name])); } $spawn = $this->getDefaultLevel()->getSafeSpawn(); - $nbt = new Compound(false, [ + $nbt = new Compound("", [ new Long("firstPlayed", floor(microtime(true) * 1000)), new Long("lastPlayed", floor(microtime(true) * 1000)), new Enum("Pos", [ @@ -758,7 +758,7 @@ class Server{ $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerOld", [$name])); foreach($data->get("inventory") as $slot => $item){ if(count($item) === 3){ - $nbt->Inventory[$slot + 9] = new Compound(false, [ + $nbt->Inventory[$slot + 9] = new Compound("", [ new Short("id", $item[0]), new Short("Damage", $item[1]), new Byte("Count", $item[2]), @@ -770,7 +770,7 @@ class Server{ foreach($data->get("hotbar") as $slot => $itemSlot){ if(isset($nbt->Inventory[$itemSlot + 9])){ $item = $nbt->Inventory[$itemSlot + 9]; - $nbt->Inventory[$slot] = new Compound(false, [ + $nbt->Inventory[$slot] = new Compound("", [ new Short("id", $item["id"]), new Short("Damage", $item["Damage"]), new Byte("Count", $item["Count"]), @@ -781,7 +781,7 @@ class Server{ } foreach($data->get("armor") as $slot => $item){ if(count($item) === 2){ - $nbt->Inventory[$slot + 100] = new Compound(false, [ + $nbt->Inventory[$slot + 100] = new Compound("", [ new Short("id", $item[0]), new Short("Damage", $item[1]), new Byte("Count", 1), diff --git a/src/pocketmine/block/BurningFurnace.php b/src/pocketmine/block/BurningFurnace.php index d4c5cbdfd..b70b46888 100644 --- a/src/pocketmine/block/BurningFurnace.php +++ b/src/pocketmine/block/BurningFurnace.php @@ -64,7 +64,7 @@ class BurningFurnace extends Solid{ ]; $this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0]; $this->getLevel()->setBlock($block, $this, true, true); - $nbt = new Compound(false, [ + $nbt = new Compound("", [ new Enum("Items", []), new String("id", Tile::FURNACE), new Int("x", $this->x), @@ -90,7 +90,7 @@ class BurningFurnace extends Solid{ if($t instanceof Furnace){ $furnace = $t; }else{ - $nbt = new Compound(false, [ + $nbt = new Compound("", [ new Enum("Items", []), new String("id", Tile::FURNACE), new Int("x", $this->x), diff --git a/src/pocketmine/block/Chest.php b/src/pocketmine/block/Chest.php index 72f0261ba..91b52d9fe 100644 --- a/src/pocketmine/block/Chest.php +++ b/src/pocketmine/block/Chest.php @@ -91,7 +91,7 @@ class Chest extends Transparent{ } $this->getLevel()->setBlock($block, $this, true, true); - $nbt = new Compound(false, [ + $nbt = new Compound("", [ new Enum("Items", []), new String("id", Tile::CHEST), new Int("x", $this->x), @@ -131,7 +131,7 @@ class Chest extends Transparent{ if($t instanceof TileChest){ $chest = $t; }else{ - $nbt = new Compound(false, [ + $nbt = new Compound("", [ new Enum("Items", []), new String("id", Tile::CHEST), new Int("x", $this->x), diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 7999dca11..aa01e3527 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -142,7 +142,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ if($hotbarSlot !== -1){ $item = $this->inventory->getItem($hotbarSlot); if($item->getId() !== 0 and $item->getCount() > 0){ - $this->namedtag->Inventory[$slot] = new Compound(false, [ + $this->namedtag->Inventory[$slot] = new Compound("", [ new Byte("Count", $item->getCount()), new Short("Damage", $item->getDamage()), new Byte("Slot", $slot), @@ -152,7 +152,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ continue; } } - $this->namedtag->Inventory[$slot] = new Compound(false, [ + $this->namedtag->Inventory[$slot] = new Compound("", [ new Byte("Count", 0), new Short("Damage", 0), new Byte("Slot", $slot), @@ -166,7 +166,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ //$slotCount = (($this instanceof Player and ($this->gamemode & 0x01) === 1) ? Player::CREATIVE_SLOTS : Player::SURVIVAL_SLOTS) + 9; for($slot = 9; $slot < $slotCount; ++$slot){ $item = $this->inventory->getItem($slot - 9); - $this->namedtag->Inventory[$slot] = new Compound(false, [ + $this->namedtag->Inventory[$slot] = new Compound("", [ new Byte("Count", $item->getCount()), new Short("Damage", $item->getDamage()), new Byte("Slot", $slot), @@ -178,7 +178,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ for($slot = 100; $slot < 104; ++$slot){ $item = $this->inventory->getItem($this->inventory->getSize() + $slot - 100); if($item instanceof ItemItem and $item->getId() !== ItemItem::AIR){ - $this->namedtag->Inventory[$slot] = new Compound(false, [ + $this->namedtag->Inventory[$slot] = new Compound("", [ new Byte("Count", $item->getCount()), new Short("Damage", $item->getDamage()), new Byte("Slot", $slot), diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 329f3c857..ad8f7efed 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -1469,7 +1469,7 @@ class Level implements ChunkManager, Metadatable{ } if($hand->getId() === Item::SIGN_POST or $hand->getId() === Item::WALL_SIGN){ - $tile = Tile::createTile("Sign", $this->getChunk($block->x >> 4, $block->z >> 4), new Compound(false, [ + $tile = Tile::createTile("Sign", $this->getChunk($block->x >> 4, $block->z >> 4), new Compound("", [ "id" => new String("id", Tile::SIGN), "x" => new Int("x", $block->x), "y" => new Int("y", $block->y), diff --git a/src/pocketmine/level/format/anvil/Anvil.php b/src/pocketmine/level/format/anvil/Anvil.php index 1c92e66ac..4b2c35455 100644 --- a/src/pocketmine/level/format/anvil/Anvil.php +++ b/src/pocketmine/level/format/anvil/Anvil.php @@ -106,7 +106,7 @@ class Anvil extends McRegion{ } public static function createChunkSection($Y){ - return new ChunkSection(new Compound(null, [ + return new ChunkSection(new Compound("", [ "Y" => new Byte("Y", $Y), "Blocks" => new ByteArray("Blocks", str_repeat("\x00", 4096)), "Data" => new ByteArray("Data", str_repeat("\x00", 2048)), diff --git a/src/pocketmine/level/format/generic/BaseLevelProvider.php b/src/pocketmine/level/format/generic/BaseLevelProvider.php index b77878f3f..2682b5cdf 100644 --- a/src/pocketmine/level/format/generic/BaseLevelProvider.php +++ b/src/pocketmine/level/format/generic/BaseLevelProvider.php @@ -118,7 +118,7 @@ abstract class BaseLevelProvider implements LevelProvider{ public function saveLevelData(){ $nbt = new NBT(NBT::BIG_ENDIAN); - $nbt->setData(new Compound(null, [ + $nbt->setData(new Compound("", [ "Data" => $this->levelData ])); $buffer = $nbt->writeCompressed(); diff --git a/src/pocketmine/level/format/leveldb/LevelDB.php b/src/pocketmine/level/format/leveldb/LevelDB.php index dac41d6fd..92a3e5624 100644 --- a/src/pocketmine/level/format/leveldb/LevelDB.php +++ b/src/pocketmine/level/format/leveldb/LevelDB.php @@ -96,7 +96,7 @@ class LevelDB extends BaseLevelProvider{ mkdir($path . "/db", 0777, true); } //TODO, add extra details - $levelData = new Compound(null, [ + $levelData = new Compound("", [ "hardcore" => new Byte("hardcore", 0), "initialized" => new Byte("initialized", 1), "GameType" => new Int("GameType", 0), diff --git a/src/pocketmine/level/format/mcregion/McRegion.php b/src/pocketmine/level/format/mcregion/McRegion.php index 3bee7fa63..020e2228e 100644 --- a/src/pocketmine/level/format/mcregion/McRegion.php +++ b/src/pocketmine/level/format/mcregion/McRegion.php @@ -100,7 +100,7 @@ class McRegion extends BaseLevelProvider{ "GameRules" => new Compound("GameRules", []) ]); $nbt = new NBT(NBT::BIG_ENDIAN); - $nbt->setData(new Compound(null, [ + $nbt->setData(new Compound("", [ "Data" => $levelData ])); $buffer = $nbt->writeCompressed(); diff --git a/src/pocketmine/nbt/NBT.php b/src/pocketmine/nbt/NBT.php index 8a0b5ccee..28e333286 100644 --- a/src/pocketmine/nbt/NBT.php +++ b/src/pocketmine/nbt/NBT.php @@ -119,28 +119,26 @@ class NBT{ */ public function write(){ $this->offset = 0; - $data = false; if($this->data instanceof Compound){ $this->writeTag($this->data); - $data = $this->buffer; + return $this->buffer; }elseif(is_array($this->data)){ foreach($this->data as $tag){ $this->writeTag($tag); } - $data = $this->buffer; + return $this->buffer; } - return $data; + return false; } public function writeCompressed($compression = ZLIB_ENCODING_GZIP, $level = 7){ - $data = false; if(($write = $this->write()) !== false){ - $data = zlib_encode($write, $compression, $level); + return zlib_encode($write, $compression, $level); } - return $data; + return false; } public function readTag(){ @@ -312,7 +310,7 @@ class NBT{ } public function setArray(array $data){ - $this->data = new Compound(null, []); + $this->data = new Compound("", []); $this->fromArray($this->data, $data); } diff --git a/src/pocketmine/nbt/tag/Enum.php b/src/pocketmine/nbt/tag/Enum.php index 58901e85b..602fbbbb8 100644 --- a/src/pocketmine/nbt/tag/Enum.php +++ b/src/pocketmine/nbt/tag/Enum.php @@ -110,57 +110,57 @@ class Enum extends NamedTag implements \ArrayAccess, \Countable{ for($i = 0; $i < $size and !$nbt->feof(); ++$i){ switch($this->tagType){ case NBT::TAG_Byte: - $tag = new Byte(false); + $tag = new Byte(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Short: - $tag = new Short(false); + $tag = new Short(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Int: - $tag = new Int(false); + $tag = new Int(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Long: - $tag = new Long(false); + $tag = new Long(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Float: - $tag = new Float(false); + $tag = new Float(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Double: - $tag = new Double(false); + $tag = new Double(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_ByteArray: - $tag = new ByteArray(false); + $tag = new ByteArray(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_String: - $tag = new String(false); + $tag = new String(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Enum: - $tag = new TagEnum(false); + $tag = new TagEnum(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_Compound: - $tag = new Compound(false); + $tag = new Compound(""); $tag->read($nbt); $this->{$i} = $tag; break; case NBT::TAG_IntArray: - $tag = new IntArray(false); + $tag = new IntArray(""); $tag->read($nbt); $this->{$i} = $tag; break; diff --git a/src/pocketmine/nbt/tag/NamedTag.php b/src/pocketmine/nbt/tag/NamedTag.php index 12d7d537d..169b02c8d 100644 --- a/src/pocketmine/nbt/tag/NamedTag.php +++ b/src/pocketmine/nbt/tag/NamedTag.php @@ -31,7 +31,7 @@ abstract class NamedTag extends Tag{ * @param bool|float|double|int|byte|short|array|Compound|Enum|string $value */ public function __construct($name = "", $value = null){ - $this->name = $name; + $this->name = ($name === null or $name === false) ? "" : $name; if($value !== null){ $this->value = $value; } diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index e6725f5f7..6a26b46e3 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -126,7 +126,7 @@ class Chest extends Spawnable implements InventoryHolder, Container{ public function setItem($index, Item $item){ $i = $this->getSlotIndex($index); - $d = new Compound(false, [ + $d = new Compound("", [ new Byte("Count", $item->getCount()), new Byte("Slot", $index), new Short("id", $item->getId()), diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 126d4ec1d..01cea0d9a 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -135,7 +135,7 @@ class Furnace extends Tile implements InventoryHolder, Container{ public function setItem($index, Item $item){ $i = $this->getSlotIndex($index); - $d = new Compound(false, [ + $d = new Compound("", [ new Byte("Count", $item->getCount()), new Byte("Slot", $index), new Short("id", $item->getId()),