Skip to content

Commit cc25c01

Browse files
committed
Remove local player
1 parent da36944 commit cc25c01

File tree

21 files changed

+160
-204
lines changed

21 files changed

+160
-204
lines changed

bot/src/main/java/fredboat/audio/player/LavalinkManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package fredboat.audio.player;
2020

2121
import dev.arbjerg.lavalink.client.*;
22-
import dev.arbjerg.lavalink.client.protocol.Track;
2322
import me.duncte123.skybot.objects.config.DunctebotConfig;
2423
import me.duncte123.skybot.utils.AirUtils;
2524
import me.duncte123.skybot.utils.AudioUtils;

bot/src/main/java/me/duncte123/skybot/audio/GuildMusicManager.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
package me.duncte123.skybot.audio;
2020

21+
import dev.arbjerg.lavalink.client.LavalinkPlayer;
22+
import dev.arbjerg.lavalink.client.Link;
23+
import fredboat.audio.player.LavalinkManager;
2124
import me.duncte123.skybot.SkyBot;
2225
import me.duncte123.skybot.Variables;
2326
import me.duncte123.skybot.utils.GuildSettingsUtils;
@@ -29,15 +32,13 @@
2932
import java.util.function.Supplier;
3033

3134
public class GuildMusicManager {
32-
private final LocalPlayer player;
3335
private final long guildId;
3436
private final TrackScheduler scheduler;
3537
private final AtomicLong latestChannel = new AtomicLong(-1);
3638
private final Supplier<Boolean> announceTracksSupplier;
3739

3840
public GuildMusicManager(long guildId, Variables variables) {
3941
this.guildId = guildId;
40-
player = new LocalPlayer(guildId);
4142
this.scheduler = new TrackScheduler(this);
4243
this.announceTracksSupplier = () -> GuildSettingsUtils.getGuild(guildId, variables).isAnnounceTracks();
4344
}
@@ -60,7 +61,9 @@ public TrackScheduler getScheduler() {
6061
}
6162

6263
public void stopAndClear() {
63-
this.player.stopPlayback();
64+
this.getPlayer().ifPresent(
65+
(player) -> player.setPaused(false).setEncodedTrack(null).subscribe()
66+
);
6467

6568
this.scheduler.getQueue().clear();
6669
}
@@ -78,8 +81,14 @@ public Optional<MessageChannel> getLatestChannel() {
7881
);
7982
}
8083

81-
public LocalPlayer getPlayer() {
82-
return this.player;
84+
public Link getLink() {
85+
return LavalinkManager.INS.getLavalink().getLink(this.guildId);
86+
}
87+
88+
public Optional<LavalinkPlayer> getPlayer() {
89+
return Optional.ofNullable(
90+
this.getLink().getCachedPlayer()
91+
);
8392
}
8493

8594
public long getGuildId() {

bot/src/main/java/me/duncte123/skybot/audio/LocalPlayer.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

bot/src/main/java/me/duncte123/skybot/audio/TrackScheduler.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@ public void addToQueue(Track track, boolean isPatron) throws LimitReachedExcepti
7777
throw new LimitReachedException("The queue is full", MAX_QUEUE_SIZE);
7878
}
7979

80-
if (this.guildMusicManager.getPlayer().getCurrentTrack() == null) {
81-
this.play(track);
82-
} else {
83-
queue.offer(track);
84-
}
80+
this.guildMusicManager.getPlayer()
81+
.ifPresentOrElse((player) -> {
82+
if (player.getTrack() == null) {
83+
this.play(track);
84+
} else {
85+
queue.offer(track);
86+
}
87+
}, () -> this.play(track));
8588
}
8689

8790
/**
@@ -100,9 +103,12 @@ public int queuePlaylistTracks(List<Track> tracks, boolean isPatron) throws Limi
100103

101104
final int tracksAdded = tmpQueue.size();
102105

103-
if (this.guildMusicManager.getPlayer().getCurrentTrack() == null) {
104-
this.play(tmpQueue.removeFirst());
105-
}
106+
this.guildMusicManager.getPlayer()
107+
.ifPresentOrElse((player) -> {
108+
if (player.getTrack() == null) {
109+
this.play(tmpQueue.removeFirst());
110+
}
111+
}, () -> this.play(tmpQueue.removeFirst()));
106112

107113
queue.addAll(tmpQueue);
108114

@@ -129,7 +135,9 @@ public void skipTracks(int count, boolean forceAnnounce) {
129135
}
130136

131137
if (nextTrack == null) {
132-
this.guildMusicManager.getPlayer().stopPlayback();
138+
this.guildMusicManager.getPlayer().ifPresent((player) ->
139+
player.setPaused(false).setEncodedTrack(null).subscribe()
140+
);
133141

134142
guildMusicManager.getLatestChannel()
135143
.ifPresent((channel) -> sendMsg(
@@ -285,8 +293,8 @@ public void removeUserData(Track track) {
285293
}
286294

287295
private void play(Track track) {
288-
this.guildMusicManager.getPlayer()
289-
.update()
296+
this.guildMusicManager.getLink()
297+
.createOrUpdatePlayer()
290298
.setTrack(track)
291299
.subscribe();
292300
}

bot/src/main/java/me/duncte123/skybot/commands/music/SeekCommand.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
package me.duncte123.skybot.commands.music;
2020

21+
import dev.arbjerg.lavalink.client.LavalinkPlayer;
2122
import dev.arbjerg.lavalink.client.protocol.Track;
2223
import me.duncte123.skybot.Variables;
23-
import me.duncte123.skybot.audio.LocalPlayer;
2424
import me.duncte123.skybot.objects.command.CommandContext;
2525
import me.duncte123.skybot.objects.command.MusicCommand;
2626
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -61,13 +61,19 @@ public void run(@NotNull CommandContext ctx) {
6161
try {
6262
this.run0(ctx);
6363
} catch (NumberFormatException e) {
64-
sendMsg(ctx, "Your input \"" + ctx.getArgs().get(0) + "\" is not a valid number.");
64+
sendMsg(ctx, "Your input \"" + ctx.getArgs().getFirst() + "\" is not a valid number.");
6565
}
6666
}
6767

6868
public void run0(@Nonnull CommandContext ctx) throws NumberFormatException {
69-
final var player = ctx.getAudioUtils().getMusicManager(ctx.getGuildId()).getPlayer();
70-
final Track currentTrack = player.getCurrentTrack();
69+
final var optionalPlayer = ctx.getAudioUtils().getMusicManager(ctx.getGuildId()).getPlayer();
70+
71+
if (optionalPlayer.isEmpty()) {
72+
return;
73+
}
74+
75+
final var player = optionalPlayer.get();
76+
final Track currentTrack = player.getTrack();
7177

7278
if (currentTrack == null) {
7379
sendMsg(ctx, "The player is currently not playing anything");
@@ -80,7 +86,7 @@ public void run0(@Nonnull CommandContext ctx) throws NumberFormatException {
8086
}
8187

8288
final List<String> args = ctx.getArgs();
83-
final String arg0 = args.get(0);
89+
final String arg0 = args.getFirst();
8490
final String seekTime = arg0.replaceFirst("-", "");
8591
final Matcher matcher = TIME_REGEX.matcher(seekTime);
8692

@@ -95,7 +101,7 @@ public void run0(@Nonnull CommandContext ctx) throws NumberFormatException {
95101
}
96102

97103
// To hopefully prevent race conditions
98-
final Supplier<Long> trackDuration = () -> player.getCurrentTrack().getInfo().getLength();
104+
final Supplier<Long> trackDuration = () -> player.getTrack().getInfo().getLength();
99105

100106
int seconds = Integer.parseInt(seekTime) * 1000;
101107

@@ -118,11 +124,11 @@ public void run0(@Nonnull CommandContext ctx) throws NumberFormatException {
118124
return;
119125
}
120126

121-
player.seekTo(newPosition);
122-
123-
if (newPosition < trackDuration.get()) {
124-
sendNowPlaying(ctx);
125-
}
127+
player.setPosition(newPosition).subscribe((p) -> {
128+
if (newPosition < trackDuration.get()) {
129+
sendNowPlaying(ctx);
130+
}
131+
});
126132

127133
}
128134

@@ -131,29 +137,28 @@ public void handleEvent(@NotNull SlashCommandInteractionEvent event, @NotNull Va
131137
event.reply("Slash command not supported yet, sorry. Please report this issue.").queue();
132138
}
133139

134-
private void handleOverSkip(@NotNull CommandContext ctx, LocalPlayer player, String arg0, Supplier<Long> trackDuration) {
140+
private void handleOverSkip(@NotNull CommandContext ctx, LavalinkPlayer player, String arg0, Supplier<Long> trackDuration) {
135141
if (arg0.charAt(0) == '-') {
136142
sendMsg(ctx, "You're trying to skip more than the length of the track into the negatives?");
137143
return;
138144
}
139145

140146
// No need to announce as we just skip to the end
141-
player.seekTo(trackDuration.get());
147+
player.setPosition(trackDuration.get()).subscribe();
142148
}
143149

144-
private void handleTimeSkip(@NotNull CommandContext ctx, LocalPlayer player, Matcher matcher) {
150+
private void handleTimeSkip(@NotNull CommandContext ctx, LavalinkPlayer player, Matcher matcher) {
145151
final long minutes = Long.parseLong(matcher.group(1)) * 60 * 1000;
146152
final long seconds = Long.parseLong(matcher.group(2)) * 1000;
147153

148154
final long finalTime = minutes + seconds;
149155

150-
player.seekTo(finalTime);
151-
sendNowPlaying(ctx);
156+
player.setPosition(finalTime).subscribe((p) -> sendNowPlaying(ctx));
152157
}
153158

154159
private void sendNowPlaying(CommandContext ctx) {
155160
try {
156-
// Race condition
161+
// TODO: is this still needed?
157162
Thread.sleep(700);
158163
}
159164
catch (InterruptedException ignored) {

bot/src/main/java/me/duncte123/skybot/utils/MusicEmbedUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MusicEmbedUtils {
3737
private MusicEmbedUtils() {}
3838

3939
public static void createPlayerString(GuildMusicManager mng, Consumer<String> callback) {
40-
mng.getPlayer().getLavalinkPlayer().ifPresentOrElse((player) -> {
40+
mng.getPlayer().ifPresentOrElse((player) -> {
4141
final TrackInfo trackInfo = player.getTrack().getInfo();
4242
final long position = player.getPosition();
4343
final long duration = trackInfo.getLength();

bot/src/main/kotlin/me/duncte123/skybot/commands/music/BassBoostCommand.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,20 @@ class BassBoostCommand : MusicCommand() {
8080

8181
private fun setLavalinkEQ(gain: Float, variable: Variables, guildId: Long) {
8282
val player = variable.audioUtils.getMusicManager(guildId).player
83-
val bands = mutableListOf<Band>()
8483

85-
for (i in 0..2) {
86-
bands.add(Band(i, gain))
87-
}
84+
player.ifPresent {
85+
val bands = mutableListOf<Band>()
8886

89-
player.setFilters(
90-
Filters(
91-
equalizer = bands.toOmissible()
92-
)
93-
)
87+
for (i in 0..2) {
88+
bands.add(Band(i, gain))
89+
}
90+
91+
it.setFilters(
92+
Filters(
93+
equalizer = bands.toOmissible()
94+
)
95+
).subscribe()
96+
}
9497
}
9598

9699
override fun getSubData(): SubcommandData {

0 commit comments

Comments
 (0)