How do I do make a random chance thing in php?

Discussion in 'Facepalm' started by DiamondGamer30, Nov 9, 2017.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    177
    GitHub:
    diamondgamermcpe
    I'm trying to make an item gettable on BlockBreakEvent but I don't know how to set up chances, can anyone help?
     
  2. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    use mt_rand on onBreakBlockEvent and dont forgot setDrop();

    PHP:
    mt_rand(130// 1 chance out of 30
    mt_rand(1999// 1 chance out of 999
    mt_rand(10,100// 10 chance out of 100
     
    Last edited: Nov 9, 2017
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    In your BlockBreakEvent method:
    PHP:

    $ch
    /*
     * @var Player $player;
     */
    $chance 35// chance in percentage
    $random round(mt_rand(1, ($chance) * 100)); // round it to make an integer
    if($random == 1) { // 35% chance to get a diamond (264)
        
    $drops $event->getDrops();
        
    $drops[] = Item::get(264);
        
    $event->setDrops($drops);
    }
    If you want to give each item a specific percentage to drop:
    PHP:
    /*
     * @var Player $player;
     */
    $items = ["357:0:8" => 50"388:0:1" => 20"399:0:1" => 20"293:0:1" => 10]; // key = <ID>:<meta>:<amount>, value = percentage
    // The sum of ALL values together needs to be 100
    // Example: ""351:3:32" => 50" will be "32 Cocoa Beans => Drop chance 50%"

    $random mt_rand(0100);
    $w 0;
    foreach(
    $items as $id => $percentage) { // chooses a number
        
    $w += $percentage;
        if (
    $w $random) {
            
    $data explode(":"$id);
            
    $item Item::get($data[0], $data[1], $data[2]);
            break;
        }
    }

    $drops $event->getDrops();
    $drops[] = $item;
    $event->setDrops($drops); // drop the item
    If you want 10% chance to drop an emerald and 15% to drop a diamond (the rest 65% will be nothing), just use:

    PHP:
    $items = ["388:0:1" => 10"264:0:1" => 15"0:0:0" => 65]; // 0 = air
     
    Last edited: Nov 9, 2017
  4. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    Undefined $item
     
  5. QuiverlyRivalry

    QuiverlyRivalry Zombie Pigman

    Messages:
    490
    GitHub:
    quiverlyrivalry
    Its items
     
  6. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    No $item
     
  7. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,351
    GitHub:
    robske110
    I think you might have defined the array in a way that the values counted together are not 100.
     
  8. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    he closed the loops he only defined the $items array and not $item
     
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,143
    GitHub:
    Thunder33345
    but if the case does run correctly it would be defined
     
    ItzAribie likes this.
  10. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    would not do it like that anyway it's a bad way.
    thank you for understanding me
     
  11. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,143
    GitHub:
    Thunder33345
    you should have said it's a bad practice to check if it's not defined in case of a runtime error
     
    ItzAribie likes this.
  12. ItzAribie

    ItzAribie Spider Jockey

    Messages:
    41
    it's exactly that what i said with another word.
     
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,493
    GitHub:
    jasonwynn10
    Development section
     
  14. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,351
    GitHub:
    robske110
    I don't think you're helpful with that. Everyone knows that this is supposed to be there, and OP probably won't understand what you intend to communicate.
     
    SOFe, iiFlamiinBlaze and Thunder33345 like this.
  15. Phqzing

    Phqzing Spider

    Messages:
    9
    GitHub:
    phqzing
    How do I use this but instead of blockbreak event it'll be everytime I touch the chest with a key and jt gives custom renamed ITems
     
  16. QuiverlyRivalry

    QuiverlyRivalry Zombie Pigman

    Messages:
    490
    GitHub:
    quiverlyrivalry
    Use interactevent; check if touching chest and item in hand is equal to key; and if it is give what you wanted to.
     

Share This Page

  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.