Solved Unable to fix error: illegal start of expression

Discussion in 'Spigot Plugin Development' started by ArisDraws, Jan 9, 2020.

  1. Hi! I'm new to Java and I'm making my first plugin. It's a fly plugin with a config and everything. However, when I run the Maven Build it always comes up with:

    Error: (40,9) java: illegal start of expression

    I've researched for hours on the problem, but nothing seems to work for me.
    Any help is greatly appreciated!

    Method:
    Code (Java):
    private void flyMethod(Player player);
            {
                if (player.hasPermission("flyplugin.fly")) {
                    if (list_of_flying_players.contains(player)) {
                        list_of_flying_players.remove(player);
                        player.setAllowFlight(false);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-on-message").replace("{player}", "player.getDisplayName()")));
                    } else if (!list_of_flying_players.contains(player)) {
                        list_of_flying_players.add(player);
                        player.setAllowFlight(true);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-off-message").replace("{player}", "player.getDisplayName()")));
                    }
                }
            }
    Entire Code:
    Code (Java):
    package commands;

    import me.arisdraws.commandfly.CommandFly;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import java.lang.reflect.Array;
    import java.util.ArrayList;

    public class Fly implements CommandExecutor {

        private CommandFly playerVar;
        private ArrayList<Player> list_of_flying_players = new ArrayList<>();

        public Fly(CommandFly playerVar) {
            this.playerVar = playerVar;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length == 0) {
                    flyMethod(player);
                } else if (args.length == 1) {
                    if (player.hasPermission("flyplugin.flyothers")) {
                        Player target = Bukkit.getPlayer(args[0]);
                        flyMethod(target);
                    } else {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("deny-permission")));
                    }
                }
                return true;
            }
            private void flyMethod(Player player);
            {
                if (player.hasPermission("flyplugin.fly")) {
                    if (list_of_flying_players.contains(player)) {
                        list_of_flying_players.remove(player);
                        player.setAllowFlight(false);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-on-message").replace("{player}", "player.getDisplayName()")));
                    } else if (!list_of_flying_players.contains(player)) {
                        list_of_flying_players.add(player);
                        player.setAllowFlight(true);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-off-message").replace("{player}", "player.getDisplayName()")));
                    }
                }
            }
        }
    Thanks for reading! :)
     
  2. There is a semicolon ( ; ) at the method flyMethod that should not be there ;)
     
  3. Thanks for the help! Unfortunately, I've tried adding and removing it but it makes no difference.
     
  4. Oh, now I see the error. You cannot nest methods in java (or you simply forgot a } at the end). You need to move the whole method out of the onCommand method and you should be fine!
     
  5. Sorry, I'm new to Java coding. How did I make multiple methods?
     
  6. You put the flyMethod() method inside of your onCommand() method. Just do this:

    Code (Java):
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length == 0) {
                    flyMethod(player);
                } else if (args.length == 1) {
                    if (player.hasPermission("flyplugin.flyothers")) {
                        Player target = Bukkit.getPlayer(args[0]);
                        flyMethod(target);
                    } else {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("deny-permission")));
                    }
                }
                return true;
            }
        }
       
        private void flyMethod(Player player) {
            if (player.hasPermission("flyplugin.fly")) {
                if (list_of_flying_players.contains(player)) {
                    list_of_flying_players.remove(player);
                    player.setAllowFlight(false);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-on-message").replace("{player}", "player.getDisplayName()")));
                } else if (!list_of_flying_players.contains(player)) {
                    list_of_flying_players.add(player);
                    player.setAllowFlight(true);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-off-message").replace("{player}", "player.getDisplayName()")));
                }
            }
        }
    Also, make sure that you remove the semi-colon at the end of your flyMethod() line because then it will think it is abstract.
     
  7. Your method flyMethod is already a method (thou, if we look at standards, one would not write flyMethod but instead fly but that's personal preference).
    You simply move that thing out of the onCommand-method. Looks like this:

    Code (Java):
    package commands;

    import me.arisdraws.commandfly.CommandFly;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import java.lang.reflect.Array;
    import java.util.ArrayList;

    public class Fly implements CommandExecutor {

        private CommandFly playerVar;
        private ArrayList<Player> list_of_flying_players = new ArrayList<>();

        public Fly(CommandFly playerVar) {
            this.playerVar = playerVar;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length == 0) {
                    flyMethod(player);
                } else if (args.length == 1) {
                    if (player.hasPermission("flyplugin.flyothers")) {
                        Player target = Bukkit.getPlayer(args[0]);
                        flyMethod(target);
                    } else {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("deny-permission")));
                    }
                }
                return true;
            }
        }
           
        private void flyMethod(Player player);
        {
             if (player.hasPermission("flyplugin.fly")) {
                 if (list_of_flying_players.contains(player)) {
                     list_of_flying_players.remove(player);
                     player.setAllowFlight(false);
                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-on-message").replace("{player}", "player.getDisplayName()")));
                  } else if (!list_of_flying_players.contains(player)) {
                     list_of_flying_players.add(player);
                     player.setAllowFlight(true);
                      player.sendMessage(ChatColor.translateAlternateColorCodes('&', playerVar.getConfig().getString("fly-off-message").replace("{player}", "player.getDisplayName()")));
                  }
              }
          }
    }
     
  8. Thanks for the help guys! I'm really grateful that Spigot has such an active and supportive community like you guys.

    Thanks, Aris
     
  9. If your issue is resolved, make sure you mark the thread as Solved