Solved Cannot be resolved to a variable

Discussion in 'Spigot Plugin Development' started by Nighterance, Jun 8, 2017.

  1. I started to make new plugin, and decided to do it properly, no shortcuts, everything trying to make as smooth as possible. Now I managed somehow to make small mistake or something.

    Problem is, that I can't access variable.

    Code (Text):

    public boolean onCommand(CommandSender cs, org.bukkit.command.Command cmd, String string, String[] args) {
         Player p = (Player) cs;
            if (string.equalsIgnoreCase("playerinfo")) {
              if(args.length == 0){
                 if(p.isOp()){
                     String op = ChatColor.GREEN + "Yes";
                 }else{
                     String op = ChatColor.RED + "No";
                 }
                 if(p.isFlying()){
                     String fly = ChatColor.GREEN + "Yes";
                 }else{
                     String fly = ChatColor.RED + "No";
                 }
                 if(p.getGameMode().equals(GameMode.CREATIVE)){
                     String gm = ChatColor.GREEN + "C";
                 }else if(p.getGameMode().equals(GameMode.SURVIVAL)){
                     String gm = ChatColor.GREEN + "S";
                 }else{
                     String gm = ChatColor.GREEN + "A";
                 }
                 p.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "*" + ChatColor.GRAY + "]" + " " + ChatColor.GOLD + p.getName());
                 p.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "*" + ChatColor.GRAY + "]" + " " + ChatColor.GOLD + "Displayname:" + " " + ChatColor.GREEN + p.getDisplayName());
                 p.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "*" + ChatColor.GRAY + "]" + " " + ChatColor.GOLD + "Fly/GM/OP:" + " " + fly + ChatColor.WHITE + "/"+ ChatColor.WHITE + gm + "/" + ChatColor.WHITE + op);
              }

            }
            return false;
        }
     
     
  2. Declare the variable before your if statements. This is a beginner "scope" issue.
     
  3. Hmm, I tried that, but had problem, now i figured out why :D