Skip to content

Instantly share code, notes, and snippets.

@jkcgs
Created October 16, 2013 17:50
Show Gist options
  • Save jkcgs/7011971 to your computer and use it in GitHub Desktop.
Save jkcgs/7011971 to your computer and use it in GitHub Desktop.
Player NBT
<?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