FileConfiguration and loadConfiguration() with the 1.12 api

Discussion in 'Plugin Development' started by Reflxction, Sep 18, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Reflxction

    Hello,
    I've been encountering issues when trying to make custom yaml files with the new 1.12 api. It used to work with 1.11.2 and past, but there seems to be some different method for it. Here's my code so far:
    Code:
    public File file() {
            return new File(Bukkit.getPluginManager().getPlugin("blablabla").getDataFolder(), "messages.yml");
        }
    
        FileConfiguration yaml = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
        public void load() {
            File stream = new File(Bukkit.getPluginManager().getPlugin("blablabla").getDataFolder(), "messages.yml");
            if (!(stream).exists()) {
               
            }
            yaml = YamlConfiguration.loadConfiguration(stream);
        }
    
    It doesn't generate the messages.yml. I printed out a string from it, and it prints "null". Any help would be much appreciated
     
  2. Online

    timtower Administrator Administrator Moderator

    @xTechno_ You probably want to create the file if it doesn't exist
     
  3. Offline

    Reflxction

    @timtower it's already embedded in the plugin. If you mean I create it manually after the plugin loads then this is the last thing I would do
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    Reflxction

    @timtower So getResource("messages.yml") should be used instead of getDataFolder()? Will try the copyDefault() once I get on my computer
     
  6. Online

    timtower Administrator Administrator Moderator

    @xTechno_ getDataFolder just returns the plugin specific folder
     
  7. Offline

    RcExtract

    Actually I am confused too. If the folder does not exist, the data folder method should not return null. It should return a non-existed file.
     
  8. Online

    timtower Administrator Administrator Moderator

    The value of yaml = YamlConfiguration.loadConfiguration(stream); returned null
     
  9. Offline

    Reflxction

    @timtower @RcExtract Okay I got the file to generate, but it's blank. I tried using addDefault() but it's not working. Any idea?
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Offline

    Reflxction

    @timtower
    Code:
    @Override
        public void onEnable() {
            load();
            file();
            yaml.addDefault("test", "testy");
            saveDefaultConfig();
            try {
                newConfigStream.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
          
            }
        }
    
        @Override
        public void onDisable() {
            save();
        }
    
        public File file() {
            return new File("plugins/blabla/messages.yml");
        }
    
        FileConfiguration yaml = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
        File newConfigStream;
    
        public void load() {
            newConfigStream = new File(Bukkit.getPluginManager().getPlugin("blabla").getDataFolder(),
                    "messages.yml");
            if (!(newConfigStream).exists()) {
                yaml = YamlConfiguration.loadConfiguration(newConfigStream);
    
            }
        }
    
     
  12. Online

    timtower Administrator Administrator Moderator

    @xTechno_ You aren't writing the resource from the jar to the folder.
    And saveDefaultConfig is not yaml.saveDefaultConfig, remember that.
     
  13. Offline

    Reflxction

    @timtower I already call save() in the onDisable(), and the yml is saved in the jar file with the string in it
     
  14. Online

    timtower Administrator Administrator Moderator

    @xTechno_ But then you don't have the values from the file in the jar.
     
  15. Offline

    Reflxction

    @timtower It seems to be working when I write it out manually. I just don't know how could I use getResource(), and the javadocs don't seem to help
     
  16. Online

    timtower Administrator Administrator Moderator

  17. Offline

    Reflxction

    @timtower This is outdated, it doesn't work with 1.12 api
     
  18. Online

    timtower Administrator Administrator Moderator

  19. Offline

    Reflxction

    Last edited: Sep 21, 2017
    timtower likes this.
  20. Offline

    Minesuchtiiii

    Let me give you an idea how I do it on my Troll Plugin.
    Code:
      File stats = new File(getDataFolder() + "/stats.yml");
      FileConfiguration statscfg = YamlConfiguration.loadConfiguration(stats);
    First I am defining the file and its file configuration.
    Then I created a method which checks if the file exists, if not it creates it.
    Code:
    public void check4File() {
       
        if(!(stats.exists())) {
           
            try {
                stats.createNewFile();
                addStatsDefaults();
                System.out.println("[TrollBoss] Created stats file.");
                saveStats();
                System.out.println("[TrollBoss] Saved stats file.");
            } catch (IOException e) {
                e.printStackTrace();
            }
           
        }
        else {
           
            saveStats();
           
        }
       
    }
    To save the file I created someting like
    Code:
    public void saveStats() {
       
        try {
           
            statscfg.save(stats);
           
        } catch (IOException e) {
            e.printStackTrace();
        }
       
    }
    Adding defaults like
    Code:
    public void addStatsDefaults() {
    
        statscfg.addDefault("Troll.Burn", 0);
        statscfg.addDefault("Troll.Freeze", 0);
        statscfg.addDefault("Troll.Bolt", 0);
    
    statscfg.options().copyDefaults(true);
    
    }
    and finally getting values from it
    Code:
    public int getStats(String cmd) {
       
        int stats = statscfg.getInt("Troll." + cmd);
       
        return stats;
       
    }
    setting values is similar, just remember saving it after putting in some values. in the onEnable() part just put the check4file() method and it's done. I don't know if this is the easiest or best way, but it's working for me. I hope this gave you an idea :D
     
    Reflxction likes this.
  21. Offline

    Reflxction

    Another problem, when I am getting a string from it, it's not working :/
    file:
    Code:
    test:
      testpath: "teststring"
    Code:
    Code:
                            sender.sendMessage(ChatColor.YELLOW + yaml.getString("test.testpath"));
    
    @timtower
    It sends "testpath" but not "teststring". Got any idea?

    sorry for bumping
     
  22. Offline

    Minesuchtiiii

    didn't my code examples help you out?
     
  23. Offline

    Reflxction

    @Minesuchtiiii It did, I did exactly like you did, but it's not working :/
     
  24. Online

    timtower Administrator Administrator Moderator

    @xTechno_ Could you post the entire method / code that is using these variables?
     
  25. Offline

    Reflxction

    @timtower
    Code:
    } else {
            for (int z = 1; z < reports.getInt("current-reports"); z++) {
               sender.sendMessage(ChatColor.YELLOW
                                            +  " -" + reports.getString("reports.report" + z).toLowerCase().replace("[", "").replace("]", ""));
                                }
    
     
  26. Online

    timtower Administrator Administrator Moderator

    @xTechno_ Now you are posting something else...
    What is reports? How is it set?
    Please post all your code.
     
  27. Offline

    Reflxction

    @timtower
    Sorry, I did some changes to the code while waiting for some replies. Here's my whole onCommand method
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
            int ci = reports.getInt("current-reports");
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This command can be used by players only!");
            }
            if (sender instanceof Player) {
                if (cmd.getName().equalsIgnoreCase("report")) {
                    if (!sender.hasPermission("areport.send") || !sender.isOp()) {
                        sender.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                    }
                    if (sender.hasPermission("areport.send") || sender.isOp()) {
                        if (args.length == 0) {
                            sender.sendMessage(ChatColor.RED + "Incorrect usage. Try " + ChatColor.YELLOW
                                    + "/report {message}" + ChatColor.RED + ".");
                        }
                        if (args.length >= 1) {
                            String reason = "";
    
                            for (int i = 0; i < args.length; i++) {
                                String arg = args[i] + " ";
                                reason = reason + arg;
    
                            }
                            for (Player staff : Bukkit.getOnlinePlayers()) {
                                if (staff.hasPermission("areport.read") || staff.isOp()) {
                                    String reportmsg = getConfig().getString("format").replace("{player}",
                                            sender.getName());
                                    String reportmsgupdated = reportmsg.replace("{report_message}", reason);
                                    staff.sendMessage(ChatColor.translateAlternateColorCodes('&', reportmsgupdated));
                                    staff.playSound(staff.getLocation(),
                                            Sound.valueOf(getConfig().getString("sound-on-report").toUpperCase()), 1, 1);
                                    reports.getStringList("reports").add("report" + ci);
                                    ArrayList<String> rl = (ArrayList<String>) reports.getStringList("reports");
                                    rl.add("report" + ci);
                                    reports.set("reports.report" + reports.getInt("current-reports"), rl);
                                    reports.set("current-reports", ci + 1);
                                    saveConfig();
                                    save();
                                }
                            }
                        }
                    }
                }
            return false;
          }
    
    reports is yaml
     
Thread Status:
Not open for further replies.

Share This Page