Created
April 9, 2018 18:32
-
-
Save dktapps/239ce80f822180b8dc30eb9ec38bf766 to your computer and use it in GitHub Desktop.
Class with utilities to unfuck serialized NBT objects from before PocketMine-MP API 3.0.0-ALPHA11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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/ | |
* | |
* | |
*/ | |
declare(strict_types=1); | |
namespace dktapps\SerializedNbtFixer; | |
use pocketmine\nbt\tag\CompoundTag; | |
use pocketmine\nbt\tag\ListTag; | |
use pocketmine\nbt\tag\NamedTag; | |
class SerializedNbtFixer{ | |
public static function fixSerializedCompoundTag(CompoundTag $tag) : CompoundTag{ | |
$result = new CompoundTag($tag->getName()); | |
$properties = (new \ReflectionObject($tag))->getProperties(\ReflectionProperty::IS_PUBLIC); | |
foreach($properties as $property){ | |
$child = $property->getValue($tag); | |
if($child instanceof NamedTag){ | |
$result->setTag(self::processSerializedNamedTag($child)); | |
} | |
} | |
return $result; | |
} | |
public static function fixSerializedListTag(ListTag $tag) : ListTag{ | |
$properties = (new \ReflectionObject($tag))->getProperties(\ReflectionProperty::IS_PUBLIC); | |
$result = []; | |
foreach($properties as $i => $property){ | |
$child = $property->getValue($tag); | |
if($child instanceof NamedTag and is_numeric($property->getName())){ | |
$result[(int) $i] = self::processSerializedNamedTag($child); | |
} | |
} | |
return new ListTag($tag->getName(), $result); | |
} | |
public static function processSerializedNamedTag(NamedTag $tag) : NamedTag{ | |
if($tag instanceof CompoundTag){ | |
return self::fixSerializedCompoundTag($tag); | |
}elseif($tag instanceof ListTag){ | |
return self::fixSerializedListTag($tag); | |
}else{ | |
return $tag; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment