My code is messed up.

Discussion in 'Plugin Development' started by tacos1223, Oct 29, 2014.

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

    tacos1223

    So I got into coding a year back, got really good and learned a lot in 6 months, but then got busy. Now that I am back it is really hard and I have forgot the most simple stuff. Can someone help me with this problem I am having? When I type /find (player) it works perfectly and if the person is offline that works too, but when I type /find, I am getting an internal error. I think it might have to do with where I put my "{" and "}"

    Code:
          public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[])
          {
              Player player = (Player)sender;
              Player player2 = player.getServer().getPlayer(args[0]);
              if(commandLabel.equalsIgnoreCase("find"))
              {
                  if(args.length == 0){
                     
                  player.sendMessage(ChatColor.RED + "You must enter in the player you want to find. /find (player)");
                  }
                  if(args.length == 1)
                  {
                      if(player.getServer().getPlayer(args[0])!= null){
                         
                      player.sendMessage(ChatColor.GREEN + "You can find " + player2.getDisplayName() + " at " + player2.getLocation() + ".");
                    }
                  else{
                      player.sendMessage(ChatColor.RED + "That player is not online!");
                      }
                  }
              }
            return false;
          }
     
  2. Offline

    mythbusterma

    tacos1223

    Because you're accessing args[0] without actually checking that args[0] exists. And you're casting to Player without checking if the cast is safe.
     
  3. Offline

    teej107

    Not checking the args length, not safely casting.....
     
  4. Offline

    tacos1223

    Ok I fixed it. Thanks.
     
  5. Offline

    fireblast709

    tacos1223 use cmd.getName() instead of commandLabel for alias support. Also, you could use Bukkit.getPlayer() instead of player.getServer() (so you don't need the player).
     
  6. Offline

    tacos1223

    This is still confusing me. What do I have to do for it to work?
     
  7. Offline

    ColonelHedgehog

    Well, pretty much what you're doing (if you're not doing anymore unsafe operations, that is) should work. However, what fireblast709 means is the getLabel() method only checks for one specific alias. If you use the Command.getName() function, then it will return true even if your command has an alias that is, say, "f".
     
  8. Offline

    mine-care

Thread Status:
Not open for further replies.

Share This Page