[Tutorial/Basic] Commands - Outdated

Discussion in 'Resources' started by Jaker232, Jul 4, 2011.

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

    Jaker232

    UPDATE - Thanks to @Phaedrus I was able to remove the ugly code tag at the bottom and fix some stuff. Please update the permission check line to remove (Player) so it says Sender.

    I just recently learned how to make commands for my plugins. I want to share the information as well, so let's get down to the basics.

    1. onCommand and it's function and Extending down

    onCommand is an important function that starts the command body. This will need to be made in the main class at the bottom or somewhere.
    Code:
     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    This will tell the computer to use the onCommand function and start a collection of code through a curly bracket. Onto the second line of your function
    Code:
    if(cmd.getName().equalsIgnoreCase("<pluginname>")) {
    This is interesting.. it starts the root (imagine a tree, going down from Commands downwards plugin name, then command, then arguments) by labeling <pluginname> as the base of /<pluginname>. After this, you won't need to do "<pluginname> <command>" because the computer recognizes the second root after "Commands."
    Code:
               if(args[0].equals("Stone")) {
    This extends "/<pluginname>" to "/<pluginname> Stone" because the args[0] marks the first argument, you can extend it further if you want, but more complicated so I won't get to that topic.
    You don't have to do .equals("<pluginname> Stone") because it already recognize the second root.
    Code:
                    if (permissionHandler.has(sender, "base.one.two")) {
    Things are getting more complicated now, this is the official Permission API. It must be registered before the code on what the command does, or else everyone will have access. First, you can change "base" to your plugin name and then you can start extending it. A good example is "searchid.search.bedrock" (this node is fake, but SearchID is my progress project) It starts SearchID then goes down to Search, then finishes with bedrock. Anyone with this node is allowed to access "/searchid bedrock." Anything past this line is the code you must add so the command can do it's job. Leave nothing, it'll just do nothing. You must give the computer to execute that. When you are finished, put
    Code:
    return true;
    at the end of it. It will finish it off, but we're not done yet! Just put an "}" below "return true;" and continue to Section 2.

    Finished with this section, your code should look like this (permission node, plugin name, and arguments will be different though, configure them to your liking):
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("search")){
               if(args[0].equals("Stone")) {
                    if (permissionHandler.has(sender, "searchid.search")) {
                        // Put a code here to let the computer execute it or else it won't work.
                        return true;
    }
    2. Else function
    We now have this cool command, but we are missing an "else." Take it out and compile it, it becomes an error and shuts the plugin down. We have a solution to that! After the "}" in Section 1 you just finished, press the Enter key to make a new line, then type
    Code:
    else {
    Permissions will check to see if the player is in a group or have access through users.yml with that node and continue it, or else throw it to the "else" function. After that, do this:
    Code:
    // Allow the console to say something to the player (preferably using player.sendMessage) that //they cannot use this.
    return false;
    I strongly recommend using the player.sendMessage to tell the player he doesn't have permission to use this command. You can type whatever you want in the quotes, but don't type it too long! It might be cut off.
    To finish off with this section, add a "}" and more to complete the if body. Congratulations! You built your first command! The next section is optional, but helpful if you want a few commands.

    If you followed the tutorial correctly, you can get this:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("search")){
               if(args[0].equals("Stone")) {
                    if (permissionHandler.has(sender, "base.one.two")) {
                        player.sendMessage("bla bla");
                        return true;
                    }
                    else {
                        player.sendMessage"You do not have permission to use this!");
                        return false;
                    }
                }
    3. Adding more Commands (optional)
    Now, you really want to add more commands? Duplicating smacks you hard that it's a duplication? This section's for you!
    First, to extend our plugins to have more commands, we'll need to use the "else if" to start it after your first command, don't currently use it after the last "}" and a new body class below, or you'll get an error! Copy this after your first command you did last two sections:
    Code:
               else if(args[0].equals("Grass")) {

    This is a new command, that adds another ability. You can use "/searchid Grass" (in my plugin) as well "/searchid Stone" so it won't keep conflicting. Note that there is a "{" so we're starting another collection of code!
    Code:
                if (permissionHandler.has((Player)sender, "base.one.two.three")) {

    Configure "base.one.two.three" to a permission node you are happy with. I just use searchid.search for all the objects so far, so I don't have to create hundreds of them. Then, extend it downwards (we're starting another collection, if you noticed again!)
    Code:
    // Put stuff here to make command work!
    return true;
    }
    It's like almost the same we did in Section 1. Just replace those slash marks with the piece of code. I just tell the player the data value (this does it in SearchID.)
    Now we're moving to the else function, but use the above to help you. You can add a lot of commands, and there is no limit! Once your done for good, put a final "}" to finish the onCommand function. Congrats, that's all about commands, but there's one more! If you already set up plugin.yml to work with all the commands, you can finally skip the next section and get your prize.
    4. plugin.yml
    We are going to need to put all of our commands into our plugin.yml so Bukkit can recognize a command and make it work properly.
    If you haven't already, putted
    Code:
    commands:
    after the first header. It should now look like this:CODE]name: <PluginName>
    main: <packagename>.<PluginName>
    version: <Version Number>
    commands:[/CODE]
    Now, put 4 spaces the line after commands and put your first command's name there. For example:
    Code:
    name: <PluginName>
    main: <packagename>.<PluginName>
    version: <Version Number>
    commands:
        search:
    Now, put 8 spaces the line after 'search' or whatever your first command is, and put the follow:
    Code:
          description: Description
          usage: /<base> <command> [optional, arguments]
    My example would be:
    Code:
          description: Search for a block's ID (data value.)
        usage: /searchid search [block]
    If you copied all the default one I have provided you, it would look like this:
    Code:
    name: <PluginName>
    main: <packagename>.<PluginName>
    version: <Version Number>
    commands:
        blabla:
            description: Description
            usage: /<base> <command> [Optional, arguments]
    My example would look like this:
    Code:
    name: SearchID
    main: me.Jaker232.SearchID
    version: v0.1.0
    commands:
        search:
            description: Search for a block's ID (data value.)
            usage: /searchid search [block]
    5. Outro
    This is the very first tutorial I have written for bukkit, it took me over than 10 minutes to write. I hope you learn how to create a command from this simple explaining tutorial.

    READ THE FAQ BEFORE REPORTING BUGS. FAQ IS BELOW. (For some reason, click the last. It's possibly a forum bug it duplicated itself two times.)
    FAQ (open)
    FAQ (open)

    Q: I imported the Permissions, yet I'm having a lot of errors?
    A: You must download the latest Permissions.jar and configure your library to add it.

    Q: Will you extend the tutorial to have a multiple argument command?
    A: If I can learn this from someone, I can pass that down so not until someone teaches me.

    Q: I get nearly everything underlined in red.
    A: Have you imported the required resource? I haven't included in this tutorial, but you can look up Permissions, see how to hook into the API and find the imports and add them.

    Q: I have issues other than the problems above, where do I report them?
    A: You are allowed to post in this topic your errors, just underline where the IDE tells you it's an error and list it's Quick Fixes.


    If you have learned a lot from this tutorial, consider putting this into your signature to support it.
    [​IMG]
    Code:
    [url=http://forums.bukkit.org/threads/tutorial-basic-commands-want-to-learn-how-to-make-them-permission-api-support.24635/][img]mag.racked.eu/image/340/Learned+Commands%21/mca.png[/img][/url]


    I was updating it and it messed up, excuse me, I'm going to go fix it.

    EDIT: It seems to be stuck there permanently, it's not my fault. Blame the forum hoster.

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

    rex48

    You don't even say where do we put the code.
     
  3. Offline

    Lolmewn

    on the bottom or something in the main class.
     
  4. Offline

    Jaker232

    Lolmewn is right.

    @rex48 You probably looked too quickly.
     
  5. Offline

    rex48

    Thanks!

    Edit: Oh wait. Were do we put the code for onCommand.

    Edit: P.S.: An IDE, notepad, java?
     
  6. Offline

    Jaker232

    Eclipse's the best. Provides you with quick fixes.
     
  7. Offline

    chernobyl360

    For Example this onCommand im trying to use is supposed to send OPS the message. but im getting errors in return. what was my mistake?

    EDIT: Nvm i fixed the issue.
     
  8. Offline

    Jaker232

    I haven't spotted this yet.
    I think the (Player)sender part should be "sender" instead of that. It breaks the plugin.
     
  9. Offline

    chernobyl360

    you gonna correct that box up there? its spammed with all those size amounts...
     
  10. Offline

    Jaker232

    I can't kill it, so I might have to do a re-write.
     
  11. Offline

    wouter0100

    I have reported the post, i hope that a admin ore something will fix it.
     
  12. Offline

    Phaedrus

    Should be fixed now. If the BB code ever eats your post in the future, you can go into the BB code editor to try and fix it manually. You get there by editing your post, and clicking on the little Aa in the top right corner.
     
  13. Offline

    wouter0100

    an admin have changed it, thanks you!
    (This is beter then a codebox xd)
     
  14. Offline

    Jaker232

    @Phaedrus I love you no joking. Thanks!
     
  15. Offline

    Mentioum

    Id suggest Netbeans - its cleaner :) and IMO easier to use.
     
  16. Offline

    Techwiz101

    I'm making a motd plugin and I wanted to add in commands so I tried following this guide and I get Illegal modifier for parameter onCommand; only final is permitted.
    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
     
  17. I am completly new to writting plugins for bukkit (have some basic java knowledge) and I used
    your tutorial to do my first steps. One thing that you did not mentioned and I had to find out
    myself. You need to add a case to catch if someone types the command without any arguments.
    In your example it would be /searchid.
    I did add the following:
    Code:
    if (args.length == 0){
              // Decide how to handle that case...
                    return true;
                }
     
  18. Offline

    chernobyl360

    could you update this to work with the new permissions system?
     
  19. Offline

    Xtra

    I don't agree with this guy. Netbeans is only better for GUI's. That's what netbeans is focused on.
     
  20. Offline

    Mentioum

    To be honest if you read any big post about IDE's on the internet you will find that the conclusion is - It's almost completely down to personal preference.
     
  21. Offline

    Xtra

    I use both. I use eclipse when i want CODE heavy stuff and i use netbeans when i want gui's.
    If you use gui's, then yes netbeans all the way.(plus, it's easier to use eclipse cause most of the bukkit coding guides are made for it.
     
  22. Offline

    Jaker232

    @Techwiz101 That's weird. I never got that error before. List the quick fixes, please?
    @Schm0ftie Interesting find. Thanks!
    ---
    @Xtra
    @Mentioum
    It doesn't matter what IDE people use, it's their opinion. like Mentioum likes NetBeans, and Xtra and me prefers Eclipse over Netbeans.
     
    Mentioum likes this.
  23. Offline

    Techwiz101

    I have no idea on what they are?
    I still cant get them working..
     
  24. Offline

    Jaker232

    Do you use Eclipse?
     
  25. Offline

    Techwiz101

    Yeah, but that's all that eclipse shows.
     
  26. Offline

    Jaker232

    Weird. Ask in the Plugin Development section.
     
  27. Offline

    TopGear93

    @Jaker232 how would i get other commands?
    with this command im trying, i want to do /switch <player> <command>
    the <command> needs to be another command within my plugin. the other commands in my StarterJawbs plugin are
    /farmer
    /builder
    /hunter
    /logger
    /miner


    Code:
    else if(label.equalsIgnoreCase("switch")){
        player.getServer().getPlayer(args[0]);
     
  28. Offline

    Jaker232

    This tutorial is broken man. Long broken.
     
  29. Offline

    Icelaunche

    I agree and you can do lots more on it too

    well its mainly the same thing... maybe ill make anet beans guide

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

    asleeponduty

    I have the same problem - I think the tutorial is broken
     
Thread Status:
Not open for further replies.

Share This Page