Created
October 16, 2013 17:50
-
-
Save jkcgs/7011971 to your computer and use it in GitHub Desktop.
Player NBT
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 | |
/** | |
* Class for reading Player NBT from Minecraft | |
* | |
* @author Jonathan Gutiérrez <me@makzk.cl> | |
* @version 1.0 | |
* | |
* Dependencies: | |
* NBT Class from https://github.com/TheFrozenFire/PHP-NBT-Decoder-Encoder | |
*/ | |
require("nbt.class.php"); | |
class NBTPlayer{ | |
private $nbt; | |
public $root = array(); | |
// Reads NBT file with NBT class | |
function __construct($filename) { | |
$this->nbt = new nbt(); | |
$this->nbt->loadFile($filename); | |
$this->root = $this->nbt->root[0]["value"]; | |
} | |
// Get NBT item by its name | |
public function getByName($value) { | |
foreach($this->root as $arr) | |
if($arr["name"] == $value) | |
return $arr["value"]; | |
return false; | |
} | |
// Get the player's inventory | |
// Array returned example: | |
/* Array | |
( | |
[0] => Array | |
( | |
[id] => 302 | |
[Damage] => 0 | |
[Count] => 1 | |
[Slot] => 0 | |
) | |
) */ | |
public function getInventory() { | |
// Get Inventory element from NBT | |
$inv = self::getByName("Inventory")["value"]; | |
$parsedinv = array(); // return the parsed inventory | |
foreach($inv as $item) { | |
$parseditem = array(); // parse the item as a single array | |
foreach($item as $el) | |
$parseditem[$el["name"]] = $el["value"]; | |
$parsedinv[] = $parseditem; | |
} | |
return $parsedinv; | |
} | |
// Get an item by the slot id | |
public function getInventorySlot($n) { | |
$inv = self::getInventory(); | |
foreach($inv as $item) | |
if($item["slot"] == $n) | |
return $item: | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment