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

Is this code Correct.

Discussion in 'Plugin Help' started by Omama Salehin, Jul 28, 2021.

  1. Omama Salehin

    Omama Salehin Spider

    Messages:
    11
    GitHub:
    GreenTeam
    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;
    }
    }
    }
     
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
  3. Omama Salehin

    Omama Salehin Spider

    Messages:
    11
    GitHub:
    GreenTeam
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    Soo, you're going to write 200 cases?
     
    NTT and minijaham like this.
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    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;
    }
     
    Agent, NTT and Primus like this.
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    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
     
    NTT and minijaham 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.