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

setDrops() on BlockBreakEvent?

Discussion in 'Development' started by Zuruki, Apr 9, 2017.

  1. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    How would I make iron ore drop iron ingots on breaking it?
     
    WinterBuild7074 likes this.
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    define it
    example, make leave drop iron
    on block break
    if event-getblock == leaveblocks
    drops = event->getdrops
    drops[] = new Item(ironingot)
    event->setdrop(drops)

    Warning: DO NOT COPY THIS IS A CONCEPT AND WILL NOT RUN IN PMMP
     
    Primus likes this.
  3. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Can you put it in the code format please? I can't really read it xD
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    This would add an iron ingot to the existing iron ore drop, not in place of, so it would really be better to create a new array to add the drop to than to use the array from item->getDrops.
     
    Primus likes this.
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You don't really need the whole thing as long as you understand how arrays work. Try taking a look at pocketmine\event\block\BlockBreakEvent and it's functions. You can use $event->getDrops(); to get the current array of drops, and $event->setDrops($array); to set the drops. The rest is just listening for the event, checking the block id, defining items, etc.
     
    Primus likes this.
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    OP never stated anything regarding to replace it or not, so i just figured i will use the add an iron ingot method
     
    Primus likes this.
  7. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    I did mean replace it, sorry
     
    Primus likes this.
  8. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    PHP:
    public $arrays = array(Item::get(ITEM::GOLD_ORE));
    //or
    (Item::get(14,0,0));
    PHP:
    if ($e->getBlock() == (Item::get(14,0,1) {
    $drops $e->getDrops();
    drops[] = new Item(Item::get(14,0,1));
    $e->setDrops($drops);
    Tried my hardest lol. Any errors?
     
    Primus likes this.
  9. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I assumed you knew arrays and how to work with item objects and IDs. I was wrong. XD

    More like this:
    PHP:
     public function onBreak(BlockBreakEvent $event) {
      if(
    $event->getBlock()->getId() === Block::IRON_ORE) {
       
    $drops = array();
       
    $drops[] = Item::get(Item::IRON_INGOT01);
       
    $event->setDrops($drops);
      }
     }
     
  10. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Hehe, thankyou for your help. I'm still currently learning OOP, so bear with me ;)
     
    OnTheVerge and Primus like this.
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    lol gg nice try!
    Here's some constructive criticism:
    If you are checking a block's type, you should check by ID instead of class.
    PHP:
    if ($e->getBlock()->getId() === 14) {}
    When you made the new Item class on line 4, it was unnecessary because Item::get() returns an item anyway
    If you were going to use a new Item instead, you would have to input the correct parameters
    PHP:
    new Item(14);
    Please don't take this the wrong way. I am just pointing these mistakes to help you learn ;)
     
    OnTheVerge, Zuruki and corytortoise like this.
  12. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I want to make a plugin like this too, but my code doesn't work – please help:
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;

    class 
    Main extends PluginBase {
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = new Item(155);
                
    $event->setDrops($drops);
            }
         }
    }
     
  13. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you forgot to register event listener
     
  14. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Um, like this?
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = Item::get(Item::IRON_INGOT01);
                
    $event->setDrops($drops);
            }
         }
    }
     
  15. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    something like
    $this->getServer()->getPluginManager()->registerEvents(listener, plugin base);
     
  16. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    OK, is this correct?
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents(listenerplugin base);
        }
        
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = Item::get(Item::IRON_INGOT01);
                
    $event->setDrops($drops);
            }
         }
    }
     
  17. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    no, you dont just copy the code
     
    jasonwynn10 likes this.
  18. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    This?
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {

        
    $this->getServer()->getPluginManager()->registerEvents(listenerplugin base);
        
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = Item::get(Item::IRON_INGOT01);
                
    $event->setDrops($drops);
            }
         }
    }
     
  19. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I tried breaking a Gold Ore (ID 14), but it doesn't drop a Quartz Block (ID 155). It drops a Gold Ore like always.
     
  20. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    This is why you should learn PHP. I'm surprised you didn't get an error.
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {
        
        public function 
    onEnable() {
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = Item::get(Item::IRON_INGOT01);
                
    $event->setDrops($drops);
            }
         }
    }
    I'll explain why this is correct and yours wasn't if you want, but I feel like it would be pointless at this point.
     
    jasonwynn10 and Zuruki like 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.