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
    I tried this, but it totally crashed my server:
    PHP:
    class PurpurBlock extends Solid implements ItemBlock
    Code:
    Extra\PurpurBlock cannot implement pocketmine\item\ItemBlock - it is not an interface
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    I already said, you need to extend the ItemBlock class
     
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How? Like this?
    PHP:
    class PurpurBlock extends ItemBlock {
        
    blablabla
    }
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    That looks better. I believe that is can be added to the creative inventory at this point. Try testing a few things on your own. ;)
     
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Server Crashdump:
    Code:
    Error: Cannot override final method pocketmine\item\Item::getName()
    File: /Extra/src/Extra/PurpurBlock
    Line: 0
    Type: E_COMPILE_ERROR
    
    THIS CRASH WAS CAUSED BY A PLUGIN
    BAD PLUGIN: Extra v2
    
    Code:
    [1] <?php
    [2] 
    [3] namespace Extra;
    [4] 
    [5] use pocketmine\item\Item;
    [6] use pocketmine\block\Block;
    [7] use pocketmine\item\Tool;
    [8] use pocketmine\block\Solid;
    [9] use pocketmine\item\ItemBlock;
    [10] 
    
    PHP:
    <?php

    namespace Extra;

    use 
    pocketmine\item\Item;
    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 
    getName(){
            return 
    "Purpur Block";
        }
       
        public function 
    getHardness(){
            return 
    2;
        }
       
        public function 
    getToolType(){
            return 
    Tool::TYPE_PICKAXE;
        }
    }
     
  6. EdwardHamHam

    EdwardHamHam Skeleton

    Messages:
    962
    GitHub:
    edwardhamham
    You can't overide essential parts of other files.
     
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I now fix this?
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you seem to be lacking knowledge of OOP
    like you dont know keywords of final and what's not implemented, you are advised to learn more on OOP
    if you dont care and just want to know how to fix it,
    remove getname() should do it, i hope...
     
  9. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    But if I remove getName(), the block won't have a name?

    I removed getName(), the 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
    I understand the error, but a mistake in every single line? Maybe it's impossible to add a new block with a plugin?
     
  10. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    The item and block classes must be separate
     
  11. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    What do you mean? How do I do that?
     
  12. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    As stated, make two separate classes. One for the block. One for the ItemBlock.
     
    EdwardHamHam likes this.
  13. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Uh, I not a single idea how to do that. I know how to create a class, but I don't know what to put where.
    Shall I put the constructor into Block or ItemBlock?What should be the class name of ItemBlock? What should be the class name of Block? Where shall I put things like getHardness() and etc.?
     
  14. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Maybe like this?

    PHP:
    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;
        }
    }

    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;
        }
    }
    Sorry if this is only :facepalm: to you. I learned PHP in Codecademy, but it won't help me much with this.
     
    Last edited: May 15, 2017
  15. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Extend an auctual block, you cannot create blocks that do not exist. I can't create my own block but I can extend a block ex) Stone and give it a damage value and it could be used for something. It would be a new block. You can also add items to the creative inventory with Item::addCreativeItem();. You don't register new ItemBlocks either, PM creates an "ItemBlock" automatically when you give a player an item with an ID over 256.
     
  16. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    MCPE won't recognize a block with ID 1999 because it doesn't exist in vanilla MCPE.
     
  17. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Yes, but what will the whole right code be? So many errors!
    Explaining won't help me. I'm an extremely beginner I don't know anything.
     
  18. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    PHP:
    namespace MyPlugin;

    use 
    pocketmine/block/Block;

    class 
    MyBlock extends Block{

    protected 
    $id 201;

    public function 
    __construct($meta 1)//use 1 because we are using a block that exists
    {
       
    $this-meta $meta;
    }
    }
        
    My apologies for the formatting there (hard to code on a phone).
     
  19. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    it's probably because there's more error
    again a file can have more then one errors

    beginner or not PHP OOP and namespace are needed to even begin making PMMP plugin
    regardless of a beginner or advance mountain climber they both will carry necessary tools, advance ones may carry extra tools to make their climb easier but these aren't required
    and no one should ever attempt to climb a mountain bringing nothing
    you dont just go climbing by copying what others are doing
     
  20. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    not "BlockItem" it is "ItemBlock" please look over your code FULLY next time
     
  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.