Search code examples
javabukkit

How exactly would you add optional arguments to a command?


I am trying to give the option to add an optional argument to some of my commands, this is just one in example:

public class CommandHug implements CommandExecutor {

    String MessageHugPrefix;
    String MessageHugEmote;
    String MessageNoPermission;
    
    public CommandHug() {
        this.MessageHugPrefix = ChatColor.translateAlternateColorCodes('&', Core.getPlugin().getConfig().getString("Messages.HugPrefix"));
        this.MessageHugEmote = ChatColor.translateAlternateColorCodes('&', Core.getPlugin().getConfig().getString("Messages.HugEmote"));
        this.MessageNoPermission = ChatColor.translateAlternateColorCodes('&', Core.getPlugin().getConfig().getString("Messages.NoPermission"));
    } 
    
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("hug")) {
            if (!sender.hasPermission("netsync.fun")) {
                sender.sendMessage(String.valueOf(this.MessageNoPermission));   
            } else
            for (Player players : Bukkit.getOnlinePlayers()) {
                if (args.length == 0) {
                    players.sendMessage(String.valueOf(this.MessageHugPrefix) + ChatColor.DARK_PURPLE + ((Player)sender).getName() + " hugged you!");
                    players.sendTitle(String.valueOf(this.MessageHugEmote), ChatColor.DARK_PURPLE + ((Player)sender).getName() + " hugged you!", 7, 50, 7);
                } else {
                    players.sendMessage(String.valueOf(this.MessageHugPrefix) + ChatColor.DARK_PURPLE + args[1] + " hugged you!");
                    players.sendTitle(String.valueOf(this.MessageHugEmote), ChatColor.DARK_PURPLE + args[1] + " hugged you!", 7, 50, 7);
                }
                    
            }
        }
    return true;
    }
}

It's a command called /hug that will put a message in chat saying that the sender hugged them or the sender can put another player's username / a random argument after the command to hug everyone. The only thing is when I try to run the /hug command on a test server it literally does nothing, no errors in console, no output in chat, no messages / titles sent to the players; nothing. The strings are defined in a config file already, and those pull correctly. Any idea what went wrong?


Solution

  • That is actually relatively easy. Start by setting the default value of whatever variable this optional value would be. Then add a conditional statement that if the argument length is greater then 0, then change that default value. In the example you gave me, here is how it would be done:

        @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("hug")) {
            if (!sender.hasPermission("netsync.fun")) {
                sender.sendMessage(String.valueOf(this.MessageNoPermission));   
            } else
            for (Player players : Bukkit.getOnlinePlayers()) {
                // this is the default value
                Player target = (Player) sender;
                // if the argument length is greater the 0, set the value of "target" to args[1]
                if (args.length > 0) {
                    Player target = Bukkit.getPlayerExact(args[1]);
                    // its a good idea to make sure that the player exists by running a null check
                    if (player == null) {
                        players.sendMessage("Player not found!");
    
                    }
                }
                players.sendMessage(String.valueOf(this.MessageHugPrefix) + ChatColor.DARK_PURPLE + player.getName() + " hugged you!");
                players.sendTitle(String.valueOf(this.MessageHugEmote), ChatColor.DARK_PURPLE + player.getName() + " hugged you!", 7, 50, 7);
                }
                    
            }
        }
    return true;
    }