Playlist contains a full shuffle now.
This commit is contained in:
parent
62ccfbc12b
commit
435007a502
3 changed files with 35 additions and 14 deletions
|
@ -2,7 +2,12 @@ package me.mctp.api.maps;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
public class Playlist<E> extends ArrayList<E> {
|
||||
/**
|
||||
* An {@link ArrayList} with shuffle support.
|
||||
*
|
||||
* @param <E> The type you want to store
|
||||
*/
|
||||
public class SongList<E> extends ArrayList<E> {
|
||||
public void shuffle() {
|
||||
Random random = new Random();
|
||||
|
|
@ -36,7 +36,7 @@ public class MCTPAudioCMD implements CommandExecutor {
|
|||
Main.getPlaylist().stop();
|
||||
sender.sendMessage(prefix + "De auto radio is stopgezet. Zodra het huidige nummer is afgelopen, gebeurt er niks meer.");
|
||||
} else {
|
||||
Main.getPlaylist().init();
|
||||
Main.getPlaylist().start();
|
||||
sender.sendMessage(prefix + "De auto radio is weer gestart.");
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -3,6 +3,7 @@ package me.mctp.radio;
|
|||
import com.mpatric.mp3agic.InvalidDataException;
|
||||
import com.mpatric.mp3agic.UnsupportedTagException;
|
||||
import me.mctp.Main;
|
||||
import me.mctp.api.maps.SongList;
|
||||
import me.mctp.managers.PinManager;
|
||||
import me.mctp.utils.HeadUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -13,21 +14,24 @@ import org.json.simple.JSONObject;
|
|||
import java.io.IOException;
|
||||
|
||||
public class Playlist {
|
||||
private final me.mctp.api.maps.Playlist<String> songList = new me.mctp.api.maps.Playlist<>();
|
||||
private SongList<String> playList = new SongList<>();
|
||||
private final SongList<String> playedList = new SongList<>();
|
||||
|
||||
private boolean running = false;
|
||||
private BukkitTask currentTimer;
|
||||
|
||||
/**
|
||||
* Init, load all the songs from the data and start first
|
||||
* Create a new PlayList object
|
||||
* Starts the playlist
|
||||
*/
|
||||
public Playlist() {
|
||||
init();
|
||||
start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the songs into the system and start
|
||||
* Start this playlist
|
||||
*/
|
||||
public void init() {
|
||||
public void start() {
|
||||
for (String URL : Main.getPlugin().getConfig().getStringList("RadioSongs")) {
|
||||
addSong(URL);
|
||||
}
|
||||
|
@ -38,10 +42,10 @@ public class Playlist {
|
|||
}
|
||||
|
||||
/**
|
||||
* Stop the playlist (clears the queue)
|
||||
* Stop the playlist
|
||||
*/
|
||||
public void stop() {
|
||||
songList.clear();
|
||||
playList.clear();
|
||||
|
||||
if (currentTimer != null) {
|
||||
currentTimer.cancel();
|
||||
|
@ -56,8 +60,8 @@ public class Playlist {
|
|||
* @param url The song url (mp3)
|
||||
*/
|
||||
public void addSong(String url) {
|
||||
songList.add(url);
|
||||
songList.shuffle();
|
||||
playList.add(url);
|
||||
playList.shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,12 +70,19 @@ public class Playlist {
|
|||
public void nextSong() {
|
||||
if (currentTimer != null) return;
|
||||
|
||||
if (songList.isEmpty()) return;
|
||||
if (playList.isEmpty()) {
|
||||
//All numbers played
|
||||
playList = (SongList<String>) playedList.clone(); //Copy a clone.
|
||||
playedList.clear();
|
||||
}
|
||||
|
||||
//Get song
|
||||
String nextURL = songList.getRandom();
|
||||
String nextURL = playList.getRandom();
|
||||
if (nextURL == null) return;
|
||||
|
||||
playList.remove(nextURL);
|
||||
playedList.add(nextURL);
|
||||
|
||||
JSONObject data;
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
data = new JSONObject();
|
||||
|
@ -107,9 +118,14 @@ public class Playlist {
|
|||
}, ticks);
|
||||
|
||||
//Shuffle playlist again
|
||||
songList.shuffle();
|
||||
playList.shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the playlist is running
|
||||
*
|
||||
* @return true if running
|
||||
*/
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue