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

Percentage chance to

Discussion in 'Plugin Help' started by Juqxi, Mar 25, 2017.

  1. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    PHP:
    <?php

    namespace drop;

    use 
    pocketmine\plugin\PluginBase as P;
    use 
    pocketmine\event\Listener as L;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\MainLogger;
    use 
    pocketmine\block\Air;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\level\sound\PopSound as Pop;
    use 
    pocketmine\level\particle\LavaParticle as Lava;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\item\Item;

    class 
    Main extends implements L{
       
        public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
            
    $this->saveDefaultConfig();
            
    $this->getServer()->getLogger()->info(TextFormat::GREEN."[db] Włączony!");
        }

       
        public function 
    onBreak(BlockBreakEvent $e){
            
    $player $e->getPlayer();
            
    $block $e->getBlock();
            
    $gracz $e->getPlayer()->getName();
            if(
    $e->getBlock()->getId() == 1){
                 switch(
    mt_rand(1,80)){
             case 
    1:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (1) Emerald •");
             
    $player->getInventory()->addItem(Item::get(38801));
             break;
             case 
    2:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (5) Lapizu•");
             
    $player->getInventory()->addItem(Item::get(35145));
             break;
             case 
    3:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (1) Diament •");
             
    $player->getInventory()->addItem(Item::get(264,01));
             break;
             case 
    4:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (5) Biblioteczek •");
             
    $player->getInventory()->addItem(Item::get(47,05));
             break;
             case 
    5:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (5) Obsydianu•");
             
    $player->getInventory()->addItem(Item::get(49,05));
             break;
             case 
    6:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (2) Sztabki Złota •");
             
    $player->getInventory()->addItem(Item::get(266,02));
             break;
             case 
    7:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (1) Koxa •");
             
    $player->getInventory()->addItem(Item::get(322,01));
             break;
             case 
    8:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (1) Sztabke Żelaza•");
             
    $player->getInventory()->addItem(Item::get(265,01));
             break;
             case 
    9:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (3) Diamenty•");
             
    $player->getInventory()->addItem(Item::get(264,03));
             break;
             case 
    10:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (3) Obsydianu•");
             
    $player->getInventory()->addItem(Item::get(49,03));
             break;
             case 
    11:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (3) Jabłka•");
             
    $player->getInventory()->addItem(Item::get(260,03));
             break;
             case 
    12:
             
    $player->sendMessage("§e• [Drop] Znalazłeś (5)Sztabek Żelaza•");
             
    $player->getInventory()->addItem(Item::get(265,05));





    }
            }
        }
    }
    My question is how to make a different percentage chance to every of the case . Please reply
     
  2. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    I assume that you are trying to make chances variate for each item, am I correct?
     
  3. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    Yeah ;) I think I want that
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You mean..
    PHP:
    switch(mt_rand(180)){
        case 
    1:
            switch(
    mt_rand(13)){
                case 
    1:
                    break;
            }
            break;
    }
     
  5. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Try and make a function, maybe like this:
    PHP:
    /*
    * @param int $value
    * @param int $min
    * @param int $max
    *
    * @return boolean
    */
    public function isPercentage($value$min$max){
       
    $rand mt_rand($min$max);
       return (
    $rand $min && $rand <= $value)
    }
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    A percentage is taken out of 100. $max should be 100 and $min should be <= 100. :shoghi:
     
  7. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    Can u write that in my code for example give case 1 13,5% chance please ;)
     
  8. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    Can u write that in my code , give for example case 1 13,5% chance please ;)
     
  9. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    Sorry but I'm new in plugin development and still don't know how the code 100% work
     
  10. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Did you mean 13.5% or 13% and 5%?
    It isn't recommended to get started with the PocketMine API before you even know the principles of PHP.
     
    Muqsit and EdwardHamHam like this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    25% example.
    PHP:
    $percentage 25;
    if(
    mt_rand(1100) <= $percentage){
        
    //...25%
    }
     
    Juqxi likes this.
  12. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    Thanks bro u got like from me;)
     
  13. Juqxi

    Juqxi Spider Jockey

    Messages:
    33
    But now tell me is this will work
    PHP:
    switch mt_rand(1,100){
     if (
    mt_rand <=25){
      case 
    1:
                
    /* 25 % to do case 1 */
      
    break;}
    elseif (
    mt_rand <=30){
      case 
    2:
               
    /* 5% to do case 2 */
      
    break;}
    elseif (
    mt_rand <=40){
      case 
    3:
               
    /* 10% to do case 3 */
    break;}
    else {
    case 
    4:
               
    /* 60% to do case 4*/
    break; }
    Am i think good ? Btw written on phone
    Or will it works ?
    PHP:
    switch mt_rand(1,100){
     if (
    mt_rand <=25){
      case 
    1:
                
    /* 25 % to do case 1 */
      
    break;}
    elseif (
    mt_rand <=30){
      case 
    2:
               
    /* 5% to do case 2 */
      
    break;}
    elseif (
    mt_rand <=65){ 
      case 
    3:
               
    /* 10% to do case 3 */
    break;}
    else {
    case 
    4:
               
    /* 60% to do case 4*/
    break; }
    [/
    PHP
     
    Last edited: Mar 26, 2017
  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.