Advertisement
fefasdasd

ConfigManager

Jul 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package configs;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.nio.charset.StandardCharsets;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11. import org.bukkit.configuration.file.YamlConfiguration;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13.  
  14. import com.charly.telar.Main;
  15.  
  16. public enum Configs {
  17.  
  18. configuracao, localizacao;
  19.  
  20.  
  21. private RawConfig config;
  22.  
  23. Configs() {
  24. this.config = new RawConfig(Main.getPlugin(com.charly.telar.Main.class), this.name().toLowerCase() + ".yml");
  25. }
  26.  
  27. /**
  28. * @return the config na main
  29. */
  30.  
  31. public static void setup() {
  32. for (Configs cfg : Configs.values()) {
  33. cfg.getRawConfig().saveDefaultConfig();
  34. }
  35. }
  36.  
  37. public RawConfig getRawConfig() {
  38. return config;
  39. }
  40.  
  41. public FileConfiguration getConfig() {
  42. return config.getConfig();
  43. }
  44.  
  45. public void saveConfig() {
  46. this.config.saveConfig();
  47. }
  48.  
  49. public class RawConfig {
  50.  
  51. private JavaPlugin plugin;
  52. private String configName;
  53. private File configFile;
  54. private FileConfiguration config;
  55.  
  56. public RawConfig(JavaPlugin plugin, String fileName) {
  57. this.plugin = plugin;
  58. this.configName = fileName;
  59. File dataFolder = plugin.getDataFolder();
  60. this.configFile = new File(dataFolder.toString() + File.separatorChar + this.configName);
  61. }
  62.  
  63. public void reloadConfig() {
  64. try {
  65. this.config = YamlConfiguration
  66. .loadConfiguration(new InputStreamReader(new FileInputStream(this.configFile), StandardCharsets.UTF_8));
  67. } catch (FileNotFoundException e) {
  68. e.printStackTrace();
  69. }
  70. InputStream is = this.plugin.getResource(this.configName);
  71. if (is != null) {
  72. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(is);
  73. this.config.setDefaults(defConfig);
  74. }
  75. }
  76.  
  77. public FileConfiguration getConfig() {
  78. if (this.config == null) {
  79. reloadConfig();
  80. }
  81. return this.config;
  82. }
  83.  
  84. public void saveConfig() {
  85. if ((this.config == null) || (this.configFile == null)) {
  86. return;
  87. }
  88. try {
  89. getConfig().save(this.configFile);
  90. } catch (IOException ex) {
  91. ex.printStackTrace();
  92. }
  93. }
  94.  
  95. public void saveDefaultConfig() {
  96. if (!this.configFile.exists()) {
  97. this.plugin.saveResource(this.configName, false);
  98. }
  99. }
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement