1

My /announce command only broadcasts 1st argument, rather than all of them.

For more clarity: If you type /broadcast I love apples

it should broadcast "I love apples", but it only broadcasts "i"

I've tried a lot of different code but never seemed to get it right. I can't post my old code here as it's very much overwritten into oblivion.

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(command.getName().equalsIgnoreCase("announce")){
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if(args.length < 1) {
                    player.sendMessage(ChatColor.DARK_RED + "Usage: /announce <text>");
                    return true;
                } else if(args.length >= 1) {
                    String logToChat = args[0];
                    Bukkit.broadcastMessage(logToChat);
                }

The output should be the text after the /announce command. Due to my poor programming skills, it only detects the 1st word (as an argument) and prints that word only.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • You only use `args[0]` to log to chat. I'm assuming that `args` contains all the Strings you need, but you're only getting the first of them. You'll need to access `args[1], args[2], args[3],` and so on to access all the Strings that you are passed. For your example, I'd guess that `args` looks like `[I, love, apples]`, where `args[0].equals("I")`, `args[1].equals("love")`, and `args[2].equals("apples")`. You can do something like this: `String logToChat = Arrays.toString(args);` if you want to see the whole array of arguments in chat. – Avi Aug 28 '19 at 13:11
  • `args` is an array, `args[0]` is the first element in that array. The solution is simple: Use all elements in the array instead of just the first. Also, you should read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Max Vollmer Aug 28 '19 at 13:12
  • Thanks guys. I figured it out in the post that marked me as a duplicate, which I am happy for because I managed to fix the code. – itsjayden19 Aug 29 '19 at 02:13

0 Answers0