"Unreachable Code" in Eclipse

Discussion in 'Plugin Development' started by ab9876, Apr 5, 2013.

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

    ab9876

    I have this code:
    package com.hotmail.aman560900.kitpvp;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class kitpvp extends JavaPlugin {

    @Override
    public void onEnable(){
    getLogger().info("KitPvP by ab9876 is working! :D");
    }

    @Override
    public void onDisable(){
    getLogger().info("KitPvP by ab9876 has been turned off. :)");
    }
    public boolean onCommand(CommandSender sender,Command cmd, String commandLabel, String[] args){
    if(commandLabel.equalsIgnoreCase("warrior")){
    Player player = (Player) sender;
    ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
    player.getInventory().clear();
    player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
    player.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
    player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    player.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE, 0));
    player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 18));
    } if (!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.YELLOW + "Only a player can access this command.");
    returntrue;
    } else {
    Player player = (Player) sender;
    player.sendMessage(ChatColor.RED + "You do not own this kit.");
    returnfalse;
    }
    if (commandLabel.equalsIgnoreCase("archer")){
    Player player = (Player) sender;
    ItemStack powerbow = new ItemStack(Material.BOW);
    powerbow.addEnchantment(Enchantment.KNOCKBACK, 2);
    player.getInventory().addItem(powerbow);
    player.getInventory().clear();
    player.getInventory().addItem(new ItemStack(Material.BOW));
    player.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    ItemStack sword = new ItemStack(Material.STONE_SWORD);
    sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    player.getInventory().addItem(sword);
    player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 18));
    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE, 0));
    returntrue;
    } if (!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.AQUA + "Only a player can access this command.");
    returntrue;
    }
    else {
    Player player = (Player) sender;
    player.sendMessage(ChatColor.RED + "You do not own this kit.");
    returnfalse;
    }
    if (commandLabel.equalsIgnoreCase("heavy")){
    Player player = (Player) sender;
    ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
    player.getInventory().clear();
    player.getInventory().addItem(dsword);
    player.getInventory().addItem(new ItemStack(Material.IRON_SWORD));
    player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 9));
    player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    returntrue;
    } if (!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.BLUE + "Only players can access this command.");
    returntrue;
    } else {
    Player player = (Player) sender;
    player.sendMessage(ChatColor.RED + "You do not own this kit.");
    returnfalse;

    }


    }

    and I get "unreachable code all the time. Please help! I really hope to fix it soon!
     
  2. Offline

    skipperguy12

    Why is no one posting with syntax or code tags! Please...!

    Also, tell us where. That error LITERALLY means it's unreachable, there's just some code that makes it absolutely impossible to get there :p
     
  3. Player player;
    player = null;

    if (player != null) {
    //Unreachable code
    }
     
  4. Offline

    CorrieKay

    its unreachable because the player object is null at that point, and nothing else is saying otherwise. The answer will ALWAYS be false, and the if statement will NEVER be triggered
     
  5. I'd thought that he could figure this out by himself. :p
    But yes, true. Good explanation.
     
  6. Offline

    CorrieKay

    ...Oh, sorry. I wasn't paying attention, you're not the OP lmao
     
  7. Offline

    ab9876

    Sorry. This is my first plugin, and I used the plugin tutorial, and I took off from there. May you please tell me what to change about it? I used to code in C# a couple years ago, but not in a while. I am now learning Java. Thanks.
     
Thread Status:
Not open for further replies.

Share This Page