Skip to content

Instantly share code, notes, and snippets.

@DavyCraft648
Created October 23, 2024 16:54
Show Gist options
  • Select an option

  • Save DavyCraft648/942e8cf8534d3e48ea990aa4503b59f1 to your computer and use it in GitHub Desktop.

Select an option

Save DavyCraft648/942e8cf8534d3e48ea990aa4503b59f1 to your computer and use it in GitHub Desktop.
This PHP script converts block palette file from Nukkit to PocketMine, so PocketMine can read it. It takes the block palette file as input, decompresses and processes the block data, then converts it to a format compatible with PocketMine.
<?php
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\CompoundTag;
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\Filesystem;
use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path;
require_once "vendor/autoload.php";
$path = "";
if ($argc > 1) {
$path = $argv[1];
echo "Block palette path: " . $path, "\n";
} else {
echo "Usage: " . PHP_BINARY . " " . __FILE__ . " <block palette file>.", "\n";
return;
}
try{
$contents = Filesystem::fileGetContents($path);
}catch(\RuntimeException $e){
echo "Failed to read data file \"$path\": " . $e->getMessage();
return;
}
try{
$decompressed = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => zlib_decode($contents));
}catch(\ErrorException $e){
echo "Failed to decompress raw data: " . $e->getMessage();
return;
}
try{
$compoundTag = (new BigEndianNbtSerializer())->read($decompressed)->mustGetCompoundTag();
}catch(NbtDataException $e){ //corrupt data
echo "Failed to decode NBT data: " . $e->getMessage();
return;
}
$block_states = [];
/** @var CompoundTag $block */
foreach($compoundTag->getListTag("blocks") as $block){
$block->removeTag("name_hash", "network_id", "block_id");
$block_states[] = new TreeRoot(BlockStateData::fromNbt($block)->toVanillaNbt());
}
file_put_contents("canonical_block_states.nbt", (new NetworkNbtSerializer())->writeMultiple($block_states));
echo "Success!!", "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment