Advertisement
Guest User

Untitled

a guest
May 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.Reader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.nio.file.DirectoryNotEmptyException;
  7. import java.nio.file.Files;
  8. import java.nio.file.NoSuchFileException;
  9. import java.nio.file.Path;
  10. import java.util.logging.Level;
  11.  
  12. import org.bukkit.configuration.file.FileConfiguration;
  13. import org.bukkit.configuration.file.YamlConfiguration;
  14.  
  15. import net.frosted.Skywars.Skywars;
  16.  
  17. public class Configuration {
  18.  
  19. private FileConfiguration customConfig = null;
  20. private File customConfigFile = null;
  21. private JavaPlugin plugin = null;
  22.  
  23. public Configuration(JavaPlugin plugin, String fileName) {
  24. this.plugin = plugin;
  25. this.name = name;
  26. }
  27.  
  28. public void reloadConfig() {
  29. if (customConfigFile == null) {
  30. customConfigFile = new File(Skywars.getInstance().getDataFolder(), name);
  31. }
  32. customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
  33.  
  34. // Look for defaults in the jar
  35. Reader defConfigStream;
  36. try {
  37. defConfigStream = new InputStreamReader(plugin.getResource(name), "UTF8");
  38.  
  39. if (defConfigStream != null) {
  40. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  41. customConfig.setDefaults(defConfig);
  42. }
  43. } catch (UnsupportedEncodingException e) {
  44. // TODO Auto-generated catch block
  45. e.printStackTrace();
  46. }
  47. }
  48.  
  49. public FileConfiguration getConfig() {
  50. if (customConfig == null) {
  51. reloadConfig();
  52. }
  53. return customConfig;
  54. }
  55.  
  56. public boolean deleteConfig() {
  57. customConfigFile = new File(plugin.getDataFolder(), name);
  58. Path path = customConfigFile.toPath();
  59. try {
  60. Files.delete(path);
  61. return true;
  62. } catch (NoSuchFileException x) {
  63. System.err.format("%s: no such" + " file or directory%n", path);
  64. return false;
  65. } catch (DirectoryNotEmptyException x) {
  66. System.err.format("%s not empty%n", path);
  67. return false;
  68. } catch (IOException x) {
  69. // File permission problems are caught here.
  70. System.err.println(x);
  71. return false;
  72. }
  73. }
  74.  
  75. public void saveConfig() {
  76. if (customConfig == null || customConfigFile == null) {
  77. return;
  78. }
  79. try {
  80. getConfig().save(customConfigFile);
  81. } catch (IOException ex) {
  82. plugin.getServer().getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
  83. }
  84. }
  85.  
  86. public void saveDefaultConfig() {
  87. if (customConfigFile == null) {
  88. customConfigFile = new File(plugin.getDataFolder(), name);
  89. }
  90. if (!customConfigFile.exists()) {
  91. plugin.saveResource(name, false);
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement