From 790d9d4cb4fe193c1b4c6e432f4be565a3749186 Mon Sep 17 00:00:00 2001 From: Joris Guffens Date: Sat, 3 Dec 2022 19:13:05 +0100 Subject: [PATCH] it's not gonna get better than this --- ...ryLoader.java => PluginLibraryLoader.java} | 20 ++++++----- .../spigot/SpigotBrickLibLoader.java | 34 +++++++++++++------ 2 files changed, 35 insertions(+), 19 deletions(-) rename spigot/src/main/java/com/guflimc/brick/libloader/spigot/{SpigotBrickLibraryLoader.java => PluginLibraryLoader.java} (88%) diff --git a/spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibraryLoader.java b/spigot/src/main/java/com/guflimc/brick/libloader/spigot/PluginLibraryLoader.java similarity index 88% rename from spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibraryLoader.java rename to spigot/src/main/java/com/guflimc/brick/libloader/spigot/PluginLibraryLoader.java index 2fc6cc2..7caf2e4 100644 --- a/spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibraryLoader.java +++ b/spigot/src/main/java/com/guflimc/brick/libloader/spigot/PluginLibraryLoader.java @@ -26,22 +26,24 @@ import java.util.logging.Level; import java.util.logging.Logger; -public class SpigotBrickLibraryLoader { +public class PluginLibraryLoader { - private final Logger logger; - private final RepositorySystem repository; + public static PluginLibraryLoader INSTANCE; + + final RepositorySystem repository; private final DefaultRepositorySystemSession session; private final List repositories; - public SpigotBrickLibraryLoader(Logger logger) { - this.logger = logger; + public PluginLibraryLoader(Logger logger) { + INSTANCE = this; DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class); locator.addService(TransporterFactory.class, HttpTransporterFactory.class); this.repository = locator.getService(RepositorySystem.class); + this.session = MavenRepositorySystemUtils.newSession(); session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_FAIL); @@ -59,17 +61,17 @@ public void transferStarted(@NotNull TransferEvent event) { } public void addRepository(String id, String url) { + if ( repositories.stream().anyMatch(r -> r.getUrl().equalsIgnoreCase(url) || r.getId().equals(id)) ) { + return; + } repositories.add(new RemoteRepository.Builder(id, "default", url).build()); } - public void load(@NotNull String pluginName, @NotNull List libraries) { + public void load(@NotNull List libraries) { if (libraries.isEmpty()) { return; } - logger.log(Level.INFO, "[{0}] Loading {1} libraries... please wait", - new Object[]{pluginName, libraries.size()}); - List dependencies = new ArrayList<>(); for (String library : libraries) { Artifact artifact = new DefaultArtifact(library); diff --git a/spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibLoader.java b/spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibLoader.java index 44d75c6..5e36904 100644 --- a/spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibLoader.java +++ b/spigot/src/main/java/com/guflimc/brick/libloader/spigot/SpigotBrickLibLoader.java @@ -1,5 +1,6 @@ package com.guflimc.brick.libloader.spigot; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; @@ -8,20 +9,25 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.jar.JarFile; +import java.util.logging.Level; public class SpigotBrickLibLoader extends JavaPlugin { - private final SpigotBrickLibraryLoader loader; - public SpigotBrickLibLoader() { super(); - loader = new SpigotBrickLibraryLoader(getLogger()); + getLogger().info("Checking plugins for custom repositories."); File pluginDir = getDataFolder().getParentFile(); for (File file : pluginDir.listFiles()) { - if (file.isFile() && file.getName().endsWith(".jar")) { + if ( !file.isFile() || !file.getName().endsWith(".jar")) { + continue; + } + + try { loadLibraries(file); + } catch (Exception ex) { + getLogger().log(Level.WARNING, "Unable to load external dependencies for " + file.getName() + ".", ex); } } } @@ -36,14 +42,22 @@ private void loadLibraries(File file) { throw new RuntimeException(e); } - if (pluginyml.contains("repositories")) { - for (String key : pluginyml.getConfigurationSection("repositories").getKeys(false)) { - loader.addRepository(key, pluginyml.getString("repositories." + key)); - } + String name = pluginyml.getString("name"); + ConfigurationSection repos = pluginyml.getConfigurationSection("repositories"); + + if ( name == null || repos == null ) { + return; + } + + getLogger().info("Checking libraries for " + name + "."); + PluginLibraryLoader loader = new PluginLibraryLoader(getLogger()); + + for (String key : repos.getKeys(false)) { + loader.addRepository(key, pluginyml.getString("repositories." + key)); } - if (pluginyml.contains("libraries")) { - loader.load(pluginyml.getString("name"), pluginyml.getStringList("libraries")); + if ( pluginyml.contains("libraries") ) { + loader.load(pluginyml.getStringList("libraries")); } }