Solved Help: Get all online usernames as a string array

Discussion in 'Spigot Plugin Development' started by abstractionAlpha, Sep 27, 2020.

  1. I want to have an array made up of the usernames of all players, but my code just returns the object format of the player's info. Here's the code:

    Code (Java):
    String[] onlinePlayersList = new String[manhunt.getServer().getOnlinePlayers().size()];

    int i = 0;
    for (Player player : manhunt.getServer().getOnlinePlayers()) {
        onlinePlayersList[i] = player.getName();
        i++;
    }

    manhunt.getLogger().info(String.valueOf(onlinePlayersList));
    Here's the output:

    Code (Java):
    [Ljava.lang.String;@323c7278
    How can I resolve this? I feel like it ought to be a quick fix, but for the life of me I just can't figure it out.
     
  2. Arrays#toString
     
    • Agree Agree x 2
  3. SteelPhoenix

    Moderator

    Why do you need it as an array anyways? If you just want to print all names can't you just do something like this:

    Code (Java):
    String s = Bukkit.getServer().getOnlinePlayers().stream().map(Player::getName).collect(Collectors.joining(", ", "Online players: ", ""));

    // s = "Online players: player1, player2, player3."
     
  4. The printing part was just some troubleshooting. I'm developing a Manhunt (Dream Team style) plugin for myself and my friends to use, and have it set to iterate through the list of online users such as that it doesn't perform some function for the player being hunted.

    EDIT: Also, I like your profile picture.