forked from jagrosh/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add LinearQueue * rework queueType to be server-specific * rework QueueType enum and command * add QueueType supplier * fix queue type formatting * add QueueSupplier util * fix code issues * Fix unit tests * add suggested changes on PR --------- Co-authored-by: Michaili K <[email protected]> Co-authored-by: Michail <[email protected]>
- Loading branch information
1 parent
44e44f9
commit c1dbd1d
Showing
14 changed files
with
403 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/jagrosh/jmusicbot/commands/admin/QueueTypeCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2022 John Grosh <[email protected]>. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.jagrosh.jmusicbot.commands.admin; | ||
|
||
import com.jagrosh.jdautilities.command.CommandEvent; | ||
import com.jagrosh.jmusicbot.Bot; | ||
import com.jagrosh.jmusicbot.audio.AudioHandler; | ||
import com.jagrosh.jmusicbot.commands.AdminCommand; | ||
import com.jagrosh.jmusicbot.settings.QueueType; | ||
import com.jagrosh.jmusicbot.settings.Settings; | ||
|
||
/** | ||
* | ||
* @author Wolfgang Schwendtbauer | ||
*/ | ||
public class QueueTypeCmd extends AdminCommand | ||
{ | ||
public QueueTypeCmd(Bot bot) | ||
{ | ||
super(); | ||
this.name = "queuetype"; | ||
this.help = "changes the queue type"; | ||
this.arguments = "[" + String.join("|", QueueType.getNames()) + "]"; | ||
this.aliases = bot.getConfig().getAliases(this.name); | ||
} | ||
|
||
@Override | ||
protected void execute(CommandEvent event) | ||
{ | ||
String args = event.getArgs(); | ||
QueueType value; | ||
Settings settings = event.getClient().getSettingsFor(event.getGuild()); | ||
|
||
if (args.isEmpty()) | ||
{ | ||
QueueType currentType = settings.getQueueType(); | ||
event.reply(currentType.getEmoji() + " Current queue type is: `" + currentType.getUserFriendlyName() + "`."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
value = QueueType.valueOf(args.toUpperCase()); | ||
} | ||
catch (IllegalArgumentException e) | ||
{ | ||
event.replyError("Invalid queue type. Valid types are: [" + String.join("|", QueueType.getNames()) + "]"); | ||
return; | ||
} | ||
|
||
if (settings.getQueueType() != value) | ||
{ | ||
settings.setQueueType(value); | ||
|
||
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler(); | ||
if (handler != null) | ||
handler.setQueueType(value); | ||
} | ||
|
||
event.reply(value.getEmoji() + " Queue type was set to `" + value.getUserFriendlyName() + "`."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/main/java/com/jagrosh/jmusicbot/queue/AbstractQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2022 John Grosh (jagrosh). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.jagrosh.jmusicbot.queue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author Wolfgang Schwendtbauer | ||
* @param <T> | ||
*/ | ||
public abstract class AbstractQueue<T extends Queueable> | ||
{ | ||
protected AbstractQueue(AbstractQueue<T> queue) | ||
{ | ||
this.list = queue != null ? queue.getList() : new LinkedList<>(); | ||
} | ||
|
||
protected final List<T> list; | ||
|
||
public abstract int add(T item); | ||
|
||
public void addAt(int index, T item) | ||
{ | ||
if(index >= list.size()) | ||
list.add(item); | ||
else | ||
list.add(index, item); | ||
} | ||
|
||
public int size() { | ||
return list.size(); | ||
} | ||
|
||
public T pull() { | ||
return list.remove(0); | ||
} | ||
|
||
public boolean isEmpty() | ||
{ | ||
return list.isEmpty(); | ||
} | ||
|
||
public List<T> getList() | ||
{ | ||
return list; | ||
} | ||
|
||
public T get(int index) { | ||
return list.get(index); | ||
} | ||
|
||
public T remove(int index) | ||
{ | ||
return list.remove(index); | ||
} | ||
|
||
public int removeAll(long identifier) | ||
{ | ||
int count = 0; | ||
for(int i=list.size()-1; i>=0; i--) | ||
{ | ||
if(list.get(i).getIdentifier()==identifier) | ||
{ | ||
list.remove(i); | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
public void clear() | ||
{ | ||
list.clear(); | ||
} | ||
|
||
public int shuffle(long identifier) | ||
{ | ||
List<Integer> iset = new ArrayList<>(); | ||
for(int i=0; i<list.size(); i++) | ||
{ | ||
if(list.get(i).getIdentifier()==identifier) | ||
iset.add(i); | ||
} | ||
for(int j=0; j<iset.size(); j++) | ||
{ | ||
int first = iset.get(j); | ||
int second = iset.get((int)(Math.random()*iset.size())); | ||
T temp = list.get(first); | ||
list.set(first, list.get(second)); | ||
list.set(second, temp); | ||
} | ||
return iset.size(); | ||
} | ||
|
||
public void skip(int number) | ||
{ | ||
if (number > 0) { | ||
list.subList(0, number).clear(); | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
Oops, something went wrong.