Command only returning usage

Discussion in 'Plugin Development' started by Tster, Jan 7, 2012.

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

    Tster

    Help please, my commands only return the usage string!
    Code:
    package me.tster.ChatFun;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ChatFun extends JavaPlugin{
    
        Logger log = Logger.getLogger("Minecraft");
    
        public void onDisable() {
            log.info("ChatFun V1.0 has been disabled");
        }
    
        public void onEnable() {
            log.info("ChatFun V1.0 has been enabled");
        }
    
        public boolean onCommand(CommandSender sender, String commandLabel, String[] args){
    
            Player p = (Player) sender;
    
            if (p.isOp()){
                String[] commandArgs = args;
                if (commandArgs[0] == "spanks" || commandArgs[0] == "destroys" || commandArgs[0] == "loves" || commandArgs[0] == "kisses" || commandArgs[0] == "slaps" || commandArgs[0] == "smacks" || commandArgs[0] == "whacks" || commandArgs[0] == "hugs" || commandArgs[0] == "drops" || commandArgs[0] == "beats" || commandArgs[0] == "thanks"){
                    Bukkit.getServer().broadcastMessage("" + ChatColor.BLUE + p.getDisplayName() + ChatColor.WHITE + commandArgs[0] + commandArgs[1]);
                }
            }
            return false;
        }
    }
    
     
  2. Offline

    skore87

    For starters, no need to redeclare args XD and also, look for cmd.name or commandlabel first to be sure it's the command you want to catch, provided you have more than one command. Anyway, within the second IF statement, put a "return true;".

    And also, the boolean onCommand is like this:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {}
     
  3. Offline

    Tster

    Is that the problem?
     
  4. Offline

    Technius

    No, put return true in the p.isOp() block
     
  5. Offline

    Tster

    Like this?
    Code:
            if (p.isOp()){
                String[] commandArgs = args;
                if (commandArgs[0] == "spanks" || commandArgs[0] == "destroys" || commandArgs[0] == "loves" || commandArgs[0] == "kisses" || commandArgs[0] == "slaps" || commandArgs[0] == "smacks" || commandArgs[0] == "whacks" || commandArgs[0] == "hugs" || commandArgs[0] == "drops" || commandArgs[0] == "beats" || commandArgs[0] == "thanks"){
                    Bukkit.getServer().broadcastMessage("" + ChatColor.BLUE + p.getDisplayName() + ChatColor.WHITE + commandArgs[0] + commandArgs[1]);
                    return true;
                }
            }
            return false;
        }
    No luck >.<

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  6. Offline

    theguynextdoor

    change
    if (commandArgs[0] == "spanks"
    to

    if (commandArgs[0].equalsIgnoreCase("spanks")
    and do that for each one
     
  7. Offline

    skore87

    no... if you do that, the nested IF statement could still passed without actually doing anything. You would want to return true after the second if statement for when the command executes successfully unless you decide to do your own messages where you would return true once at the bottom instead.
    Code:
        if(first statement){
            if(second statement){
                // code
                return true;
            }
        }
        return false;
    I just want to be sure what you're actually writing. Are you typing "/mycommand spanks" or "/spanks"?

    Because String[] args starts after the command. So in this example below, the first argument (0) would be "argument1":
    /<command> argument1 argumet2 argument3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  8. Offline

    Tster

    i am typing "/spanks [player]"
     
  9. Offline

    Technius

    You realized I just read through it for half a second, right?

    Anyways,
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    3. {
    4. Player player = null;
    5. if(sender instanceof Player)player = (Player) sender;
    6. if(args.length == 1)
    7. {
    8. if (commandArgs[0] == "spanks" || commandArgs[0] == "destroys" || commandArgs[0] == "loves" || commandArgs[0] == "kisses" || commandArgs[0] == "slaps" || commandArgs[0] == "smacks" || commandArgs[0] == "whacks" || commandArgs[0] == "hugs" || commandArgs[0] == "drops" || commandArgs[0] == "beats" || commandArgs[0] == "thanks")
    9. {
    10. Bukkit.getServer().broadcastMessage("" + ChatColor.BLUE + p.getDisplayName() + ChatColor.WHITE + commandArgs[0] + commandArgs[1]);
    11. return true;
    12. }
    13. }
    14. return false;
    15. }
     
  10. Offline

    Tster

    Code:java
    1.  
    2. The user must be op? - Thanks so far guys :D
    3.  
    4. Perhaps it is something to do with my plugin.yml ?
    5.  
    6. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  11. Offline

    ZephyrSigmar

    Oh guys,it isn't complicated,come on!
    Because the "onCommand" is a boolean you MUST return a true or a false.
    Please note if you're code reach a "return",i mean if you're returning something then the code execution on this void or boolean or anything stops.
    Something like this.
    Code:
    public boolean animal(){
       System.out.println("Dog");
       return true;
       System.out.println("Cat");
    }
    So in this example if we call this animal boolean it should print out "dog" then return a true,and it will NOT print out cat,because it's already returned. (Eclipse and softwares like this should drop an error because of unreachable code,that is the cat part).
    If you're "return false" then you get back your command in the chat,however,the command would be executed.
    But if you're returning true then you does not get back the command in chat.
    That's what we want,thats how we want to react for our plugin's commands.
    You have to put "return false" at the bottom of your block,because we don't want to return true to every command.
    Ex.: If you're plugin supposed to be handle the "dog" command,then we want to return false to every command which is not "dog".This doesn't make these messages getting returned in chat,simply let bukkit handle it and write "Unknown command" (well,this is not the message but it is the essence).
    So we're handling the "dog" command.
    For ex. someone type in "/dog is hungry" then we get 1 command and two arguments (words).
    The "dog" is going to be the command and the "is" going to be args[0] (because in programing everything start with 0,not 1) and "hungry" is args[1].
    Thats how we going to code it:
    Code:
    public boolean onCommand(CommandSender sender,Command cmd,String cmdLayout,String[] args){
       //If the command equals "dog"
       if(cmdLayout.equalsIgnoreCase("dog"){
           /*If the command is called with 2 words after it (so "args" 's lenght is 2 -> args[0] and args[1])*/
           if(args.lenght==2){
               if(args[0].equalsIgnoreCase("is") && args[1].equalsIgnoreCase("hungry")){
               sender.sendMessage("Then go and feed him!");
           }
           return true;
       }
       //Return false for every other command
       return false;
    }
    However,it's not enough,in our plugin.yml we should put this too:
    Code:
    commands:
       dog:
          description: You doesn't need to specify a description but you can
          usage: <command>
    <command> means the name of the command what we typed above it,in that case it's "dog".
    It's the most easier a quicker way to handle commands.
    If you still have questions tag me in this thread.
     
  12. Offline

    Tster

    Still not working D:
    Code:
    package me.tster.ChatFun;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ChatFun extends JavaPlugin{
    
        Logger log = Logger.getLogger("Minecraft");
    
        public void onDisable() {
            log.info("ChatFun V1.0 has been disabled");
        }
    
        public void onEnable() {
            log.info("ChatFun V1.0 has been enabled");
        }
    
        public boolean onCommand(CommandSender sender, String commandLabel, String[] args){
    
            Player p = (Player) sender;
    
            if (p.isOp()){
                String[] commandArgs = args;
                if (commandArgs[0].equalsIgnoreCase("spanks") || commandArgs[0].equalsIgnoreCase("destroys") || commandArgs[0].equalsIgnoreCase("loves") || commandArgs[0].equalsIgnoreCase("kisses") || commandArgs[0].equalsIgnoreCase("slaps") || commandArgs[0].equalsIgnoreCase("smacks") || commandArgs[0].equalsIgnoreCase("whacks") || commandArgs[0].equalsIgnoreCase("hugs") || commandArgs[0].equalsIgnoreCase("drops") || commandArgs[0].equalsIgnoreCase("beats") || commandArgs[0].equalsIgnoreCase("thanks")){
                    Bukkit.getServer().broadcastMessage("" + ChatColor.BLUE + p.getDisplayName() + ChatColor.WHITE + commandArgs[0] + commandArgs[1]);
                }
                return true;
            }
            return false;
        }
    }
    
    Code:
    name: ChatFun
    main: me.tster.ChatFun.ChatFun
    author: Tster
    version: 1.0
    description: For all your TPing needs in a single plugin!
    commands:
       spanks:
          description: Echo *user* spanks *user*
          usage: spanks [name]
       destroys:
          description: Echo *user* destroys *user*
          usage: destroys [name]
       loves:
          description: Echo *user* loves *user*
          usage: loves [name]
       kisses:
          description: Echo *user* kisses *user*
          usage: kisses [name]
       slaps:
          description: Echo *user* slaps *user*
          usage: slaps [name]
       smacks:
          description: Echo *user* smacks *user*
          usage: smacks [name]
       whacks:
          description: Echo *user* spanks *user*
          usage: whacks [name]
       hugs:
          description: Echo *user* hugs *user*
          usage: hugs [name]
       drops:
          description: Echo *user* drops *user*
          usage: drops [name]
       beats:
          description: Echo *user* beats *user*
          usage: spanks [name]
       thanks:
          description: Echo *user* thanks *user*
          usage: thanks [name]
     
  13. Offline

    iPhysX

    Just remove the usage from the yml.
    Nobody reads it.
     
  14. Offline

    Tster

    kay
     
    iPhysX likes this.
  15. Offline

    skore87

    You guys do realize the code you're giving as a solution toTster is incomplete right?

    Returning false shows this message. It is sometimes used, but often replaced by returning true and sending the player a custom error message in place.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  16. Offline

    theguynextdoor

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (label.equalsIgnoreCase("spank")) {
    3. Player player = Bukkit.getServer().getPlayer(args[1]);
    4. for (Player p : getServer().getOnlinePlayers()) {
    5. if (p.isOp()) {
    6. if (player.isOnline()) {
    7. Bukkit.getServer().broadcastMessage(ChatColor.BLUE + p.getDisplayName() + ChatColor.WHITE + " spanked " + args[1]);
    8. return true;
    9. }
    10. }
    11. }
    12. }
    13. return false;
    14. }


    That is how i would do the spanks command

    That is implying you want the command to be
    /spank <player to spank>
     
  17. Offline

    hatstand

    Yes, so he has to do some work himself.

    A few things:
    • Why are you searching through every player to find an op? Wouldn't you want to check if the sender is an op: sender.isOp(), and use his/her name: sender.getName() or, if instanceof Player ((Player)sender).getDisplayName()?
    • If you're supporting display names, why not use player.getDisplayName() instead of args[1]?
    • args[1] would throw an indexoutofbounds exception, arrays are 0-indexed.
    • And finally, cmd.getName() will return the non-aliased command name (the actual name), while the label will return whatever alias is used.
     
  18. Offline

    theguynextdoor

    Well, not saying mine is the perfect example, yes in this case it would not be the best (quite the opposite the way you put it). But with this you could use permissions aswell with the for loop. tbh i wouldn't do much of this in a command because this command does not do much for what its worth. But you are right about the 'If you're supporting display names, why not use player.getDisplayName() instead of args[1]?' thing, i guess i rushed this.
     
  19. Offline

    Tster

    I know I will have to do some work myself, I came here with a problem - I didn't come here asking someone to write me a plugin with xyz command. I am just wondering why it is not working, I pulled this from aother of my plgins
     
  20. Offline

    ZephyrSigmar

    Well,i wrote a thousand lines for nothing,because you're lazy to read it.
    I complained what is an arg and what is THE command.
    You're still try to check the args,without commands.
    Everybody try to help you out,but if you don't read their posts and codes you will never be able to develope this plugin.
    The third property in the "onCommand" is the cmdLayout,or the label,everyone call it different,the important thing is this string contains the command.
    So if you name this cmdLayout then don't check the arguments,CHECK THE COMMAND,with cmdLayout.equalsIgnorecase("spank") .....
     
  21. Offline

    Tster

    Hold on - when you say 'command' do you mean the whole string or just the base not including the argumnets?


    - after thinking about it, I get it, I am used to using player on command pre process, and I split the command my self.i ow ndersand that on command already splits the arguments and the base for you

    /facepalm x2
     
Thread Status:
Not open for further replies.

Share This Page