Solved Simple command not working?

Discussion in 'Plugin Development' started by dan14941, Oct 6, 2014.

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

    dan14941

    I have no idea why this isnt working?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel)
        {
            if(commandLabel.equalsIgnoreCase("under"))
            {
                Player player = (Player) sender;
                player.sendMessage("test");
            }
            return true;
        }
    if i type /under i dont get an error but it dosent say test?
     
  2. dan14941 1) You didn't include the String array of arguments.
    2) Check the command name not the command label - command name supports aliases.
    3) Don't cast to a Player without checking it's a Player.
    4) Don't cast to a Player if you don't need to access Player methods.
     
  3. Offline

    dan14941

    AdamQpzm is this ok?
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    2. {
    3. if(cmd.getName().equalsIgnoreCase("under"))
    4. {
    5. Player player = (Player) sender;
    6. player.sendMessage("test");
    7. }
    8. return true;
    9. }

    what should i change?
     
  4. dan14941 You're still not checking that it is a Player first.
     
  5. Offline

    dan14941

    AdamQpzm would i use the instanceOf thing?
     
  6. dan14941 Yep! Although I think you should spend time learning the basics of Java, since this is covered under that. I'd recommend the Oracle tutorials or a Java book. :)
     
  7. Offline

    Minesuchtiiii

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
    2. if(cmd.getName().equalsIgnoreCase("under")) {
    3. if(args.length == 0) {
    4. if(sender instanceof Player) {
    5. Player player = (Player) sender;
    6. player.sendMessage("test");
    7. }
    8. return true;
    9. } else {
    10. p.sendMessage("You are using too many arguments!");
    11. }
    12. }
    13. }


    Didn't code in Eclipse, so it could be that some if-and-else things are wrong..
     
  8. Offline

    Minesuchtiiii

    AdamQpzm
    Yes, but they should even see once the code.. so next time they know what to do and don't need to ask again.
     
Thread Status:
Not open for further replies.

Share This Page