Solved If else statement gives syntax error

Discussion in 'Plugin Development' started by Auke, Nov 17, 2017.

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

    Auke

    I have a problem with my code.

    When I add an else statement to my code if gives the following errors:
    1.) Syntax error, insert "}" to complete Statement
    2.) Syntax error on token "}", delete this token

    Here's my code:

    Code:
    if (GeneratorClass.generators.containsKey(generatorLocation));
                        {
                           
                            GeneratorClass.removeGenerator(generatorLocation);
                            generatorLocation.getBlock().setType(Material.AIR);
                            generatorLocation.add(0, 1, 0).getBlock().setType(Material.AIR);
                            generatorLocation.subtract(0, 0, 1).getBlock().setType(Material.AIR);
                           
                        } else {
                            player.sendMessage(ChatColor.RED + "That's not a generator!");
                        }
    Here is my whole class:

    Code:
    package me.gameplanet;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.BlockFace;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class CommandClass extends JavaPlugin {
    
        @Override
        public void onEnable() {
           
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
               
                public void run() {
                    GeneratorClass.runGenerator();
                }
               
            }, 60, 40);
           
            List<String> s = getConfig().getStringList("generators");
           
            for (String str : s) {
               
                String[] wordsLT = str.split(":");
               
                String[] wordsL = wordsLT[0].split(",");
               
                World world = Bukkit.getWorld(wordsL[0]);
                int x = Integer.parseInt(wordsL[1]);
                int y = Integer.parseInt(wordsL[2]);
                int z = Integer.parseInt(wordsL[3]);
                int Tier = Integer.parseInt(wordsLT[1]);
               
                Location location = new Location(world, x, y, z);
               
                GeneratorClass.generators.put(location, Tier);
           
            }
           
        }
       
        @Override
        public void onDisable() {
           
            List<String> s = new ArrayList<String>();
           
            for (Location location : GeneratorClass.generators.keySet()) {
               
                String w = location.getWorld().getName();
                int x = location.getBlockX();
                int y = location.getBlockY();
                int z = location.getBlockZ();
               
                s.add(w + "," + x + "," + y + "," + z + ":" + GeneratorClass.generators.get(location));
               
            }
           
            getConfig().set("generators", s);
            saveConfig();
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
           
            if (cmd.getName().equalsIgnoreCase("generator") && sender instanceof Player)
            {
               
                Player player = (Player) sender;
               
                // Arg 0 = first word entered after command
                int length = args.length;
               
                if (length == 1)
                {
                   
                    if (args[0].equalsIgnoreCase("add"))
                    {
                       
                        //Adds generator to generator HashMap
                        Location generatorLocation = player.getTargetBlock(null, 50).getLocation();
                        GeneratorClass.addGenerator(generatorLocation, 0);
                        generatorLocation.getBlock().setType(Material.BEDROCK);
                       
                        //Places a sign
                        generatorLocation.getBlock().getRelative(BlockFace.NORTH).setType(Material.WALL_SIGN);
                       
                    }
                   
                    if (args[0].equalsIgnoreCase("remove"))
                    {
                       
                        Location generatorLocation = player.getTargetBlock(null, 50).getLocation();
                       
                        if (GeneratorClass.generators.containsKey(generatorLocation));
                        {
                           
                            GeneratorClass.removeGenerator(generatorLocation);
                            generatorLocation.getBlock().setType(Material.AIR);
                            generatorLocation.add(0, 1, 0).getBlock().setType(Material.AIR);
                            generatorLocation.subtract(0, 0, 1).getBlock().setType(Material.AIR);
                           
                        } else {
                            player.sendMessage(ChatColor.RED + "That's not a generator!");
                        }
                   
                    }
                   
                    if (args[0].equalsIgnoreCase("list"))
                    {
                       
                        player.sendMessage(ChatColor.DARK_GRAY + "Generators:");
                       
                        for (Location location : GeneratorClass.generators.keySet())
                        {
                           
                            int x = location.getBlockX();
                            int y = location.getBlockY();
                            int z = location.getBlockZ();
                           
                            player.sendMessage("Location: " + "x= " + x + ", y= " + y + ", z= " + z);
                           
                        }
                       
                    } 
                   
                }
       
            }
           
            return false;
       
        }
           
    }
    
    I can't figure out why it gives these errors. Help would be appreciated :)
     
    JustRendering likes this.
  2. Offline

    JustRendering

    if (GeneratorClass.generators.containsKey(generatorLocation)); <------ HERE {

    GeneratorClass.removeGenerator(generatorLocation);
    generatorLocation.getBlock().setType(Material.AIR);
    generatorLocation.add(0, 1, 0).getBlock().setType(Material.AIR);
    generatorLocation.subtract(0, 0, 1).getBlock().setType(Material.AIR);

    } else {
    player.sendMessage(ChatColor.RED + "That's not a generator!");
    }

    Note: orange is not supposed to go into the code. its just notes


    the ";" after the if doesn't need to be there. The ";" is bad in a if because the "{" says what to do if true, the ";" is at the end of a void, which means go to next line, you can only have one of them.

    Don't worry, we all make sili mistakes

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 17, 2017
  3. Offline

    Auke

    Thanks, I didn't see it.
     
Thread Status:
Not open for further replies.

Share This Page