Advertisement
Guest User

Config Creation Tool

a guest
Dec 3rd, 2012
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package net.projectinferno.games.other;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import org.bukkit.configuration.file.FileConfiguration;
  7. import org.bukkit.configuration.file.YamlConfiguration;
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. public class ConfigAccessor {
  11.  
  12.     public JavaPlugin plugin;
  13.     public String fileName;
  14.  
  15.     public ConfigAccessor(JavaPlugin plugin) {
  16.         this.plugin = plugin;
  17.     }
  18.  
  19.     public FileConfiguration createConfig(String name) {
  20.         String pluginDir = plugin.getDataFolder().toString();
  21.         File dir = new File(pluginDir);
  22.         // this is optional. Only needed if you want to put it into a
  23.         // subdirectory. If not, and you just wanna put your file in the plugin
  24.         // data folder, no need to add "+File.separator+subdirectory" to the
  25.         // constructor parameter. You can remove the second parameter even. If
  26.         // you wanna get fancy, check for null, or overload the method.
  27.         if (!dir.exists()) {
  28.             dir.mkdir();
  29.         }
  30.         if (!name.endsWith(".yml")) {
  31.             name = name + ".yml";
  32.         }
  33.         File file = new File(dir, name); // initializes the file object
  34.         if (!file.exists()) {
  35.             try {
  36.                 file.createNewFile();
  37.             } catch (IOException e) {
  38.                 // TODO Auto-generated catch block
  39.                 e.printStackTrace();
  40.             } // creates a new file if it doesnt exist
  41.         }
  42.         return YamlConfiguration.loadConfiguration(file);// returns the newly created configuration object.
  43.     }
  44.  
  45.     public void saveConfig(String name) {
  46.         FileConfiguration config = getConfig(name);
  47.         String pluginDir = plugin.getDataFolder().toString();
  48.         File dir = new File(pluginDir);
  49.         if (!dir.exists()) {
  50.             dir.mkdir();
  51.         }
  52.         if (!name.endsWith(".yml")) {
  53.             name = name + ".yml";
  54.         }
  55.         File file = new File(dir, name);
  56.         YamlConfiguration.loadConfiguration(file);
  57.         // saves to file
  58.         try {
  59.             config.save(file);
  60.         } catch (IOException e) {
  61.             // TODO Auto-generated catch block
  62.             e.printStackTrace();
  63.         }
  64.     }
  65.  
  66.     public FileConfiguration getConfig(String name) {
  67.         String pluginDir = plugin.getDataFolder().toString();
  68.         File dir = new File(pluginDir);
  69.         if (!dir.exists()) {
  70.             dir.mkdir();
  71.         }
  72.         if (!name.endsWith(".yml")) {
  73.             name = name + ".yml";
  74.         }
  75.         File file = new File(dir, name);
  76.         if (!file.exists()) {
  77.             createConfig(name);
  78.         }
  79.         return YamlConfiguration.loadConfiguration(file); // file found, load into config and return it.
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement