Solved Event Chance

Discussion in 'Plugin Development' started by noraver, May 1, 2013.

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

    noraver

    hey guys i need some assistance :)
    i tried a few ways of doing this but i never applied chance to anything before

    i tryed Random as u can see and it makes a Wooden Sword with the name "Wooden Sword" or it makes the first one "test1" but non of the others.

    what im hoping for is someone can help with what i did wrong or knows how to make it so
    out of 100%
    test1 has a 60% chance to be made
    test2 has a 30% chance to be made
    test3 has a 10% chance to be made
    something along those lines

    Code:java
    1.  
    2. @EventHandler
    3. public void onCraft(CraftItemEvent event) {
    4.  
    5. Random random = new Random();
    6. int randomNum = random.nextInt(99) + 1;
    7. if (randomNum <= 60) {
    8.  
    9. ItemStack woodensword = new ItemStack(Material.WOOD_SWORD, 1);
    10. ItemMeta item = woodensword.getItemMeta();
    11. item.setDisplayName("test1");
    12. woodensword.setItemMeta(item);
    13. event.getInventory().setResult(woodensword);
    14.  
    15. } else if (randomNum <= 30) {
    16.  
    17. ItemStack woodensword = new ItemStack(Material.WOOD_SWORD, 1);
    18. ItemMeta item = woodensword.getItemMeta();
    19. item.setDisplayName("test2");
    20. woodensword.setItemMeta(item);
    21. event.getInventory().setResult(woodensword);
    22.  
    23. } else if (randomNum <= 10) {
    24.  
    25. ItemStack woodensword = new ItemStack(Material.WOOD_SWORD, 1);
    26. ItemMeta item = woodensword.getItemMeta();
    27. item.setDisplayName("test3");
    28. woodensword.setItemMeta(item);
    29. event.getInventory().setResult(woodensword);
    30. }
    31. }
    32. }
    33.  


    Thanks in advance
    Bless_ [tnt]
     
  2. First, nextInt() is between 0 and max number exclusive, therefore you have generated a 0 to 98 number, which makes 99 options... set it to 100.

    Next, you've used the wrong order of conditions... the first one will overwrite all other cases because 30 and 10 are both smaller than 60, check them starting with lowest.

    Also, you've got duplicated code there... I suggest you get the duplicated code out of the conditional statements and only place there what will change due to chance.
     
  3. Offline

    noraver

    Digi ....marry me.... i feel so derp now because of that ty =D
     
Thread Status:
Not open for further replies.

Share This Page