Solved Chance of drop in double?

Discussion in 'Plugin Development' started by MrAndeos, Jan 7, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    MrAndeos

    Hello.

    I want to make a drop chance in double, for example 0.01%. Here is what i have for now, and it works with chance in int:
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent event) {
    3. if (event.getEntity().getKiller() instanceof Player) {
    4. if (event.getEntity().getType() == EntityType.SKELETON) {
    5. Skeleton skeleton = (Skeleton)event.getEntity();
    6. if (skeleton.getSkeletonType().equals(SkeletonType.WITHER)) {
    7. event.getDrops().clear();
    8. double chance = Main.plugin.getConfig().getDouble("wither-skeleton-head-drop-chance");
    9. Random r = new Random();
    10. int n = r.nextInt(100) + 1;
    11. if (n <= chance) {
    12. event.getDrops().add(new ItemStack(Material.SKULL_ITEM, 1, (short)1));
    13. }
    14. }
    15. }
    16. }
    17. }

    How to do this?

    Sorry for any mistakes, English is not my native language.
     
  2. Offline

    Max8801

  3. Offline

    MrAndeos

    @Max8801
    Code:java
    1. double chance = Main.plugin.getConfig().getDouble("wither-skeleton-head-drop-chance");
    2. if (Math.random() <= chance) {
    3. event.getDrops().add(new ItemStack(Material.SKULL_ITEM, 1, (short)1));
    4. }

    Thank's for response, did I understand it correctly?
     
  4. Offline

    Max8801

    It depends: Is the value you enter in the config a percentage or a probability value?

    EDIT:
    Examples:
    If the percentage was 50%, the corresponding probability value would be 0.5
    If the probability value was 1, the corresponding percentage would be 100%
     
    Last edited: Jan 7, 2018
  5. Offline

    MrAndeos

    @Max8801
    The value is "0.003" or "0.01" (i want to do the same thing for other mob), i want it to work as a percentage chance, if the drawn percentage number is smaller, or the same as defined, or 0.01% to 100%. I don't know, how to explain this better.
     
  6. Offline

    Max8801

    Alright, let's see:
    If you want to put a percentage value into your config (e.g. skeletonDropChance: 50), then you have to either devide that config value through 100 or multiply the random value returned by the Math class with 100, because Math.random() returns a value between 0 and 1.

    Example:

    config:
    Code:
    # Value in percent
    Chances:
      skeleton: 50.0
    check:
    Code:java
    1.  
    2. if(getConfig().getDouble("Chances.skeleton") >= Math.random() * 100) {
    3. // Do stuff
    4. }
    5.  
     
    Last edited: Jan 7, 2018
  7. Offline

    MrAndeos

    @Max8801
    OK, thanks, this works if defined chance is at least 1%, but what if I want it to be smaller than 1%, for example 0.01%?
     
  8. Offline

    Max8801

    This ought to work for values below 1%. Why shouldn't it?
     
  9. Offline

    MrAndeos

    @Max8801
    Hmm... If i set the config value below 1%, the item is dropped every time.
     
  10. Offline

    Zombie_Striker

    @Max8801
    Your math is wrong. The value in the config should be greater than or equal to, not less than.

    @MrAndeos
    Change the '<' to a '>'
     
  11. Offline

    Max8801

    Thanks @Zombie_Striker . I guess I copied it wrongly from above.
     
  12. Offline

    MrAndeos

Thread Status:
Not open for further replies.

Share This Page