Added API methods to determine if a block or item is already registered

This commit is contained in:
Dylan K. Taylor 2017-09-11 16:22:55 +01:00
parent aaa3b6e59a
commit 07268e4b37
2 changed files with 23 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}