From 1323d8913918ef32a1c45a9fc8f1c6dff7dfdecb Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 19 Sep 2017 19:07:12 +0100 Subject: [PATCH] Remove redundant duplicated code for sendContents() and sendSlot() --- src/pocketmine/inventory/BaseInventory.php | 8 ++- .../inventory/PlayerCursorInventory.php | 29 -------- src/pocketmine/inventory/PlayerInventory.php | 69 ------------------- 3 files changed, 5 insertions(+), 101 deletions(-) diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 3c9846c91f..2a976aa83d 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -403,12 +403,14 @@ abstract class BaseInventory implements Inventory{ } $pk = new InventoryContentPacket(); - for($i = 0; $i < $this->getSize(); ++$i){ + + //Using getSize() here allows PlayerInventory to report that it's 4 slots smaller than it actually is (armor hack) + for($i = 0, $size = $this->getSize(); $i < $size; ++$i){ $pk->items[$i] = $this->getItem($i); } foreach($target as $player){ - if(($id = $player->getWindowId($this)) === -1 or $player->spawned !== true){ + if(($id = $player->getWindowId($this)) === ContainerIds::NONE or $player->spawned !== true){ $this->close($player); continue; } @@ -431,7 +433,7 @@ abstract class BaseInventory implements Inventory{ $pk->item = $this->getItem($index); foreach($target as $player){ - if(($id = $player->getWindowId($this)) === -1){ + if(($id = $player->getWindowId($this)) === ContainerIds::NONE){ $this->close($player); continue; } diff --git a/src/pocketmine/inventory/PlayerCursorInventory.php b/src/pocketmine/inventory/PlayerCursorInventory.php index 93f5422dc9..dd41ec10c5 100644 --- a/src/pocketmine/inventory/PlayerCursorInventory.php +++ b/src/pocketmine/inventory/PlayerCursorInventory.php @@ -52,35 +52,6 @@ class PlayerCursorInventory extends BaseInventory{ return WindowTypes::INVENTORY; //This should never be spawned to clients } - /** - * @param int $index - * @param Player|Player[] $target - */ - public function sendSlot(int $index, $target){ - if($target instanceof Player){ - $target = [$target]; - } - - $pk = new InventorySlotPacket(); - $pk->inventorySlot = $index; - $pk->item = $this->getItem($index); - - foreach($target as $player){ - if($player === $this->getHolder()){ - /** @var Player $player */ - $pk->windowId = ContainerIds::CURSOR; - $player->dataPacket(clone $pk); - }else{ - if(($id = $player->getWindowId($this)) === ContainerIds::NONE){ - $this->close($player); - continue; - } - $pk->windowId = $id; - $player->dataPacket(clone $pk); - } - } - } - /** * This override is here for documentation and code completion purposes only. * @return Player diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index b323a3c9eb..f6dee847a2 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -128,18 +128,6 @@ class PlayerInventory extends BaseInventory{ return $this->getItem($hotbarSlot); } - /** - * This is only used when sending inventory contents. Since we now assume that all hotbar slots are the same as - * their respective inventory slots, we simply fill this wil 0-8. - */ - private function sendHotbar(){ - $pk = new PlayerHotbarPacket(); - $pk->windowId = ContainerIds::INVENTORY; - $pk->selectedHotbarSlot = $this->getHeldItemIndex(); - $pk->slots = range(0, $this->getHotbarSize() - 1, 1); - $this->getHolder()->dataPacket($pk); - } - /** * @deprecated * @return int @@ -413,34 +401,6 @@ class PlayerInventory extends BaseInventory{ } } - /** - * @param Player|Player[] $target - */ - public function sendContents($target){ - if($target instanceof Player){ - $target = [$target]; - } - - $pk = new InventoryContentPacket(); - - for($i = 0; $i < $this->getSize(); ++$i){ //Do not send armor by error here - $pk->items[$i] = $this->getItem($i); - } - - foreach($target as $player){ - if(($id = $player->getWindowId($this)) === -1 or $player->spawned !== true){ - $this->close($player); - continue; - } - $pk->windowId = $id; - $player->dataPacket(clone $pk); - - if($player === $this->getHolder()){ - $this->sendHotbar(); - } - } - } - public function sendCreativeContents(){ $pk = new InventoryContentPacket(); $pk->windowId = ContainerIds::CREATIVE; @@ -454,35 +414,6 @@ class PlayerInventory extends BaseInventory{ $this->getHolder()->dataPacket($pk); } - /** - * @param int $index - * @param Player|Player[] $target - */ - public function sendSlot(int $index, $target){ - if($target instanceof Player){ - $target = [$target]; - } - - $pk = new InventorySlotPacket(); - $pk->inventorySlot = $index; - $pk->item = $this->getItem($index); - - foreach($target as $player){ - if($player === $this->getHolder()){ - /** @var Player $player */ - $pk->windowId = ContainerIds::INVENTORY; - $player->dataPacket(clone $pk); - }else{ - if(($id = $player->getWindowId($this)) === -1){ - $this->close($player); - continue; - } - $pk->windowId = $id; - $player->dataPacket(clone $pk); - } - } - } - /** * This override is here for documentation and code completion purposes only. * @return Human|Player