From b252be1c7a0976a91030f93360eaa17731e1374b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 16 Feb 2019 11:58:08 +0000 Subject: [PATCH] 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. --- src/pocketmine/inventory/BaseInventory.php | 9 ++++----- src/pocketmine/inventory/ShapedRecipe.php | 2 +- .../inventory/transaction/action/DropItemAction.php | 2 +- src/pocketmine/item/FlintSteel.php | 2 +- src/pocketmine/item/Food.php | 2 +- src/pocketmine/item/Item.php | 4 ++-- src/pocketmine/item/ItemFactory.php | 7 +++++++ src/pocketmine/level/Explosion.php | 3 +-- src/pocketmine/level/Level.php | 2 +- src/pocketmine/level/particle/FloatingTextParticle.php | 3 +-- src/pocketmine/tile/FlowerPot.php | 4 ++-- src/pocketmine/tile/ItemFrame.php | 4 ++-- 12 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 4ac258e93..974ba3b26 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -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{ diff --git a/src/pocketmine/inventory/ShapedRecipe.php b/src/pocketmine/inventory/ShapedRecipe.php index 13eb4b9d1..16e46d569 100644 --- a/src/pocketmine/inventory/ShapedRecipe.php +++ b/src/pocketmine/inventory/ShapedRecipe.php @@ -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(); } /** diff --git a/src/pocketmine/inventory/transaction/action/DropItemAction.php b/src/pocketmine/inventory/transaction/action/DropItemAction.php index fcbe694d8..692af129f 100644 --- a/src/pocketmine/inventory/transaction/action/DropItemAction.php +++ b/src/pocketmine/inventory/transaction/action/DropItemAction.php @@ -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{ diff --git a/src/pocketmine/item/FlintSteel.php b/src/pocketmine/item/FlintSteel.php index f36b687fa..bb1d94dfd 100644 --- a/src/pocketmine/item/FlintSteel.php +++ b/src/pocketmine/item/FlintSteel.php @@ -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)); diff --git a/src/pocketmine/item/Food.php b/src/pocketmine/item/Food.php index 36f4cee4f..b880280c9 100644 --- a/src/pocketmine/item/Food.php +++ b/src/pocketmine/item/Food.php @@ -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{ diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index d877b204e..743ccb81b 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -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{ diff --git a/src/pocketmine/item/ItemFactory.php b/src/pocketmine/item/ItemFactory.php index 3b671d65f..3192cdd7c 100644 --- a/src/pocketmine/item/ItemFactory.php +++ b/src/pocketmine/item/ItemFactory.php @@ -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. * diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index c58ffe059..2b93386dd 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -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){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 127f2c6a8..297c067bd 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -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 = []; diff --git a/src/pocketmine/level/particle/FloatingTextParticle.php b/src/pocketmine/level/particle/FloatingTextParticle.php index 043e4435c..e800a1c74 100644 --- a/src/pocketmine/level/particle/FloatingTextParticle.php +++ b/src/pocketmine/level/particle/FloatingTextParticle.php @@ -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 diff --git a/src/pocketmine/tile/FlowerPot.php b/src/pocketmine/tile/FlowerPot.php index b8aefefc6..977391db1 100644 --- a/src/pocketmine/tile/FlowerPot.php +++ b/src/pocketmine/tile/FlowerPot.php @@ -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{ diff --git a/src/pocketmine/tile/ItemFrame.php b/src/pocketmine/tile/ItemFrame.php index a4c510817..da6d3792f 100644 --- a/src/pocketmine/tile/ItemFrame.php +++ b/src/pocketmine/tile/ItemFrame.php @@ -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(); }