Solved YAML question

Discussion in 'Spigot Plugin Development' started by Justin393, Aug 1, 2019.

  1. I am trying to pull data from towny regarding offline players. In order to not freeze the server with name lookups to Mojang servers in order to return the UUID I was wondering how I can go about getting a value out of a config if I know the key.

    Example:
    Code (YAML):
    9bdd0741-2731-43a1-82b7-16c6a3631111: justin_393
    How would I go about searching the file for the key "justin_393" and returning the UUID?
     
  2. It would need the be the inverse of the current way. (Or not). You could switch it, and do name checks to make sure the name mathes the uuid. Or you could loop each part and find the uuid. No? The simple solution is to not store the player's name at all. It can change and lead to issues. Where there is a player, there is a UUID.
     
  3. This file is created by Towny's "TownyNameUpdater" therefore it is not a file that I control, it just has the info in it that I need.
     
  4. I see, how exactly are you getting the uuid in the first place?
     
  5. Reread the main post. I don’t have the UUID and that is what I need. I am making an automated system that will inform staff when a town has been inactive for x days based off of player activity. Towny by default only stores player names, not UUIDs. In order to get the last online time as stored by Spigot you need the player UUID.

    In order to get the player UUID you have to use OfflinePlayer(String name) which queries Mojang servers to return a UUID. The problem is that when you do this in bulk it causes the main thread to WAIT, which in turn causes the JVM to kill the thread since it waits for over a minute. There is also a limit to how many names you can resolve within x amount of time.

    The plugin TownyNameUpdater creates the file with the format uuid:name so I’m trying to get the uuid when I only know the name.
     
  6. If there's no path leading up to that couldn't you just iterate through the config and check if the corresponding value is equal to the one you want?
     
  7. That’s what I thought, but I’ve only been able to find examples on how to iterate through a ConfigurationSection, maybe I’m not searching enough, but that’s all I’m finding
     
  8. Im making a example right now, double checking it works.

    EDIT:
    It works, so I had it check at the start of the plugin and print out the UUID it gets just to make it easy. I'm probably going to get yelled at for how sloppy and quickly I made this but I don't really care -

    Code (Java):
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.Set;
    import java.util.UUID;

    public class Main extends JavaPlugin {

        public void onEnable() {
            saveDefaultConfig();

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

        private UUID getPlayer() {
            FileConfiguration config = getConfig();

            Set<String> UUIDs = config.getConfigurationSection("").getKeys(false);

            for (String s : UUIDs) {
                if (config.getString(s).equalsIgnoreCase("justin_393")) { return UUID.fromString(s); }
            }
            return null;
        }
    }
    Config
    Code (YAML):
    9bdd0741-2731-43a1-82b7-16c6a3631111: justin_393
    9aaaaa0741-2731-43a1-82b7-16cca3631111
    : testPlayer
    ccc
    : testPlayer3
    Output -
    Code (Text):
    [23:07:06 INFO]: 9bdd0741-2731-43a1-82b7-16c6a3631111
     
    • Informative Informative x 1
  9. Huh, now that I see it, I guess that makes sense that getConfigurationSection(“”) would return the entire File.
    I appreciate it!
     
    • Like Like x 1
  10. No worries! Let me know if you have any problems.
     
  11. drives_a_ford

    Moderator

    Actually, FileConfiguration implements ConfigurationSection so that part is unnecessary. You can simply use getConfig().getKeys(false);.
     
    • Like Like x 1
    • Agree Agree x 1
  12. Did not know that, thats pretty interesting. Thanks for the info!