diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 26f76e022..1e407604c 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -40,8 +40,8 @@ use pocketmine\event\server\CommandEvent; use pocketmine\event\server\DataPacketBroadcastEvent; use pocketmine\event\server\QueryRegenerateEvent; use pocketmine\inventory\CraftingManager; +use pocketmine\inventory\CreativeInventory; use pocketmine\item\enchantment\Enchantment; -use pocketmine\item\Item; use pocketmine\item\ItemFactory; use pocketmine\lang\Language; use pocketmine\lang\LanguageNotFoundException; @@ -1186,7 +1186,7 @@ class Server{ BlockFactory::init(); Enchantment::init(); ItemFactory::init(); - Item::initCreativeItems(); + CreativeInventory::init(); Biome::init(); $this->craftingManager = new CraftingManager(); diff --git a/src/pocketmine/inventory/CreativeInventory.php b/src/pocketmine/inventory/CreativeInventory.php new file mode 100644 index 000000000..2c2597c11 --- /dev/null +++ b/src/pocketmine/inventory/CreativeInventory.php @@ -0,0 +1,99 @@ +getName() === "Unknown"){ + continue; + } + self::add($item); + } + } + + public static function clear(){ + self::$creative = []; + } + + /** + * @return Item[] + */ + public static function getAll() : array{ + return self::$creative; + } + + /** + * @param int $index + * + * @return Item|null + */ + public static function getItem(int $index) : ?Item{ + return self::$creative[$index] ?? null; + } + + public static function getItemIndex(Item $item) : int{ + foreach(self::$creative as $i => $d){ + if($item->equals($d, !($item instanceof Durable))){ + return $i; + } + } + + return -1; + } + + public static function add(Item $item){ + self::$creative[] = clone $item; + } + + public static function remove(Item $item){ + $index = self::getItemIndex($item); + if($index !== -1){ + unset(self::$creative[$index]); + } + } + + public static function contains(Item $item) : bool{ + return self::getItemIndex($item) !== -1; + } +} diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index 4224923b7..5629a706e 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -165,7 +165,7 @@ class PlayerInventory extends BaseInventory{ $items = []; if(!$holder->isSpectator()){ //fill it for all gamemodes except spectator - foreach(Item::getCreativeItems() as $i => $item){ + foreach(CreativeInventory::getAll() as $i => $item){ $items[$i] = clone $item; } } diff --git a/src/pocketmine/inventory/transaction/action/CreativeInventoryAction.php b/src/pocketmine/inventory/transaction/action/CreativeInventoryAction.php index 4b1f61803..7c399bc95 100644 --- a/src/pocketmine/inventory/transaction/action/CreativeInventoryAction.php +++ b/src/pocketmine/inventory/transaction/action/CreativeInventoryAction.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\inventory\transaction\action; +use pocketmine\inventory\CreativeInventory; use pocketmine\item\Item; use pocketmine\Player; @@ -53,7 +54,7 @@ class CreativeInventoryAction extends InventoryAction{ */ public function isValid(Player $source) : bool{ return !$source->hasFiniteResources() and - ($this->actionType === self::TYPE_DELETE_ITEM or Item::getCreativeItemIndex($this->sourceItem) !== -1); + ($this->actionType === self::TYPE_DELETE_ITEM or CreativeInventory::getItemIndex($this->sourceItem) !== -1); } /** diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index 39d03d4db..3c551c9a2 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -48,11 +48,8 @@ use pocketmine\utils\Binary; use function array_map; use function base64_decode; use function base64_encode; -use function file_get_contents; use function get_class; use function hex2bin; -use function json_decode; -use const DIRECTORY_SEPARATOR; class Item implements ItemIds, \JsonSerializable{ public const TAG_ENCH = "ench"; @@ -92,66 +89,6 @@ class Item implements ItemIds, \JsonSerializable{ return ItemFactory::fromString($str); } - - /** @var Item[] */ - private static $creative = []; - - public static function initCreativeItems(){ - self::clearCreativeItems(); - - $creativeItems = json_decode(file_get_contents(\pocketmine\RESOURCE_PATH . "vanilla" . DIRECTORY_SEPARATOR . "creativeitems.json"), true); - - foreach($creativeItems as $data){ - $item = Item::jsonDeserialize($data); - if($item->getName() === "Unknown"){ - continue; - } - self::addCreativeItem($item); - } - } - - public static function clearCreativeItems(){ - Item::$creative = []; - } - - public static function getCreativeItems() : array{ - return Item::$creative; - } - - public static function addCreativeItem(Item $item){ - Item::$creative[] = clone $item; - } - - public static function removeCreativeItem(Item $item){ - $index = self::getCreativeItemIndex($item); - if($index !== -1){ - unset(Item::$creative[$index]); - } - } - - public static function isCreativeItem(Item $item) : bool{ - return Item::getCreativeItemIndex($item) !== -1; - } - - /** - * @param int $index - * - * @return Item|null - */ - public static function getCreativeItem(int $index) : ?Item{ - return Item::$creative[$index] ?? null; - } - - public static function getCreativeItemIndex(Item $item) : int{ - foreach(Item::$creative as $i => $d){ - if($item->equals($d, !($item instanceof Durable))){ - return $i; - } - } - - return -1; - } - /** @var int */ protected $id; /** @var int */