[SOLVED]Need some help with EntityExplodeEvent and Randoms

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Aug 9, 2012.

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

    CRAZYxMUNK3Y

    I am trying to make a plugin for a person who asked for it. It is all done but one small part... The percentage of blocks that drop(With config).

    EntityExplodeEvent Code:
    Code:java
    1.  
    2. public void explode(EntityExplodeEvent e) {
    3.  
    4. Random chance = new Random();
    5. int dropChance = chance.nextInt(100);
    6. List<Block> inRadius = e.blockList();
    7. for (Block block : inRadius) {
    8. if (dropChance < dropChanceConfig) {
    9. block.breakNaturally();
    10. } else {
    11. block.setTypeId(0);
    12. }
    13. }
    14. }
    15.  


    Full Code (open)

    Code:java
    1.  
    2. package me.crazy.blockdrop;
    3.  
    4. import java.io.File;
    5. import java.io.FileWriter;
    6. import java.util.List;
    7. import java.util.Random;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.block.Block;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.event.entity.EntityExplodeEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class BlockDrop extends JavaPlugin {
    16.  
    17. Logger log = Logger.getLogger("Minecraft");
    18.  
    19. FileConfiguration config;
    20.  
    21. protected File configDir;
    22. protected File configFile;
    23. int dropChanceConfig;
    24.  
    25. @Override
    26. public void onDisable() {}
    27.  
    28. @Override
    29. public void onEnable() {
    30. configDir = getDataFolder();
    31. configFile = new File(configDir, "config.yml");
    32. try {
    33. configCheck();
    34. config = getConfig();
    35. dropChanceConfig = config.getInt("dropChance");
    36. } catch (Exception e) {
    37. log.severe("[BlockDrop] Unale to load config file! Show Dev stack trace");
    38. e.printStackTrace();
    39. }
    40. }
    41.  
    42. public void explode(EntityExplodeEvent e) {
    43.  
    44. Random chance = new Random();
    45. int dropChance = chance.nextInt(100);
    46. List<Block> inRadius = e.blockList();
    47. for (Block block : inRadius) {
    48. if (dropChance < dropChanceConfig) {
    49. block.breakNaturally();
    50. } else {
    51. block.setTypeId(0);
    52. }
    53. }
    54. }
    55.  
    56. public void configCheck() {
    57. if (!configDir.exists()) {
    58. try {
    59. configDir.mkdir();
    60. } catch (Exception e) {
    61. e.printStackTrace();
    62. }
    63. }
    64. if (!configFile.exists()) {
    65. try {
    66. configFile.createNewFile();
    67. FileWriter writer = new FileWriter(configFile);
    68. writer.write("dropChance : 100");
    69. writer.close();
    70. log.info("[BlockBreak] Config created");
    71. } catch (Exception e) {
    72. e.printStackTrace();
    73.  
    74. }
    75. }
    76. }
    77. }
    78.  



    Why isn't it working?
    All blocks seem to drop at a normal rate when the creeper explodes(I was going to do if(getType instaceof Creeper) but i need it for all Entities.

    Thanks
     
  2. Offline

    Courier

    You need to get a new int for each block... what you are doing now is all or nothing.
    Code:java
    1. public void explode(EntityExplodeEvent e) {
    2. //you could put this here, but it would probably be better to have...
    3. //it initialised globally since you don't need a new one each explosion
    4. Random chance = new Random();
    5. List<Block> inRadius = e.blockList();
    6. for (Block block : inRadius) {
    7. if(chance.nextInt(100) < dropChanceConfig) {
    8. block.breakNaturally();
    9. } else {
    10. block.setTypeId(0);
    11. }
    12. }
    13. //not needed, but prevents all the blocks (which are now air) from being re-destroyed
    14. //might improve performance a little tiny bit
    15. inRadius.clear();
    16. }
     
  3. Offline

    CRAZYxMUNK3Y

    Courier
    I feel so stupid now :p
    Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page