thebmanswan541

HubPlugin - SettingsManager (Bukkit)

Oct 6th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.07 KB | None | 0 0
  1. package me.thebmanswan541;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10. import org.bukkit.plugin.Plugin;
  11. import org.bukkit.plugin.PluginDescriptionFile;
  12.  
  13. public class SettingsManager {
  14.  
  15.         private SettingsManager() { }
  16.        
  17.         static SettingsManager instance = new SettingsManager();
  18.        
  19.         public static SettingsManager getInstance() {
  20.                 return instance;
  21.         }
  22.        
  23.         Plugin p;
  24.        
  25.         FileConfiguration config;
  26.         File cfile;
  27.        
  28.         FileConfiguration admin;
  29.         File admins;
  30.        
  31.         FileConfiguration mod;
  32.         File mods;
  33.        
  34.         FileConfiguration helper;
  35.         File helpers;
  36.        
  37.         FileConfiguration mute;
  38.         File muted;
  39.        
  40.         FileConfiguration ban;
  41.         File banned;
  42.        
  43.         public void setup(Plugin p) {
  44.                 cfile = new File(p.getDataFolder(), "config.yml");
  45.                 config = p.getConfig();
  46.                 config.options().copyDefaults(true);
  47.                 saveConfig();
  48.                
  49.                 admins = new File(p.getDataFolder(), "admins.yml");
  50.                 mods = new File(p.getDataFolder(), "mods.yml");
  51.                 helpers = new File(p.getDataFolder(), "helpers.yml");
  52.                 muted = new File(p.getDataFolder(), "muted.yml");
  53.                 banned = new File(p.getDataFolder(), "banned.yml");
  54.                
  55.                 if (!admins.exists()) {
  56.                         try {
  57.                                 admins.createNewFile();
  58.                         }
  59.                         catch (IOException e) {
  60.                                 Bukkit.getServer().getLogger().severe("Could not create admins.yml!");
  61.                         }
  62.                 }
  63.                 if (!mods.exists()) {
  64.                     try {
  65.                         mods.createNewFile();
  66.                     }
  67.                     catch (IOException e) {
  68.                         Bukkit.getServer().getLogger().severe("Could not create mods.yml!");
  69.                     }
  70.                 }
  71.                 if (!helpers.exists()) {
  72.                     try {
  73.                         helpers.createNewFile();
  74.                     }
  75.                     catch (IOException e) {
  76.                         Bukkit.getServer().getLogger().severe("Could not create helpers.yml!");
  77.                     }
  78.                 }
  79.                 if (!muted.exists()) {
  80.                     try {
  81.                         muted.createNewFile();
  82.                     }
  83.                     catch (IOException e) {
  84.                         e.printStackTrace();
  85.                     }
  86.                 }
  87.                 if (!banned.exists()) {
  88.                     try{
  89.                         banned.createNewFile();
  90.                     }
  91.                     catch(IOException e) {
  92.                         e.printStackTrace();
  93.                     }
  94.                 }
  95.                
  96.                 admin = YamlConfiguration.loadConfiguration(admins);
  97.                 mod = YamlConfiguration.loadConfiguration(mods);
  98.                 helper = YamlConfiguration.loadConfiguration(helpers);
  99.                 mute = YamlConfiguration.loadConfiguration(muted);
  100.                 ban = YamlConfiguration.loadConfiguration(banned);
  101.         }
  102.        
  103.         public FileConfiguration getAdmins() {
  104.                 return admin;
  105.         }
  106.         public FileConfiguration getMods() {
  107.             return mod;
  108.         }
  109.         public FileConfiguration getHelpers() {
  110.             return helper;
  111.         }
  112.         public FileConfiguration getMuted() {
  113.             return mute;
  114.         }
  115.         public FileConfiguration getBanned() {
  116.             return ban;
  117.         }
  118.        
  119.         public void saveAdmins() {
  120.                 try {
  121.                         admin.save(admins);
  122.                 }
  123.                 catch (IOException e) {
  124.                         Bukkit.getServer().getLogger().severe("Could not save admins.yml!");
  125.                 }
  126.         }
  127.         public void saveMods() {
  128.             try {
  129.                 mod.save(mods);
  130.             }
  131.             catch (IOException e) {
  132.                 Bukkit.getServer().getLogger().severe("Could not save mods.yml!");
  133.             }
  134.         }
  135.         public void saveHelpers() {
  136.             try {
  137.                 helper.save(helpers);
  138.             }
  139.             catch (IOException e) {
  140.                 Bukkit.getServer().getLogger().severe("Could not save helpers.yml!");
  141.             }
  142.         }
  143.         public void saveMuted() {
  144.             try {
  145.                 mute.save(muted);
  146.             }
  147.             catch(IOException e) {
  148.                 e.printStackTrace();
  149.             }
  150.         }
  151.         public void saveBanned() {
  152.             try {
  153.                 ban.save(banned);
  154.             }
  155.             catch(IOException e) {
  156.                 e.printStackTrace();
  157.             }
  158.         }
  159.        
  160.         public void reloadAdmins() {
  161.                 admin = YamlConfiguration.loadConfiguration(admins);
  162.         }
  163.         public void reloadMods() {
  164.             mod = YamlConfiguration.loadConfiguration(mods);
  165.         }
  166.         public void reloadHelpers() {
  167.             helper = YamlConfiguration.loadConfiguration(helpers);
  168.         }
  169.         public void reloadMuted() {
  170.             mute = YamlConfiguration.loadConfiguration(muted);
  171.         }
  172.         public void reloadBanned() {
  173.             ban = YamlConfiguration.loadConfiguration(banned);
  174.         }
  175.        
  176.         public FileConfiguration getConfig() {
  177.                 return config;
  178.         }
  179.        
  180.         public void saveConfig() {
  181.                 try {
  182.                         config.save(cfile);
  183.                 }
  184.                 catch (IOException e) {
  185.                         Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
  186.                 }
  187.         }
  188.        
  189.         public void reloadConfig() {
  190.                 config = YamlConfiguration.loadConfiguration(cfile);
  191.         }
  192.        
  193.         public PluginDescriptionFile getDesc() {
  194.                 return p.getDescription();
  195.         }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment