diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2018-04-14 15:49:24 -0400 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2018-04-14 15:49:24 -0400 |
commit | 70f6beee0fb9569e8549752e627929ae506c5e0c (patch) | |
tree | 3786e817b37b0be9ae1a7d2bfc3f5080869bad0d | |
parent | 780705840a868f0b776c9f18832996ea67e70aa3 (diff) | |
parent | e983b51b8c125663d8b1401d559d6b578d8f156d (diff) | |
download | SoundChan-70f6beee0fb9569e8549752e627929ae506c5e0c.tar.gz SoundChan-70f6beee0fb9569e8549752e627929ae506c5e0c.tar.bz2 SoundChan-70f6beee0fb9569e8549752e627929ae506c5e0c.zip |
Merge branch 'master' of https://github.com/bwaggone/SoundChan into feature/add-help
-rw-r--r-- | src/main/java/soundchan/BotListener/BotListener.java (renamed from src/main/java/soundchan/BotListener.java) | 109 | ||||
-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, 81 insertions, 56 deletions
diff --git a/src/main/java/soundchan/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index 02170af..fe148ce 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,7 +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 net.dv8tion.jda.core.managers.ChannelManager; +import soundchan.*; import java.util.HashMap; import java.util.List; @@ -95,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(); @@ -103,52 +103,48 @@ 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; - } - case summon: { - connectToUserVoiceChannel(guild.getAudioManager(), event.getMember().getEffectiveName()); - break; - } - case help: { - help(channel); - 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); + }else if(enumCommand == Commands.summon){ + connectToUserVoiceChannel(monitoredGuild.getAudioManager(), event.getMember().getEffectiveName()); + }else if(enumCommand == Commands.help){ + help(channel); } + } } @@ -185,7 +181,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() { @@ -199,9 +202,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 @@ -214,7 +218,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 @@ -229,10 +233,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 e6ccf0e..e054de1 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(); |