Guest User

Config class

a guest
Dec 12th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package me.robifoxx.block;
  2.  
  3. import com.google.common.base.Charsets;
  4. import com.google.common.io.ByteStreams;
  5.  
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import java.nio.charset.Charset;
  13. import java.util.logging.Level;
  14.  
  15. import me.robifoxx.block.Main;
  16.  
  17. import org.bukkit.configuration.InvalidConfigurationException;
  18. import org.bukkit.configuration.file.FileConfiguration;
  19. import org.bukkit.configuration.file.YamlConfiguration;
  20. import org.bukkit.plugin.PluginAwareness.Flags;
  21.  
  22. public class Config {
  23. File f;
  24. FileConfiguration cfg;
  25. String path_;
  26. String fileName_;
  27.  
  28. public Config(String path, String fileName) {
  29. this.path_ = path;
  30. this.fileName_ = fileName;
  31. }
  32.  
  33. public void create() {
  34. this.f = new File(this.path_, this.fileName_);
  35. this.cfg = YamlConfiguration.loadConfiguration(this.f);
  36. }
  37.  
  38. public void setDefault(String filename) {
  39. InputStream defConfigStream = Main.getProvidingPlugin(Main.class)
  40. .getResource(filename);
  41. if (defConfigStream != null) {
  42. YamlConfiguration defConfig;
  43. if (this.isStrictlyUTF8()) {
  44. defConfig = YamlConfiguration
  45. .loadConfiguration(new InputStreamReader(
  46. defConfigStream, Charsets.UTF_8));
  47. } else {
  48. defConfig = new YamlConfiguration();
  49.  
  50. byte[] contents;
  51. try {
  52. contents = ByteStreams.toByteArray(defConfigStream);
  53. } catch (IOException arg7) {
  54. Main.getProvidingPlugin(Main.class)
  55. .getLogger()
  56. .log(Level.SEVERE,
  57. "Unexpected failure reading " + filename,
  58. arg7);
  59. return;
  60. }
  61.  
  62. String text = new String(contents, Charset.defaultCharset());
  63. if (!text.equals(new String(contents, Charsets.UTF_8))) {
  64. Main.getProvidingPlugin(Main.class)
  65. .getLogger()
  66. .warning(
  67. "Default system encoding may have misread "
  68. + filename + " from plugin jar");
  69. }
  70.  
  71. try {
  72. defConfig.loadFromString(text);
  73. } catch (InvalidConfigurationException arg6) {
  74. Main.getProvidingPlugin(Main.class)
  75. .getLogger()
  76. .log(Level.SEVERE,
  77. "Cannot load configuration from jar", arg6);
  78. }
  79. }
  80.  
  81. this.cfg.setDefaults(defConfig);
  82. }
  83. }
  84.  
  85. public InputStream getResource(String filename) {
  86. if (filename == null) {
  87. throw new IllegalArgumentException("Filename cannot be null");
  88. } else {
  89. try {
  90. ClassLoader classLoader = super.getClass().getClassLoader();
  91. URL url = classLoader.getResource(filename);
  92. if (url == null) {
  93. return null;
  94. } else {
  95. URLConnection connection = url.openConnection();
  96. connection.setUseCaches(false);
  97. return connection.getInputStream();
  98. }
  99. } catch (IOException arg4) {
  100. return null;
  101. }
  102. }
  103. }
  104.  
  105. @SuppressWarnings("deprecation")
  106. private boolean isStrictlyUTF8() {
  107. return Main.getProvidingPlugin(Main.class).getDescription()
  108. .getAwareness().contains(Flags.UTF8);
  109. }
  110.  
  111. public void saveConfig() {
  112. try {
  113. this.cfg.save(this.f);
  114. } catch (IOException arg1) {
  115. arg1.printStackTrace();
  116. }
  117.  
  118. }
  119.  
  120. public void reloadConfig() {
  121. this.cfg = YamlConfiguration.loadConfiguration(this.f);
  122. InputStream defConfigStream = this.getResource(this.fileName_);
  123. if (defConfigStream != null) {
  124. this.cfg.setDefaults(YamlConfiguration
  125. .loadConfiguration(new InputStreamReader(defConfigStream,
  126. Charsets.UTF_8)));
  127. }
  128.  
  129. }
  130.  
  131. public FileConfiguration getConfig() {
  132. return this.cfg;
  133. }
  134.  
  135. public File toFile() {
  136. return this.f;
  137. }
  138.  
  139. public boolean exists() {
  140. return this.f.exists();
  141. }
  142. }
Add Comment
Please, Sign In to add comment