From ed4978c31b1def8c0b066933726c2b2d0dc724d9 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 7 Dec 2021 00:41:07 +0000 Subject: [PATCH] Added VanillaItems::AIR() we don't usually add VanillaItems entries for blocks since they already exist in VanillaBlocks, but air has a special use case specifically as an itemstack, so we make an exception for this case. --- src/block/tile/ItemFrame.php | 6 +++--- src/crafting/ShapedRecipe.php | 4 ++-- src/inventory/BaseInventory.php | 6 +++--- src/inventory/SimpleInventory.php | 6 +++--- src/inventory/transaction/TransactionBuilderInventory.php | 4 ++-- src/inventory/transaction/action/CreateItemAction.php | 4 ++-- src/inventory/transaction/action/DestroyItemAction.php | 4 ++-- src/inventory/transaction/action/DropItemAction.php | 4 ++-- src/item/Food.php | 2 +- src/item/Item.php | 2 +- src/item/ItemFactory.php | 4 ++++ src/item/VanillaItems.php | 3 +++ src/world/Explosion.php | 4 ++-- src/world/World.php | 4 ++-- 14 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/block/tile/ItemFrame.php b/src/block/tile/ItemFrame.php index 32d55f201..eb8ff3d32 100644 --- a/src/block/tile/ItemFrame.php +++ b/src/block/tile/ItemFrame.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\block\tile; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; use pocketmine\world\World; @@ -46,7 +46,7 @@ class ItemFrame extends Spawnable{ private $itemDropChance = 1.0; public function __construct(World $world, Vector3 $pos){ - $this->item = ItemFactory::air(); + $this->item = VanillaItems::AIR(); parent::__construct($world, $pos); } @@ -76,7 +76,7 @@ class ItemFrame extends Spawnable{ if($item !== null and !$item->isNull()){ $this->item = clone $item; }else{ - $this->item = ItemFactory::air(); + $this->item = VanillaItems::AIR(); } } diff --git a/src/crafting/ShapedRecipe.php b/src/crafting/ShapedRecipe.php index e346f22ad..5fc881a97 100644 --- a/src/crafting/ShapedRecipe.php +++ b/src/crafting/ShapedRecipe.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\crafting; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\utils\Utils; use function array_values; use function count; @@ -155,7 +155,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::air(); + return $exists !== null ? clone $exists : VanillaItems::AIR(); } /** diff --git a/src/inventory/BaseInventory.php b/src/inventory/BaseInventory.php index c5b854a6d..589254d85 100644 --- a/src/inventory/BaseInventory.php +++ b/src/inventory/BaseInventory.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\inventory; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\player\Player; use pocketmine\utils\ObjectSet; use function array_slice; @@ -89,7 +89,7 @@ abstract class BaseInventory implements Inventory{ public function setItem(int $index, Item $item) : void{ if($item->isNull()){ - $item = ItemFactory::air(); + $item = VanillaItems::AIR(); }else{ $item = clone $item; } @@ -290,7 +290,7 @@ abstract class BaseInventory implements Inventory{ } public function clear(int $index) : void{ - $this->setItem($index, ItemFactory::air()); + $this->setItem($index, VanillaItems::AIR()); } public function clearAll() : void{ diff --git a/src/inventory/SimpleInventory.php b/src/inventory/SimpleInventory.php index f05b527a2..cab3f06e3 100644 --- a/src/inventory/SimpleInventory.php +++ b/src/inventory/SimpleInventory.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\inventory; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; /** * This class provides a complete implementation of a regular inventory. @@ -49,7 +49,7 @@ class SimpleInventory extends BaseInventory{ } public function getItem(int $index) : Item{ - return $this->slots[$index] !== null ? clone $this->slots[$index] : ItemFactory::air(); + return $this->slots[$index] !== null ? clone $this->slots[$index] : VanillaItems::AIR(); } /** @@ -62,7 +62,7 @@ class SimpleInventory extends BaseInventory{ if($slot !== null){ $contents[$i] = clone $slot; }elseif($includeEmpty){ - $contents[$i] = ItemFactory::air(); + $contents[$i] = VanillaItems::AIR(); } } diff --git a/src/inventory/transaction/TransactionBuilderInventory.php b/src/inventory/transaction/TransactionBuilderInventory.php index 8888b4a45..58efacd16 100644 --- a/src/inventory/transaction/TransactionBuilderInventory.php +++ b/src/inventory/transaction/TransactionBuilderInventory.php @@ -27,7 +27,7 @@ use pocketmine\inventory\BaseInventory; use pocketmine\inventory\Inventory; use pocketmine\inventory\transaction\action\SlotChangeAction; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; /** * This class facilitates generating SlotChangeActions to build an inventory transaction. @@ -62,7 +62,7 @@ final class TransactionBuilderInventory extends BaseInventory{ protected function internalSetItem(int $index, Item $item) : void{ if(!$item->equalsExact($this->actualInventory->getItem($index))){ - $this->changedSlots[$index] = $item->isNull() ? ItemFactory::air() : clone $item; + $this->changedSlots[$index] = $item->isNull() ? VanillaItems::AIR() : clone $item; } } diff --git a/src/inventory/transaction/action/CreateItemAction.php b/src/inventory/transaction/action/CreateItemAction.php index dbe02798e..a7a28b47e 100644 --- a/src/inventory/transaction/action/CreateItemAction.php +++ b/src/inventory/transaction/action/CreateItemAction.php @@ -26,7 +26,7 @@ namespace pocketmine\inventory\transaction\action; use pocketmine\inventory\CreativeInventory; use pocketmine\inventory\transaction\TransactionValidationException; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\player\Player; /** @@ -36,7 +36,7 @@ use pocketmine\player\Player; class CreateItemAction extends InventoryAction{ public function __construct(Item $sourceItem){ - parent::__construct($sourceItem, ItemFactory::air()); + parent::__construct($sourceItem, VanillaItems::AIR()); } public function validate(Player $source) : void{ diff --git a/src/inventory/transaction/action/DestroyItemAction.php b/src/inventory/transaction/action/DestroyItemAction.php index 6edd30af2..a623d5700 100644 --- a/src/inventory/transaction/action/DestroyItemAction.php +++ b/src/inventory/transaction/action/DestroyItemAction.php @@ -25,7 +25,7 @@ namespace pocketmine\inventory\transaction\action; use pocketmine\inventory\transaction\TransactionValidationException; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\player\Player; /** @@ -35,7 +35,7 @@ use pocketmine\player\Player; class DestroyItemAction extends InventoryAction{ public function __construct(Item $targetItem){ - parent::__construct(ItemFactory::air(), $targetItem); + parent::__construct(VanillaItems::AIR(), $targetItem); } public function validate(Player $source) : void{ diff --git a/src/inventory/transaction/action/DropItemAction.php b/src/inventory/transaction/action/DropItemAction.php index 2e20d5f93..62db5abac 100644 --- a/src/inventory/transaction/action/DropItemAction.php +++ b/src/inventory/transaction/action/DropItemAction.php @@ -26,7 +26,7 @@ namespace pocketmine\inventory\transaction\action; use pocketmine\event\player\PlayerDropItemEvent; use pocketmine\inventory\transaction\TransactionValidationException; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\player\Player; /** @@ -35,7 +35,7 @@ use pocketmine\player\Player; class DropItemAction extends InventoryAction{ public function __construct(Item $targetItem){ - parent::__construct(ItemFactory::air(), $targetItem); + parent::__construct(VanillaItems::AIR(), $targetItem); } public function validate(Player $source) : void{ diff --git a/src/item/Food.php b/src/item/Food.php index ddaa77da6..e58924a30 100644 --- a/src/item/Food.php +++ b/src/item/Food.php @@ -32,7 +32,7 @@ abstract class Food extends Item implements FoodSourceItem{ } public function getResidue() : Item{ - return ItemFactory::air(); + return VanillaItems::AIR(); } public function getAdditionalEffects() : array{ diff --git a/src/item/Item.php b/src/item/Item.php index bceb92142..a1a3b8c89 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -691,7 +691,7 @@ class Item implements \JsonSerializable{ $item = LegacyStringToItemParser::getInstance()->parse($idTag->getValue() . ":$meta"); }catch(LegacyStringToItemParserException $e){ //TODO: improve error handling - return ItemFactory::air(); + return VanillaItems::AIR(); } $item->setCount($count); }else{ diff --git a/src/item/ItemFactory.php b/src/item/ItemFactory.php index 95622b4e9..ae6a596fb 100644 --- a/src/item/ItemFactory.php +++ b/src/item/ItemFactory.php @@ -478,6 +478,10 @@ class ItemFactory{ return $item; } + /** + * @deprecated + * @see VanillaItems::AIR() + */ public static function air() : Item{ return self::getInstance()->get(ItemIds::AIR, 0, 0); } diff --git a/src/item/VanillaItems.php b/src/item/VanillaItems.php index c817ae686..79676945e 100644 --- a/src/item/VanillaItems.php +++ b/src/item/VanillaItems.php @@ -32,6 +32,7 @@ use pocketmine\utils\CloningRegistryTrait; * @generate-registry-docblock * * @method static Boat ACACIA_BOAT() + * @method static ItemBlock AIR() * @method static Apple APPLE() * @method static Arrow ARROW() * @method static Potion AWKWARD_POTION() @@ -392,6 +393,8 @@ final class VanillaItems{ protected static function setup() : void{ $factory = ItemFactory::getInstance(); + self::register("air", $factory->get(ItemIds::AIR, 0, 0)); + self::register("acacia_boat", $factory->get(333, 4)); self::register("apple", $factory->get(260)); self::register("arrow", $factory->get(262)); diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 4fc1a1fb0..b02964dd6 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -32,7 +32,7 @@ use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityExplodeEvent; -use pocketmine\item\ItemFactory; +use pocketmine\item\VanillaItems; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Vector3; use pocketmine\world\format\SubChunk; @@ -208,7 +208,7 @@ class Explosion{ } } - $air = ItemFactory::air(); + $air = VanillaItems::AIR(); $airBlock = VanillaBlocks::AIR(); foreach($this->affectedBlocks as $block){ diff --git a/src/world/World.php b/src/world/World.php index 37236de22..22900f744 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -52,9 +52,9 @@ use pocketmine\event\world\ChunkUnloadEvent; use pocketmine\event\world\SpawnChangeEvent; use pocketmine\event\world\WorldSaveEvent; use pocketmine\item\Item; -use pocketmine\item\ItemFactory; use pocketmine\item\ItemUseResult; use pocketmine\item\LegacyStringToItemParser; +use pocketmine\item\VanillaItems; use pocketmine\lang\KnownTranslationFactory; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Vector3; @@ -1687,7 +1687,7 @@ class World implements ChunkManager{ $affectedBlocks = $target->getAffectedBlocks(); if($item === null){ - $item = ItemFactory::air(); + $item = VanillaItems::AIR(); } $drops = [];