From 0e0fb925787b4542c23d6705ed06cc40c7474882 Mon Sep 17 00:00:00 2001 From: George V Date: Sun, 10 Apr 2022 14:31:13 +0300 Subject: [PATCH] fix(ExtensionLoader): Record constructor Removed jarFile from record constructor --- .../api/extensions/ExtensionLoader.java | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/georgev22/api/extensions/ExtensionLoader.java b/src/main/java/com/georgev22/api/extensions/ExtensionLoader.java index 8e9d0b2..48b964a 100644 --- a/src/main/java/com/georgev22/api/extensions/ExtensionLoader.java +++ b/src/main/java/com/georgev22/api/extensions/ExtensionLoader.java @@ -11,46 +11,49 @@ import java.util.jar.JarFile; import java.util.logging.Logger; -public record ExtensionLoader(File dataFolder, Logger logger, File jarFile) { +public record ExtensionLoader(File dataFolder, Logger logger) { public void load() throws Exception { - Utils.Assertions.notNull("File cannot be null", jarFile); + File[] jarFiles = getDataFolder().listFiles((dir, name) -> name.endsWith(".jar")); + if (jarFiles != null) { + for (File jarFile : jarFiles) { + Utils.Assertions.notNull("File cannot be null", jarFile); - JarFile jar = null; - InputStream stream = null; + JarFile jar = null; + InputStream stream = null; - try { - jar = new JarFile(jarFile); - JarEntry entry = jar.getJarEntry("extension.yml"); + try { + jar = new JarFile(jarFile); + JarEntry entry = jar.getJarEntry("extension.yml"); - if (entry == null) { - throw new FileNotFoundException("Jar does not contain extension.yml"); - } + if (entry == null) { + throw new FileNotFoundException("Jar does not contain extension.yml"); + } - stream = jar.getInputStream(entry); + stream = jar.getInputStream(entry); - } catch (IOException | YAMLException ex) { - ex.printStackTrace(); - } finally { - if (jar != null) { - try { - jar.close(); - } catch (IOException ignored) { + } catch (IOException | YAMLException ex) { + ex.printStackTrace(); + } finally { + if (jar != null) { + try { + jar.close(); + } catch (IOException ignored) { + } + } + if (stream != null) { + try { + stream.close(); + } catch (IOException ignored) { + } + } } - } - if (stream != null) { - try { - stream.close(); - } catch (IOException ignored) { + if (stream != null) { + ExtensionClassLoader extensionClassLoader = new ExtensionClassLoader(getClass().getClassLoader(), new ExtensionDescriptionFile(YamlConfiguration.loadConfiguration(new InputStreamReader(stream))), getDataFolder(), jarFile, logger); + extensionClassLoader.initialize(extensionClassLoader.extension); } } } - if (stream != null) { - FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(stream)); - ExtensionClassLoader extensionClassLoader = new ExtensionClassLoader(getClass().getClassLoader(), new ExtensionDescriptionFile(fileConfiguration), getDataFolder(), jarFile, logger); - extensionClassLoader.initialize(extensionClassLoader.extension); - } - } @NotNull