From 95eddbdd7478d734edefb40b699817023dca3692 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 12 Feb 2020 17:36:29 +0000 Subject: [PATCH] InventoryManager: move add/remove logic to separate functions --- src/network/mcpe/InventoryManager.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/network/mcpe/InventoryManager.php b/src/network/mcpe/InventoryManager.php index 1348e8a5f..61f68142e 100644 --- a/src/network/mcpe/InventoryManager.php +++ b/src/network/mcpe/InventoryManager.php @@ -68,9 +68,17 @@ class InventoryManager{ $this->player = $player; $this->session = $session; - $this->windowMap[ContainerIds::INVENTORY] = $this->player->getInventory(); - $this->windowMap[ContainerIds::ARMOR] = $this->player->getArmorInventory(); - $this->windowMap[ContainerIds::UI] = $this->player->getCursorInventory(); + $this->add(ContainerIds::INVENTORY, $this->player->getInventory()); + $this->add(ContainerIds::ARMOR, $this->player->getArmorInventory()); + $this->add(ContainerIds::UI, $this->player->getCursorInventory()); + } + + private function add(int $id, Inventory $inventory) : void{ + $this->windowMap[$id] = $inventory; + } + + private function remove(int $id) : void{ + unset($this->windowMap[$id], $this->initiatedSlotChanges[$id]); } public function getWindowId(Inventory $inventory) : ?int{ @@ -96,7 +104,7 @@ class InventoryManager{ public function onCurrentWindowChange(Inventory $inventory) : void{ $this->onCurrentWindowRemove(); - $this->windowMap[$this->lastInventoryNetworkId = max(ContainerIds::FIRST, ($this->lastInventoryNetworkId + 1) % ContainerIds::LAST)] = $inventory; + $this->add($this->lastInventoryNetworkId = max(ContainerIds::FIRST, ($this->lastInventoryNetworkId + 1) % ContainerIds::LAST), $inventory); $pk = $this->createContainerOpen($this->lastInventoryNetworkId, $inventory); if($pk !== null){ @@ -131,16 +139,14 @@ class InventoryManager{ public function onCurrentWindowRemove() : void{ if(isset($this->windowMap[$this->lastInventoryNetworkId])){ - unset($this->windowMap[$this->lastInventoryNetworkId]); - unset($this->initiatedSlotChanges[$this->lastInventoryNetworkId]); + $this->remove($this->lastInventoryNetworkId); $this->session->sendDataPacket(ContainerClosePacket::create($this->lastInventoryNetworkId)); } } public function onClientRemoveWindow(int $id) : void{ if($id === $this->lastInventoryNetworkId){ - unset($this->windowMap[$id]); - unset($this->initiatedSlotChanges[$id]); + $this->remove($id); $this->player->removeCurrentWindow(); }else{ $this->session->getLogger()->debug("Attempted to close inventory with network ID $id, but current is $this->lastInventoryNetworkId");