Advertisement
Guest User

configManager.class

a guest
Apr 26th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. public class cManager
  2. {
  3. private JavaPlugin plugin;
  4.  
  5. public cManager(JavaPlugin plugin)
  6. {
  7. this.plugin = plugin;
  8. }
  9.  
  10. public hConfig getNewConfig(String fileName, String[] header)
  11. {
  12. File file = getConfigFile(fileName);
  13. if (!file.exists())
  14. {
  15. prepareFile(fileName);
  16. if ((header != null) && (header.length != 0)) {
  17. setHeader(file, header);
  18. }
  19. }
  20. hConfig config = new hConfig(getConfigContent(fileName), file, getCommentsNum(file), this.plugin);
  21. return config;
  22. }
  23.  
  24. public hConfig getNewConfig(String fileName)
  25. {
  26. return getNewConfig(fileName, null);
  27. }
  28.  
  29. private File getConfigFile(String file)
  30. {
  31. if ((file.isEmpty()) || (file == null)) {
  32. return null;
  33. }
  34. File configFile;
  35. File configFile;
  36. if (file.contains("/"))
  37. {
  38. File configFile;
  39. if (file.startsWith("/")) {
  40. configFile = new File(this.plugin.getDataFolder() + file.replace("/", File.separator));
  41. } else {
  42. configFile = new File(this.plugin.getDataFolder() + File.separator + file.replace("/", File.separator));
  43. }
  44. }
  45. else
  46. {
  47. configFile = new File(this.plugin.getDataFolder(), file);
  48. }
  49. return configFile;
  50. }
  51.  
  52. public void prepareFile(String filePath, String resource)
  53. {
  54. File file = getConfigFile(filePath);
  55. if (file.exists()) {
  56. return;
  57. }
  58. try
  59. {
  60. file.getParentFile().mkdirs();
  61. file.createNewFile();
  62. if ((resource != null) &&
  63. (!resource.isEmpty())) {
  64. copyResource(this.plugin.getResource(resource), file);
  65. }
  66. }
  67. catch (IOException e)
  68. {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. public void prepareFile(String filePath)
  74. {
  75. prepareFile(filePath, null);
  76. }
  77.  
  78. public void setHeader(File file, String[] header)
  79. {
  80. if (!file.exists()) {
  81. return;
  82. }
  83. try
  84. {
  85. StringBuilder config = new StringBuilder("");
  86. BufferedReader reader = new BufferedReader(new FileReader(file));
  87. String currentLine;
  88. while ((currentLine = reader.readLine()) != null)
  89. {
  90. String currentLine;
  91. config.append(currentLine + "\n");
  92. }
  93. reader.close();
  94. config.append("# +----------------------------------------------------+ #\n");
  95. String[] arrayOfString;
  96. int j = (arrayOfString = header).length;
  97. for (int i = 0; i < j; i++)
  98. {
  99. String line = arrayOfString[i];
  100. if (line.length() <= 50)
  101. {
  102. int lenght = (50 - line.length()) / 2;
  103. StringBuilder finalLine = new StringBuilder(line);
  104. for (int i = 0; i < lenght; i++)
  105. {
  106. finalLine.append(" ");
  107. finalLine.reverse();
  108. finalLine.append(" ");
  109. finalLine.reverse();
  110. }
  111. if (line.length() % 2 != 0) {
  112. finalLine.append(" ");
  113. }
  114. config.append("# < " + finalLine.toString() + " > #\n");
  115. }
  116. }
  117. config.append("# +----------------------------------------------------+ #");
  118.  
  119. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  120. writer.write(prepareConfigString(config.toString()));
  121. writer.flush();
  122. writer.close();
  123. }
  124. catch (IOException e)
  125. {
  126. e.printStackTrace();
  127. }
  128. }
  129.  
  130. public InputStream getConfigContent(File file)
  131. {
  132. if (!file.exists()) {
  133. return null;
  134. }
  135. try
  136. {
  137. int commentNum = 0;
  138.  
  139. String pluginName = getPluginName();
  140.  
  141. StringBuilder whole = new StringBuilder("");
  142. BufferedReader reader = new BufferedReader(new FileReader(file));
  143. String currentLine;
  144. while ((currentLine = reader.readLine()) != null)
  145. {
  146. String currentLine;
  147. if (currentLine.startsWith("#"))
  148. {
  149. String addLine = currentLine.replaceFirst("#", pluginName + "_COMMENT_" + commentNum + ":");
  150. whole.append(addLine + "\n");
  151. commentNum++;
  152. }
  153. else
  154. {
  155. whole.append(currentLine + "\n");
  156. }
  157. }
  158. String config = whole.toString();
  159. InputStream configStream = new ByteArrayInputStream(config.getBytes(Charset.forName("UTF-8")));
  160.  
  161. reader.close();
  162. return configStream;
  163. }
  164. catch (IOException e)
  165. {
  166. e.printStackTrace();
  167. }
  168. return null;
  169. }
  170.  
  171. private int getCommentsNum(File file)
  172. {
  173. if (!file.exists()) {
  174. return 0;
  175. }
  176. try
  177. {
  178. int comments = 0;
  179.  
  180. BufferedReader reader = new BufferedReader(new FileReader(file));
  181. String currentLine;
  182. while ((currentLine = reader.readLine()) != null)
  183. {
  184. String currentLine;
  185. if (currentLine.startsWith("#")) {
  186. comments++;
  187. }
  188. }
  189. reader.close();
  190. return comments;
  191. }
  192. catch (IOException e)
  193. {
  194. e.printStackTrace();
  195. }
  196. return 0;
  197. }
  198.  
  199. public InputStream getConfigContent(String filePath)
  200. {
  201. return getConfigContent(getConfigFile(filePath));
  202. }
  203.  
  204. private String prepareConfigString(String configString)
  205. {
  206. int lastLine = 0;
  207. int headerLine = 0;
  208.  
  209. String[] lines = configString.split("\n");
  210. StringBuilder config = new StringBuilder("");
  211. String[] arrayOfString1;
  212. int j = (arrayOfString1 = lines).length;
  213. for (int i = 0; i < j; i++)
  214. {
  215. String line = arrayOfString1[i];
  216. if (line.startsWith(getPluginName() + "_COMMENT"))
  217. {
  218. String comment = "#" + line.trim().substring(line.indexOf(":") + 1);
  219. if (comment.startsWith("# +-"))
  220. {
  221. if (headerLine == 0)
  222. {
  223. config.append(comment + "\n");
  224. lastLine = 0;
  225. headerLine = 1;
  226. }
  227. else if (headerLine == 1)
  228. {
  229. config.append(comment + "\n\n");
  230. lastLine = 0;
  231. headerLine = 0;
  232. }
  233. }
  234. else
  235. {
  236. String normalComment;
  237. String normalComment;
  238. if (comment.startsWith("# ' ")) {
  239. normalComment = comment.substring(0, comment.length() - 1).replaceFirst("# ' ", "# ");
  240. } else {
  241. normalComment = comment;
  242. }
  243. if (lastLine == 0) {
  244. config.append(normalComment + "\n");
  245. } else if (lastLine == 1) {
  246. config.append("\n" + normalComment + "\n");
  247. }
  248. lastLine = 0;
  249. }
  250. }
  251. else
  252. {
  253. config.append(line + "\n");
  254. lastLine = 1;
  255. }
  256. }
  257. return config.toString();
  258. }
  259.  
  260. public void saveConfig(String configString, File file)
  261. {
  262. String configuration = prepareConfigString(configString);
  263. try
  264. {
  265. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  266. writer.write(configuration);
  267. writer.flush();
  268. writer.close();
  269. }
  270. catch (IOException e)
  271. {
  272. e.printStackTrace();
  273. }
  274. }
  275.  
  276. public String getPluginName()
  277. {
  278. return this.plugin.getDescription().getName();
  279. }
  280.  
  281. private void copyResource(InputStream resource, File file)
  282. {
  283. try
  284. {
  285. OutputStream out = new FileOutputStream(file);
  286.  
  287. byte[] buf = new byte['?'];
  288. int length;
  289. while ((length = resource.read(buf)) > 0)
  290. {
  291. int length;
  292. out.write(buf, 0, length);
  293. }
  294. out.close();
  295. resource.close();
  296. }
  297. catch (Exception e)
  298. {
  299. e.printStackTrace();
  300. }
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement