Added ItemFactory::air() sugar

This makes it easier to create air stacks without accidents, and also reduces the amount of throwaway air objects which get created.
This commit is contained in:
Dylan K. Taylor 2019-02-16 11:58:08 +00:00
parent 0ac7164b16
commit b252be1c7a
12 changed files with 24 additions and 20 deletions

View File

@ -95,7 +95,7 @@ abstract class BaseInventory implements Inventory{
}
public function getItem(int $index) : Item{
return $this->slots[$index] !== null ? clone $this->slots[$index] : ItemFactory::get(Item::AIR, 0, 0);
return $this->slots[$index] !== null ? clone $this->slots[$index] : ItemFactory::air();
}
/**
@ -105,13 +105,12 @@ abstract class BaseInventory implements Inventory{
*/
public function getContents(bool $includeEmpty = false) : array{
$contents = [];
$air = null;
foreach($this->slots as $i => $slot){
if($slot !== null){
$contents[$i] = clone $slot;
}elseif($includeEmpty){
$contents[$i] = $air ?? ($air = ItemFactory::get(Item::AIR, 0, 0));
$contents[$i] = ItemFactory::air();
}
}
@ -146,7 +145,7 @@ abstract class BaseInventory implements Inventory{
public function setItem(int $index, Item $item, bool $send = true) : bool{
if($item->isNull()){
$item = ItemFactory::get(Item::AIR, 0, 0);
$item = ItemFactory::air();
}else{
$item = clone $item;
}
@ -351,7 +350,7 @@ abstract class BaseInventory implements Inventory{
}
public function clear(int $index, bool $send = true) : bool{
return $this->setItem($index, ItemFactory::get(Item::AIR, 0, 0), $send);
return $this->setItem($index, ItemFactory::air(), $send);
}
public function clearAll(bool $send = true) : void{

View File

@ -176,7 +176,7 @@ class ShapedRecipe implements CraftingRecipe{
*/
public function getIngredient(int $x, int $y) : Item{
$exists = $this->ingredientList[$this->shape[$y]{$x}] ?? null;
return $exists !== null ? clone $exists : ItemFactory::get(Item::AIR, 0, 0);
return $exists !== null ? clone $exists : ItemFactory::air();
}
/**

View File

@ -34,7 +34,7 @@ use pocketmine\Player;
class DropItemAction extends InventoryAction{
public function __construct(Item $targetItem){
parent::__construct(ItemFactory::get(Item::AIR, 0, 0), $targetItem);
parent::__construct(ItemFactory::air(), $targetItem);
}
public function isValid(Player $source) : bool{

View File

@ -36,7 +36,7 @@ class FlintSteel extends Tool{
}
public function onActivate(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector) : ItemUseResult{
if($blockReplace->getId() === self::AIR){
if($blockReplace->getId() === Block::AIR){
$level = $player->getLevel();
assert($level !== null);
$level->setBlock($blockReplace, BlockFactory::get(Block::FIRE));

View File

@ -34,7 +34,7 @@ abstract class Food extends Item implements FoodSource{
* @return Item
*/
public function getResidue(){
return ItemFactory::get(Item::AIR, 0, 0);
return ItemFactory::air();
}
public function getAdditionalEffects() : array{

View File

@ -640,7 +640,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return Block
*/
public function getBlock() : Block{
return BlockFactory::get(self::AIR);
return BlockFactory::get(Block::AIR);
}
/**
@ -939,7 +939,7 @@ class Item implements ItemIds, \JsonSerializable{
$item = ItemFactory::fromString($idTag->getValue() . ":$meta");
}catch(\InvalidArgumentException $e){
//TODO: improve error handling
return ItemFactory::get(Item::AIR, 0, 0);
return ItemFactory::air();
}
$item->setCount($count);
}else{

View File

@ -47,6 +47,9 @@ class ItemFactory{
/** @var \SplFixedArray */
private static $list = [];
/** @var Item|null */
private static $air = null;
public static function init(){
self::$list = []; //in case of re-initializing
@ -423,6 +426,10 @@ class ItemFactory{
return $item;
}
public static function air() : Item{
return self::$air ?? (self::$air = self::get(ItemIds::AIR, 0, 0));
}
/**
* Returns whether the specified item ID is already registered in the item factory.
*

View File

@ -32,7 +32,6 @@ use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityExplodeEvent;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\particle\HugeExplodeSeedParticle;
use pocketmine\level\utils\SubChunkIteratorManager;
@ -199,7 +198,7 @@ class Explosion{
}
$air = ItemFactory::get(Item::AIR);
$air = ItemFactory::air();
$airBlock = BlockFactory::get(Block::AIR);
foreach($this->affectedBlocks as $block){

View File

@ -1674,7 +1674,7 @@ class Level implements ChunkManager, Metadatable{
$affectedBlocks = $target->getAffectedBlocks();
if($item === null){
$item = ItemFactory::get(Item::AIR, 0, 0);
$item = ItemFactory::air();
}
$drops = [];

View File

@ -26,7 +26,6 @@ namespace pocketmine\level\particle;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Skin;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\AddPlayerPacket;
@ -103,7 +102,7 @@ class FloatingTextParticle implements Particle{
$pk->username = $name;
$pk->entityRuntimeId = $this->entityId;
$pk->position = $pos; //TODO: check offset
$pk->item = ItemFactory::get(Item::AIR, 0, 0);
$pk->item = ItemFactory::air();
$flags = (
1 << Entity::DATA_FLAG_IMMOBILE

View File

@ -39,7 +39,7 @@ class FlowerPot extends Spawnable{
private $item;
public function __construct(Level $level, Vector3 $pos){
$this->item = ItemFactory::get(Item::AIR, 0, 0);
$this->item = ItemFactory::air();
parent::__construct($level, $pos);
}
@ -87,7 +87,7 @@ class FlowerPot extends Spawnable{
}
public function removeItem(){
$this->setItem(ItemFactory::get(Item::AIR, 0, 0));
$this->setItem(ItemFactory::air());
}
public function isEmpty() : bool{

View File

@ -42,7 +42,7 @@ class ItemFrame extends Spawnable{
private $itemDropChance = 1.0;
public function __construct(Level $level, Vector3 $pos){
$this->item = ItemFactory::get(Item::AIR, 0, 0);
$this->item = ItemFactory::air();
parent::__construct($level, $pos);
}
@ -72,7 +72,7 @@ class ItemFrame extends Spawnable{
if($item !== null and !$item->isNull()){
$this->item = clone $item;
}else{
$this->item = ItemFactory::get(Item::AIR, 0, 0);
$this->item = ItemFactory::air();
}
$this->onChanged();
}