From 967a929723d698ea1742e7ad43499ac09a2611f6 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Fri, 8 Feb 2013 14:17:52 +0100 Subject: [PATCH] Send Inventory Support --- src/classes/material/Item.php | 2 +- src/classes/network/CustomPacketHandler.php | 32 +++++++++++++++++++++ src/classes/utils/Utils.php | 12 ++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/classes/material/Item.php b/src/classes/material/Item.php index 723665c89..d04cb7cba 100644 --- a/src/classes/material/Item.php +++ b/src/classes/material/Item.php @@ -48,7 +48,7 @@ class Item{ protected $block; protected $id; protected $meta; - protected $count; + public $count; protected $maxStackSize = 64; protected $durability = 0; protected $name; diff --git a/src/classes/network/CustomPacketHandler.php b/src/classes/network/CustomPacketHandler.php index 94c769f14..d87539372 100644 --- a/src/classes/network/CustomPacketHandler.php +++ b/src/classes/network/CustomPacketHandler.php @@ -575,6 +575,38 @@ class CustomPacketHandler{ $this->raw .= Utils::writeFloat($this->data["z"]); } break; + case MC_SEND_INVENTORY: + if($this->c === false){ + $this->data["eid"] = Utils::readInt($this->get(4)); + $this->data["windowid"] = ord($this->get(1)); + $this->data["count"] = Utils::readShort($this->get(2), false); + $this->data["slots"] = array(); + for($s = 0; $s < $this->data["count"]; ++$s){ + $this->data["slots"][$s] = Utils::readSlot($this->get(5)); + } + if($this->data["windowid"] === 1){ //Armor is also sent + $this->data["armor"] = array( + Utils::readSlot($this->get(5)), + Utils::readSlot($this->get(5)), + Utils::readSlot($this->get(5)), + Utils::readSlot($this->get(5)) + ); + } + }else{ + $this->raw .= Utils::writeInt($this->data["eid"]); + $this->raw .= chr($this->data["windowid"]); + $this->raw .= Utils::writeShort(count($this->data["slots"])); + foreach($this->data["slots"] as $slot){ + $this->raw .= Utils::writeSlot($slot); + } + if($this->data["windowid"] === 1 and isset($this->data["armor"])){ + $this->raw .= Utils::writeSlot($this->data["armor"][0]); + $this->raw .= Utils::writeSlot($this->data["armor"][1]); + $this->raw .= Utils::writeSlot($this->data["armor"][2]); + $this->raw .= Utils::writeSlot($this->data["armor"][3]); + } + } + break; case MC_DROP_ITEM: if($this->c === false){ $this->data["eid"] = Utils::readInt($this->get(4)); diff --git a/src/classes/utils/Utils.php b/src/classes/utils/Utils.php index ba9f78f7d..7d4f9ed67 100644 --- a/src/classes/utils/Utils.php +++ b/src/classes/utils/Utils.php @@ -148,6 +148,18 @@ class Utils extends Thread{ $m .= "\x7f"; return $m; } + + public static function writeSlot(Item $item){ + return Utils::writeShort($item->getID()).chr($item->count).Utils::writeShort($item->getMetadata()); + } + + public static function readSlot($str){ + return BlockAPI::getItem( + Utils::readShort(substr($str, 0, 2), false), + Utils::readShort(substr($str, 3, 2), false), + ord($str{2}) + ); + } public static function readMetadata($value, $types = false){ $offset = 0;