Crafting recipe network serialization no longer depends on PM's internal legacy metadata

WOOOOOOOOOOOOOOOOOOOOOOHOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!
This commit is contained in:
Dylan K. Taylor
2022-07-02 16:37:39 +01:00
parent 8858b16a25
commit 7994da07be
6 changed files with 156 additions and 28 deletions

View File

@ -23,14 +23,19 @@ declare(strict_types=1);
namespace pocketmine\tools\generate_block_palette_spec;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\TreeRoot;
use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\network\mcpe\convert\BlockStateDictionaryEntry;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils;
use function array_map;
use function array_values;
use function count;
use function dirname;
@ -51,15 +56,26 @@ if(count($argv) !== 3){
[, $inputFile, $outputFile] = $argv;
try{
$palette = BlockStateDictionary::loadFromString(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($inputFile)));
$states = array_map(
fn(TreeRoot $root) => BlockStateData::fromNbt($root->mustGetCompoundTag()),
(new NetworkNbtSerializer())->readMultiple(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($inputFile)))
);
}catch(NbtException){
fwrite(STDERR, "Invalid block palette file $argv[1]\n");
exit(1);
}
$entries = [];
$fakeMeta = [];
foreach($states as $state){
$fakeMeta[$state->getName()] ??= 0;
$entries[] = new BlockStateDictionaryEntry($state, $fakeMeta[$state->getName()]++);
}
$palette = new BlockStateDictionary($entries);
$reportMap = [];
foreach($palette->getStates() as $state){
foreach($palette->getStates() as $entry){
$state = $entry->getStateData();
$name = $state->getName();
foreach($state->getStates() as $propertyName => $value){
if($value instanceof IntTag || $value instanceof StringTag){