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

@ -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.
*