mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-21 10:26:38 +00:00
Convert CreativeInventory to singleton
This commit is contained in:
parent
5cc03775d3
commit
08ac6a3c43
@ -39,7 +39,6 @@ use pocketmine\event\player\PlayerDataSaveEvent;
|
|||||||
use pocketmine\event\server\CommandEvent;
|
use pocketmine\event\server\CommandEvent;
|
||||||
use pocketmine\event\server\DataPacketSendEvent;
|
use pocketmine\event\server\DataPacketSendEvent;
|
||||||
use pocketmine\event\server\QueryRegenerateEvent;
|
use pocketmine\event\server\QueryRegenerateEvent;
|
||||||
use pocketmine\inventory\CreativeInventory;
|
|
||||||
use pocketmine\item\enchantment\Enchantment;
|
use pocketmine\item\enchantment\Enchantment;
|
||||||
use pocketmine\lang\Language;
|
use pocketmine\lang\Language;
|
||||||
use pocketmine\lang\LanguageNotFoundException;
|
use pocketmine\lang\LanguageNotFoundException;
|
||||||
@ -984,7 +983,6 @@ class Server{
|
|||||||
|
|
||||||
EntityFactory::init();
|
EntityFactory::init();
|
||||||
Enchantment::init();
|
Enchantment::init();
|
||||||
CreativeInventory::init();
|
|
||||||
Biome::init();
|
Biome::init();
|
||||||
|
|
||||||
$this->craftingManager = new CraftingManager();
|
$this->craftingManager = new CraftingManager();
|
||||||
|
@ -25,22 +25,18 @@ namespace pocketmine\inventory;
|
|||||||
|
|
||||||
use pocketmine\item\Durable;
|
use pocketmine\item\Durable;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
|
use pocketmine\utils\SingletonTrait;
|
||||||
use function file_get_contents;
|
use function file_get_contents;
|
||||||
use function json_decode;
|
use function json_decode;
|
||||||
use const DIRECTORY_SEPARATOR;
|
use const DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
final class CreativeInventory{
|
final class CreativeInventory{
|
||||||
|
use SingletonTrait;
|
||||||
|
|
||||||
/** @var Item[] */
|
/** @var Item[] */
|
||||||
public static $creative = [];
|
private $creative = [];
|
||||||
|
|
||||||
private function __construct(){
|
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);
|
$creativeItems = json_decode(file_get_contents(\pocketmine\RESOURCE_PATH . "vanilla" . DIRECTORY_SEPARATOR . "creativeitems.json"), true);
|
||||||
|
|
||||||
foreach($creativeItems as $data){
|
foreach($creativeItems as $data){
|
||||||
@ -48,7 +44,7 @@ final class CreativeInventory{
|
|||||||
if($item->getName() === "Unknown"){
|
if($item->getName() === "Unknown"){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
self::add($item);
|
$this->add($item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,23 +52,23 @@ final class CreativeInventory{
|
|||||||
* Removes all previously added items from the creative menu.
|
* Removes all previously added items from the creative menu.
|
||||||
* Note: Players who are already online when this is called will not see this change.
|
* Note: Players who are already online when this is called will not see this change.
|
||||||
*/
|
*/
|
||||||
public static function clear() : void{
|
public function clear() : void{
|
||||||
self::$creative = [];
|
$this->creative = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Item[]
|
* @return Item[]
|
||||||
*/
|
*/
|
||||||
public static function getAll() : array{
|
public function getAll() : array{
|
||||||
return self::$creative;
|
return $this->creative;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getItem(int $index) : ?Item{
|
public function getItem(int $index) : ?Item{
|
||||||
return self::$creative[$index] ?? null;
|
return $this->creative[$index] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getItemIndex(Item $item) : int{
|
public function getItemIndex(Item $item) : int{
|
||||||
foreach(self::$creative as $i => $d){
|
foreach($this->creative as $i => $d){
|
||||||
if($item->equals($d, !($item instanceof Durable))){
|
if($item->equals($d, !($item instanceof Durable))){
|
||||||
return $i;
|
return $i;
|
||||||
}
|
}
|
||||||
@ -85,22 +81,22 @@ final class CreativeInventory{
|
|||||||
* Adds an item to the creative menu.
|
* Adds an item to the creative menu.
|
||||||
* Note: Players who are already online when this is called will not see this change.
|
* Note: Players who are already online when this is called will not see this change.
|
||||||
*/
|
*/
|
||||||
public static function add(Item $item) : void{
|
public function add(Item $item) : void{
|
||||||
self::$creative[] = clone $item;
|
$this->creative[] = clone $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes an item from the creative menu.
|
* Removes an item from the creative menu.
|
||||||
* Note: Players who are already online when this is called will not see this change.
|
* Note: Players who are already online when this is called will not see this change.
|
||||||
*/
|
*/
|
||||||
public static function remove(Item $item) : void{
|
public function remove(Item $item) : void{
|
||||||
$index = self::getItemIndex($item);
|
$index = $this->getItemIndex($item);
|
||||||
if($index !== -1){
|
if($index !== -1){
|
||||||
unset(self::$creative[$index]);
|
unset($this->creative[$index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function contains(Item $item) : bool{
|
public function contains(Item $item) : bool{
|
||||||
return self::getItemIndex($item) !== -1;
|
return $this->getItemIndex($item) !== -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ class CreateItemAction extends InventoryAction{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function isValid(Player $source) : bool{
|
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{
|
public function execute(Player $source) : void{
|
||||||
|
@ -204,7 +204,7 @@ class InventoryManager{
|
|||||||
$items = [];
|
$items = [];
|
||||||
$typeConverter = TypeConverter::getInstance();
|
$typeConverter = TypeConverter::getInstance();
|
||||||
if(!$this->player->isSpectator()){ //fill it for all gamemodes except spectator
|
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);
|
$items[$i] = $typeConverter->coreItemStackToNet($item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user