piotrek5402

ConfigManager.java

May 1st, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Set;
  7.  
  8. import org.bukkit.configuration.InvalidConfigurationException;
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11.  
  12. public class ConfigManager {
  13.         private final JavaPlugin plugin;
  14.        
  15.         private File configFile;
  16.         private FileConfiguration config;
  17.      
  18.         public ConfigManager(JavaPlugin plugin, String fileName) {
  19.             if (plugin == null) throw new IllegalArgumentException("plugin cannot be null");
  20.             if (!plugin.isInitialized()) throw new IllegalArgumentException("plugin must be initiaized");
  21.            
  22.             this.plugin = plugin;
  23.             this.config = this.plugin.getConfig();
  24.             this.configFile = new File(plugin.getDataFolder(), fileName);
  25.             this.configFile.getParentFile().mkdir();
  26.         }
  27.        
  28.         public Object get(String path)
  29.         {
  30.             return get(path,"");
  31.         }
  32.        
  33.         public List<String> getList(String path)
  34.         {
  35.             return (List<String>) this.config.getList(path);
  36.         }
  37.        
  38.         public List<String> getList(String path, List<String> def)
  39.         {
  40.             return (List<String>) this.config.getList(path, def);
  41.         }
  42.        
  43.         public Object get(String path,Object def)
  44.         {
  45.             Object obj = this.config.get(path);
  46.             if(obj == null)
  47.             {
  48.                 set(path,def);
  49.                 if(def instanceof String)
  50.                 {
  51.                     return replaceColors((String)def);
  52.                 }
  53.                 else
  54.                 {
  55.                     return def;
  56.                 }
  57.             }
  58.             else
  59.             {
  60.                 if(def instanceof String)
  61.                 {
  62.                     return replaceColors((String)obj);
  63.                 }
  64.                 else
  65.                 {
  66.                     return obj;
  67.                 }
  68.             }
  69.         }
  70.        
  71.         public void set(String path,Object value)
  72.         {
  73.             this.config.set(path,value);
  74.         }
  75.        
  76.         public void save()
  77.         {
  78.             try {
  79.                 this.config.save(configFile);
  80.             } catch (IOException e) {
  81.                 e.printStackTrace();
  82.             }
  83.         }
  84.        
  85.         public void load()
  86.         {
  87.             if(configFile.exists())
  88.             {
  89.                 try {
  90.                     this.config.load(configFile);
  91.                 } catch (IOException | InvalidConfigurationException e) {
  92.                     e.printStackTrace();
  93.                 }
  94.             }
  95.            
  96.         }
  97.        
  98.         private String replaceColors (String message) {
  99.             return message.replaceAll("(?i)&([a-r0-9])", "\u00A7$1");
  100.         }
  101.        
  102. }
Advertisement
Add Comment
Please, Sign In to add comment