Skip to content

Commit f67aae9

Browse files
authored
feat: better loadItemSync (#134)
1 parent a91db12 commit f67aae9

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

main/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ dependencies {
2929
implementation(libs.base64)
3030
implementation(libs.json)
3131

32+
implementation(libs.intellij.annotations)
33+
3234
testImplementation(libs.groovy)
3335
testImplementation(libs.spock.core)
3436
testImplementation(libs.logback.classic)

main/src/main/java/com/sedmelluq/discord/lavaplayer/player/AudioPlayerManager.java

+25
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
44
import com.sedmelluq.discord.lavaplayer.tools.io.MessageInput;
55
import com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput;
6+
import com.sedmelluq.discord.lavaplayer.track.AudioItem;
67
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
78
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
89
import com.sedmelluq.discord.lavaplayer.track.DecodedTrackHolder;
910
import org.apache.http.client.config.RequestConfig;
1011
import org.apache.http.impl.client.HttpClientBuilder;
12+
import org.jetbrains.annotations.Nullable;
1113

1214
import java.io.IOException;
1315
import java.util.List;
@@ -112,6 +114,29 @@ default void loadItemSync(final String identifier, final AudioLoadResultHandler
112114
*/
113115
void loadItemSync(final AudioReference reference, final AudioLoadResultHandler resultHandler);
114116

117+
/**
118+
* Loads a track or playlist with the specified identifier and returns it.
119+
*
120+
* @param reference The audio reference that holds the identifier that a specific source manager
121+
* should be able to find the track with.
122+
* @return The loaded {@link AudioItem}, or `null` if nothing was found.
123+
* @see #loadItemSync(AudioReference)
124+
*/
125+
@Nullable
126+
default AudioItem loadItemSync(final String reference) {
127+
return loadItemSync(new AudioReference(reference, null));
128+
}
129+
130+
/**
131+
* Loads a track or playlist with the specified identifier and returns it.
132+
*
133+
* @param reference The audio reference that holds the identifier that a specific source manager
134+
* should be able to find the track with.
135+
* @return The loaded {@link AudioItem}, or `null` if nothing was found.
136+
*/
137+
@Nullable
138+
AudioItem loadItemSync(final AudioReference reference);
139+
115140
/**
116141
* Schedules loading a track or playlist with the specified identifier with an ordering key so that items with the
117142
* same ordering key are handled sequentially in the order of calls to this method.

main/src/main/java/com/sedmelluq/discord/lavaplayer/player/DefaultAudioPlayerManager.java

+44-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.sedmelluq.lava.common.tools.ExecutorTools;
1414
import org.apache.http.client.config.RequestConfig;
1515
import org.apache.http.impl.client.HttpClientBuilder;
16+
import org.jetbrains.annotations.Nullable;
1617
import org.slf4j.Logger;
1718
import org.slf4j.LoggerFactory;
1819

@@ -148,15 +149,23 @@ public List<AudioSourceManager> getSourceManagers() {
148149
return Collections.unmodifiableList(sourceManagers);
149150
}
150151

152+
@Override
153+
public @Nullable AudioItem loadItemSync(AudioReference reference) {
154+
AudioItem item = checkSourcesForItem(reference);
155+
if (item == null) {
156+
log.debug("No matches for track with identifier {}.", reference.identifier);
157+
}
158+
159+
return item;
160+
}
161+
151162
@Override
152163
public void loadItemSync(final AudioReference reference, final AudioLoadResultHandler resultHandler) {
153164
boolean[] reported = new boolean[1];
154165

155166
try {
156-
if (!checkSourcesForItem(reference, resultHandler, reported)) {
157-
log.debug("No matches for track with identifier {}.", reference.identifier);
158-
resultHandler.noMatches();
159-
}
167+
AudioItem item = loadItemSync(reference);
168+
submitItemToResultHandler(item, resultHandler, reported);
160169
} catch (Throwable throwable) {
161170
if (reported[0]) {
162171
log.warn("Load result handler for {} threw an exception", reference.identifier, throwable);
@@ -387,40 +396,57 @@ public void setItemLoaderThreadPoolSize(int poolSize) {
387396
trackInfoExecutorService.setMaximumPoolSize(poolSize);
388397
}
389398

390-
private boolean checkSourcesForItem(AudioReference reference, AudioLoadResultHandler resultHandler, boolean[] reported) {
399+
private void submitItemToResultHandler(AudioItem item, AudioLoadResultHandler handler, boolean[] reported) {
400+
if (item == null) {
401+
reported[0] = true;
402+
handler.noMatches();
403+
} else if (item instanceof AudioTrack) {
404+
reported[0] = true;
405+
handler.trackLoaded((AudioTrack) item);
406+
} else if (item instanceof AudioPlaylist) {
407+
reported[0] = true;
408+
handler.playlistLoaded((AudioPlaylist) item);
409+
} else {
410+
log.warn("Cannot submit unknown item to result handler: {}", item);
411+
}
412+
}
413+
414+
/**
415+
* Attempts to load the provided {@link AudioReference} using the sources registered with this {@link AudioPlayerManager}.
416+
* Unlike {@link #checkSourcesForItemOnce} this method attempts to follow any returned redirects.
417+
*/
418+
@Nullable
419+
private AudioItem checkSourcesForItem(AudioReference reference) {
391420
AudioReference currentReference = reference;
392421

393422
for (int redirects = 0; redirects < MAXIMUM_LOAD_REDIRECTS && currentReference.identifier != null; redirects++) {
394-
AudioItem item = checkSourcesForItemOnce(currentReference, resultHandler, reported);
395-
if (item == null) {
396-
return false;
397-
} else if (!(item instanceof AudioReference)) {
398-
return true;
423+
AudioItem item = checkSourcesForItemOnce(currentReference);
424+
if (item instanceof AudioReference) {
425+
currentReference = (AudioReference) item;
426+
continue;
399427
}
400-
currentReference = (AudioReference) item;
428+
429+
return item;
401430
}
402431

403-
return false;
432+
return null;
404433
}
405434

406-
private AudioItem checkSourcesForItemOnce(AudioReference reference, AudioLoadResultHandler resultHandler, boolean[] reported) {
435+
@Nullable
436+
private AudioItem checkSourcesForItemOnce(AudioReference reference) {
407437
for (AudioSourceManager sourceManager : sourceManagers) {
408438
if (reference.containerDescriptor != null && !(sourceManager instanceof ProbingAudioSourceManager)) {
409439
continue;
410440
}
411441

412442
AudioItem item = sourceManager.loadItem(this, reference);
413-
414443
if (item != null) {
415444
if (item instanceof AudioTrack) {
416445
log.debug("Loaded a track with identifier {} using {}.", reference.identifier, sourceManager.getClass().getSimpleName());
417-
reported[0] = true;
418-
resultHandler.trackLoaded((AudioTrack) item);
419446
} else if (item instanceof AudioPlaylist) {
420447
log.debug("Loaded a playlist with identifier {} using {}.", reference.identifier, sourceManager.getClass().getSimpleName());
421-
reported[0] = true;
422-
resultHandler.playlistLoaded((AudioPlaylist) item);
423448
}
449+
424450
return item;
425451
}
426452
}

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fun VersionCatalogBuilder.plugins() {
3636
fun VersionCatalogBuilder.common() {
3737
library("slf4j", "org.slf4j", "slf4j-api").version("2.0.7")
3838
library("commons-io", "commons-io", "commons-io").version("2.13.0")
39+
library("intellij-annotations", "org.jetbrains", "annotations").version("24.0.0")
3940

4041
version("jackson", "2.15.2")
4142
library("jackson-core", "com.fasterxml.jackson.core", "jackson-core").versionRef("jackson")

0 commit comments

Comments
 (0)