mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 16:29:40 +00:00
added a SingletonTrait to reduce code duplication
This commit is contained in:
parent
a5441e009d
commit
5cc03775d3
@ -51,6 +51,7 @@ use pocketmine\block\utils\TreeType;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemIds;
|
||||
use pocketmine\item\ToolTier;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function array_fill;
|
||||
use function array_filter;
|
||||
use function get_class;
|
||||
@ -60,15 +61,7 @@ use function min;
|
||||
* Manages block registration and instance creation
|
||||
*/
|
||||
class BlockFactory{
|
||||
/** @var self|null */
|
||||
private static $instance = null;
|
||||
|
||||
public static function getInstance() : self{
|
||||
if(self::$instance === null){
|
||||
self::$instance = new self;
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var \SplFixedArray|Block[]
|
||||
|
@ -33,6 +33,7 @@ use pocketmine\entity\EntityFactory;
|
||||
use pocketmine\entity\Living;
|
||||
use pocketmine\inventory\ArmorInventory;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function constant;
|
||||
use function defined;
|
||||
use function explode;
|
||||
@ -46,15 +47,7 @@ use function trim;
|
||||
* Manages Item instance creation and registration
|
||||
*/
|
||||
class ItemFactory{
|
||||
/** @var self|null */
|
||||
private static $instance = null;
|
||||
|
||||
public static function getInstance() : self{
|
||||
if(self::$instance === null){
|
||||
self::$instance = new self;
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
use SingletonTrait;
|
||||
|
||||
/** @var Item[] */
|
||||
private $list = [];
|
||||
|
44
src/utils/SingletonTrait.php
Normal file
44
src/utils/SingletonTrait.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?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\utils;
|
||||
|
||||
trait SingletonTrait{
|
||||
/** @var self|null */
|
||||
private static $instance = null;
|
||||
|
||||
private static function make() : self{
|
||||
return new self;
|
||||
}
|
||||
|
||||
public static function getInstance() : self{
|
||||
if(self::$instance === null){
|
||||
self::$instance = self::make();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public static function reset() : void{
|
||||
self::$instance = null;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user