How to make a help command?

Discussion in 'Plugin Development' started by Rehmedy, Jun 15, 2012.

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

    Rehmedy

    Hey, I am a newb at plugin development, and I do not know how to make a command that returns a pre-defined help script to be returned. If anyone would post the code as to what I would put in it, I would be very grateful. Its for a claiming chunk plugin. The command would be /chunk with no arguments and it needs to return a help thing. Thanks
     
  2. Offline

    r0306

    Rehmedy
    You could send the player a bunch of messages using the sendMessage() method. This should be done within a separate method by itself if you are planning to use this help page for other commands as well.

    Code:
    public void helpMessage(Player player) {
    player.sendMessage("your help stuff here");
    player.sendMessage("use multiple lines of this to keep things formatted and neat");
    }
    To call that method up, you simply put this line in when no arguments are entered.

    Code:
    helpMessage(player);
     
  3. Offline

    CorrieKay

    The best way to go about doing this, if youre doing it for commands, is to just properly fill out your commands node in your plugin.yml.

    Each command under commands has a description, and a usage value. The usage value is sent to the player when they return false while trying to use the command. The description is what is returned when the user looks at the command under /help
     
  4. Offline

    Rehmedy

    No, I am not planning on making it so It comes up for every command when it is entered incorrectly, I am wanting it to come up only when they type /chunk, any specific way I have to do that? Or can I just use the way r0306 said?
     
  5. Offline

    CorrieKay

    If you have deemed that the command was entered incorrectly, (via incorrect parameters, or lackthereof, etc) just type return false;

    It will send that player the usage string from in your plugin.yml
     
  6. Offline

    Rehmedy

    Well, i want the commands to be /chunk and /chunk claim and stuff so would the code look something like this?:
    package com.github.rehmedy.Chunk;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;

    public class ChunkCommandExecutor {

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(label.equalsIgnoreCase("chunk")) {
    //return false or whatever
    if args[0].equalsignorecase("claim")
    //code here
    }
    }
    }
     
  7. Offline

    r0306

    CorrieKay
    Actually, some people (like me :D) prefer to return true even when the command wasn't executed properly because we send our own help messages, not the default usage which looks plain in white color. But that's just an opinion. Rehmedy, you should try both methods and see whichever one you like better. But I think that the only way to add color to a message is if you send the message using the sendMessage() method.

    Rehmedy
    Well, you should add brackets around the if statement and also put the return statement at the end of the method. You also need to implement CommandExecutor. Make sure that you register the command executor in your onEnable() in your main class.

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

    Rehmedy

    If i do that, would it be like:
    player.sendMessage("&cMessage In Red")?
     
  9. Offline

    r0306

    Rehmedy
    No, in the Bukkit API, you use ChatColor.
    Code:
    player.sendMessage(ChatColor.RED + "Message in Red");
     
  10. Offline

    Rehmedy

    so would it be:
    Code:
    package com.github.rehmedy.Chunk;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
     
    public class ChunkCommandExecutor {
     
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(label.equalsIgnoreCase("chunk")) {
    //return false or whatever
    }
    {
    if args[0].equalsignorecase("claim")
    //code here
    }
    }
    }
    }
    
    ?
     
  11. Offline

    r0306

    Rehmedy
    More like this:
    Code:
    package com.github.rehmedy.Chunk;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
     
    public class ChunkCommandExecutor implements CommandExecutor{
     
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(label.equalsIgnoreCase("chunk")) {
      Player player = (player) sender;
      if args[0].equalsignorecase("claim")
      {
      //code here
      }
      else
      {
       helpMessage(player);
       }
    }
    }
    }
     
  12. Offline

    Rehmedy

    oh! Thanks r0306. You really helped me on this.

    one more question, where do I put the help message? In a different .class?

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

    r0306

    Rehmedy
    It's really up to you but I prefer putting it in the same class because it will only be used for this specific class anyways.
    Code:
    package com.github.rehmedy.Chunk;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
     
    public class ChunkCommandExecutor implements CommandExecutor{
     
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(label.equalsIgnoreCase("chunk")) {
      Player player = (player) sender;
      if args[0].equalsignorecase("claim")
      {
      //code here
      }
      else
      {
      helpMessage(player);
      }
    }
    }
     
    public void helpMessage(Player player) {
       //help messages
    }
     
    }
     
  14. Offline

    CorrieKay

    oi, you can add color to it silly :p
     
  15. Offline

    r0306

    Really? O.O How, may I ask? :p Never knew that lol.
     
  16. Offline

    CorrieKay

    getCommand(string).setUsage(ChatColor.BLUE+usage);

    you could probably also just do this:

    commands:
    command:
    usage: ยง1Your usage here
     
    r0306 likes this.
Thread Status:
Not open for further replies.

Share This Page