Advertisement
ZP4RKER

ConfigManager

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