Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Set;
- import org.bukkit.configuration.InvalidConfigurationException;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- public class ConfigManager {
- private final JavaPlugin plugin;
- private File configFile;
- private FileConfiguration config;
- public ConfigManager(JavaPlugin plugin, String fileName) {
- if (plugin == null) throw new IllegalArgumentException("plugin cannot be null");
- if (!plugin.isInitialized()) throw new IllegalArgumentException("plugin must be initiaized");
- this.plugin = plugin;
- this.config = this.plugin.getConfig();
- this.configFile = new File(plugin.getDataFolder(), fileName);
- this.configFile.getParentFile().mkdir();
- }
- public Object get(String path)
- {
- return get(path,"");
- }
- public List<String> getList(String path)
- {
- return (List<String>) this.config.getList(path);
- }
- public List<String> getList(String path, List<String> def)
- {
- return (List<String>) this.config.getList(path, def);
- }
- public Object get(String path,Object def)
- {
- Object obj = this.config.get(path);
- if(obj == null)
- {
- set(path,def);
- if(def instanceof String)
- {
- return replaceColors((String)def);
- }
- else
- {
- return def;
- }
- }
- else
- {
- if(def instanceof String)
- {
- return replaceColors((String)obj);
- }
- else
- {
- return obj;
- }
- }
- }
- public void set(String path,Object value)
- {
- this.config.set(path,value);
- }
- public void save()
- {
- try {
- this.config.save(configFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void load()
- {
- if(configFile.exists())
- {
- try {
- this.config.load(configFile);
- } catch (IOException | InvalidConfigurationException e) {
- e.printStackTrace();
- }
- }
- }
- private String replaceColors (String message) {
- return message.replaceAll("(?i)&([a-r0-9])", "\u00A7$1");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment