Solved get string and scoreboard content separately in file list

Discussion in 'Spigot Plugin Development' started by Nico12, Feb 12, 2020.

Thread Status:
Not open for further replies.
  1. Hello! What I am looking for first of all is to obtain properties from a list of files, that is Strings, ConfigurationSection, etc., but taking into account that all the files will be almost the same, I will give you an example:
    Code (Text):

    Yaml File nº1:
    Scoreboard:
      Name: 'MineVikings'
      Scores:
      - 'Hello% player_name%'
      - 'Your money is% vault_balance%'
      - 'Enjoy your stay on the server'
      - 'mc.minevikings.n
     Permission: miplugin.scoreboard.1
    ------------------------------------------------------------------------------------
    Yaml File nº2:
    Scoreboard:
      Name: 'BEDWARS'
      Scores:
      - 'Hello% player_name%'
      - 'Your money is% vault_balance%'
      - 'Enjoy your stay on the server'
      - 'Destroyed beds: 4'
      - 'mc.ultracraft.n
     Permission: miplugin.scoreboard.2
     
    I hope I have explained myself, thank you very much to all who respond I will look forward to your help!
    By the way here is my code with which I get all the files:
    Code (Text):

        public void saveYML() {
            try {
                fc.save(file);
                }catch(IOException e) {
                e.printStackTrace();
            }
        }
        public File folder() {
            File folder = new File(this.getDataFolder() + File.separator + "Inventory");
            if(!folder.exists()) {
                folder.mkdir();
            }
            return folder;
        }
        public FileConfiguration getFiles() {
            if(fc == null) {
                reloadCustomConfig();
            }
            return fc;
        }
        public File[] files() {
            File[] files = folder().listFiles();
            return files;
        }
        public void reloadCustomConfig() {
            for(int i = 0; i < files().length; i++) {
                if(files()[i].isFile()) {
                    String path = files()[i].getName();      

                if(file != null)  {
                    file = new File(folder(), path);
                }
           
                Reader defConfigStream;  
           
                fc = YamlConfiguration.loadConfiguration(file);
                    try {
                        defConfigStream = new InputStreamReader(this.getResource(path), "UTF8");
                   
                        if (defConfigStream != null) {
                            YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);

                            fc.setDefaults(defConfig);
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public void registerCustomConfig() {
            for(int i = 0; i < files().length; i++) {
                if(files()[i].isFile()) {
                    String path = files()[i].getName();      
                    file = new File(folder(), path);
                    if(!file.exists() ) {
                        this.getFiles().options().copyDefaults(true);
                        saveYML();
                    }
                }
            }
                File file2 = new File(this.getDataFolder() + File.separator + "Inventory", "example.yml");
                FileConfiguration fc2 = YamlConfiguration.loadConfiguration(file2);
                if(!file2.exists()) {
                    fc2.options().copyDefaults(true);
                    try {
                        fc2.save(file2);
                    } catch (IOException e) {
                        e.printStackTrace();
                        }
                    }
        }
     
    Keep in mind that the user can create the amount of files he wants
     
  2. I don’t understand with what you are having troubles. You have a list of files and a method to load these files into FileConfigurarions, so what is the problem with accessing the data from these files?
     
  3. I don't know how to get the strings, configurationsection etc, since I've tried a player.sendMesaage ("" + fc.getString ("Scoreboard.Name")) and it didn't work, it was only for testing
     
  4. Nope, simply return the string of the first file in the directory (Obviously if the file contains the requested string)
     
  5. Is the path well typed?
    try printing every key on the fileconfiguration to check what you are reading
    Code (Java):
    for(String key : fc.getConfigurationSection("").getKeys(true){
      Bukkit.broadcastMessage(key);
    }
     
  6. The path is well written but as I said it only returns the requested string of the first file
     
  7. I might be looking at this wrong but it seems like you're treating the entire thing that you're trying to return as a string list but that's not how string lists are setup to my experience? In your example you're getting "Scoreboard.Name" but also expecting it to return as a string list somehow getting the Scores section as well? Do correct me if I'm wrong and your test message was not trying to get the entire next section as well.
     
  8. I am honestly quite lost, I will summarize what I need, I have a method to create a scoreboard and its contents are stored configuration files, and in theory every new file that is in this.getDataFolder () \ Inventory directory would be a new scoreboard, like for example the AnimatedMenu plugin, which each configuration file can be understood as a new inventory / menu, the AnimatedMenu example is basically what I am looking to do.
     
  9. Firstly, it looks like your config string for your IP is missing an apostrophe. Secondly, as far as the scores are concerned, try:
    Code (Java):
    ArrayList<String> scores = new ArrayList<String>();

    for (Object o : fc.getConfigurationSection("Scoreboard.Scores").getKeys(false).toArray())
                    {
                        scores.add((String) o);
                    }
     
  10. With the Scores are you using getStringList?
     
Thread Status:
Not open for further replies.