Config problem

Discussion in 'Spigot Plugin Development' started by WailTosti, Apr 19, 2017.

  1. Hello SpigotMC!

    I've got a problem with my ConfigManager.
    When i make a Config in the Project File.
    And put some # and spacers.
    It will delete that all?

    Code (Text):
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    public class CustomConfigUtils
    {
      public static void CreateNewConfigFile(Plugin plugin, String filename)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        if (!plugin.getDataFolder().exists())
        {
          plugin.getDataFolder().mkdir();
          if (!dfile.exists()) {
            try
            {
              dfile.createNewFile();
            }
            catch (Exception localException) {}
          }
        }
        else if (!dfile.exists())
        {
          try
          {
            dfile.createNewFile();
          }
          catch (Exception localException1) {}
        }
      }
      public static Boolean configContains(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        if (data.contains(path)) {
          return Boolean.valueOf(true);
        }
        return Boolean.valueOf(false);
      }
      public static String getString(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return ChatColor.translateAlternateColorCodes('&', data.getString(path));
      }
      public static Integer getInteger(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return Integer.valueOf(data.getInt(path));
      }
      public static Double getDouble(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return Double.valueOf(data.getDouble(path));
      }
      public static Boolean getBoolean(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return Boolean.valueOf(data.getBoolean(path));
      }
      public static List<String> getStringList(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return data.getStringList(path);
      }
      public static List<Integer> getIntegerList(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return data.getIntegerList(path);
      }
      public static List<Double> getDoubleList(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        return data.getDoubleList(path);
      }
      public static List<Location> getLocationList(Plugin plugin, String filename, String path)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        List<Location> temp = new ArrayList();
        List<String> tempLocs = data.getStringList(path);
        for (String loc : tempLocs)
        {
          String[] coords = loc.split(",");
          World w = Bukkit.getWorld(coords[0]);
          double x = Double.parseDouble(coords[1]);
          double y = Double.parseDouble(coords[2]);
          double z = Double.parseDouble(coords[3]);
          int pitch = Integer.parseInt(coords[4]);
          int yaw = Integer.parseInt(coords[5]);
          Location location = new Location(w, x, y, z);
          location.setPitch(pitch);
          location.setYaw(yaw);
          temp.add(location);
        }
        return temp;
      }
      public static void setString(Plugin plugin, String filename, String path, String value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, value);
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
      public static void setInteger(Plugin plugin, String filename, String path, int value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, Integer.valueOf(value));
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
      public static void setDouble(Plugin plugin, String filename, String path, double value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, Double.valueOf(value));
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
      public static void setBoolean(Plugin plugin, String filename, String path, boolean value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, Boolean.valueOf(value));
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
      public static void setStringList(Plugin plugin, String filename, String path, List<String> value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, value);
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
      public static void setIntegerList(Plugin plugin, String filename, String path, List<Integer> value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, value);
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
      public static void setDoubleList(Plugin plugin, String filename, String path, List<Double> value)
      {
        File dfile = new File(plugin.getDataFolder(), filename + ".yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dfile);
        data.set(path, value);
        try
        {
          data.save(dfile);
        }
        catch (IOException localIOException) {}
      }
    }
     
     
  2. Yes, every time you save the FileConfiguration to a File all the comments are removed from the file.
     
  3. Please use the forums search function before starting new posts.
     
  4. If you wanted to you can still use YAML to retrieve values but you can use a PrintWriter to edit the file (This isn't the best solution but it's good for simple cases of removing/adding a section to a config so when players update they don't have to do it themselves)
     
  5. What must i do?
     
  6. Store data in custom configs where you don't need comments cause people won't be touching them or edit/save data in the config.yml without using YAMLConfiguration#set