Files
PocketMine-MP/src/inventory/CreativeInventory.php
2020-01-22 11:55:03 +00:00

107 lines
2.6 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\item\Durable;
use pocketmine\item\Item;
use function file_get_contents;
use function json_decode;
use const DIRECTORY_SEPARATOR;
final class CreativeInventory{
/** @var Item[] */
public static $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){
$item = Item::jsonDeserialize($data);
if($item->getName() === "Unknown"){
continue;
}
self::add($item);
}
}
/**
* 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 = [];
}
/**
* @return Item[]
*/
public static function getAll() : array{
return self::$creative;
}
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;
}
/**
* 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;
}
/**
* 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);
if($index !== -1){
unset(self::$creative[$index]);
}
}
public static function contains(Item $item) : bool{
return self::getItemIndex($item) !== -1;
}
}