(my english is bad, sorry) My plugin with BlockBreakEvent sometimes gives an error (Internal Server Error), code: PHP: <?phpnamespace 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(0, 100); $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 ._.
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
same error PHP: <?phpnamespace 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(0, 99); $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); } } }}?>
Thank you. It works! What I need to use to set the random amount of items? Code: setAmount(mt_rand(1,3)) or what?