Solved Syantax Error: else if() {

Discussion in 'Plugin Development' started by Jawfy, Sep 19, 2015.

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

    Jawfy

    Hello, i'm struggling to figure out why I keep getting a 'Syntax error on token “(”' when ever I try to put. else if() {

    I'm trying to create a kits plugin therfor I want to carry my code of so I can do multiple commands with /kit so I need to put else if() { at the end of my closing bracket but it doesn't work! please help!



    Here's my code:

    Code:
    package me.jackears;
    
    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.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.Potion;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.potion.PotionType;
    
    public class Kits extends JavaPlugin{
       
        public void onEnable() {
            getLogger().info("Kits Has Been Enabled!");
        }
       
        public void onDisable() {
            getLogger().info("Kits Has Been Disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
                Player p = (Player) sender;
                PlayerInventory pi = p.getInventory();
                if(args.length == 1) {
                    if (cmd.getName().equalsIgnoreCase("kit") && args[0].equalsIgnoreCase("tank")) {
                        sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Kits" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " You Sucsefully Recived The " + ChatColor.AQUA + ChatColor.BOLD + "Tank" + ChatColor.GRAY + "kit!"); {
    
                    //Stone Sword
                    ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
                    ItemMeta swordMeta = sword.getItemMeta();
                    swordMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Tank Sword" + ChatColor.DARK_GRAY + "]");
                    sword.setItemMeta(swordMeta);
                    sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
                    sword.addUnsafeEnchantment(Enchantment.DURABILITY, 3);
                    pi.addItem(sword);
                   
                    //Bow
                    ItemStack b = new ItemStack(Material.BOW, 1);
                    ItemMeta bMeta = b.getItemMeta();
                    bMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Bow" + ChatColor.DARK_GRAY + "]");
                    b.setItemMeta(bMeta);
                    pi.addItem(b);
                   
                    //Diamond Helmet
                    ItemStack dhelm = new ItemStack(Material.IRON_HELMET, 1);
                    ItemMeta dhelmMeta = dhelm.getItemMeta();
                    dhelmMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Iron Helmet" + ChatColor.DARK_GRAY + "]");
                    dhelm.setItemMeta(dhelmMeta);
                    dhelm.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    dhelm.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                    pi.addItem(dhelm);
                   
                    //Diamond Chest
                    ItemStack dchest = new ItemStack(Material.IRON_CHESTPLATE, 1);
                    ItemMeta dchestMeta = dchest.getItemMeta();
                    dchestMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Iron ChestPlate" + ChatColor.DARK_GRAY + "]");
                    dchest.setItemMeta(dchestMeta);
                    dchest.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    dchest.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                    pi.addItem(dchest);
                   
                    //Diamond Legs
                    ItemStack dlegs = new ItemStack(Material.IRON_LEGGINGS, 1);
                    ItemMeta dlegsMeta = dlegs.getItemMeta();
                    dlegsMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + " Iron Leggings " + ChatColor.DARK_GRAY + "]");
                    dlegs.setItemMeta(dlegsMeta);
                    dlegs.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    dlegs.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                    pi.addItem(dlegs);
                   
                    //Diamond Boots
                    ItemStack dboots = new ItemStack(Material.IRON_BOOTS, 1);
                    ItemMeta dbootsMeta = dboots.getItemMeta();
                    dbootsMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + " Iron Boots " + ChatColor.DARK_GRAY + "]");
                    dboots.setItemMeta(dbootsMeta);
                    dboots.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    dboots.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                    pi.addItem(dboots);
                   
                    //Healing
                    Potion potion = new Potion(PotionType.INSTANT_HEAL, 1);
                    potion.setSplash(true);
                    pi.addItem(potion.toItemStack(1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 100, 0));
                   
                    //Arrow
                    ItemStack a = new ItemStack(Material.ARROW, 16);
                    ItemMeta aMeta = a.getItemMeta();
                    aMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + " Arrows " + ChatColor.DARK_GRAY + "]");
                    a.setItemMeta(aMeta);
                    pi.addItem(a);
                   
                        }
                   
                if(args.length == 1) {
                    if (cmd.getName().equalsIgnoreCase("kit") && args[0].equalsIgnoreCase("archery")) {
                        sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Kits" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + " You Sucsefully Recived The " + ChatColor.AQUA + ChatColor.BOLD + "Archery" + ChatColor.GRAY + "kit!");
                }
    
                    //Stone Sword
                    ItemStack as = new ItemStack(Material.STONE_SWORD, 1);
                    ItemMeta asMeta = as.getItemMeta();
                    asMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Archery Sword" + ChatColor.DARK_GRAY + "]");
                    as.setItemMeta(asMeta);
                    pi.addItem(as);
                   
                    //Bow
                    ItemStack ba = new ItemStack(Material.BOW, 1);
                    ItemMeta baMeta = ba.getItemMeta();
                    baMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Bow" + ChatColor.DARK_GRAY + "]");
                    ba.setItemMeta(baMeta);
                    ba.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1);
                    ba.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1);
                    pi.addItem(ba);
                   
                    //Diamond Helmet
                    ItemStack dhelma = new ItemStack(Material.IRON_HELMET, 1);
                    ItemMeta dhelmaMeta = dhelma.getItemMeta();
                    dhelmaMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Iron Helmet" + ChatColor.DARK_GRAY + "]");
                    dhelma.setItemMeta(dhelmaMeta);
                    dhelma.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    pi.addItem(dhelma);
                   
                    //Diamond Chest
                    ItemStack dchesta = new ItemStack(Material.IRON_CHESTPLATE, 1);
                    ItemMeta dchestaMeta = dchesta.getItemMeta();
                    dchestaMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Iron Chestplate" + ChatColor.DARK_GRAY + "]");
                    dchesta.setItemMeta(dchestaMeta);
                    dchesta.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    pi.addItem(dchesta);
                   
                    //Diamond Legs
                    ItemStack dlegsa = new ItemStack(Material.IRON_LEGGINGS, 1);
                    ItemMeta dlegsaMeta = dlegsa.getItemMeta();
                    dlegsaMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "Iron Leggings" + ChatColor.DARK_GRAY + "]");
                    dlegsa.setItemMeta(dlegsaMeta);
                    dlegsa.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    pi.addItem(dlegsa);
                   
                    //Diamond Boots
                    ItemStack dbootsa = new ItemStack(Material.IRON_BOOTS, 1);
                    ItemMeta dbootsaMeta = dbootsa.getItemMeta();
                    dbootsaMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Iron Boots " + ChatColor.DARK_GRAY + "]");
                    dbootsa.setItemMeta(dbootsaMeta);
                    dbootsa.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
                    pi.addItem(dbootsa);
                   
                    //Speed
                    Potion speed = new Potion(PotionType.SPEED, 2);
                    pi.addItem(speed.toItemStack(1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 0));
                   
                   
                    //Healing
                    Potion potiona = new Potion(PotionType.INSTANT_HEAL, 1);
                    potiona.setSplash(true);
                    pi.addItem(potiona.toItemStack(1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 100, 0));
                   
                    //Arrow
                    ItemStack aa = new ItemStack(Material.ARROW, 1);
                    ItemMeta aaMeta = aa.getItemMeta();
                    aaMeta.setDisplayName(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + ChatColor.BOLD + "Arrows" + ChatColor.DARK_GRAY + "]");
                    aa.setItemMeta(aaMeta);
                    pi.addItem(aa);
                       
                    }
       
                    }
                }
                return true;
        }
        }
    
    where should I put the else if() {?

    Plugin.yml:
    Code:
    name: Kits
    version: 1.8
    main: me.jackears.Kits
    author: JACKEARS
    commands:
      kit:
        usage: /kit <kit>
        description: Gives you your selected kit.
     
    Last edited: Sep 19, 2015
  2. Offline

    mythbusterma

    @Jawfy

    After an "if"-statement, one would think.
     
  3. Offline

    Jawfy

    But when ever I do that it still says there's an error?
     
  4. Offline

    Zombie_Striker

    Do you see the problem? You check if the args are equal to 1, and then if they are equal to 1! That is a useless if statement. If you looked at what I have above, you would be able to see the problem within a couple seconds. It took me a while to find this error because your code is terrible. You should not have to create new instances of itemstacks every time a player sends out a command. You should keep those itemstack instances outside the method, have it so when the plugin is enabled it will add the enchantments/names to the items ONCE, and just give the player those items (removing that 50 something lines from the onCommand and replacing them with 6 or 7)

    I'm not even going to talk about the duplicate Command check.

    TL;DR: move the itemstacks out of the onCommand and remove the useless if statement.

    [Edit] Also on the sendmessage method, you added a open bracket. This is where that error is coming from.
     
    Last edited: Sep 19, 2015
  5. Offline

    Jawfy

    Now this happens:
    Code:
    [21:26:13 ERROR]: null
    
    org.bukkit.command.CommandException: Unhandled exception executing command 'kit' in plugin Kits v2.0
    
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_09]
    
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) [?:1.7.0_09]
    
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) [?:1.7.0_09]
    
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    at java.lang.Thread.run(Thread.java:722) [?:1.7.0_09]
    
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    
    at me.jackears.Kits.onCommand(Kits.java:34) ~[?:?]
    
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
    
    ... 16 more[code]
     
  6. Offline

    RoboticPlayer

    Check before casting sender to player.
     
    Gonmarte likes this.
  7. Offline

    Zombie_Striker

    [Edit] This is the old code. Post updated code AND Line 34.

    PS. You have far to many of these lines with open brackets at the end.
     
Thread Status:
Not open for further replies.

Share This Page