From 27e82ea60af7c4a390a8f56ad005ad57a2507a61 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 22 May 2014 23:13:46 +0200 Subject: [PATCH] Updated Inventory classes --- src/pocketmine/entity/InventorySource.php | 58 ----- src/pocketmine/inventory/BaseInventory.php | 27 ++- src/pocketmine/inventory/InventoryType.php | 2 +- src/pocketmine/inventory/PlayerInventory.php | 219 +++++++++++++++++++ 4 files changed, 246 insertions(+), 60 deletions(-) delete mode 100644 src/pocketmine/entity/InventorySource.php create mode 100644 src/pocketmine/inventory/PlayerInventory.php diff --git a/src/pocketmine/entity/InventorySource.php b/src/pocketmine/entity/InventorySource.php deleted file mode 100644 index d2fad5006..000000000 --- a/src/pocketmine/entity/InventorySource.php +++ /dev/null @@ -1,58 +0,0 @@ -slots[$index]) ? $this->slots[$index] : null; + return isset($this->slots[$index]) ? $this->slots[$index] : Item::get(Item::AIR, null, 0); } public function getContents(){ @@ -290,4 +291,28 @@ abstract class BaseInventory implements Inventory{ $this->setMaxStackSize($size); } + public function onOpen(Player $who){ + + } + + public function onClose(Player $who){ + + } + + public function onSlotChange($index, $before){ + $pk = new ContainerSetSlotPacket; + $pk->slot = $index; + $pk->item = clone $this->getItem($index); + + /** @var Player $player */ + foreach($this->getViewers() as $player){ + $pk->windowid = $player->getWindowId($this); + $player->dataPacket(clone $pk); + } + } + + public function getType(){ + return $this->type; + } + } \ No newline at end of file diff --git a/src/pocketmine/inventory/InventoryType.php b/src/pocketmine/inventory/InventoryType.php index e3b903cc6..0904de1a1 100644 --- a/src/pocketmine/inventory/InventoryType.php +++ b/src/pocketmine/inventory/InventoryType.php @@ -46,7 +46,7 @@ class InventoryType{ } static::$default[static::CHEST] = new InventoryType(27, "Chest"); - static::$default[static::PLAYER] = new InventoryType(36, "Player"); //9 HOTBAR slots, 27 CONTAINER, 4 ARMOR + static::$default[static::PLAYER] = new InventoryType(31, "Player"); //27 CONTAINER, 4 ARMOR (9 reference HOTBAR slots) static::$default[static::FURNACE] = new InventoryType(3, "Furnace"); static::$default[static::CRAFTING] = new InventoryType(5, "Crafting"); //4 CRAFTING slots, 1 RESULT static::$default[static::WORKBENCH] = new InventoryType(10, "Crafting"); //9 CRAFTING slots, 1 RESULT diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php new file mode 100644 index 000000000..306eb8d82 --- /dev/null +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -0,0 +1,219 @@ +hotbar = array_fill(0, $this->getHotbarSize(), -1); + parent::__construct($player, InventoryType::get(InventoryType::PLAYER)); + } + + public function getSize(){ + return parent::getSize() - 4; //Remove armor slots + } + + public function getHotbarSlotIndex($index){ + return ($index >= 0 and $index < $this->getHotbarSize()) ? $this->hotbar[$index] : -1; + } + + public function setHotbarSlotIndex($index, $slot){ + if($index >= 0 and $index < $this->getHotbarSize() and $slot >= -1 and $slot < $this->getSize()){ + $this->hotbar[$index] = $slot; + } + } + + public function getHeldItemIndex(){ + return $this->itemInHandIndex; + } + + public function setHeldItemIndex($index){ + if($index >= 0 and $index < $this->getHotbarSize()){ + $this->itemInHandIndex = $index; + $item = $this->getItemInHand(); + + $pk = new PlayerEquipmentPacket; + $pk->eid = $this->getHolder()->getID(); + $pk->item = $item->getID(); + $pk->meta = $item->getDamage(); + $pk->slot = $this->getHeldItemIndex(); + + foreach($this->getHolder()->getViewers() as $player){ + $player->dataPacket(clone $pk); + } + } + } + + public function getItemInHand(){ + $item = $this->getItem($this->getHeldItemSlot()); + if($item instanceof Item){ + return $item; + }else{ + return Item::get(Item::AIR); + } + } + + public function setItemInHand(Item $item){ + $this->setItem($this->getHeldItemSlot(), $item); + } + + public function getHeldItemSlot(){ + return $this->getHotbarSlotIndex($this->itemInHandIndex); + } + + public function setHeldItemSlot($slot){ + if($slot >= 0 and $slot < $this->getSize()){ + $this->setHotbarSlotIndex($this->itemInHandIndex, $slot); + $item = $this->getItemInHand(); + + $pk = new PlayerEquipmentPacket; + $pk->eid = $this->getHolder()->getID(); + $pk->item = $item->getID(); + $pk->meta = $item->getDamage(); + $pk->slot = 0; + + foreach($this->getHolder()->getViewers() as $player){ + $player->dataPacket(clone $pk); + } + } + } + + public function onSlotChange($index, $before){ + parent::onSlotChange($index, $before); + + if($index >= $this->getSize()){ + $armor = $this->getArmorContents(); + $slots = []; + + foreach($armor as $i => $slot){ + if($slot->getID() === Item::AIR){ + $slots[$i] = 255; + }else{ + $slots[$i] = $slot->getID(); + } + } + + $pk = new PlayerArmorEquipmentPacket; + $pk->eid = $this->getHolder()->getID(); + $pk->slots = $slots; + + if($index >= $this->getSize()){ //Armor change + foreach($this->getHolder()->getViewers() as $player){ + if($player === $this->getHolder()){ + /** @var Player $player */ + $pk2 = new ContainerSetContentPacket; + $pk2->windowid = 0x78; //Armor window id constant + $pk2->slots = $armor; + $player->dataPacket($pk2); + }else{ + $player->dataPacket(clone $pk); + } + } + } + } + } + + public function getHotbarSize(){ + return 9; + } + + public function getHelmet(){ + return $this->getItem($this->getSize() + 3); + } + + public function getChestplate(){ + return $this->getItem($this->getSize() + 2); + } + + public function getLeggings(){ + return $this->getItem($this->getSize() + 1); + } + + public function getBoots(){ + return $this->getItem($this->getSize()); + } + + public function setHelmet(Item $helmet){ + $this->setItem($this->getSize() + 3, $helmet); + } + + public function setChestplate(Item $chestplate){ + $this->setItem($this->getSize() + 2, $chestplate); + } + + public function setLeggings(Item $leggings){ + $this->setItem($this->getSize() + 1, $leggings); + } + + public function setBoots(Item $boots){ + $this->setItem($this->getSize(), $boots); + } + + /** + * @return Item[] + */ + public function getArmorContents(){ + $armor = []; + + for($i = 0; $i < 4; ++$i){ + $armor[$i] = $this->getItem($this->getSize() + $i); + } + + return $armor; + } + + /** + * @param Item[] $items + */ + public function setArmorContents(array $items){ + for($i = 0; $i < 4; ++$i){ + if(!isset($items[$i]) or !($items[$i] instanceof Item)){ + $items[$i] = Item::get(Item::AIR, null, 0); + } + + if($items[$i]->getID() === Item::AIR){ + $this->clear($this->getSize() + $i); + }else{ + $this->setItem($this->getSize() + $i, $items[$i]); + } + } + } + + /** + * @return Human + */ + public function getHolder(){ + return parent::getHolder(); + } + +} \ No newline at end of file