unreachable code?

Discussion in 'Plugin Development' started by GalaxyPrisonMc, Jul 7, 2014.

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

    GalaxyPrisonMc

    Code:java
    1.  
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.enchantments.Enchantment;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. String blue = ChatColor.BLUE + "";
    23. String red = ChatColor.RED + "";
    24. String aqua = ChatColor.AQUA + "";
    25. String green = ChatColor.GREEN + "";
    26. String daqua = ChatColor.DARK_AQUA + "";
    27. String bold = ChatColor.BOLD + "";
    28. String uline = ChatColor.UNDERLINE + "";
    29. String reset = ChatColor.RESET + "";
    30. String gold = ChatColor.GOLD + "";
    31. String purple = ChatColor.DARK_PURPLE + "";
    32. String white = ChatColor.WHITE + "";
    33. String kk = ChatColor.STRIKETHROUGH + "";
    34. String Prefix = green + "[" + purple + "join kits" + green + "]";
    35.  
    36. public void onEnable() {
    37. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    38. Bukkit.getServer().getConsoleSender().sendMessage(Prefix + green + bold + "Has Been Enabled");
    39.  
    40. }
    41. public void openGUI(Player player) {
    42. Inventory inv = Bukkit.createInventory(null, 9, red + "Kit Selector");
    43.  
    44. ItemStack bowman = new ItemStack(Material.BOW);
    45. ItemMeta bowmanMeta = bowman.getItemMeta();
    46. ItemStack swordman = new ItemStack(Material.DIAMOND_SWORD);
    47. ItemMeta swordmanMeta = swordman.getItemMeta();
    48.  
    49. bowmanMeta.setDisplayName(red + "Bows Man");
    50. bowman.setItemMeta(bowmanMeta);
    51. swordmanMeta.setDisplayName(green + "Sword Man");
    52. swordman.setItemMeta(swordmanMeta);
    53.  
    54.  
    55. inv.setItem(3, bowman);
    56. inv.setItem(5, swordman);
    57.  
    58. player.openInventory(inv);
    59. }
    60.  
    61. @EventHandler
    62. public void onInventoryClick(InventoryClickEvent event) {
    63. Player player = (Player) event.getWhoClicked();
    64. PlayerInventory pl = player.getInventory();
    65. ItemStack clicked = event.getCurrentItem();
    66. Inventory inventory = event.getInventory();
    67. if (clicked == null) return;
    68. if ((inventory.getName().equals(red + "Kit Selector")) &&
    69. (clicked.getType() == Material.BOW))
    70. {
    71. event.getWhoClicked().closeInventory();
    72. ItemStack bow = new ItemStack(Material.BOW, 1);
    73. bow.addEnchantment(Enchantment.ARROW_DAMAGE, 1);
    74. bow.addEnchantment(Enchantment.ARROW_KNOCKBACK, 1);
    75. player.getInventory().setItemInHand(bow);
    76. player.getInventory().addItem(new ItemStack[] {new ItemStack(Material.ARROW, 64)});
    77. player.getInventory().addItem(new ItemStack[] {new ItemStack(Material.COOKED_BEEF, 8)});
    78. player.getInventory().setHelmet(new ItemStack(Material.CHAINMAIL_HELMET));
    79. player.getInventory().setChestplate(new ItemStack(Material.CHAINMAIL_CHESTPLATE));
    80. player.getInventory().setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS));
    81. player.getInventory().setBoots(new ItemStack(Material.CHAINMAIL_BOOTS));
    82. }
    83. }
    84.  
    85. @EventHandler
    86. public void onPlayerJoin(PlayerJoinEvent event) {
    87. Player player = event.getPlayer();
    88. PlayerInventory pl = player.getInventory();
    89. ItemStack comp = new ItemStack(Material.COMPASS);
    90. ItemMeta compMeta = comp.getItemMeta();
    91. compMeta.setDisplayName(red + bold + "Kit Selector");
    92. comp.setItemMeta(compMeta);
    93. pl.addItem(comp);
    94. }
    95.  
    96. @EventHandler
    97. public void onPlayerInteract(PlayerInteractEvent event) {
    98. Player player = (Player) event.getPlayer();
    99. Action a = event.getAction();
    100. ItemStack is = event.getItem();
    101. if(a == Action.PHYSICAL || is == null || is.getType()==Material.AIR);
    102. return;
    103. //this is the unreachable code
    104. if (event.getPlayer().getInventory().getItemInHand()
    105. .getItemMeta().getDisplayName()
    106. .equals(red + bold + "Kit Selector"));
    107. openGUI(player);
    108. }
    109. }
     
  2. Offline

    EviiL

    Remove the semicolon from the end of line 101. That might fix the issue
     
  3. Offline

    mythbusterma

    GalaxyPrisonMc

    Why do you redefine all the ChatColor constants at the beginning of the class.....? And you don't even define them as strings correctly...

    Anyways the error is quite simple and self-explanatory, under no circumstances will the code be reached at run-time.
     
  4. Offline

    RawCode

    if(a == Action.PHYSICAL || is == null || is.getType()==Material.AIR);

    read this line very very caruful from begining to very end
     
  5. Offline

    GalaxyPrisonMc

    mythbusterma lets stay on topic we are not worried about my string usage we are worried about the unreachable code that has nothing to do with it even when i remove it it don't change anything
     
  6. Offline

    mythbusterma

    See the other two responses and my own, the code will not be reached to to an errant "return" statement.
     
  7. Offline

    GalaxyPrisonMc

  8. Offline

    Rocoty

    It is unreachable because you have a return statement just above it, which will be called every time you reach that part under all circumstances. Therefore the next part of the code will never ever be executed.
     
  9. Offline

    GalaxyPrisonMc

    Rocoty but when i move return statement and the action code bellow the open gui , it works but when you right click any item it spams console errors on the playerinteractevent
     
  10. Offline

    Rocoty

    I don't think you understand. Your error lies in the line above the return statement. After the if statement you have put a semicolon, so the one statement that the if-block contains is an empty one. And the return statement is outside of the if altogether.

    EDIT: please don't bump 4 minutes after posting. It is tasteless.
     
  11. Offline

    1Rogue Retired Staff

    People have directly told you the answer 4 times now. Read your code.
     
  12. Offline

    GalaxyPrisonMc

    i fixed the unreachable code if you would read what i said in the post before 1Rogue
     
  13. Offline

    Wizehh

    Please make a new thread if your problem is not related to the subject of the original post. Really, though: how do you expect us to be able to help you if you don't tell us what went wrong? Please include the relevant stacktraces/errors next time.
     
    kxpeep93 likes this.
  14. Offline

    Rocoty

    GalaxyPrisonMc You didn't fix it. You made it throw errors. Read my above post.

    And...The very first reply to this thread told you the answer to your problem and you just ignored it...what?
     
Thread Status:
Not open for further replies.

Share This Page