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. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    That issue was previously resolved. Please look at the LATEST post next time.
     
  2. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    Calm down. WHEN i posted my REPLY it just loaded what the ANSWER was.
     
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    My code:
    PHP:
    <?php

    namespace Extra;

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

    class 
    PurpurBlock extends ItemBlock{
      
        public function 
    __construct($meta 0$count 1){
           
    parent::__construct(201$meta$count"Purpur Block");
        }
      
        public function 
    getHardness(){
            return 
    2;
        }
      
        public function 
    getToolType(){
            return 
    Tool::TYPE_PICKAXE;
        }
    }
    My error:
    Code:
    TypeError: "Argument 1 passed to pocketmine\item\ItemBlock::__construct() must be an instance of pocketmine\block\Block, integer given, called in /home/mcpe/plugins/Extra/src/Extra/PurpurBlock.php on line 13" (EXCEPTION) in "/src/pocketmine/item/ItemBlock" at line 30
    This is because he doesn't know any block with the ID 201. But how can I create that ID and set the Purpur Block to it?
     
    Last edited: May 16, 2017
  4. EdwardHamHam

    EdwardHamHam Skeleton

    Messages:
    962
    GitHub:
    edwardhamham
    The error is with line 30, but we can't see that much.
     
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    1. Line 13, not 30.
    2. How can I now add that blocks to creative?
    3. How do I add the block's ID (201) to the BlockIDs?

    The error wants me to change this
    PHP:
    parent::__construct(201$meta$count"Purpur Block");
    to this
    PHP:
    parent::__construct(Block::get(201), $meta$count"Purpur Block");
    I think. But there's no block with ID 201, but how do I add it? Or do I not need to do it?
    I tried adding the Shulder Shell ID (445), and it worked, it appeared in the Creative List. But then I tried typing in the ID of Purpur Block (201), and it crashes my server, no crashdump.

    Plus, this code won't add my block to the Creative List:
    PHP:
    Item::addCreativeItem(new PurpurBlock());
     
    Last edited: May 16, 2017
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    This should work for that you need
     
    Last edited: May 16, 2017
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    PHP:
    ItemBlock::addCreativeItem(new PurpurItem());
    Why PurpurItem? PurpurBlock you mean?
    There's no class with that name.
     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    He created one in his example.
     
  9. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Because the name PurpurBlock was already used?
     
  10. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I put the main class in Main.php and the classes PurpurBlock and PurpurItem are in a file called PurpurBlock.php.
    Code:
    ClassNotFoundException: "Class Extra\PurpurItem not found" (EXCEPTION) in "/src/spl/BaseClassLoader" at line 144
    I tried use Extra\PurpurBlock; but that didn't fix my problem.
     
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    I made the Item and Block classes separate because, as I previously stated, they need to be separate.
     
  12. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Don't just copy code. The script I made was designed to run as a single-file plugin script, not as a phar plugin. You will need to separate the classes into their own files for it to work in the folder/phar format
     
  13. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I make it work as folder? I don't wan to put everything into 1 file.
     
  14. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    As previously stated, you will need to separate the classes into their own files for it to work in the folder/phar format.
     
    WinterBuild7074 likes this.
  15. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Is it possible to change the position of the Purpur Block "icon" in the Creative List?
     
  16. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  17. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I tried adding the End Rod and the Ender Pearl to Creative, but they disppear after placing, and Ender Pearls can't be thrown.
     
  18. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    ender pearls are items and entities, not blocks. that is more complex. End rods need proper metadata for direction and position.
     
  19. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I don't know how to do it.

    1. My End Rod code:
    PHP:
    <?php

    namespace Extra;

    use 
    pocketmine\block\Block;
    use 
    pocketmine\block\Solid;
    use 
    pocketmine\Player;
    use 
    pocketmine\block\Flowable;
    use 
    pocketmine\item\ItemBlock;

    class 
    EndRod extends Flowable {
        public function 
    __construct() {
            
    parent::__construct(208);
        }
       
        public function 
    getHardness() {
            return 
    0;
        }
       
        public function 
    getName(){
            return 
    "End Rod";
        }
       
        public function 
    getLightLevel(){
            return 
    14;
        }
       
        public function 
    getResistance(){
            return 
    0;
        }
       
        public function 
    place(Item $itemBlock $blockBlock $target$face$fx$fy$fzPlayer $player null){
                
    $faces = [
                    
    => 0,
                    
    => 1,
                    
    => 3,
                    
    => 2,
                    
    => 5,
                    
    => 4,
                ];
                
    $this->meta = ($target->getId() === 208 && $faces[$face] == $target->getDamage()) ? Vector3::getOppositeSide($faces[$face]) : $faces[$face];
                
    $this->getLevel()->setBlock($block$thistruetrue);
                return 
    true;
        }

    }
    2. My Ender Pearl code:
    PHP:
    <?php

    namespace Extra;

    use 
    pocketmine\item\Item;

    class 
    EnderPearl extends Item{
        public function 
    __construct($meta 0$count 1){
            
    parent::__construct(368$meta$count"Ender Pearl");
        }
        public function 
    getMaxStackSize() : int {
            return 
    16;
        }
    }
     
  20. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    end rod should extend Transparent, not flowable.
    The Ender pearl will not work on it's own as an item. in order to be able to throw it, you need to make it's corresponding entity class and link it to the Item class.
     
    HimbeersaftLP likes this.
  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.