PiggyPiglet
PiggyPiglet

Reputation: 155

Convert List<String> of uuids to player names spigot java

I have a List in my main class that gets data from a certain section in config. Here's the code for it,

private List<String> datalist;

public List<String> getDataList() {
    if (datalist == null) {
        datalist = new ArrayList<>();
    }
    datalist = datalist.stream().distinct().collect(Collectors.toList());
    return datalist;
}
void loadConfig() {
    final FileConfiguration config = this.getConfig();
    config.options().copyDefaults(true);
    saveConfig();

    ConfigurationSection section = this.getConfig().getConfigurationSection("data");
    if (section != null) {
        Set<String> datas = section.getKeys(false);
        if (datas != null && !datas.isEmpty()) {
            for (String data : datas) {
                getDataList().add(data);
            }
        }
    }
}

loadConfig() get's called in the onEnable method. Currently, if I sent this to a player or console, it would be formatted in an arraylist. ([uuid,uuid,uuid,etc,etc]). My config is formatted like this,

data:
  e81a48c8-6e82-304a-b435-832a362b4cbf:
    name: PiggyPiglet
    stat1: 0
    stat2: 0
    stat3: 0
    hasjoined: true

One of these getting generated for a player on PlayerJoinEvent. I have a command, /lb show in my commands class, here's the code for it.

if (type.equalsIgnoreCase("show")) {
                    if (sender.hasPermission("leaderboard.show")) {
                        sender.sendMessage(cc("&7LeaderBoard:"));

                        String lb = String.valueOf(plugin.getDataList()).replace("[",
                                "").replace("]",
                                "").replace(",",
                                "\n");
                        TextComponent leaderboard = new TextComponent(lb);
                        leaderboard.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("test").create() ) );
                        sender.spigot().sendMessage(leaderboard);
                    }
                }

That code sends in chat the uuids in config like this.

uuid
uuid
uuid

And when you hover over one of the uuids, it says "test". My question is how would I change those uuids to player names and also instead saying "test", saying

"EGCW | EGCL | KWC\n" + String.valueOf(cfg.getInt("data." + uuid + ".EGCW")) + " | " + String.valueOf(cfg.getInt("data." + uuid + ".EGCL")) + " | " + String.valueOf(cfg.getInt("data." + uuid + ".KWC"))"

Replacing uuid with the uuid on the current line.

Upvotes: 3

Views: 2649

Answers (2)

dly
dly

Reputation: 1088

Your UUIDs are another ConfigSection as child of data, so you need to go one level deeper.

This approach might not be the best, but it works.

ConfigurationSection section = config.getConfigurationSection("data");
        if (section != null) {
            Set<String> datas = section.getKeys(false);
            if (datas != null && !datas.isEmpty()) {
                for (String uuid : datas) {
                    ConfigurationSection section2 = config.getConfigurationSection("data." + uuid);
                    if (section2 != null) {

                    getDataList().add(section2.getString("name"));

                    }
                }
            }
        }
        System.out.println(datalist);

Output from using a slightly altered config.yml (it's better to have more than one user in it):
[PiggyPiglet1, PiggyPiglet2, PiggyPiglet3]

The ComponentBuilder should then work as intended. Just replace "test" with the String you want to be displayed.

Upvotes: 3

Jeremy Grand
Jeremy Grand

Reputation: 2370

You need some kind of PlayerService that gives you the method findByUuid(String uuid) and returns you a Player.

Once you have it, you can convert your list of String into a list of names like this :

datalist.stream().map(uuid -> playerService.findByUuid(uuid))
    .filter(player -> player != null)
    .map(player -> player.getName()) 
    .collect(Collectors.toList())

But in your case, you'd better retrieve the Map<String,Player> from the config rather than just the keys. Then, no need from the service, you would just have to perform a get(uuid) on the map.

Upvotes: 0

Related Questions