Advertisement
broken-arrow

Untitled

Dec 27th, 2021
1,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. public class AllYamlFilesInFolder {
  2.  
  3.     private final String folderName;
  4.     private final boolean shallGenerateFiles;
  5.  
  6.     public AllYamlFilesInFolder(String folderName, boolean shallGenerateFiles) {
  7.         this.folderName = folderName;
  8.         this.shallGenerateFiles = shallGenerateFiles;
  9.     }
  10.  
  11.     public File[] reload() {
  12.         Map<String, File> map = new HashMap<>();
  13.         List<String> filenamesFromDir = null;
  14.         try {
  15.             filenamesFromDir = getFilenamesForDirnameFromCP(this.folderName);
  16.         } catch (URISyntaxException | IOException e) {
  17.             e.printStackTrace();
  18.         }
  19.         File[] files = getYamlFiles(this.folderName, "yml");
  20.  
  21.         if (shallGenerateFiles) {
  22.             int conter = 0;
  23.             for (File file : files) {
  24.                 map.put(file.getName().replace(".yml", ""), file);
  25.             }
  26.  
  27.  
  28.             if (filenamesFromDir != null && (!map.isEmpty() || files.length == 0)) {
  29.                 for (String file : filenamesFromDir) {
  30.  
  31.                     if (map.get(getFileName(file)) == null) {
  32.                         CustomContainersMainClass.getInstance().saveResource(file, false);
  33.                         conter++;
  34.                     }
  35.  
  36.                     if (conter + 1 > filenamesFromDir.size())
  37.                         map.clear();
  38.                 }
  39.             }
  40.         }
  41.         return files;
  42.     }
  43.  
  44.     public String getFolders() {
  45.         return "Chests_and_HoppersSettings";
  46.     }
  47.  
  48.     public List<String> getFolder() {
  49.         List<String> filenamesFromDir = null;
  50.         try {
  51.             filenamesFromDir = getFilenamesForDirnameFromCP(this.folderName);
  52.         } catch (URISyntaxException | IOException e) {
  53.             e.printStackTrace();
  54.         }
  55.         assert filenamesFromDir != null;
  56.  
  57.         return new ArrayList<>(filenamesFromDir);
  58.     }
  59.  
  60.  
  61.     public File[] getYamlFiles(String directory, String extension) {
  62.         if (extension.startsWith("."))
  63.             extension = extension.substring(1);
  64.  
  65.         final File dataFolder = new File(CustomContainersMainClass.getInstance().getDataFolder(), directory);
  66.  
  67.         if (!dataFolder.exists())
  68.             dataFolder.mkdirs();
  69.  
  70.         final String finalExtension = extension;
  71.  
  72.         return dataFolder.listFiles((FileFilter) file -> !file.isDirectory() && file.getName().endsWith("." + finalExtension));
  73.     }
  74.  
  75.     public String getFileName(String path) {
  76.         Valid.checkBoolean(path != null && !path.isEmpty(), "The given path must not be empty!");
  77.         int pos;
  78.  
  79.         if (path.lastIndexOf("/") == -1)
  80.             pos = path.lastIndexOf("\\");
  81.         else
  82.             pos = path.lastIndexOf("/");
  83.  
  84.         if (pos > 0)
  85.             path = path.substring(pos + 1, path.length());
  86.  
  87.         pos = path.lastIndexOf(".");
  88.  
  89.         if (pos > 0)
  90.             path = path.substring(0, pos);
  91.         return path;
  92.     }
  93.  
  94.     public static List<String> getFilenamesForDirnameFromCP(String directoryName) throws
  95.             URISyntaxException, IOException {
  96.         List<String> filenames = new ArrayList<>();
  97.  
  98.         URL url = CustomContainersMainClass.class.getClassLoader().getResource(directoryName);
  99.  
  100.         if (url != null) {
  101.             if (url.getProtocol().equals("file")) {
  102.                 File file = Paths.get(url.toURI()).toFile();
  103.                 if (file != null) {
  104.                     File[] files = file.listFiles();
  105.                     if (files != null) {
  106.                         for (File filename : files) {
  107.                             filenames.add(filename.toString());
  108.                         }
  109.                     }
  110.                 }
  111.             } else if (url.getProtocol().equals("jar")) {
  112.                 String dirname = directoryName + "/";
  113.                 String path = url.getPath();
  114.                 String jarPath = path.substring(5, path.indexOf("!"));
  115.                 try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
  116.                     Enumeration<JarEntry> entries = jar.entries();
  117.                     while (entries.hasMoreElements()) {
  118.                         JarEntry entry = entries.nextElement();
  119.                         String name = entry.getName();
  120.                         //System.out.println("name " + name + "entry " + entry + jarPath);
  121.                         if (name.startsWith(dirname) && !dirname.equals(name)) {
  122.                             URL resource = CustomContainersMainClass.class.getClassLoader().getResource(name);
  123.                             if (resource != null) {
  124.                                 filenames.add(name);
  125.                             } else
  126.                                 Common.log("Missing files in plugins/" + CustomContainersMainClass.getInstance() + ".jar/" + directoryName + "/, contact the author of " + CustomContainersMainClass.getInstance().getName() + ".");
  127.                         }
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         return filenames;
  133.     }
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement