Advertisement
Mortem420

configManager

Sep 26th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.50 KB | None | 0 0
  1. package me.Mortem420.PracticePlugin;
  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. {
  20.     private JavaPlugin plugin;
  21.  
  22.     /*
  23.      * Manage custom configurations and files
  24.      */
  25.     public configManager(JavaPlugin plugin)
  26.     {
  27.         this.plugin = plugin;
  28.     }
  29.  
  30.     /*
  31.      * Get new configuration with header
  32.      * @param filePath - Path to file
  33.      * @return - New myConfig
  34.      */
  35.     public myConfig getNewConfig(String filePath, String[] header)
  36.     {
  37.  
  38.         File file = this.getConfigFile(filePath);
  39.  
  40.         if(!file.exists()) {
  41.             this.prepareFile(filePath);
  42.  
  43.             if(header != null && header.length != 0)
  44.             {
  45.                 this.setHeader(file, header);
  46.             }
  47.  
  48.         }
  49.  
  50.         myConfig config = new myConfig(this.getConfigContent(filePath), file, this.getCommentsNum(file), plugin);
  51.         return config;
  52.  
  53.     }
  54.  
  55.     /*
  56.      * Get new configuration
  57.      * @param filePath - Path to file
  58.      * @return - New myConfig
  59.      */
  60.     public myConfig getNewConfig(String filePath)
  61.     {
  62.         return this.getNewConfig(filePath, null);
  63.     }
  64.  
  65.     /*
  66.      * Get configuration file from string
  67.      * @param file - File path
  68.      * @return - New file object
  69.      */
  70.     private File getConfigFile(String file)
  71.     {
  72.  
  73.         if(file.isEmpty() || file == null)
  74.         {
  75.             return null;
  76.         }
  77.  
  78.         File configFile;
  79.  
  80.         if(file.contains("/"))
  81.         {
  82.  
  83.             if(file.startsWith("/"))
  84.             {
  85.                 configFile = new File(plugin.getDataFolder() + file.replace("/", File.separator));
  86.             }
  87.             else
  88.             {
  89.                 configFile = new File(plugin.getDataFolder() + File.separator + file.replace("/", File.separator));
  90.             }
  91.  
  92.         }
  93.         else
  94.         {
  95.             configFile = new File(plugin.getDataFolder(), file);
  96.         }
  97.  
  98.         return configFile;
  99.  
  100.     }
  101.  
  102.     /*
  103.      * Create new file for config and copy resource into it
  104.      * @param file - Path to file
  105.      * @param resource - Resource to copy
  106.      */
  107.     public void prepareFile(String filePath, String resource)
  108.     {
  109.  
  110.         File file = this.getConfigFile(filePath);
  111.  
  112.         if(file.exists())
  113.         {
  114.             return;
  115.         }
  116.  
  117.         try {
  118.             file.getParentFile().mkdirs();
  119.             file.createNewFile();
  120.  
  121.             if(!resource.isEmpty() && resource != null)
  122.             {
  123.                 this.copyResource(plugin.getResource(resource), file);
  124.             }
  125.  
  126.         }
  127.         catch (IOException e)
  128.         {
  129.             e.printStackTrace();
  130.         }
  131.  
  132.     }
  133.  
  134.     /*
  135.      * Create new file for config without resource
  136.      * @param file - File to create
  137.      */
  138.     public void prepareFile(String filePath)
  139.     {
  140.         this.prepareFile(filePath, null);
  141.     }
  142.  
  143.     /*
  144.      * Adds header block to config
  145.      * @param file - Config file
  146.      * @param header - Header lines
  147.      */
  148.     public void setHeader(File file, String[] header)
  149.     {
  150.  
  151.         if(!file.exists())
  152.         {
  153.             return;
  154.         }
  155.  
  156.         try
  157.         {
  158.             String currentLine;
  159.             StringBuilder config = new StringBuilder("");
  160.             BufferedReader reader = new BufferedReader(new FileReader(file));
  161.  
  162.             while((currentLine = reader.readLine()) != null)
  163.             {
  164.                 config.append(currentLine + "\n");
  165.             }
  166.  
  167.             reader.close();
  168.             config.append("# +----------------------------------------------------+ #\n");
  169.  
  170.             for(String line : header)
  171.             {
  172.  
  173.                 if(line.length() > 50)
  174.                 {
  175.                     continue;
  176.                 }
  177.  
  178.                 int lenght = (50 - line.length()) / 2;
  179.                 StringBuilder finalLine = new StringBuilder(line);
  180.  
  181.                 for(int i = 0; i < lenght; i++)
  182.                 {
  183.                     finalLine.append(" ");
  184.                     finalLine.reverse();
  185.                     finalLine.append(" ");
  186.                     finalLine.reverse();
  187.                 }
  188.  
  189.                 if(line.length() % 2 != 0)
  190.                 {
  191.                     finalLine.append(" ");
  192.                 }
  193.  
  194.                 config.append("# < " + finalLine.toString() + " > #\n");
  195.  
  196.             }
  197.  
  198.             config.append("# +----------------------------------------------------+ #");
  199.  
  200.             BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  201.             writer.write(this.prepareConfigString(config.toString()));
  202.             writer.flush();
  203.             writer.close();
  204.  
  205.         } catch (IOException e)
  206.         {
  207.             e.printStackTrace();
  208.         }
  209.  
  210.     }
  211.  
  212.     /*
  213.      * Read file and make comments SnakeYAML friendly
  214.      * @param filePath - Path to file
  215.      * @return - File as Input Stream
  216.      */
  217.     public InputStream getConfigContent(File file)
  218.     {
  219.  
  220.         if(!file.exists())
  221.         {
  222.             return null;
  223.         }
  224.  
  225.         try {
  226.             int commentNum = 0;
  227.  
  228.             String addLine;
  229.             String currentLine;
  230.             String pluginName = this.getPluginName();
  231.  
  232.             StringBuilder whole = new StringBuilder("");
  233.             BufferedReader reader = new BufferedReader(new FileReader(file));
  234.  
  235.             while((currentLine = reader.readLine()) != null) {
  236.  
  237.                 if(currentLine.startsWith("#"))
  238.                 {
  239.                     addLine = currentLine.replaceFirst("#", pluginName + "_COMMENT_" + commentNum + ":");
  240.                     whole.append(addLine + "\n");
  241.                     commentNum++;
  242.  
  243.                 }
  244.                 else
  245.                 {
  246.                     whole.append(currentLine + "\n");
  247.                 }
  248.             }
  249.             String config = whole.toString();
  250.             InputStream configStream = new ByteArrayInputStream(config.getBytes(Charset.forName("UTF-8")));
  251.  
  252.             reader.close();
  253.             return configStream;
  254.  
  255.         } catch (IOException e)
  256.         {
  257.             e.printStackTrace();
  258.             return null;
  259.         }
  260.     }
  261.     /*
  262.      * Get comments from file
  263.      * @param file - File
  264.      * @return - Comments number
  265.      */
  266.     private int getCommentsNum(File file)
  267.     {
  268.         if(!file.exists())
  269.         {
  270.             return 0;
  271.         }
  272.         try
  273.         {
  274.             int comments = 0;
  275.             String currentLine;
  276.  
  277.             BufferedReader reader = new BufferedReader(new FileReader(file));
  278.  
  279.             while((currentLine = reader.readLine()) != null)
  280.             {
  281.                 if(currentLine.startsWith("#"))
  282.                 {
  283.                     comments++;
  284.                 }
  285.             }
  286.             reader.close();
  287.             return comments;
  288.  
  289.         } catch (IOException e)
  290.         {
  291.             e.printStackTrace();
  292.             return 0;
  293.         }
  294.     }
  295.     /*
  296.      * Get config content from file
  297.      * @param filePath - Path to file
  298.      * @return - readied file
  299.      */
  300.     public InputStream getConfigContent(String filePath)
  301.     {
  302.         return this.getConfigContent(this.getConfigFile(filePath));
  303.     }
  304.  
  305.     private String prepareConfigString(String configString)
  306.     {
  307.         int lastLine = 0;
  308.         int headerLine = 0;
  309.  
  310.         String[] lines = configString.split("\n");
  311.         StringBuilder config = new StringBuilder("");
  312.  
  313.         for(String line : lines) {
  314.  
  315.             if(line.startsWith(this.getPluginName() + "_COMMENT"))
  316.             {
  317.                 String comment = "#" + line.trim().substring(line.indexOf(":") + 1);
  318.  
  319.                 if(comment.startsWith("# +-"))
  320.                 {
  321.                     /*
  322.                      * If header line = 0 then it is
  323.                      * header start, if it's equal
  324.                      * to 1 it's the end of header
  325.                      */
  326.                     if(headerLine == 0)
  327.                     {
  328.                         config.append(comment + "\n");
  329.  
  330.                         lastLine = 0;
  331.                         headerLine = 1;
  332.                     }
  333.                     else if(headerLine == 1)
  334.                     {
  335.                         config.append(comment + "\n\n");
  336.  
  337.                         lastLine = 0;
  338.                         headerLine = 0;
  339.                     }
  340.                 }
  341.                 else
  342.                 {
  343.                     /*
  344.                      * Last line = 0 - Comment
  345.                      * Last line = 1 - Normal path
  346.                      */
  347.                     String normalComment;
  348.  
  349.                     if(comment.startsWith("# ' "))
  350.                     {
  351.                         normalComment = comment.substring(0, comment.length() - 1).replaceFirst("# ' ", "# ");
  352.                     }
  353.                     else
  354.                     {
  355.                         normalComment = comment;
  356.                     }
  357.  
  358.                     if(lastLine == 0)
  359.                     {
  360.                         config.append(normalComment + "\n");
  361.                     }
  362.                     else if(lastLine == 1)
  363.                     {
  364.                         config.append("\n" + normalComment + "\n");
  365.                     }
  366.                     lastLine = 0;
  367.                 }
  368.             }
  369.             else
  370.             {
  371.                 config.append(line + "\n");
  372.                 lastLine = 1;
  373.             }
  374.         }
  375.         return config.toString();
  376.  
  377.     }
  378.     /*
  379.      * Saves configuration to file
  380.      * @param configString - Config string
  381.      * @param file - Config file
  382.      */
  383.     public void saveConfig(String configString, File file)
  384.     {
  385.         String configuration = this.prepareConfigString(configString);
  386.  
  387.         try
  388.         {
  389.             BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  390.             writer.write(configuration);
  391.             writer.flush();
  392.             writer.close();
  393.  
  394.         }
  395.         catch (IOException e)
  396.         {
  397.             e.printStackTrace();
  398.         }
  399.  
  400.     }
  401.  
  402.     public String getPluginName()
  403.     {
  404.         return plugin.getDescription().getName();
  405.     }
  406.  
  407.     /*
  408.      * Copy resource from Input Stream to file
  409.      * @param resource - Resource from .jar
  410.      * @param file - File to write
  411.      */
  412.     private void copyResource(InputStream resource, File file)
  413.     {
  414.  
  415.         try {
  416.             OutputStream out = new FileOutputStream(file);
  417.  
  418.             int lenght;
  419.             byte[] buf = new byte[1024];
  420.  
  421.             while((lenght = resource.read(buf)) > 0)
  422.             {
  423.                 out.write(buf, 0, lenght);
  424.             }
  425.  
  426.             out.close();
  427.             resource.close();
  428.  
  429.         }
  430.         catch (Exception e)
  431.         {
  432.             e.printStackTrace();
  433.         }
  434.  
  435.     }
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement