Multiple args and stuff

Discussion in 'Plugin Development' started by generaldon, Jun 12, 2012.

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

    generaldon

    Alright I need a big learning moment here. I'm totally stuck, and my experimenting has gotten me no where except backwards. I'm trying to make a theoretically simple plugin that has 3 (I think 3) args, one containing either Send, Revoke, or Accept, the next for a player, and the last for a time. The last isn't entirely necessary because I fear it may be too advanced for my level. (Maybe a set time would be easier rather than a variable time?)
    So anyway what I can't figure out is how to get the args to work. All I have right now is that it sends a message on the command and another message when any amount of args are included, what I can't figure out is how to make it do something with those added args. That's what I need help with, if anyone can :) Also any general tips are welcome too, I'm sure there's lots of wrong formatting and such.
    Plugin yaml:
    Code:
    name: Warrant
    main: com.github.generaldon.Warrant
    version: 0.0.1
    author: GeneralDon
    commands:
        warrant:
            description: Main Warrant command.
            usage: /warrant
            permission: warrant.warrant
            permission-message: You don't have permission!
    Main (and only) code:
    Code:
    package com.github.generaldon;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Warrant extends JavaPlugin {
       
        public static Warrant plugin;
        Logger log;
       
        public void onEnable(){
            log = this.getLogger();
            log.info("Warrant version 0.0.1 enabled sucessfully!");
        }
        @Override
        public void onDisable(){
            log.info("Warrant version 0.0.1 disabled sucessfully!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("warrant")){
            if(args.length == 0){
                sender.sendMessage(ChatColor.RED + "No parameters given!" + ChatColor.GREEN + " Send, Revoke, accept");
                return true;
                }  else {
                    sender.sendMessage(ChatColor.RED + "Incorrect usage! " + ChatColor.BLUE + "Usage: " + ChatColor.GREEN + "/warrant");
                return true;
                }
            }
            else if(args[0].equalsIgnoreCase("warrant")) {
                    if (args.length == 1) {
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if(target == null){
                            player.sendMessage(ChatColor.RED + "That user is not online!");
                            return true;
                        } else {
                            //Player has been found
                            player.sendMessage(ChatColor.GRAY + "Sending a warrant to " + target.getDisplayName()); //Use .getName() instead of .getDisplayName(), unless you want their display name explicitly, their display name can change, their player name will always be the same
                            target.sendMessage(ChatColor.DARK_PURPLE + player.getName() + " has sent you a warrant!");
                            return true;
                        }
                    } else {
                    sender.sendMessage(ChatColor.RED + "Incorrect usage! " + ChatColor.WHITE + "Usage: " + ChatColor.BLUE + "/warrant send <user>" + ChatColor.WHITE + "!");
                    return true; //again ;)
                   
            }
                }
            return false;
            }
    }
     
     
    
     
  2. Offline

    tomjw64

    I think your brackets may be a bit messed up, but I'll check it out some more. Also, hey there generaldon! :)
     
  3. Offline

    generaldon

    Hey there! Oh yeah i'm sure they're really messed up. My formatting isn't good at all here
     
  4. Offline

    tomjw64

    This is how I would do what you are trying to do. I've fixed a couple weird things, made it a bit easier to read(hopefully), and added some notes that you can use if you choose. Also, may I ask what exactly was the problem you were having with the arguments themselves?
    Code:
    package com.github.generaldon;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Warrant extends JavaPlugin {
        /*
        * I don't believe making an instance (public static Warrant plugin;) is necessary for this plugin
        * I may be wrong though. Feel free to keep it.
        */
        //This is how I do the logger, you don't have to do it this way
        public static final Logger logger=Logger.getLogger("Minecraft");
        //Initialize the ChatColors as fields to make them easier to access later
        private final ChatColor WHITE=ChatColor.WHITE;
        private final ChatColor RED=ChatColor.RED;
        private final ChatColor BLUE=ChatColor.BLUE;
        private final ChatColor GRAY=ChatColor.GRAY;
        private final ChatColor PURPLE=ChatColor.DARK_PURPLE;
     
        @Override
        public void onEnable(){
            //No need to initialize logger again
            logger.info("Warrant version 0.0.1 enabled sucessfully!");
        }
       
        @Override
        public void onDisable(){
            logger.info("Warrant version 0.0.1 disabled sucessfully!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            //I prefer to check the command, before I do anything else, but it's up to you
            if(cmd.getName().equalsIgnoreCase("warrant"))
            {
                //Be sure to check if the sender is a player, and not a remote sender before you cast
                if(sender instanceof Player)
                {
                    Player player=(Player)sender;
                    if(args.length==0)
                    {
                        //No arguments, only "/warrant" has been typed
                        player.sendMessage(RED + "No parameters given!" + WHITE + "Usage: " + BLUE + "/warrant send|revoke|accept <user>" + WHITE + "!");
                    }
                    /*
                    * I don't know why you checked to see if the first argument was "warrant"
                    * and then checked to see if the same argument was a player's name.
                    * I will assume it was a mistake and leave it out.
                    */
                    else if(args.length==2)
                    {
                        //There are two arguments: /warrant args[0] args[1]
                        //Start checking the first argument for "send", "revoke", and "accept"
                        if(args[0].equalsIgnoreCase("send"))
                        {
                            //Bukkit.getServer().getPlayer(String) isn't necessary, you only need to use Bukkit.getPlayer(String)
                            Player target=Bukkit.getPlayer(args[1]);
                            if(target!=null)
                            {
                                //Player is found
                                player.sendMessage(GRAY + "Sending a warrant to " + target.getDisplayName());
                                target.sendMessage(PURPLE + player.getName() + " has sent you a warrant!");
                            }
                            else
                            {
                                //Player is not found
                                player.sendMessage(RED + "That user is not online!");
                            }
                        }
                        else if(args[0].equalsIgnoreCase("revoke"))
                        {
                            //Revoke
                        }
                        else if(args[0].equalsIgnoreCase("accept"))
                        {
                            //Accept
                        }
                        else
                        {
                            //First argument is not "send", "revoke", or "accept", therefore usage is incorrect.
                            player.sendMessage(RED + "Incorrect usage! " + WHITE + "Usage: " + BLUE + "/warrant send|revoke|accept <user>" + WHITE + "!");
                        }
                    }
                    else
                    {
                        //args.length==1||args.length>2
                        player.sendMessage(RED + "Incorrect usage! " + WHITE + "Usage: " + BLUE + "/warrant send|revoke|accept <user>" + WHITE + "!");
                    }
                }
            }
            return false;
        }
    }
    There, I think I'm done obsessively editing the post above. :p Hopefully it helps you some.

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

    generaldon

    That helps a lot, thanks so much! The problem was just that I didn't know how to make it recognize having multiple arguments, or what to do with them.
    also does my plugin.yml stay the same or do I need to add....something?
     
  6. Offline

    tomjw64

    No, you can keep it the same, because your only base command is /warrant. :) Glad I could help!
     
  7. Offline

    generaldon

    Okay awesome :D
     
Thread Status:
Not open for further replies.

Share This Page