简体   繁体   中英

Why is this not working? New to Java. (Bukkit)

Making an alert system in Java for a 1.8 Bukkit test server. Extremely new to java.

            if (args.length == 0) {
                sender.sendMessage("§cUsage: /alert <message>");
                return true; // << "return true" means that it will stop running the code
            }

            if (args[0].equalsIgnoreCase(args[0])) {
                String message = args[0];
                for (int i = 1; i < args.length; i++) {
                    message += args[i] + " ";
                }
                Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', message + " "));

I don't know what's wrong with this. Whenever I do /alert in game it sends the first word fine but then no space then it goes straight to the next word. Then the rest of the message is fine.

You currently initialize message with a value, but in your loop you don't prepend the space. Change

message += args[i] + " ";

to

message += " " + args[i];

Also, args[0] will always equals args[0] (and in the original case, so ignoring case for comparing it to itself is odd). Finally, and alternatively, if using Java 8+ you can use a collector like

String message = Arrays.stream(args).collect(Collectors.joining(" "));
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', message));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM