Saving resources to different folders.

Discussion in 'Plugin Development' started by acecheesecr14, Apr 8, 2014.

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

    acecheesecr14

    How might one save a resource (differentConfig.yml) to a different folder?
    (plugins/images/differentConfig.yml) Instead it's saving to (plugins/[PLUGIN NAME]/differentConfig.yml).
    I created the folder succesfully. And I'm using the Bukkit config API.
     
  2. Offline

    mazentheamazin

    acecheesecr14
    It would look like this:
    Code:java
    1. File fl = new File(getDataFolder + File.separator + "images", "differentConfig.yml");
    2. FileConfiguration fg = YamlConfiguration.load(fl);
     
  3. Offline

    JBoss925

    You use a new configuration file and give the directory.

    EDIT: Like mazentheamazin ^^^
     
  4. Offline

    acecheesecr14

    So here's what I'm using. It somewhat resembles what you have said to do...
    Code:java
    1.  
    2. private FileConfiguration TestConfig = null;
    3. private File TestConfigFile = null;
    4. private File PluginsFolder = new File(testConfig.class.getProtectionDomain().getCodeSource().getLocation().getPath().replaceAll("%20", " "));
    5. private File ImagesFolder = new File(PluginsFolder.getParentFile().getPath() + File.separator + "Images");
    6.  
    7. public void reloadTestConfig() {
    8. if(!ImagesFolder.exists()){
    9. ImagesFolder.mkdir();
    10. System.out.println("Images folder created @.");
    11. System.out.println(AllAllowFolder.getPath());
    12. }else{
    13. System.out.println("Using Images folder @.");
    14. System.out.println(AllAllowFolder.getPath());
    15. }
    16. if (TestdConfigFile == null) {
    17. TestConfigFile = new File(ImagesFolder, "ImagesConfig.yml");
    18. }
    19. TestConfig = YamlConfiguration.loadConfiguration(TestConfigFile);
    20.  
    21. // Look for defaults in the jar
    22. InputStream defConfigStream = this.plugin.getResource("ImagesConfig.yml");
    23. if (defConfigStream != null) {
    24. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    25. TestConfig.setDefaults(defConfig);
    26. }
    27. }
    28.  

    P.S. This still doesn't work...
     
  5. Offline

    Xeno_1

    I made this code. Works fine for me.

    Code:java
    1. package com.XenoJava.DJArcade;
    2.  
    3. import java.io.File;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.io.OutputStream;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.plugin.Plugin;
    14.  
    15. public class DataManager {
    16.  
    17. Plugin p;
    18. FileConfiguration data;
    19. File dfile;
    20. File dataFolder;
    21. static DataManager instance = new DataManager();
    22.  
    23. public static DataManager getInstance() {
    24. return instance;
    25. }
    26.  
    27. public void setupData(Plugin p) {
    28.  
    29. dataFolder = new File(p.getDataFolder(), "data");
    30. if (!dataFolder.exists()) {
    31. dataFolder.mkdir();
    32. }
    33.  
    34. if (dfile == null) {
    35. dfile = new File(dataFolder.getPath(), "location.yml");
    36. }
    37.  
    38. InputStream inputStream = null;
    39. OutputStream outputStream = null;
    40.  
    41. if (!dfile.exists()) {
    42. try {
    43. // read this file into InputStream
    44. inputStream = p.getResource("location.yml");
    45.  
    46. // write the inputStream to a FileOutputStream
    47. outputStream = new FileOutputStream(dfile);
    48.  
    49. int read = 0;
    50. byte[] bytes = new byte[1024];
    51.  
    52. while ((read = inputStream.read(bytes)) != -1) {
    53. outputStream.write(bytes, 0, read);
    54. }
    55.  
    56. System.out.println("Done!");
    57.  
    58. } catch (IOException e) {
    59. e.printStackTrace();
    60. Bukkit.getServer()
    61. .getLogger()
    62. .severe(ChatColor.RED
    63. + "Could not create location.yml!");
    64. } finally {
    65. if (inputStream != null) {
    66. try {
    67. inputStream.close();
    68. } catch (IOException e) {
    69. e.printStackTrace();
    70. }
    71. }
    72. if (outputStream != null) {
    73. try {
    74. // outputStream.flush();
    75. outputStream.close();
    76. } catch (IOException e) {
    77. e.printStackTrace();
    78. }
    79.  
    80. }
    81. }
    82.  
    83. }
    84. this.data = YamlConfiguration.loadConfiguration(this.dfile);
    85. }
    86.  
    87. public FileConfiguration getData() {
    88. return this.data;
    89. }
    90.  
    91. public void saveData() {
    92. try {
    93. this.data.save(this.dfile);
    94. } catch (IOException e) {
    95. Bukkit.getServer().getLogger()
    96. .severe(ChatColor.RED + "Could not save data file!");
    97. }
    98. }
    99.  
    100. public void reloadData() {
    101. this.data = YamlConfiguration.loadConfiguration(this.dfile);
    102. }
    103.  
    104. }
    105.  
     
Thread Status:
Not open for further replies.

Share This Page