diff options
author | Brandon <bwaggone@umich.edu> | 2018-04-14 15:20:23 -0400 |
---|---|---|
committer | Brandon <bwaggone@umich.edu> | 2018-04-14 15:20:23 -0400 |
commit | 97dd68fbbfd28c6c0f42ac06f4d70606990667b4 (patch) | |
tree | a666be81bd6019970123e79bfc50b720490fa8f2 | |
parent | 3bc325f7d249cd25002f32ba8b516c7644d8c506 (diff) | |
download | SoundChan-97dd68fbbfd28c6c0f42ac06f4d70606990667b4.tar.gz SoundChan-97dd68fbbfd28c6c0f42ac06f4d70606990667b4.tar.bz2 SoundChan-97dd68fbbfd28c6c0f42ac06f4d70606990667b4.zip |
Add sound preemption + change structure
-rw-r--r-- | src/main/java/soundchan/BotListener/BotListener.java (renamed from src/main/java/soundchan/BotListener.java) | 99 | ||||
-rw-r--r-- | src/main/java/soundchan/BotListener/BotListenerHelpers.java (renamed from src/main/java/soundchan/BotListenerHelpers.java) | 2 | ||||
-rw-r--r-- | src/main/java/soundchan/BotListener/Commands.java (renamed from src/main/java/soundchan/Commands.java) | 2 | ||||
-rw-r--r-- | src/main/java/soundchan/LocalAudioManager.java | 3 | ||||
-rw-r--r-- | src/main/java/soundchan/Main.java | 1 | ||||
-rw-r--r-- | src/main/java/soundchan/TrackScheduler.java | 20 |
6 files changed, 80 insertions, 47 deletions
diff --git a/src/main/java/soundchan/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index 8e43ca9..f97db95 100644 --- a/src/main/java/soundchan/BotListener.java +++ b/src/main/java/soundchan/BotListener/BotListener.java @@ -1,4 +1,4 @@ -package soundchan; +package soundchan.BotListener; import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; @@ -15,6 +15,7 @@ import net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.hooks.ListenerAdapter; import net.dv8tion.jda.core.managers.AudioManager; +import soundchan.*; import java.util.HashMap; import java.util.List; @@ -94,7 +95,7 @@ public class BotListener extends ListenerAdapter{ if(command[0].startsWith("!") && command[0].length() > 1){ String filepath = localManager.GetFilePath(command[0].substring(1)); if(!filepath.contentEquals("")) { - loadAndPlay(channel, filepath); + loadAndPlay(channel, filepath, true); } else{ channel.sendMessage("File \"" + command[0].substring(1) + "\" not found!").queue(); @@ -102,44 +103,47 @@ public class BotListener extends ListenerAdapter{ } if(command[0].startsWith("~")){ - switch (Commands.valueOf(command[0].substring(1))){ - case play: { - if(command.length == 2) - loadAndPlay(channel, command[1]); - break; - } - case skip: { - skipTrack(channel); - break; - } - case volume: { - if(command.length == 2) - changeVolume(channel, command[1]); - break; - } - case list: { - if(command.length == 2){ - if(command[1].equals("queue")){ - listTracks(channel); - } - else if(command[1].equals("sounds")){ - localManager.ListSounds(channel); - } + Commands enumCommand = Commands.valueOf(command[0].substring(1)); + if(enumCommand == Commands.play){ + // Play a song or video + if(command.length == 2) + loadAndPlay(channel, command[1], false); + + }else if(enumCommand == Commands.skip){ + // Skip a song or video in the queue + skipTrack(channel); + + }else if(enumCommand == Commands.volume){ + // Change volume + if(command.length == 2) + changeVolume(channel, command[1]); + + }else if(enumCommand == Commands.list){ + // List the songs/commands in the queue + if(command.length == 2){ + if(command[1].equals("queue")){ + listTracks(channel); + } + else if(command[1].equals("sounds")){ + localManager.ListSounds(channel); } - break; } - case pause: { + + }else if(enumCommand == Commands.pause){ + // Pause the song/sound in the queue pauseTrack(channel); - break; - } - case unpause: { - unpauseTrack(channel); - break; - } - case playingnow: { - break; - } + + }else if(enumCommand == Commands.unpause){ + // Unpause the song/sound in the queue + unpauseTrack(channel); + + }else if(enumCommand == Commands.playingnow){ + // Print the currently playing song + printCurrentlyPlaying(channel); + } + + } } @@ -176,7 +180,14 @@ public class BotListener extends ListenerAdapter{ channel.sendMessage("Unpaused playback.").queue(); } - private void loadAndPlay(final MessageChannel channel, final String trackUrl) { + private void printCurrentlyPlaying(final MessageChannel channel){ + GuildMusicManager musicManager = getGuildAudioPlayer(); + AudioTrack currentlyPlaying = musicManager.player.getPlayingTrack(); + channel.sendMessage("Currently Playing: " + currentlyPlaying.getInfo().title + " by " + currentlyPlaying.getInfo().author).queue(); + + } + + private void loadAndPlay(final MessageChannel channel, final String trackUrl, boolean preempt) { GuildMusicManager musicManager = getGuildAudioPlayer(); playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() { @@ -190,9 +201,10 @@ public class BotListener extends ListenerAdapter{ //track.setPosition(long position) } - channel.sendMessage("Adding to queue " + track.getInfo().title).queue(); + if(!preempt) + channel.sendMessage("Adding to queue " + track.getInfo().title).queue(); - play(monitoredGuild, musicManager, track); + play(monitoredGuild, musicManager, track, preempt); } @Override @@ -205,7 +217,7 @@ public class BotListener extends ListenerAdapter{ channel.sendMessage("Adding to queue " + firstTrack.getInfo().title + " (first track of playlist " + playlist.getName() + ")").queue(); - play(monitoredGuild, musicManager, firstTrack); + play(monitoredGuild, musicManager, firstTrack, false); } @Override @@ -220,10 +232,13 @@ public class BotListener extends ListenerAdapter{ }); } - private void play(Guild guild, GuildMusicManager musicManager, AudioTrack track) { + private void play(Guild guild, GuildMusicManager musicManager, AudioTrack track, boolean preempt) { connectToFollowingVoiceChannel(guild.getAudioManager()); - musicManager.scheduler.queue(track); + if(!preempt) + musicManager.scheduler.queue(track); + else + musicManager.scheduler.playNow(track); } private void skipTrack(MessageChannel channel) { diff --git a/src/main/java/soundchan/BotListenerHelpers.java b/src/main/java/soundchan/BotListener/BotListenerHelpers.java index 600e4b0..6689255 100644 --- a/src/main/java/soundchan/BotListenerHelpers.java +++ b/src/main/java/soundchan/BotListener/BotListenerHelpers.java @@ -1,4 +1,4 @@ -package soundchan; +package soundchan.BotListener; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.MessageChannel; diff --git a/src/main/java/soundchan/Commands.java b/src/main/java/soundchan/BotListener/Commands.java index 3f96d37..da1f45f 100644 --- a/src/main/java/soundchan/Commands.java +++ b/src/main/java/soundchan/BotListener/Commands.java @@ -1,4 +1,4 @@ -package soundchan; +package soundchan.BotListener; /** * An enum representing the possible commands SoundChan might receive diff --git a/src/main/java/soundchan/LocalAudioManager.java b/src/main/java/soundchan/LocalAudioManager.java index 314f6b9..9a4e299 100644 --- a/src/main/java/soundchan/LocalAudioManager.java +++ b/src/main/java/soundchan/LocalAudioManager.java @@ -31,11 +31,12 @@ public class LocalAudioManager { public void ListSounds(MessageChannel channel){ Set<String> localSounds = filenameDict.keySet(); - String toPrint = "The following sounds you can play are:\n"; + String toPrint = "The following sounds you can play are:\n```"; for (String sound: localSounds) { toPrint = toPrint + " * " + sound + "\n"; } + toPrint = toPrint + "```"; channel.sendMessage(toPrint).queue(); } diff --git a/src/main/java/soundchan/Main.java b/src/main/java/soundchan/Main.java index 2e666f4..a7aa6a3 100644 --- a/src/main/java/soundchan/Main.java +++ b/src/main/java/soundchan/Main.java @@ -3,6 +3,7 @@ package soundchan; import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.JDABuilder; +import soundchan.BotListener.*; import java.io.FileInputStream; import java.io.IOException; diff --git a/src/main/java/soundchan/TrackScheduler.java b/src/main/java/soundchan/TrackScheduler.java index 21bb6d4..cc68335 100644 --- a/src/main/java/soundchan/TrackScheduler.java +++ b/src/main/java/soundchan/TrackScheduler.java @@ -10,7 +10,9 @@ import com.sedmelluq.discord.lavaplayer.track.DelegatedAudioTrack; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.BlockingDeque; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingQueue; /** @@ -18,14 +20,14 @@ import java.util.concurrent.LinkedBlockingQueue; */ public class TrackScheduler extends AudioEventAdapter { private final AudioPlayer player; - private final BlockingQueue<AudioTrack> queue; + private final BlockingDeque<AudioTrack> queue; /** * @param player The audio player this scheduler uses */ public TrackScheduler(AudioPlayer player) { this.player = player; - this.queue = new LinkedBlockingQueue<>(); + this.queue = new LinkedBlockingDeque<>(); } /** @@ -42,6 +44,20 @@ public class TrackScheduler extends AudioEventAdapter { } } + public void playNow(AudioTrack track) { + AudioTrack currenlyPlaying = player.getPlayingTrack(); + AudioTrack cloned = currenlyPlaying.makeClone(); + cloned.setPosition(currenlyPlaying.getPosition()); + + // Don't re-enqueue if its just a soundclip + if(!(currenlyPlaying.getInfo().uri.contains(".mp3") || currenlyPlaying.getInfo().uri.contains(".wav"))) + // Re-enqueue the track + queue.addFirst(cloned); + + player.startTrack(track, false); + + } + public List<String> getQueueContents() { // Returns a list of the tracks in the queue Object[] queueInfo = queue.toArray(); |