Tutorial Efficient | Multiple Commands!

Discussion in 'Resources' started by Jawfy, Oct 22, 2015.

?

Was this helpfull?

  1. Yes... It helped me.

    2 vote(s)
    22.2%
  2. No... This didn't help me.

    7 vote(s)
    77.8%
Thread Status:
Not open for further replies.
  1. Offline

    Jawfy

    NOTE: THERE IS A CLASS TEMPLATE AT THE BOTTOM!!!

    Hello, in this tutorial I will take you through a simple but very efficient way to code your commands! Simply follow how I do! Please may I note, you will have to have a basic understanding of the api and how to use it as I will not cover everything.

    First of all, create your plugin with the basic layout.
    Code:
    me.yourname
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public ClassName extends JavaPlugin {
    
        @Override
        public void onEnable() {
    
        }
     
        @Override
        public void onDisable() {
         
        }
    }
    Now create your boolean:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    }
    Now, if you are one to follow youtube videos you will be mothod with the option: if(cmd.getName()
    equalsIgnoreCase("COMMAND") { This is all well, but you don't get as much of a option when coming to multiple commands.

    This method allows you to use multiple commands with ease, it's a lot less server intensive as the method I explained above.

    Code:
    Code:
    if(cmd.getName().equalsIgnoreCase("COMMAND")) {
                if(args.length == 0) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cPlace what ever you want here!"));
                    return true;
                } else if (args.length == 1) {
                    if(args[0].equalsIgnoreCase("one")) {
                        if (p.hasPermission("command.one")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aWOOHOO!!! You ran the first command!"));
                            return true;
                        } else {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cNo Permission!"));
                            return true;
                        }
                }
    Now, to add another command simply add a else if() statement on the last bracket like so:

    (LAST BRAKET) } else if(args[0].equalsIgnoreCase("two") {
    return true;
    }

    There you go, simply follow this method and you will have clean code!

    Here's a template I created to get you started:
    <Edited by bwfcwalshy: Removed Mediafire, it is not allowed here. Please use a service such as Dropbox.>
     
    Last edited by a moderator: Oct 22, 2015
  2. Offline

    Mrs. bwfctower

    @Jawfy There is no need to have the onEnable or onDisable methods if they're empty.

    Also, Since you have if.. else if.. etc, there's no need to return true at the end of each, you can just return true at the end.
     
  3. Offline

    Jawfy

    The, onEnable and onDisable statements are there as you will be using them in coding a full on plugin.
     
  4. Offline

    tkuiyeager1

    @Jawfy you are using a player called "p" but you did not write it in the code.
    and before you want to cast the player check if the sender is a player.
     
    FabeGabeMC likes this.
  5. Offline

    Mrs. bwfctower

    Yes, but since this is a tutorial on commands, there's no need to have them in your 'product' or whatever, so it makes the code in this tutorial less clean.
     
    Last edited: Oct 26, 2015
  6. Something like this hasn't been done ever...
     
  7. Offline

    mcdorli

    "First, create your boolean...", I saw this sentence today somewhere. Please, remember this:
    IT ISN'T A BOOLEAN, IT'S A FUNCTION, THAT RETURNS A BOOLEAN.

    Btw.: This tutorial can be just named as "How to manage commands". This isn't new, this is how everybody does it.
     
  8. Offline

    teej107

    Actually I do that quite a lot. I just add in return statements inside the if statements rather than having a bunch of if else statements. Depending on how different the commands are and what they actually do, I'll use a Map filled with custom "execute command" interfaces to run each individual command/arguments.
     
  9. Offline

    ChipDev

    I made the same thread long ago .-.
     
  10. Offline

    Zombie_Striker

    It's an okay sort of tutorial. The fact that you are violeting DRY make makes me not like it (all those return true; lines).
     
Thread Status:
Not open for further replies.

Share This Page