Advertisement
Guest User

ConfigAccessor

a guest
Dec 4th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 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.         if (!name.endsWith(".yml")) {
  21.             name = name + ".yml";
  22.         }
  23.         File file = new File(plugin.getDataFolder(),name);
  24.         if (!file.exists()) {
  25.             plugin.getDataFolder().mkdir();
  26.             try {
  27.                 file.createNewFile();
  28.             } catch (IOException e) {
  29.                 e.printStackTrace();
  30.             }
  31.         }
  32.         return YamlConfiguration.loadConfiguration(file);// returns the newly created configuration object.
  33.     }
  34.  
  35.     public void saveConfig(String name, FileConfiguration config) {
  36.         if (!name.endsWith(".yml")) {
  37.             name = name + ".yml";
  38.         }
  39.         File file = new File(plugin.getDataFolder(),name);
  40.         try {
  41.             config.save(file);
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     public FileConfiguration getConfig(String name) {
  48.         if (!name.endsWith(".yml")) {
  49.             name = name + ".yml";
  50.         }
  51.         createConfig(name);
  52.         File file = new File(plugin.getDataFolder(),name);
  53.         return YamlConfiguration.loadConfiguration(file); // file found, load into config and return it.
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement