[HELP] Random Drop Numbers not wroking

Discussion in 'Plugin Development' started by TheMCBukkitTut, Aug 24, 2014.

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

    TheMCBukkitTut

    Im trying to make a plugin so that if I had a diamond pickaxe or someother pickaxe it with fortune 3 or 6 or 9 etc it would drop between 1-3 or 1-6 or 1-9 etc!

    PLEASE TRY TO HELP:

    Code:

    Code:java
    1. import java.util.Random;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.enchantments.Enchantment;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.BlockBreakEvent;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class FortunBlock implements Listener {
    14.  
    15. @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
    16. public void onBreak(BlockBreakEvent e)
    17. {
    18. Player p = e.getPlayer();
    19. if (p.getItemInHand().getType() == Material.WOOD_PICKAXE || p.getItemInHand().getType() == Material.STONE_PICKAXE || p.getItemInHand().getType() == Material.IRON_PICKAXE || p.getItemInHand().getType() == Material.GOLD_PICKAXE || p.getItemInHand().getType() == Material.DIAMOND_PICKAXE)
    20. {
    21. Block b = e.getBlock();
    22.  
    23. e.setCancelled(true);
    24. e.getPlayer().giveExp(e.getExpToDrop());
    25. Material material = b.getType();
    26.  
    27. int num = 1;
    28. int enchantlvl = p.getInventory().getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
    29. if (enchantlvl == 3)
    30. num = randInt(1, 3);
    31. else if (enchantlvl == 6)
    32. num = randInt(2, 6);
    33. else if (enchantlvl == 9)
    34. num = randInt(3, 9);
    35. else if (enchantlvl == 12)
    36. num = randInt(4, 12);
    37. else if (enchantlvl >= 15) {
    38. num = randInt(5, 15);
    39. }
    40. ItemStack item = new ItemStack(material, num);
    41. if (material == Material.IRON_ORE)
    42. item = new ItemStack(Material.IRON_INGOT, num);
    43. else if (material == Material.GOLD_ORE)
    44. item = new ItemStack(Material.GOLD_INGOT, num);
    45. else if (material == Material.COAL_ORE)
    46. item = new ItemStack(Material.COAL, num);
    47. else if (material == Material.STONE)
    48. item = new ItemStack(Material.COBBLESTONE, num);
    49. else if (material == Material.EMERALD_ORE)
    50. item = new ItemStack(Material.EMERALD, num);
    51. else if (material == Material.DIAMOND_ORE)
    52. item = new ItemStack(Material.DIAMOND, num);
    53. else if (material == Material.REDSTONE_ORE)
    54. item = new ItemStack(Material.REDSTONE, num);
    55. else if (material == Material.COAL_BLOCK)
    56. item = new ItemStack(Material.COAL_BLOCK, num);
    57. else if (material == Material.IRON_BLOCK)
    58. item = new ItemStack(Material.IRON_BLOCK, num);
    59. else if (material == Material.GOLD_BLOCK)
    60. item = new ItemStack(Material.GOLD_BLOCK, num);
    61. else if (material == Material.DIAMOND_BLOCK)
    62. item = new ItemStack(Material.DIAMOND_BLOCK, num);
    63. else if (material == Material.EMERALD_BLOCK)
    64. item = new ItemStack(Material.EMERALD_BLOCK, num);
    65. else if (material == Material.GLOWSTONE)
    66. item = new ItemStack(Material.GLOWSTONE, num);
    67. else if (material == Material.QUARTZ_ORE)
    68. item = new ItemStack(Material.QUARTZ, num);
    69. else if (material == Material.QUARTZ_BLOCK)
    70. item = new ItemStack(Material.QUARTZ_BLOCK, num);
    71. else if (material == Material.LOG)
    72. item = new ItemStack(Material.LOG, num);
    73. else if (material == Material.LAPIS_BLOCK)
    74. item = new ItemStack(Material.LAPIS_BLOCK, num);
    75. else if (material == Material.LAPIS_ORE) {
    76. item = new ItemStack(Material.INK_SACK, num, (short)4);
    77. }
    78. p.getInventory().addItem(new ItemStack[] { new ItemStack(item) });
    79. e.getBlock().setType(Material.AIR);
    80. }
    81. }
    82.  
    83. public int randInt(int min, int max)
    84. {
    85. Random rand = new Random();
    86. int randomNum = rand.nextInt(max - min + 1) + min;
    87. return randomNum;
    88. }
    89.  
    90. }
    91.  
     
  2. Offline

    IkBenHarm

    have you registered your events?

    also, i dont get line 78:

    p.getInventory().addItem(new ItemStack[]{new ItemStack(item)});
     
  3. Offline

    TheMCBukkitTut

    Yes i have registered the listener in another account also line 78 is adding the block or item to their inventory otherwise the player would not get anything to sell (its a prison plugin).
     
  4. Offline

    IkBenHarm

    TheMCBukkitTut
    not sure about this (long time since i've made a plugin)

    but cant you do:

    p.getInventory().addItem(new ItemStack(Material.BLABLABLA, 69));

    69 is the amount of items
     
  5. Offline

    TheMCBukkitTut

    I not sure that would work, The only problem i have is clearly stated in the description of this thread
    "if I had a diamond pickaxe or someother pickaxe it with fortune 3 or 6 or 9 etc it would drop between 1-3 or 1-6 or 1-9 etc!"
    But its not doing that! its just giving me 1 of what ever i mine!
    Maybe timtower can help me?
     
  6. Offline

    IkBenHarm

    TheMCBukkitTut
    oh like that.
    does your pickaxe have the enchantment?
     
  7. Offline

    timtower Administrator Administrator Moderator

  8. Offline

    TheMCBukkitTut

    Yes it does.

    Where would i put that also redstone_ore is not giving me redstone!
     
  9. Offline

    timtower Administrator Administrator Moderator

    On the place where you will get the amount.
    And try redstone dust. Think it is that though.
     
  10. Offline

    TheMCBukkitTut

    Okay so instead of
    Code:java
    1. int randomNum = rand.nextInt(max - min + 1) + min;

    put:
    Code:java
    1. int amount = new Random().nextInt(furtune-1)+1

    But if i do that i get a yellow line under rand on line 91 and also REDSTONE_DUST cannot be resolved or is not a field
     
  11. Offline

    timtower Administrator Administrator Moderator

    TheMCBukkitTut You tagged me half wat, I haven't really looked at it that close. I don't have access to javadoc or an IDE at the moment.
     
  12. Offline

    TheMCBukkitTut

    Okay just reply back to this thread when you do, Thanks for helping so far :)
     
  13. Offline

    timtower Administrator Administrator Moderator

    Besides that you never check if there even is enchanted item, your switch case can be replaced with a hashmap, your amount function doesn't needs a switch but a simple devide: wasn't planning on looking into this.
     
Thread Status:
Not open for further replies.

Share This Page