diff --git a/server/src/main/java/org/allaymc/server/extension/ExtensionManager.java b/server/src/main/java/org/allaymc/server/extension/ExtensionManager.java index 33bf9a25a..f458679cd 100644 --- a/server/src/main/java/org/allaymc/server/extension/ExtensionManager.java +++ b/server/src/main/java/org/allaymc/server/extension/ExtensionManager.java @@ -37,8 +37,16 @@ public void loadExtensions(String[] args) { } private void loadExtension(Path extensionPath, String[] args) { - log.info(I18n.get().tr(TrKeys.A_EXTENSION_LOADING, extensionPath)); + log.info(I18n.get().tr(TrKeys.A_EXTENSION_LOADING, extensionPath.getFileName())); + Allay.EXTRA_RESOURCE_CLASS_LOADER.addJar(extensionPath); + + // Try to load the main class of the extension if it exists var mainClass = findMainClass(extensionPath); + if (mainClass == null) { + // Main class can be null + return; + } + if (!Extension.class.isAssignableFrom(mainClass)) { throw new ExtensionException(I18n.get().tr(TrKeys.A_EXTENSION_MAINCLASS_TYPEINVALID, mainClass.getName())); } @@ -57,8 +65,11 @@ private void loadExtension(Path extensionPath, String[] args) { private Class findMainClass(Path extensionPath) { var jarFileSystem = FileSystems.newFileSystem(extensionPath); try { - var entrance = (String) JSONUtils.fromMap(Files.readString(jarFileSystem.getPath("extension.json"))).get("entrance"); - Allay.EXTRA_RESOURCE_CLASS_LOADER.addJar(extensionPath); + var extensionDescriptorPath = jarFileSystem.getPath("extension.json"); + if (!Files.exists(extensionDescriptorPath)) { + return null; + } + var entrance = (String) JSONUtils.fromMap(Files.readString(extensionDescriptorPath)).get("entrance"); return Allay.EXTRA_RESOURCE_CLASS_LOADER.loadClass(entrance); } catch (ClassNotFoundException e1) { throw new ExtensionException(I18n.get().tr(TrKeys.A_EXTENSION_ENTRANCE_MISSING, extensionPath)); diff --git a/server/src/main/java/org/allaymc/server/utils/DynamicURLClassLoader.java b/server/src/main/java/org/allaymc/server/utils/DynamicURLClassLoader.java index a105c000d..929ec6882 100644 --- a/server/src/main/java/org/allaymc/server/utils/DynamicURLClassLoader.java +++ b/server/src/main/java/org/allaymc/server/utils/DynamicURLClassLoader.java @@ -26,23 +26,12 @@ public DynamicURLClassLoader(URL[] urls, ClassLoader parent) { public void addURL(URL url) { super.addURL(url); } - - public void addJar(File jarFile) { - Preconditions.checkNotNull(jarFile); + public void addJar(Path jarPath) { + Preconditions.checkNotNull(jarPath); try { - addURL(jarFile.toURI().toURL()); + addURL(jarPath.toUri().toURL()); } catch (MalformedURLException e) { log.error(e.getCause().getMessage()); } } - - public void addJar(Path jarPath) { - Preconditions.checkNotNull(jarPath); - addJar(jarPath.toFile()); - } - - public void addJar(String jarPath) { - Preconditions.checkNotNull(jarPath); - addJar(new File(jarPath)); - } }