From 07268e4b373f55d45497b2fff71708112d8b39e7 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 11 Sep 2017 16:22:55 +0100 Subject: [PATCH] Added API methods to determine if a block or item is already registered --- src/pocketmine/block/BlockFactory.php | 13 ++++++++++++- src/pocketmine/item/ItemFactory.php | 12 +++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pocketmine/block/BlockFactory.php b/src/pocketmine/block/BlockFactory.php index 2891407d2..dbd65d811 100644 --- a/src/pocketmine/block/BlockFactory.php +++ b/src/pocketmine/block/BlockFactory.php @@ -341,7 +341,7 @@ class BlockFactory{ public static function registerBlock(Block $block, bool $override = false){ $id = $block->getId(); - if(self::$list[$id] !== null and !(self::$list[$id] instanceof UnknownBlock) and !$override){ + if(!$override and self::isRegistered($id)){ throw new \RuntimeException("Trying to overwrite an already registered block"); } @@ -403,4 +403,15 @@ class BlockFactory{ public static function getBlockStatesArray() : \SplFixedArray{ return self::$fullList; } + + /** + * Returns whether a specified block ID is already registered in the block factory. + * + * @param int $id + * @return bool + */ + public static function isRegistered(int $id) : bool{ + $b = self::$list[$id]; + return $b !== null and !($b instanceof UnknownBlock); + } } \ No newline at end of file diff --git a/src/pocketmine/item/ItemFactory.php b/src/pocketmine/item/ItemFactory.php index cecd30e11..6075dfe82 100644 --- a/src/pocketmine/item/ItemFactory.php +++ b/src/pocketmine/item/ItemFactory.php @@ -256,7 +256,7 @@ class ItemFactory{ */ public static function registerItem(Item $item, bool $override = false){ $id = $item->getId(); - if(!$override and self::$list[$id] !== null){ + if(!$override and self::isRegistered($id)){ throw new \RuntimeException("Trying to overwrite an already registered item"); } @@ -350,4 +350,14 @@ class ItemFactory{ return $item; } } + + /** + * Returns whether the specified item ID is already registered in the item factory. + * + * @param int $id + * @return bool + */ + public static function isRegistered(int $id) : bool{ + return self::$list[$id] !== null; + } } \ No newline at end of file