Skip to content

Commit

Permalink
Merge pull request jagrosh#218 from SlapGaming/pullreqs
Browse files Browse the repository at this point in the history
Move track command
  • Loading branch information
jagrosh authored Apr 9, 2019
2 parents e1b9e01 + 1a0a73a commit 7dcd7ba
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static void main(String[] args)
new SkipCmd(bot),

new ForceskipCmd(bot),
new MoveTrackCmd(bot),
new PauseCmd(bot),
new PlaynextCmd(bot, config.getLoading()),
new RepeatCmd(bot),
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/com/jagrosh/jmusicbot/commands/dj/MoveTrackCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.jagrosh.jmusicbot.commands.dj;


import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jmusicbot.Bot;
import com.jagrosh.jmusicbot.audio.AudioHandler;
import com.jagrosh.jmusicbot.audio.QueuedTrack;
import com.jagrosh.jmusicbot.commands.DJCommand;
import com.jagrosh.jmusicbot.queue.FairQueue;

/**
* Command that provides users the ability to move a track in the playlist.
*/
public class MoveTrackCmd extends DJCommand
{

public MoveTrackCmd(Bot bot)
{
super(bot);
this.name = "movetrack";
this.help = "move a track in the current queue to a different position";
this.arguments = "<from> <to>";
this.aliases = new String[]{"move"};
this.bePlaying = true;
}

@Override
public void doCommand(CommandEvent event)
{
int from;
int to;

String[] parts = event.getArgs().split("\\s+", 2);
if(parts.length < 2)
{
event.replyError("Please include two valid indexes.");
return;
}

try
{
// Validate the args
from = Integer.parseInt(parts[0]);
to = Integer.parseInt(parts[1]);
}
catch (NumberFormatException e)
{
event.replyError("Please provide two valid indexes.");
return;
}

if (from == to)
{
event.replyError("Can't move a track to the same position.");
return;
}

// Validate that from and to are available
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
FairQueue<QueuedTrack> queue = handler.getQueue();
if (isUnavailablePosition(queue, from))
{
String reply = String.format("`%d` is not a valid position in the queue!", from);
event.replyError(reply);
return;
}
if (isUnavailablePosition(queue, to))
{
String reply = String.format("`%d` is not a valid position in the queue!", to);
event.replyError(reply);
return;
}

// Move the track
QueuedTrack track = queue.moveItem(from - 1, to - 1);
String trackTitle = track.getTrack().getInfo().title;
String reply = String.format("Moved **%s** from position `%d` to `%d`.", trackTitle, from, to);
event.replySuccess(reply);
}

private static boolean isUnavailablePosition(FairQueue<QueuedTrack> queue, int position)
{
return (position < 1 || position > queue.size());
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/jagrosh/jmusicbot/queue/FairQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,17 @@ public void skip(int number)
for(int i=0; i<number; i++)
list.remove(0);
}

/**
* Move an item to a different position in the list
* @param from The position of the item
* @param to The new position of the item
* @return the moved item
*/
public T moveItem(int from, int to)
{
T item = list.remove(from);
list.add(to, item);
return item;
}
}

0 comments on commit 7dcd7ba

Please sign in to comment.