Solved Dead code? And section doesn't seem to work...

Discussion in 'Plugin Development' started by MCaeolus, Sep 4, 2014.

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

    MCaeolus

    Hi. I recently been combining plugin Fortune Blocks & Safe Mining into one plugin so (a. both work with each other, and (b. To, well combine two plugins together :p

    So, I seem to be getting a few errors with what I have. It never seems that the item going straight to inventory does not work (after implementing code for fortune blocks)

    I have commented code to make it easier to find sections i'm talking about

    Code:
    Code:java
    1. {
    2. Random random = new Random();
    3. public void onEnable()
    4. {
    5. Bukkit.getPluginManager().registerEvents(this, this);
    6. getLogger().info("Plugin enabled.");
    7. }
    8.  
    9. @SuppressWarnings("deprecation")
    10. @EventHandler(priority = EventPriority.HIGHEST)
    11. public void onBreak(BlockBreakEvent e)
    12. {
    13. Player p = e.getPlayer();
    14. Block block = e.getBlock();
    15. Collection<ItemStack> drops = e.getBlock().getDrops();
    16. if (block.getType() == null) {
    17. return;
    18. }
    19. if ((block.getType() != Material.IRON_BLOCK) && (block.getType() != Material.EMERALD_BLOCK) && (block.getType() != Material.DIAMOND_BLOCK) && (block.getType() != Material.GOLD_BLOCK) && (block.getType() != Material.REDSTONE_BLOCK) && (block.getType() != Material.COAL_BLOCK) && (block.getType() != Material.LAPIS_BLOCK)) {
    20. return;
    21. }
    22. ItemStack hand = p.getItemInHand();
    23. if (hand == null) {
    24. return;
    25. }
    26. if ((hand.getType() != Material.WOOD_PICKAXE) && (hand.getType() != Material.STONE_PICKAXE) && (hand.getType() != Material.IRON_PICKAXE) && (hand.getType() != Material.DIAMOND_PICKAXE)) {
    27. return;
    28. }
    29. int enchantlvl = hand.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
    30. if (enchantlvl < 1) {
    31. return;
    32. }
    33.  
    34.  
    35. ItemStack fortune = (ItemStack) new ItemStack(block.getType(), Math.max(3, this.random.nextInt(15)));
    36.  
    37. //Putting items into inventory.
    38. if (p.hasPermission("safemine.use"))
    39. {
    40. if(fortune != null){
    41.  
    42. if (p.getInventory().firstEmpty() != -1)
    43. {
    44. p.getInventory().addItem(new ItemStack[] { fortune });
    45. }
    46. else
    47. {
    48. p.getWorld().dropItemNaturally(p.getLocation(), fortune);
    49. p.updateInventory();
    50. }
    51. p.playSound(p.getLocation(), Sound.CHICKEN_EGG_POP, 1.0F, 1.0F);
    52. p.giveExp(e.getExpToDrop());
    53. p.updateInventory();
    54. block.setType(Material.AIR);
    55. e.setCancelled(true);
    56. }
    57. else
    58. //Dead code(as Eclipse says)
    59. {
    60. if(isPick(block.getType())) {
    61. for (ItemStack finaldrop : drops)
    62. {
    63. if (p.getInventory().firstEmpty() != -1)
    64. {
    65. p.getInventory().addItem(new ItemStack[] { finaldrop });
    66. }
    67. else
    68. {
    69. p.getWorld().dropItemNaturally(p.getLocation(), finaldrop);
    70. p.updateInventory();
    71. }
    72. p.playSound(p.getLocation(), Sound.CHICKEN_EGG_POP, 1.0F, 1.0F);
    73. p.updateInventory();
    74. block.setType(Material.AIR);
    75. e.setCancelled(true);
    76. }
    77. }
    78.  
    79. }
    80. }
    81. }
    82.  
    83.  
    84.  


    Neither with, or without the fortune aspect occurring does the code seem to work. And yes, it does fire :p

    Now, again, I will say this. I am relatively new to Coding in java/using bukkit api, so it may just be a novice mistake.

    Thanks in advance for reading and possibly helping!
     
  2. Offline

    Gamecube762

    You have an "if null then blah, else other blah" fortune is never null since you defined it as something before that if statement. The else is being labeled as dead code since its not possible for it to be ran as fortune is never null.
     
  3. Offline

    Rocoty

    MCaeolus Gamecube762 To be more precise as to why eclipse declared the block dead:
    Giving a value to a variable like you do doesn't automatically mean that the variable won't point to null, because it might. But since you made a new object there and assigned that to the variable, there is no way your variable will ever point to null, hence why eclipse says the code is dead (not reachable).

    PS: Your cast to ItemStack is entirely redundant, as the object you are creating already is one and can never not be one.
     
  4. Offline

    MCaeolus

    I fixed it, sorry about that XD Thank you. I realized I needed to rewrite the way I implemented fortune blocks, and now it fully works! Thanks, again ;)
     
Thread Status:
Not open for further replies.

Share This Page