Advertisement
Mortem420

(ApplJuze) MyConfigManager

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