Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
php parser for Minecraft villages.dat
<head><meta charset="UTF-8">
<style type="text/css">
.folder:hover { background-color:#bfb; }
.fold { display:none; }
.left { float:left; margin-left:0; padding-left:0; padding-right:5px; }
.box {
background-color:#FFF;
padding: 5px;
margin-bottom: 10px;
border-left: 1px solid #ddd;
border-top: 1px solid #ddd;
border-right: 2px solid #dcc;
border-bottom:2px solid #dcc;
width:600px;
}
</style>
</head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript">
function fold(div) { //alert( "wtf" );
$("#pre"+div).slideToggle( "fast" );
$("#"+div).slideToggle( "slow" );
}
</script>
<pre>
<h2><a href="/">← home</a></h2>
<?php
$world_dir = '/home/minecraft/best';
// NBT parser from: https://github.com/TheFrozenFire/PHP-NBT-Decoder-Encoder
// Put nbt.class.php in same directory
require("nbt.class.php");
$nbt = new nbt();
$filename = "$world_dir/data/villages.dat";
$command = "ls -alh $filename";
$output=""; $retval=0;
exec ( $command , $output, $retval );
if( $retval == 0 ) {
echo "<b>$filename</b>\n";
$s = explode( " ", $output[0]);
echo "Modified ".$s[5]." ".$s[6]." ".$s[7]."\n";
$crc = crc32(file_get_contents($filename));
printf("crc: %u\n", $crc);
echo $s[4]." bytes\n";
}
$nbt->loadFile("$world_dir/data/villages.dat");
$villages = $nbt->root[0]['value'][0]['value'][0]['value']['value'];
if( count($villages) > 0 ) {
$doorDivdx=0;
echo "<h2>There are ".count($villages)." villages loaded:</h2>";
$vattrs = Array( "CX", "CY", "CZ", "Radius", "PopSize" );
$dattrs = Array( "X", "Y", "Z" );
$pattrs = Array( "S", "Name" );
foreach( $villages as $village ) {
$door_output = "";
$p_output = "";
foreach( $village as $attr ) {
if( in_array( $attr['name'], $vattrs ) )
${$attr['name']} = $attr['value'];
if( $attr['name'] == "Doors" ) {
$ddx=0;
foreach( $attr['value']['value'] as $door ) {
foreach( $door as $_dattr )
if( in_array( $_dattr['name'], $dattrs ) )
${$_dattr['name']} = $_dattr['value'];
$ddx++;
$door_output .= sprintf( " Door %2d: %4d %2d %-4d \n",$ddx, $X, $Y, $Z);
}
}
if( $attr['name'] == "Players" ) {
$pdx=0;
foreach( $attr['value']['value'] as $player ) {
foreach( $player as $_pattr )
if( in_array( $_pattr['name'], $pattrs ) )
${$_pattr['name']} = $_pattr['value'];
$pdx++;
$p_output .= " Player $pdx: $Name Score: $S\n";
}
}
}
echo "<div class=\"box\">x,y,z: $CX $CY $CZ\n";
$ckx = (int)($CX/16); // sprintf( "%.0f", $CX/16);
$cky = (int)($CY/16);
$ckz = (int)($CZ/16); // sprintf( "%.0f", $CZ/16);
echo " chunk x,y,z: $ckx $cky $ckz\n";
echo " radius: $Radius\n population: $PopSize\n";
if( strlen( $p_output ) > 1 )
echo " Player(s):\n$p_output";
if( $ddx < 8 )
echo " $ddx doors:\n$door_output\n";
else {
$doordiv="doordiv$doorDivdx";
$doorDivdx++;
echo " $ddx doors: (<span class=\"folder\" onClick=\"fold('$doordiv' );\">click to show/hide all doors</span>)";
$doors=explode("\n",$door_output);
echo " <div id=\"pre$doordiv\">";
for( $idx=0; $idx<8; $idx++ )
echo $doors[$idx]."\n";
$skip=$ddx-$idx;
echo " <span class=\"folder\" onClick=\"fold('$doordiv' );\">... $skip doors hidden</span></div>";
echo " <div id=\"$doordiv\" class=\"fold\"><div class=\"left\">";
for( $idx=0; $idx<count($doors); $idx++ ) {
if( $idx>0 && $idx%10 == 0 )
echo " </div><div class=\"left\">";
echo $doors[$idx]."\n";
}
echo " </div></div><div style=\"clear:both\"></div>";
}
echo "</div>";
}
}
?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.