1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Help registering block in block factory

Discussion in 'Development' started by mm899, May 15, 2019.

  1. mm899

    mm899 Spider Jockey

    Messages:
    32
    Hi im just testing/messing around with different things, I wanted to add my own method to the PocketMine block class...how would I go about do this. This is so I can do something like $event->getBlock->iSInArena() (example).
    Should I just create a new 'CustomBlock' which extends 'Block'?
     
  2. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    if you do the 'CustomClass' you would have to check if the $block is an instanceof CustomClass
     
    mm899 likes this.
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    You can register a new block by extending the old block class you want to replace. or you can create a new block altogether as long as you have all the necessary functions.
    PHP:
    namespace jasonwynn10\CustomBlockClass {
        use 
    pocketmine\block\BlockFactory;
        use 
    pocketmine\block\Dirt;
        use 
    pocketmine\event\Listener;
        use 
    pocketmine\plugin\PluginBase;
        class 
    Main extends PluginBase implements Listener {
            public function 
    onEnable() {
                
    BlockFactory::registerBlock(new CustomBlock(), true);
            }
        }
        class 
    CustomBlock extends Dirt {
            public function 
    isInArena() : bool {
                return 
    true// or whatever code you have
            
    }
        }
    }
    Note: this does not add the function to all block classes. you need to check if the block from the event is an instance of your registered class before attempting to use your function.
     
    mm899 and GamakCZ like this.
  4. mm899

    mm899 Spider Jockey

    Messages:
    32
    Thanks for the help, much appreciated, I had done that before but it hadn't occurred to me that I had to check if the block was an instance of the custom block, nonetheless thanks
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.