Skip to content

Commit

Permalink
it's not gonna get better than this
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisguffens committed Dec 3, 2022
1 parent 89acd21 commit 790d9d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RemoteRepository> 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);
Expand All @@ -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<String> libraries) {
public void load(@NotNull List<String> libraries) {
if (libraries.isEmpty()) {
return;
}

logger.log(Level.INFO, "[{0}] Loading {1} libraries... please wait",
new Object[]{pluginName, libraries.size()});

List<Dependency> dependencies = new ArrayList<>();
for (String library : libraries) {
Artifact artifact = new DefaultArtifact(library);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
}
Expand All @@ -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"));
}
}

Expand Down

0 comments on commit 790d9d4

Please sign in to comment.