Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- paste
- 
- 
- Untitled
- BROKEN-ARROW
- DEC 27TH, 2021
- 589
- NEVER
- 3.99 KB
- raw download report
- public class AllYamlFilesInFolder {
- private final String folderName;
- private final boolean shallGenerateFiles;
- public AllYamlFilesInFolder(String folderName, boolean shallGenerateFiles) {
- this.folderName = folderName;
- this.shallGenerateFiles = shallGenerateFiles;
- }
- public File[] reload() {
- Map<String, File> map = new HashMap<>();
- List<String> filenamesFromDir = null;
- try {
- filenamesFromDir = getFilenamesForDirnameFromCP(this.folderName);
- } catch (URISyntaxException | IOException e) {
- e.printStackTrace();
- }
- File[] files = getYamlFiles(this.folderName, "yml");
- if (shallGenerateFiles) {
- int conter = 0;
- for (File file : files) {
- map.put(file.getName().replace(".yml", ""), file);
- }
- if (filenamesFromDir != null && (!map.isEmpty() || files.length == 0)) {
- for (String file : filenamesFromDir) {
- if (map.get(getFileName(file)) == null) {
- CustomContainersMainClass.getInstance().saveResource(file, false);
- conter++;
- }
- if (conter + 1 > filenamesFromDir.size())
- map.clear();
- }
- }
- }
- return files;
- }
- public String getFolders() {
- return "Chests_and_HoppersSettings";
- }
- public List<String> getFolder() {
- List<String> filenamesFromDir = null;
- try {
- filenamesFromDir = getFilenamesForDirnameFromCP(this.folderName);
- } catch (URISyntaxException | IOException e) {
- e.printStackTrace();
- }
- assert filenamesFromDir != null;
- return new ArrayList<>(filenamesFromDir);
- }
- public File[] getYamlFiles(String directory, String extension) {
- if (extension.startsWith("."))
- extension = extension.substring(1);
- final File dataFolder = new File(CustomContainersMainClass.getInstance().getDataFolder(), directory);
- if (!dataFolder.exists())
- dataFolder.mkdirs();
- final String finalExtension = extension;
- return dataFolder.listFiles((FileFilter) file -> !file.isDirectory() && file.getName().endsWith("." + finalExtension));
- }
- public String getFileName(String path) {
- Valid.checkBoolean(path != null && !path.isEmpty(), "The given path must not be empty!");
- int pos;
- if (path.lastIndexOf("/") == -1)
- pos = path.lastIndexOf("\\");
- else
- pos = path.lastIndexOf("/");
- if (pos > 0)
- path = path.substring(pos + 1, path.length());
- pos = path.lastIndexOf(".");
- if (pos > 0)
- path = path.substring(0, pos);
- return path;
- }
- public static List<String> getFilenamesForDirnameFromCP(String directoryName) throws
- URISyntaxException, IOException {
- List<String> filenames = new ArrayList<>();
- URL url = CustomContainersMainClass.class.getClassLoader().getResource(directoryName);
- if (url != null) {
- if (url.getProtocol().equals("file")) {
- File file = Paths.get(url.toURI()).toFile();
- if (file != null) {
- File[] files = file.listFiles();
- if (files != null) {
- for (File filename : files) {
- filenames.add(filename.toString());
- }
- }
- }
- } else if (url.getProtocol().equals("jar")) {
- String dirname = directoryName + "/";
- String path = url.getPath();
- String jarPath = path.substring(5, path.indexOf("!"));
- try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
- Enumeration<JarEntry> entries = jar.entries();
- while (entries.hasMoreElements()) {
- JarEntry entry = entries.nextElement();
- String name = entry.getName();
- //System.out.println("name " + name + "entry " + entry + jarPath);
- if (name.startsWith(dirname) && !dirname.equals(name)) {
- URL resource = CustomContainersMainClass.class.getClassLoader().getResource(name);
- if (resource != null) {
- filenames.add(name);
- } else
- Common.log("Missing files in plugins/" + CustomContainersMainClass.getInstance() + ".jar/" + directoryName + "/, contact the author of " + CustomContainersMainClass.getInstance().getName() + ".");
- }
- }
- }
- }
- }
- return filenames;
- }
- }
- RAW Paste Data
- create new paste / syntax languages / archive / faq / tools / night mode / api / scraping api / news / pro
- privacy statement / cookies policy / terms of serviceupdated / security disclosure / dmca / report abuse / contact
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement