aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorMatt <mattkohls13@gmail.com>2018-04-14 15:32:20 -0400
committerGitHub <noreply@github.com>2018-04-14 15:32:20 -0400
commite983b51b8c125663d8b1401d559d6b578d8f156d (patch)
tree9187e688ad78f9ea1ad104a6c71857e6a24c7985 /src/main/java
parentf8fe02e835c6417a54033b30a66c9c3bc2097836 (diff)
parent14006737593b80d198f0d19d820a37bd984e8ccc (diff)
downloadSoundChan-e983b51b8c125663d8b1401d559d6b578d8f156d.tar.gz
SoundChan-e983b51b8c125663d8b1401d559d6b578d8f156d.tar.bz2
SoundChan-e983b51b8c125663d8b1401d559d6b578d8f156d.zip
Merge pull request #5 from bwaggone/feature/preempt-sound-effects
Feature/preempt sound effects
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/soundchan/BotListener/BotListener.java (renamed from src/main/java/soundchan/BotListener.java)104
-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.java3
-rw-r--r--src/main/java/soundchan/Main.java1
-rw-r--r--src/main/java/soundchan/TrackScheduler.java20
6 files changed, 81 insertions, 51 deletions
diff --git a/src/main/java/soundchan/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java
index 8785892..044625d 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,48 +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;
- }
+
+ }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());
}
+
+
}
}
@@ -180,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() {
@@ -194,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
@@ -209,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
@@ -224,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 45fda7a..1a06436 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();