Solved Storing a variable to a variable in a config file

Discussion in 'Spigot Plugin Help' started by PerfMouse, Mar 7, 2020.

  1. I have a custom .yml config file and I want to store a value to every world a player joins

    Current code:
    Code (Java):
    public void savePlayerData(Player p) {
           
            //UUID
            List<String> uuids = new ArrayList<String>();
            uuids = getPlayerData().getStringList("players");
           
            if (!uuids.contains(p.getUniqueId().toString())) {
                uuids.add(p.getUniqueId().toString());
                getPlayerData().set("players", uuids);
                }
           
            //worlds
            List<String> pl_worlds = new ArrayList<String>();
            pl_worlds = getPlayerData().getStringList("players." + p.getUniqueId().toString());
           
            if (!pl_worlds.contains(p.getWorld().getName())) {
                pl_worlds.add(p.getWorld().getName());
                getPlayerData().set("players." + p.getUniqueId().toString(), pl_worlds);
                }
       
            //SAVE
           
            try {
                getPlayerData().save(playerDataFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    What I expected as output was:
    Code (YAML):
    players:
    - 24592050-9772-4dfc-8c79-672b2e0eea07
    :
     - hub
      - RLSurvival
    - 6cd3bb90-969c-4ab9-8581-f08cf51f83c4
    :
     - hub
    But what I got was:
    Code (YAML):
    players:
      24592050-9772-4dfc-8c79-672b2e0eea07
    :
     - hub
    So after some testing I figured out what I was doing wrong. But now I still have no idea how I would get and set a variable (variable_x) to a variable (variable1)
    Example:
    Code (YAML):
    constant:
    - variable1
    :
     - variable_x
    - variable2
    :
     - variable_y
     
  2. See https://bukkit.gamepedia.com/Configuration_API_Reference

    Their example:
    Code (Text):
    // Setting a List of Strings
    // The List of Strings is first defined in this array
    List<String> listOfStrings = Arrays.asList("Hello World", "Welcome to Bukkit", "Have a Good Day!");
    this.getConfig().set("path.to.list", listOfStrings);
    Their "path.to.list" is your "constant.variable1" or "constant.variable2"
    Their List is your list of worlds.

    If I understand you correctly.
     
  3. But what should I do if I don't know variable1?

    I tried "constant." + variable1.toString() as path,
    but it results in:
    Code (YAML):
    constant:
    variable1
    :
    - variable_x
    So it replaces all the strings in 'constant:' with variable1
     
  4. What does your set statement look like?

    And are you sure you're saving the file before checking it?

    Also you should ask for this thread to be moved to Spigot Plugin Development.
     
  5. set statement:
    Code (Java):
    getPlayerData().set("players." + p.getUniqueId().toString(), pl_worlds);
    output:
    Code (YAML):
    players:
    24592050-9772-4dfc-8c79-672b2e0eea07
    :
     - hub
    And how do I move a thread?
     
  6. Except for the indenting (did it paste wrong?) isn't that what you want?
     
  7. I am trying to get this
    Code (YAML):
    players:
    - 24592050-9772-4dfc-8c79-672b2e0eea07
    :
     - hub
     - RLSurvival
    - 6cd3bb90-969c-4ab9-8581-f08cf51f83c4
    :
     - hub
    But whenever I add a world to "players."+uuid (by adding it to a List with all the worlds that where already belonging to "players."+uuid. And then getConfig().set("players."+uuid, List_of_worlds)) It removes everything in "players:" and sets it to
    Code (YAML):
    players:
    24592050-9772-4dfc-8c79-672b2e0eea07
    :
     - hub
     
  8. Post the code you have now.
     
  9. MainClass:
    Code (Java):

    package me.PerfMouse.Essentials;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin{
     
        public static Plugin plugin;
        public static Logger console = Bukkit.getServer().getLogger();
     
        public void onEnable() {
            //plugin info
            plugin = Bukkit.getServer().getPluginManager().getPlugin(this.getName());
            console.info("PerfMouse Essentials is enabled!");
         
            //implemented listeners
            Bukkit.getServer().getPluginManager().registerEvents(new serverEvents(), this);
         
            //custom data files
            createPlayerDataFile();
        }

        public void savePlayerData(Player p) {
         
            //UUID
            List<String> uuids = new ArrayList<String>();
            uuids = getPlayerData().getStringList("players");
         
            if (!uuids.contains(p.getUniqueId().toString())) {
                p.sendMessage("set UUID");
             
                uuids.add(p.getUniqueId().toString());
                getPlayerData().set("players", uuids);
                }
         
            //worlds
            List<String> pl_worlds = new ArrayList<String>();
            pl_worlds = getPlayerData().getStringList("players." + p.getUniqueId().toString());
         
            if (!pl_worlds.contains(p.getWorld().getName())) {
                pl_worlds.add(p.getWorld().getName());
                getPlayerData().set("players." + p.getUniqueId().toString(), pl_worlds);
                }
     
            //SAVE
         
            try {
                getPlayerData().save(playerDataFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

     
        private File playerDataFile;
        private FileConfiguration playerData;

        public FileConfiguration getPlayerData() {
            return this.playerData;
        }


        private void createPlayerDataFile() {
            playerDataFile = new File(getDataFolder(), "playerData.yml");
            if (!playerDataFile.exists()) {
                playerDataFile.getParentFile().mkdirs();
                saveResource("playerData.yml", false);
             }

            playerData = new YamlConfiguration();
         
                try {
                    playerData.load(playerDataFile);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvalidConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

    }
     
    savePlayerData is being called by an event, which is 100% working
     
  10. I usually blunder around the config code until I get it to do what I want. :) It looks to me that your UUID section will wipe-out the old world data, at the very least. If you have a new player, you don't need to separately add his UUID in the config file. When you add players.UUID and the List of worlds, it will create the UUID key if it's not already there.

    You just need to get the list of old worlds from the config, add the new world to the list, then set the new list in the config. Which I think you're already doing.
     
  11. Waw thanks.. So I just need to remove the code from uuid
     
  12. I didn't go through your code with a fine-toothed comb. So you still may have to make other changes.
     
  13. Thank you :D
    Everything is working now!