From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sat, 3 Feb 2024 18:45:53 -0500 Subject: [PATCH] Configurable LibraryLoader maven repos Add JVM flag `-DLeaf.library-download-repo=link` to choose library download repo link. e.g. `-DLeaf.library-download-repo=https://maven.aliyun.com/repository/public` diff --git a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java index 107705db2d82b7c191e5e625ec888e0bc3b03831..d7dc6ff0b372de04c956cae6fc27d2679b88e0f7 100644 --- a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java +++ b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java @@ -3,9 +3,11 @@ package io.papermc.paper.plugin.loader.library.impl; import io.papermc.paper.plugin.loader.library.ClassPathLibrary; import io.papermc.paper.plugin.loader.library.LibraryLoadingException; import io.papermc.paper.plugin.loader.library.LibraryStore; + import java.io.File; import java.util.ArrayList; import java.util.List; + import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; @@ -57,6 +59,13 @@ public class MavenLibraryResolver implements ClassPathLibrary { private final List repositories = new ArrayList<>(); private final List dependencies = new ArrayList<>(); + // Leaf start - Configurable LibraryLoader maven repos + @org.jspecify.annotations.Nullable + public static final RemoteRepository MAVEN_CENTRAL_MIRROR_REPO = getCentralMirrorRepo(); + private static final String[] MAVEN_CENTRAL_URLS = new String[]{ + }; + // Leaf end - Configurable LibraryLoader maven repos + /** * Creates a new maven library resolver instance. *

@@ -102,9 +111,24 @@ public class MavenLibraryResolver implements ClassPathLibrary { * repository. * * @param remoteRepository the configuration that defines the maven repository this library resolver should fetch - * dependencies from + * dependencies from */ public void addRepository(final RemoteRepository remoteRepository) { + // Leaf start - Configurable LibraryLoader maven repos + for (String url : MAVEN_CENTRAL_URLS) { + if (remoteRepository.getUrl().startsWith(url)) { + RemoteRepository mirrorRepo = MAVEN_CENTRAL_MIRROR_REPO; + if (mirrorRepo != null) { + this.repositories.add(mirrorRepo); + return; + } + LOGGER.warn( + "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.", + new RuntimeException("Plugin used Maven Central for library resolution") + ); + } + } + // Leaf end - Configurable LibraryLoader maven repos this.repositories.add(remoteRepository); } @@ -130,4 +154,15 @@ public class MavenLibraryResolver implements ClassPathLibrary { store.addLibrary(file.toPath()); } } + + // Leaf start - Configurable LibraryLoader maven repos + @org.jspecify.annotations.Nullable + private static RemoteRepository getCentralMirrorRepo() { + String mirrorAddr = System.getProperty("Leaf.library-download-repo"); + if (mirrorAddr != null) { + new RemoteRepository.Builder("central", "default", mirrorAddr).build(); + } + return null; + } + // Leaf end - Configurable LibraryLoader maven repos } diff --git a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java index cfe41c0a67c8d729b6bd23b0cfa32db3c9db9f74..05d3b793bf9cb320774f9f488a3b78bedab6a9d4 100644 --- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java +++ b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java @@ -47,6 +47,8 @@ public class LibraryLoader { public static java.util.function.BiFunction LIBRARY_LOADER_FACTORY; // Paper - rewrite reflection in libraries public static java.util.function.Function, List> REMAPPER; // Paper - remap libraries + if (io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.MAVEN_CENTRAL_MIRROR_REPO != null) return List.of(io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.MAVEN_CENTRAL_MIRROR_REPO); // Leaf - Configurable LibraryLoader maven repos + public LibraryLoader(@NotNull Logger logger) { this.logger = logger;