2

I'm very new to coding. I have tried everything.I can think of to try to get a list of titles that are stored in my config to be sent to the player as messages and nothing I have tried works.

current config setup referenced

players:
    player1name:
        - title1
        - title2
        - title3
    player2name:
        - title1
        - title3

call to send message code line

    if (args.length == 2) {
                        try {
                        // insert code here to display titles list available
                        // to
                        // player
                        Player p = Bukkit.getPlayer(args[1]);
                        if (p == null) {
                            sender.sendMessage("Could Not Find Player!!");
                            return true;
                        }
                        Bukkit.broadcastMessage("I got to this point pre- list");
                        List<String> s = this.Ach.getConfig()
                                .getStringList("players." + p + "titles");

                        StringBuilder message = new StringBuilder();
                        Bukkit.broadcastMessage("I got to this point post message");
                        for (String players : s) {

                            message.append(players).append(" - ");
                        Bukkit.broadcastMessage("I got to this point post for");

                        sender.sendMessage(message.toString()); // (message.toString());
                        } 

                        // Set<String> keys =
                        // this.Ach.getConfig().getConfigurationSection("player."
                        // + p + "titles").getKeys(false);
                        // sender.sendMessage(keys + " - ");
                        Bukkit.broadcastMessage("I got to this point pre return");
                        return true;}
                        catch (Exception e) {
                            e.printStackTrace();
                        }

`Ach' is defined earlier to reference my main class that get config is defined in.

what I expect to be outputed with player1name as the args[1] value title1 - title2 - title3 -

with a valid player name instead of returning the titles the player has, all I'm getting sent to chat is [] -

any help would be greatly appreciated.

2 Answers 2

0

You'll need to iterate through all of the Strings and add them to a variable before sending the message - something like (untested)

StringBuilder message = new StringBuilder();
for (String player : s)
   message.append(player).append(" - ");
sender.sendMessage(message.toString());
8
  • 1
    in your for statement what is player supposed to be referencing? you don't make a call to it afterwards so it's an unused string? Commented Apr 16, 2013 at 10:42
  • ok tried it with your code there (after your edit) and now i'm not even getting the [] - returned it just prints a blank line to screen Commented Apr 16, 2013 at 10:47
  • Are you sure that there are players being returned? Try outputting the size of the returned list. Commented Apr 16, 2013 at 10:51
  • assuming the way to do that is in fact sender.sendMessage(Integer.toString(players.length())); then it's not returning anything or error, just a blank line still. players isn't a typo, i renamed the variable to make it less confusing for me. Commented Apr 16, 2013 at 11:06
  • You don't necessarily need to send it as a message, if you have a console available you could use System.out.println(players.length()); to print it. The Integer.toString isn't required in this case, it will be converted automatically. If there were no messages in the list then it should have returned 0, so that makes me suspect that there might be an error in either sending or reading the message. Commented Apr 16, 2013 at 11:50
0

The Bukkit API also gives you the option of using the YamlConfiguration class and it's getStringList() method where you send in a string-path to the list you want and it, well, sends you the list. If you were to use it for that file, you would want a line like

List<String> titlesList = this.Ach.getStringList("players." + sender.getName());

And for that specific output type, I would use

sender.sendMessage(titlesList.toString().replace("[", "").replace("]", "").replace(", ", " - ");

The reason you had that output was because you had (ASet + " - "). ASet when read as a String looks like [title1, title2] and your set was empty. Also, having the dash beside it wouldn't correct the formatting at all; hence my replace methods. Get rid of the square brackets, commas and spaces and in with what you actually wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.