Advertisement
Yrgb5254

Untitled

Dec 27th, 2021
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. paste
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Untitled
  8.  
  9. BROKEN-ARROW
  10.  
  11. DEC 27TH, 2021
  12.  
  13. 589
  14.  
  15. NEVER
  16.  
  17. 3.99 KB
  18.  
  19. raw download report
  20.  
  21. public class AllYamlFilesInFolder {
  22.  
  23.  
  24.  
  25.     private final String folderName;
  26.  
  27.     private final boolean shallGenerateFiles;
  28.  
  29.  
  30.  
  31.     public AllYamlFilesInFolder(String folderName, boolean shallGenerateFiles) {
  32.  
  33.         this.folderName = folderName;
  34.  
  35.         this.shallGenerateFiles = shallGenerateFiles;
  36.  
  37.     }
  38.  
  39.  
  40.  
  41.     public File[] reload() {
  42.  
  43.         Map<String, File> map = new HashMap<>();
  44.  
  45.         List<String> filenamesFromDir = null;
  46.  
  47.         try {
  48.  
  49.             filenamesFromDir = getFilenamesForDirnameFromCP(this.folderName);
  50.  
  51.         } catch (URISyntaxException | IOException e) {
  52.  
  53.             e.printStackTrace();
  54.  
  55.         }
  56.  
  57.         File[] files = getYamlFiles(this.folderName, "yml");
  58.  
  59.  
  60.  
  61.         if (shallGenerateFiles) {
  62.  
  63.             int conter = 0;
  64.  
  65.             for (File file : files) {
  66.  
  67.                 map.put(file.getName().replace(".yml", ""), file);
  68.  
  69.             }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.             if (filenamesFromDir != null && (!map.isEmpty() || files.length == 0)) {
  76.  
  77.                 for (String file : filenamesFromDir) {
  78.  
  79.  
  80.  
  81.                     if (map.get(getFileName(file)) == null) {
  82.  
  83.                         CustomContainersMainClass.getInstance().saveResource(file, false);
  84.  
  85.                         conter++;
  86.  
  87.                     }
  88.  
  89.  
  90.  
  91.                     if (conter + 1 > filenamesFromDir.size())
  92.  
  93.                         map.clear();
  94.  
  95.                 }
  96.  
  97.             }
  98.  
  99.         }
  100.  
  101.         return files;
  102.  
  103.     }
  104.  
  105.  
  106.  
  107.     public String getFolders() {
  108.  
  109.         return "Chests_and_HoppersSettings";
  110.  
  111.     }
  112.  
  113.  
  114.  
  115.     public List<String> getFolder() {
  116.  
  117.         List<String> filenamesFromDir = null;
  118.  
  119.         try {
  120.  
  121.             filenamesFromDir = getFilenamesForDirnameFromCP(this.folderName);
  122.  
  123.         } catch (URISyntaxException | IOException e) {
  124.  
  125.             e.printStackTrace();
  126.  
  127.         }
  128.  
  129.         assert filenamesFromDir != null;
  130.  
  131.  
  132.  
  133.         return new ArrayList<>(filenamesFromDir);
  134.  
  135.     }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.     public File[] getYamlFiles(String directory, String extension) {
  142.  
  143.         if (extension.startsWith("."))
  144.  
  145.             extension = extension.substring(1);
  146.  
  147.  
  148.  
  149.         final File dataFolder = new File(CustomContainersMainClass.getInstance().getDataFolder(), directory);
  150.  
  151.  
  152.  
  153.         if (!dataFolder.exists())
  154.  
  155.             dataFolder.mkdirs();
  156.  
  157.  
  158.  
  159.         final String finalExtension = extension;
  160.  
  161.  
  162.  
  163.         return dataFolder.listFiles((FileFilter) file -> !file.isDirectory() && file.getName().endsWith("." + finalExtension));
  164.  
  165.     }
  166.  
  167.  
  168.  
  169.     public String getFileName(String path) {
  170.  
  171.         Valid.checkBoolean(path != null && !path.isEmpty(), "The given path must not be empty!");
  172.  
  173.         int pos;
  174.  
  175.  
  176.  
  177.         if (path.lastIndexOf("/") == -1)
  178.  
  179.             pos = path.lastIndexOf("\\");
  180.  
  181.         else
  182.  
  183.             pos = path.lastIndexOf("/");
  184.  
  185.  
  186.  
  187.         if (pos > 0)
  188.  
  189.             path = path.substring(pos + 1, path.length());
  190.  
  191.  
  192.  
  193.         pos = path.lastIndexOf(".");
  194.  
  195.  
  196.  
  197.         if (pos > 0)
  198.  
  199.             path = path.substring(0, pos);
  200.  
  201.         return path;
  202.  
  203.     }
  204.  
  205.  
  206.  
  207.     public static List<String> getFilenamesForDirnameFromCP(String directoryName) throws
  208.  
  209.             URISyntaxException, IOException {
  210.  
  211.         List<String> filenames = new ArrayList<>();
  212.  
  213.  
  214.  
  215.         URL url = CustomContainersMainClass.class.getClassLoader().getResource(directoryName);
  216.  
  217.  
  218.  
  219.         if (url != null) {
  220.  
  221.             if (url.getProtocol().equals("file")) {
  222.  
  223.                 File file = Paths.get(url.toURI()).toFile();
  224.  
  225.                 if (file != null) {
  226.  
  227.                     File[] files = file.listFiles();
  228.  
  229.                     if (files != null) {
  230.  
  231.                         for (File filename : files) {
  232.  
  233.                             filenames.add(filename.toString());
  234.  
  235.                         }
  236.  
  237.                     }
  238.  
  239.                 }
  240.  
  241.             } else if (url.getProtocol().equals("jar")) {
  242.  
  243.                 String dirname = directoryName + "/";
  244.  
  245.                 String path = url.getPath();
  246.  
  247.                 String jarPath = path.substring(5, path.indexOf("!"));
  248.  
  249.                 try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
  250.  
  251.                     Enumeration<JarEntry> entries = jar.entries();
  252.  
  253.                     while (entries.hasMoreElements()) {
  254.  
  255.                         JarEntry entry = entries.nextElement();
  256.  
  257.                         String name = entry.getName();
  258.  
  259.                         //System.out.println("name " + name + "entry " + entry + jarPath);
  260.  
  261.                         if (name.startsWith(dirname) && !dirname.equals(name)) {
  262.  
  263.                             URL resource = CustomContainersMainClass.class.getClassLoader().getResource(name);
  264.  
  265.                             if (resource != null) {
  266.  
  267.                                 filenames.add(name);
  268.  
  269.                             } else
  270.  
  271.                                 Common.log("Missing files in plugins/" + CustomContainersMainClass.getInstance() + ".jar/" + directoryName + "/, contact the author of " + CustomContainersMainClass.getInstance().getName() + ".");
  272.  
  273.                         }
  274.  
  275.                     }
  276.  
  277.                 }
  278.  
  279.             }
  280.  
  281.         }
  282.  
  283.         return filenames;
  284.  
  285.     }
  286.  
  287.  
  288.  
  289.  
  290.  
  291. }
  292.  
  293. RAW Paste Data
  294.  
  295. create new paste  /  syntax languages  /  archive  /  faq  /  tools  /  night mode  /  api  /  scraping api  /  news  /  pro
  296. privacy statement  /  cookies policy  /  terms of serviceupdated  /  security disclosure  /  dmca  /  report abuse  /  contact
  297.  
  298.  
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement