From 08ac6a3c437528d5a4f05aa1022e36fe29d47eaa Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 24 Apr 2020 00:38:18 +0100 Subject: [PATCH] Convert CreativeInventory to singleton --- src/Server.php | 2 - src/inventory/CreativeInventory.php | 42 +++++++++---------- .../transaction/action/CreateItemAction.php | 2 +- src/network/mcpe/InventoryManager.php | 2 +- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/Server.php b/src/Server.php index 2deb2f2bf..567cfb3e3 100644 --- a/src/Server.php +++ b/src/Server.php @@ -39,7 +39,6 @@ use pocketmine\event\player\PlayerDataSaveEvent; use pocketmine\event\server\CommandEvent; use pocketmine\event\server\DataPacketSendEvent; use pocketmine\event\server\QueryRegenerateEvent; -use pocketmine\inventory\CreativeInventory; use pocketmine\item\enchantment\Enchantment; use pocketmine\lang\Language; use pocketmine\lang\LanguageNotFoundException; @@ -984,7 +983,6 @@ class Server{ EntityFactory::init(); Enchantment::init(); - CreativeInventory::init(); Biome::init(); $this->craftingManager = new CraftingManager(); diff --git a/src/inventory/CreativeInventory.php b/src/inventory/CreativeInventory.php index 45bee9f97..a2666d1be 100644 --- a/src/inventory/CreativeInventory.php +++ b/src/inventory/CreativeInventory.php @@ -25,22 +25,18 @@ namespace pocketmine\inventory; use pocketmine\item\Durable; use pocketmine\item\Item; +use pocketmine\utils\SingletonTrait; use function file_get_contents; use function json_decode; use const DIRECTORY_SEPARATOR; final class CreativeInventory{ + use SingletonTrait; /** @var Item[] */ - public static $creative = []; + private $creative = []; private function __construct(){ - //NOOP - } - - public static function init() : void{ - self::clear(); - $creativeItems = json_decode(file_get_contents(\pocketmine\RESOURCE_PATH . "vanilla" . DIRECTORY_SEPARATOR . "creativeitems.json"), true); foreach($creativeItems as $data){ @@ -48,7 +44,7 @@ final class CreativeInventory{ if($item->getName() === "Unknown"){ continue; } - self::add($item); + $this->add($item); } } @@ -56,23 +52,23 @@ final class CreativeInventory{ * Removes all previously added items from the creative menu. * Note: Players who are already online when this is called will not see this change. */ - public static function clear() : void{ - self::$creative = []; + public function clear() : void{ + $this->creative = []; } /** * @return Item[] */ - public static function getAll() : array{ - return self::$creative; + public function getAll() : array{ + return $this->creative; } - public static function getItem(int $index) : ?Item{ - return self::$creative[$index] ?? null; + public function getItem(int $index) : ?Item{ + return $this->creative[$index] ?? null; } - public static function getItemIndex(Item $item) : int{ - foreach(self::$creative as $i => $d){ + public function getItemIndex(Item $item) : int{ + foreach($this->creative as $i => $d){ if($item->equals($d, !($item instanceof Durable))){ return $i; } @@ -85,22 +81,22 @@ final class CreativeInventory{ * Adds an item to the creative menu. * Note: Players who are already online when this is called will not see this change. */ - public static function add(Item $item) : void{ - self::$creative[] = clone $item; + public function add(Item $item) : void{ + $this->creative[] = clone $item; } /** * Removes an item from the creative menu. * Note: Players who are already online when this is called will not see this change. */ - public static function remove(Item $item) : void{ - $index = self::getItemIndex($item); + public function remove(Item $item) : void{ + $index = $this->getItemIndex($item); if($index !== -1){ - unset(self::$creative[$index]); + unset($this->creative[$index]); } } - public static function contains(Item $item) : bool{ - return self::getItemIndex($item) !== -1; + public function contains(Item $item) : bool{ + return $this->getItemIndex($item) !== -1; } } diff --git a/src/inventory/transaction/action/CreateItemAction.php b/src/inventory/transaction/action/CreateItemAction.php index 2ecd868a6..18cd4a536 100644 --- a/src/inventory/transaction/action/CreateItemAction.php +++ b/src/inventory/transaction/action/CreateItemAction.php @@ -39,7 +39,7 @@ class CreateItemAction extends InventoryAction{ } public function isValid(Player $source) : bool{ - return !$source->hasFiniteResources() and CreativeInventory::contains($this->sourceItem); + return !$source->hasFiniteResources() and CreativeInventory::getInstance()->contains($this->sourceItem); } public function execute(Player $source) : void{ diff --git a/src/network/mcpe/InventoryManager.php b/src/network/mcpe/InventoryManager.php index b589e5740..bd665dee9 100644 --- a/src/network/mcpe/InventoryManager.php +++ b/src/network/mcpe/InventoryManager.php @@ -204,7 +204,7 @@ class InventoryManager{ $items = []; $typeConverter = TypeConverter::getInstance(); if(!$this->player->isSpectator()){ //fill it for all gamemodes except spectator - foreach(CreativeInventory::getAll() as $i => $item){ + foreach(CreativeInventory::getInstance()->getAll() as $i => $item){ $items[$i] = $typeConverter->coreItemStackToNet($item); } }