Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

StoneDrop plugin help

Comments in 'Plugin Development' started by KubixQ, Dec 31, 2015.

  1. KubixQ
    Offline

    KubixQ Active Member

    Joined:
    Sep 13, 2015
    Posts:
    85
    Minecraft User:
    KubixQ
    Hey, I'm created StoneDrop plugin (dimonds, iron, coal etc. drops from stone).
    I have a question, how to set for example; chance for drop diamond from stone: 1:300 , iron ingot from stone 1:200 etc..?
    This is my code (all minerals have the same chnce [1:300]):
    PHP:
        if($e->getBlock()->getId() === Block::STONE$e->setDrops([Item::get($ores[mt_rand(0300)])]);
        }

    }
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    switch(mt_rand(0300)){
       case 
    5//do something
    }
    Vaivez66 likes this.
  3. KubixQ
    Offline

    KubixQ Active Member

    Joined:
    Sep 13, 2015
    Posts:
    85
    Minecraft User:
    KubixQ
    Where do I put this? xD
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    mt_rand(1$max) <= $perc
    returns true for $perc/$max chance. (Try putting $perc=0 and $perc=$max to prove this concept)
  5. KubixQ
    Offline

    KubixQ Active Member

    Joined:
    Sep 13, 2015
    Posts:
    85
    Minecraft User:
    KubixQ
    What is $perc?
    idk where put this
    PHP:
    switch(mt_rand(0300)){
     case 
    5:
    }
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    $perc is the numerator of chance. $max is the denominator of chance.
    Ignore what @CraftYourBukkit had said. Why did he even write 'case 5'? Whatever case you put there it has a 1/301 chance to run.

    For example, if you want to run the code for 7% chance, i.e. 7/100 chance:
    PHP:
    if(mt_rand(1100) <= 7run_code();
    If you want to run the code for 20/1000 chance:
    PHP:
    if(mt_rand(11000) <= 20run_code();
    Or for 2% chance do_something() and 3% chance do_something_2() and another 5% chance do_something_3(): (but only one or none should happen)
    PHP:
    $rand mt_rand(1100);
    if((
    $rand -= 2) <= 0){
      
    do_something();
    }elseif((
    $rand -= 3) <= 0){
      
    do_something_2();
    }elseif((
    $rand -= 5) <= 0){
      
    do_something_3();
    }
    This avoids the need to recalculate the random number. If you want multiple to be able to happen, random with their L.C.M. and check with division and modulus arithmetic operators (/ and %) https://php.net/operators.arithmetic
  7. KubixQ
    Offline

    KubixQ Active Member

    Joined:
    Sep 13, 2015
    Posts:
    85
    Minecraft User:
    KubixQ
    Ok, so I don't want to run code with e. g. 7% chance, I want to set drop from stone;; 10% chance for drop iron ingot, 5% for drop diamond, 25% chance for drop coal [..] and so on..
    Can you edit this; code for me?
  8. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Case 5 was an example.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why case 5? Whatever case it is it still just has 1% 1/301 chance to run, so you're making it a bit misleading.
    In that case, it should be better to simply use if.
  10. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    As I said, it just was an example. Of course you could use whatever case you want to, and I used case 5:)
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Usually, in programming, if we want to use a random mnumber, we would just take 0 or 1 for integers, or special numbers like pi or golden ratio for floats. If you use a not-so-regular number, readers would tend to believe that it is there for some reason, and sometimes even suspect it to be you leaving some loopholes in the program for yourself.

Share This Page

Advertisement