Sam
Sam

Reputation: 313

Printing a List<Player> in Spigot 1.12.2

I'm a bit of a noob to developing plugins, but up until this point I've been able to figure things out because of prior Java experience, the API's, some tutorials, etc. Now I'm having an issue that I can't seem to fix. I'm setting up a command for my server (/builders) which shows a list of the online builders who can be summoned to your location to give you building advice. I've made a system for sending the list of builders like this (don't ask why I called my command variable bobmc):

if (bobmc.getName().equalsIgnoreCase("builders") && sender instanceof Player) {
            Player p = (Player) sender;
            List<Player> onlineBuilders = new ArrayList<Player>();
            for (Player players : Bukkit.getOnlinePlayers()) {
                if (PermissionsEx.getPermissionManager().getUser(players).inGroup("builder")) {
                    onlineBuilders.add(players);
                }

            }

            sender.sendMessage(ChatColor.BLUE + "Builders List: " + ChatColor.BOLD + "" + onlineBuilders);
            return true;

        }

The issue with this is that it prints this out: [​IMG]

I can't seem to remedy this, and there don't seem to be any other posts on the internet to help me.

Upvotes: 1

Views: 1804

Answers (2)

dly
dly

Reputation: 1088

Use a String to print the names. There is no need to create a List if you're only looking for the player names. To get the names you can use getName().

if (bobmc.getName().equalsIgnoreCase("builders") && sender instanceof Player) {
            Player p = (Player) sender;
            String onlineBuilders = "";
            for (Player players : Bukkit.getOnlinePlayers()) {
                if (PermissionsEx.getPermissionManager().getUser(players).inGroup("builder")) {
                    onlineBuilders += players.getName() + " ";
                }

            }

            sender.sendMessage(ChatColor.BLUE + "Builders List: " + ChatColor.BOLD + "" + onlineBuilders.trim());
            return true;

        }

Upvotes: 2

Sam
Sam

Reputation: 313

It may be weird to reply to my own question, but I got an easy answer on the Spigot forums. I added a String (onlineBuilderList) and used this to update it with the builders list:

onlineBuilderList = String.join(", ", onlineBuilders.stream().map(Player::getName).collect(Collectors.toList()));

This also allows for an easy error message, as I can just say String onlineBuilderList = "No builders are currently online"; when I declare the String in the first place. This does use Java 8, however, so it isn't the most perfect of solutions.

Upvotes: 1

Related Questions