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

Solved BlockBreakEvent Error

Discussion in 'Plugin Help' started by Lupi, Mar 29, 2020.

  1. Lupi

    Lupi Creeper

    Messages:
    4
    (my english is bad, sorry)

    My plugin with BlockBreakEvent sometimes gives an error (Internal Server Error), code:

    PHP:
    <?php

    namespace BlockBreak;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\item\Item;

    class 
    Main extends PluginBase implements Listener{
     
     
        public function 
    onLoad(){
         
            
    $this->getLogger()->info("Loading the plugin...");
        }
     
        public function 
    onEnable(){
         
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("Plugin is enabled");
        }
     
        public function 
    blockBr(BlockBreakEvent $event){
         
            
    $block=$event->getBlock();
            
    $items = ["0:0:0" => 90"406:0:1" => 5"352:0:1" => 5];
            
    $player=$event->getPlayer();
         
         
            if(
    $block->getId() == 1){
                if(
    $player->getGamemode() == 0){
             
            
    $random mt_rand(0100);
            
    $w 0;
         
            foreach(
    $items as $id => $percentage){
             
                
    $w += $percentage;
             
                if(
    $w $random){
                 
                    
    $data explode(":"$id);
                    
    $items Item::get($data[0], $data[1], $data[2]);
                }
                break;
            }
         
            
    $drops $event->getDrops();
            
    $drops[] = $items;
            
    $event->setDrops($drops);
                }
            }
        }
    }
    ?>
    Error:

    TypeError: "Argument 2 passed to pocketmine\event\block\BlockBreakEvent::setDropsVariadic() must be an instance of pocketmine\item\Item, array given, called in phar:///home/PocketMine-MP.phar/src/pocketmine/event/block/BlockBreakEvent.php on line 111" (EXCEPTION) in "src/pocketmine/event/block/BlockBreakEvent" at line 119

    please help, i am new at this ._.
     
    Last edited: Mar 29, 2020
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    PHPs random function is inclusive, that means 0 is the lowest possible number that can be returned and 100 is the highest possible number that can be returned in your case.
    Your check is if ($w > $random), the highest number $w can have is 100, the highest number $random can have is also 100, because 100 is not larger than 100, the code inside the if will not run and $items will not be overwritten (generally you shouldn't use the same variable for multiple different things because a bug in the code can then generate weird behaviour or confusing error messages as seen here).
    To fix this, just use mt_rand(0, 99) instead :)
     
    Lupi likes this.
  3. Lupi

    Lupi Creeper

    Messages:
    4
    same error :(

    PHP:
    <?php

    namespace BlockBreak;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\item\Item;

    class 
    Main extends PluginBase implements Listener{
        
        
        public function 
    onLoad(){
            
            
    $this->getLogger()->info("Loading the plugin...");
        }
        
        public function 
    onEnable(){
            
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("Plugin was enabled");
        }
        
        public function 
    blockBr(BlockBreakEvent $event){
            
            
    $block=$event->getBlock();
            
    $items = ["0:0:0" => 90"406:0:1" => 5"352:0:1" => 5];
            
    $player=$event->getPlayer();
            
            
            if(
    $block->getId() == 1){
                if(
    $player->getGamemode() == 0){
                
            
    $random mt_rand(099);
            
    $w 0;
            
            foreach(
    $items as $id => $percentage){
                
                
    $w += $percentage;
                
                if(
    $w $random){
                    
                    
    $data explode(":"$id);
                    
    $items Item::get($data[0], $data[1], $data[2]);
                }
                break;
            }
            
            
    $drops $event->getDrops();
            
    $drops[] = $items;
            
    $event->setDrops($drops);
                }
            }
        }
    }
    ?>
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    The break; should be inside the if.
     
    Lupi likes this.
  5. Lupi

    Lupi Creeper

    Messages:
    4
    Thank you. It works!

    What I need to use to set the random amount of items?
    Code:
    setAmount(mt_rand(1,3))
    or what?
     
    HimbeersaftLP likes this.
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You can just put it in there:
    PHP:
    Item::get($data[0], $data[1], mt_rand(13));
     
    Lupi likes this.
  7. Lupi

    Lupi Creeper

    Messages:
    4
    It works.

    Thank you. ;)
     
    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.