Looping a UUID in a YML file

Discussion in 'Plugin Development' started by Xp10d3, Jul 10, 2020.

  1. Offline

    Xp10d3

    How would I loop through unique UUID's in my config? For example, if I have this:
    Code:
    9eaf2e85-9784-48b9-bdb5-4593de0db4d0:
      uuid: 9eaf2e85-9784-48b9-bdb5-4593de0db4d0
      race: Elf
      exp: 6373
      level: 48
    1d30686f-649c-4c8c-90e2-fb14cd140d59:
      uuid: 1d30686f-649c-4c8c-90e2-fb14cd140d59
      race: Orc
      exp: 1834
      level: 13
    
    There will always be new UUID's coming through as player's join the server. If I have this command in my `Commands.java` class:
    Code:
    if (lable.equalsIgnoreCase("update")) {
      File file = new File(Bukkit.getPluginManager().getPlugin("DummyPlugin").getDataFolder(), "config.yml");
      FileConfiguration otherFile = YamlConfiguration.loadConfiguration(file);
              
      String fileConfig = otherFile.getString("test");
      Bukkit.getLogger().info(fileConfig);
      core.otherLog("[" + format.format(now) + "]" + "Successfully got info from the plugin 'DummyPlugin'. Message: " + fileConfig);
      core.otherLog("e");
    }
    
    I'll need to loop through each UUID, take that information, that insert it into a database. But what stumps me is how to loop through them... How would I accomplish this?

    Commands.java:
    Code:
    package eltik.connection.sql;
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.UUID;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    public class Commands implements CommandExecutor {
      
        Core core = Core.getPlugin(Core.class);
      
        public Commands(Core core) {
            this.core = core;
            Bukkit.getPluginCommand("update").setExecutor(this);
        }
      
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
            Player player = (Player) sender;
            String playerName = player.getName();
            UUID uuid = player.getUniqueId();
            if (lable.equalsIgnoreCase("update")) {
                File file = new File(Bukkit.getPluginManager().getPlugin("DummyPlugin").getDataFolder(), "config.yml");
                FileConfiguration otherFile = YamlConfiguration.loadConfiguration(file);
              
                String fileConfig = otherFile.getString("test");
                Bukkit.getLogger().info(fileConfig);
                core.otherLog("[" + format.format(now) + "]" + "Successfully got info from the plugin 'DummyPlugin'. Message: " + fileConfig);
                core.otherLog("e");
            }
            return false;
        }
    }
    
    
     
  2. Offline

    Strahan

    Get a ConfigurationSection object then loop the keys from it.
    Code:
    ConfigurationSection cfg = (plugin object).getConfig().getConfigurationSection("");
    if (cfg == null) {
      sender.sendMessage("No UUIDs have been logged.");
      return;
    }
    
    for (String uuidText : cfg.getKeys(false)) {
      sender.sendMessage(uuidText + "'s race is " + cfg.getString(uuidText + ".race", "Unknown"));
    }
    It seems redundant by the way to key on UUID then have UUID as a subkey.

    PS you also should check if sender is a Player before casting it thus.
     

Share This Page