From 25336b5457acea7c06dc73d957ee17dd6cb62bc2 Mon Sep 17 00:00:00 2001 From: viztea Date: Thu, 13 Jun 2024 03:04:01 -0700 Subject: [PATCH] feat: better loadItemSync --- main/build.gradle.kts | 2 + .../lavaplayer/player/AudioPlayerManager.java | 25 ++++++++ .../player/DefaultAudioPlayerManager.java | 62 +++++++++++++------ settings.gradle.kts | 1 + 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/main/build.gradle.kts b/main/build.gradle.kts index 4e1e0d6a..d70beb68 100644 --- a/main/build.gradle.kts +++ b/main/build.gradle.kts @@ -29,6 +29,8 @@ dependencies { implementation(libs.base64) implementation(libs.json) + implementation(libs.intellij.annotations) + testImplementation(libs.groovy) testImplementation(libs.spock.core) testImplementation(libs.logback.classic) diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/AudioPlayerManager.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/AudioPlayerManager.java index 046d9280..1526d08f 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/AudioPlayerManager.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/AudioPlayerManager.java @@ -3,11 +3,13 @@ import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager; import com.sedmelluq.discord.lavaplayer.tools.io.MessageInput; import com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput; +import com.sedmelluq.discord.lavaplayer.track.AudioItem; import com.sedmelluq.discord.lavaplayer.track.AudioReference; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.DecodedTrackHolder; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.HttpClientBuilder; +import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.util.List; @@ -112,6 +114,29 @@ default void loadItemSync(final String identifier, final AudioLoadResultHandler */ void loadItemSync(final AudioReference reference, final AudioLoadResultHandler resultHandler); + /** + * Loads a track or playlist with the specified identifier and returns it. + * + * @param reference The audio reference that holds the identifier that a specific source manager + * should be able to find the track with. + * @return The loaded {@link AudioItem}, or `null` if nothing was found. + * @see #loadItemSync(AudioReference) + */ + @Nullable + default AudioItem loadItemSync(final String reference) { + return loadItemSync(new AudioReference(reference, null)); + } + + /** + * Loads a track or playlist with the specified identifier and returns it. + * + * @param reference The audio reference that holds the identifier that a specific source manager + * should be able to find the track with. + * @return The loaded {@link AudioItem}, or `null` if nothing was found. + */ + @Nullable + AudioItem loadItemSync(final AudioReference reference); + /** * Schedules loading a track or playlist with the specified identifier with an ordering key so that items with the * same ordering key are handled sequentially in the order of calls to this method. diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/DefaultAudioPlayerManager.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/DefaultAudioPlayerManager.java index c173c37c..0332fbe7 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/DefaultAudioPlayerManager.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/player/DefaultAudioPlayerManager.java @@ -13,6 +13,7 @@ import com.sedmelluq.lava.common.tools.ExecutorTools; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.HttpClientBuilder; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -148,15 +149,23 @@ public List getSourceManagers() { return Collections.unmodifiableList(sourceManagers); } + @Override + public @Nullable AudioItem loadItemSync(AudioReference reference) { + AudioItem item = checkSourcesForItem(reference); + if (item == null) { + log.debug("No matches for track with identifier {}.", reference.identifier); + } + + return item; + } + @Override public void loadItemSync(final AudioReference reference, final AudioLoadResultHandler resultHandler) { boolean[] reported = new boolean[1]; try { - if (!checkSourcesForItem(reference, resultHandler, reported)) { - log.debug("No matches for track with identifier {}.", reference.identifier); - resultHandler.noMatches(); - } + AudioItem item = loadItemSync(reference); + submitItemToResultHandler(item, resultHandler, reported); } catch (Throwable throwable) { if (reported[0]) { log.warn("Load result handler for {} threw an exception", reference.identifier, throwable); @@ -387,40 +396,57 @@ public void setItemLoaderThreadPoolSize(int poolSize) { trackInfoExecutorService.setMaximumPoolSize(poolSize); } - private boolean checkSourcesForItem(AudioReference reference, AudioLoadResultHandler resultHandler, boolean[] reported) { + private void submitItemToResultHandler(AudioItem item, AudioLoadResultHandler handler, boolean[] reported) { + if (item == null) { + reported[0] = true; + handler.noMatches(); + } else if (item instanceof AudioTrack) { + reported[0] = true; + handler.trackLoaded((AudioTrack) item); + } else if (item instanceof AudioPlaylist) { + reported[0] = true; + handler.playlistLoaded((AudioPlaylist) item); + } else { + log.warn("Cannot submit unknown item to result handler: {}", item); + } + } + + /** + * Attempts to load the provided {@link AudioReference} using the sources registered with this {@link AudioPlayerManager}. + * Unlike {@link #checkSourcesForItemOnce} this method attempts to follow any returned redirects. + */ + @Nullable + private AudioItem checkSourcesForItem(AudioReference reference) { AudioReference currentReference = reference; for (int redirects = 0; redirects < MAXIMUM_LOAD_REDIRECTS && currentReference.identifier != null; redirects++) { - AudioItem item = checkSourcesForItemOnce(currentReference, resultHandler, reported); - if (item == null) { - return false; - } else if (!(item instanceof AudioReference)) { - return true; + AudioItem item = checkSourcesForItemOnce(currentReference); + if (item instanceof AudioReference) { + currentReference = (AudioReference) item; + continue; } - currentReference = (AudioReference) item; + + return item; } - return false; + return null; } - private AudioItem checkSourcesForItemOnce(AudioReference reference, AudioLoadResultHandler resultHandler, boolean[] reported) { + @Nullable + private AudioItem checkSourcesForItemOnce(AudioReference reference) { for (AudioSourceManager sourceManager : sourceManagers) { if (reference.containerDescriptor != null && !(sourceManager instanceof ProbingAudioSourceManager)) { continue; } AudioItem item = sourceManager.loadItem(this, reference); - if (item != null) { if (item instanceof AudioTrack) { log.debug("Loaded a track with identifier {} using {}.", reference.identifier, sourceManager.getClass().getSimpleName()); - reported[0] = true; - resultHandler.trackLoaded((AudioTrack) item); } else if (item instanceof AudioPlaylist) { log.debug("Loaded a playlist with identifier {} using {}.", reference.identifier, sourceManager.getClass().getSimpleName()); - reported[0] = true; - resultHandler.playlistLoaded((AudioPlaylist) item); } + return item; } } diff --git a/settings.gradle.kts b/settings.gradle.kts index d80c289a..1f5cff68 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -36,6 +36,7 @@ fun VersionCatalogBuilder.plugins() { fun VersionCatalogBuilder.common() { library("slf4j", "org.slf4j", "slf4j-api").version("2.0.7") library("commons-io", "commons-io", "commons-io").version("2.13.0") + library("intellij-annotations", "org.jetbrains", "annotations").version("24.0.0") version("jackson", "2.15.2") library("jackson-core", "com.fasterxml.jackson.core", "jackson-core").versionRef("jackson")