Solved Need help with item drop chance.

Discussion in 'Plugin Development' started by Brendyn Todd, Feb 23, 2014.

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

    Brendyn Todd

    So, I'm a bit new to developing with Java so I'm sorry if this seems a bit newbieish. I need to have a chance of items to drop from mobs, so I use Math.Random() to calculate the chance. For some reason it drops all but the diamond sword type. Why is this?

    The EntityDeathEvent that calls the Calculation event.
    Code:java
    1.  
    2. @EventHandler
    3. public void onEntityDeath(EntityDeathEvent e) {
    4. Entity entity = e.getEntity();
    5.  
    6. if (entity instanceof Monster) {
    7. createSword();
    8.  
    9. if(item == "DIAMOND_SWORD")
    10. e.getDrops().add(diamondSword);
    11.  
    12. if(item == "GOLD_SWORD")
    13. e.getDrops().add(goldSword);
    14.  
    15. if(item == "IRON_SWORD")
    16. e.getDrops().add(ironSword);
    17.  
    18. if(item == "STONE_SWORD")
    19. e.getDrops().add(stoneSword);
    20.  
    21. if(item == "WOOD_SWORD")
    22. e.getDrops().add(woodSword);
    23.  
    24.  
    25. }
    26. }


    The calculation method.
    Code:java
    1.  
    2. if (Math.random() < 0.45)
    3. item = "WOOD_SWORD";
    4. else if (Math.random() < 0.30)
    5. item = "STONE_SWORD";
    6. else if (Math.random() < 0.15)
    7. item = "IRON_SWORD";
    8. else if (Math.random() < 0.10)
    9. item = "GOLD_SWORD";
    10. else if (Math.random() < 0.5)
    11. item = "DIAMOND_SWORD";
     
  2. Offline

    NathanWolf

    I think you want 0.05 and not 0.5 :)

    Also, in that first snippet of code, you need to use the .equals() method to compare strings, == won't work.
     
  3. Offline

    Brendyn Todd

    NathanWolf

    I wrote:
    if(item == "WOOD_SWORD);

    instead of

    if(item == "WOOD_SWORD){

    I'm an idiot -.-
    ====================
    Solved, stupid syntax error.
     
  4. Offline

    NathanWolf

    Ok, but the 0.05 thing and using == on Strings will still be a problem! Just be advised :)
     
  5. Offline

    Brendyn Todd

    NathanWolf
    How so? I've just tested and they are all being dropped at different rates with my current code.
     
  6. Offline

    calebbfmv

    Better practice
    Plus, you could do equalsIgnoreCase(ChatColor.stripColor("string here"));
     
  7. Offline

    Brendyn Todd

    calebbfmv
    Really? Didn't know that thanks for the info.
     
  8. Offline

    NathanWolf

    Well... I guess I dunno how it's working then XD

    I mean, assuming item is a String ... looking again at your code I was wrong about the 0.05/0.5 thing, but for strings you definitely need to use .equals().
     
Thread Status:
Not open for further replies.

Share This Page