So I found this code in the old styled pmmp forums. I want a code like this (which will drop custom items after breaking a block, Like HIve skywars ores forexample) public function blockBreak(BlockBreakEvent $event){ $block = $event->getBlock(); $player = $event->getPlayer(); $id = $block->getId(); if($id === Block::STONE){ switch(mt_rand(1, 200)){ case 1: $player->getLevel()->setBlock($block, new Block(Block::AIR), false, true); $player->getLevel()->dropItem($block, new Item(264, 0, 1)); $player->sendMessage("§e[§aJelly§6Craft§e] §bDiamond!"); break; case 2: $player->getLevel()->setBlock($block, new Block(Block::AIR), false, true); $player->getLevel()->dropItem($block, new Item(263, 0, 1)); $player->sendMessage("§e[§aJelly§6Craft§e] §0Coal!"); break; // [...] case 200: $player->getLevel()->setBlock($block, new Block(Block::AIR), false, true); $player->getLevel()->dropItem($block, new Item(4, 0, 1)); break; } } }
A bit of addon to what primus mentioned, You can use default in switch case if the rest of the values are the same... PHP: # DO:switch($value) { case 1: // Code 1 break; case 2: // Code 2 break; default: // Rest of the code}# DON'T:switch($value) { case 1: // Code 1 break; case 2: // Code 2 break; case 3: // Code 3 break; case 4: // Code 4 break; case 5: // Code 5 break; case 6: // Code 6 break;}
While better, it's not an approach I would take. It's not flexible, too much to write, and so much repeating yourself. Just use arrays, pluck out one item randomly, or use some arbitrary random weights. And if you have an option to use php8, then use the match expression: https://www.php.net/manual/en/control-structures.match.php