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

Solved Add Block to Creative

Discussion in 'Development' started by WinterBuild7074, May 14, 2017.

  1. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I add a block(s) to Creative Block List?
    addCreativeItem() won't work because it must be be item to make it work. My error is:
    Code:
    TypeError: "Argument 1 passed to pocketmine\item\Item::addCreativeItem() must be an instance of pocketmine\item\Item
     
    Last edited: May 14, 2017
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    why are you trying to remove things via a self explanatory remove function?
     
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Sorry, I mean add not remove...
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    learning to read the error helps, PS it's hidden in BLIND SIGHT
     
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I understand the error, but I cannot find a addCreativeBlock() command. How can I do the same thing only for Blocks?
     
  6. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    There isn't a "command" the error clearly states it couldn't pass due to the first args not being an instance of pocketmine\item\Item.
     
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I make a block be an instance of pocketmine\item\Item? My block isn't an instance of it.
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    you can add the block to the creative inventory by extending the BlockItem class
     
  9. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Like this?
    PHP:
    class Main extends BlockItem implements Listener {
     
  10. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    no, I mean you have to make the item you are adding extend the ItemBlock class
     
    Last edited: May 15, 2017
    EdwardHamHam and corytortoise like this.
  11. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Like this?
    PHP:
    BlockItem::addCreativeItem();
     
  12. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    :facepalm:
    What does your code look like?
     
  13. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    PHP:
    <?php

    namespace Extra;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\utils\BlockIterator;

    class 
    Main extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
            
    Block::$list[201] = PurpurBlock::class;
            
    Item::addCreativeItem(new PurpurBlock());
        }
    }
    PurpurBlock is a block, not an item, and the function doesn't accept blocks, that's why it's called addCreativeItem()
     
  14. MalakasPlayzMC

    MalakasPlayzMC Spider Jockey

    Messages:
    37
    You could use this code:
    PHP:
    $player->getInventory()->setItem(Item::get(201))
     
  15. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    setItem requires an integer for the first parameter, and I don't think that would work correctly for Creative inventories.
    Can we see your PurpurBlock class?
     
  16. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    PHP:
    <?php

    namespace Extra;

    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\item\Tool;
    use 
    pocketmine\block\Solid;

    class 
    PurpurBlock extends Solid{
        
        public function 
    __construct($meta 0$count 1){
            
    parent::__construct(201$meta$count"Purpur Block");
        }
        
        public function 
    getName(){
            return 
    "Purpur Block";
        }
        
        public function 
    getHardness(){
            return 
    2;
        }
        
        public function 
    getToolType(){
            return 
    Tool::TYPE_PICKAXE;
        }
    }
     
  17. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    This is untrue. you can add a block to the creative inventory by making the class implement the ItemBlock class
     
    Last edited: May 15, 2017
  18. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Only need to change
    PHP:
    class PurpurBlock extends Solid
    to

    PHP:
    class PurpurBlock extends Solid implements BlockItem
    ? But where is the file called BlockItem? I cannot find one.
     
  19. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  20. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    PHP:
    use pocketmine\item\ItemBlock;

    class 
    PurpurBlock extends Solid implements BlockItem
    Code:
    ClassNotFoundException: "Class Extra\BlockItem not found" (EXCEPTION) in "/src/spl/BaseClassLoader" at line 144
    I know what the problem is, but I don't know how to fix...
     
  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.