Skip to content

Commit 6d26896

Browse files
committed
Added playlists #14
1 parent e8db628 commit 6d26896

File tree

8 files changed

+198
-18
lines changed

8 files changed

+198
-18
lines changed

src/main/java/com/xxmicloxx/NoteBlockAPI/SongPlayer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public SongPlayer(Song song, SoundCategory soundCategory) {
6161

6262
SongPlayer(com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer songPlayer){
6363
newSongPlayer = songPlayer;
64-
com.xxmicloxx.NoteBlockAPI.model.Song s = songPlayer.getSong();
64+
song = createSongFromNew(songPlayer.getSong());
65+
}
66+
67+
private Song createSongFromNew(com.xxmicloxx.NoteBlockAPI.model.Song s){
6568
HashMap<Integer, Layer> layerHashMap = new HashMap<Integer, Layer>();
6669
for (Integer i : s.getLayerHashMap().keySet()){
6770
com.xxmicloxx.NoteBlockAPI.model.Layer l = s.getLayerHashMap().get(i);
@@ -80,7 +83,8 @@ public SongPlayer(Song song, SoundCategory soundCategory) {
8083
com.xxmicloxx.NoteBlockAPI.model.CustomInstrument ci = s.getCustomInstruments()[i];
8184
instruments[i] = new CustomInstrument(ci.getIndex(), ci.getName(), ci.getSoundFileName());
8285
}
83-
song = new Song(s.getSpeed(), layerHashMap, s.getSongHeight(), s.getLength(), s.getTitle(), s.getAuthor(), s.getDescription(), s.getPath(), instruments);
86+
87+
return new Song(s.getSpeed(), layerHashMap, s.getSongHeight(), s.getLength(), s.getTitle(), s.getAuthor(), s.getDescription(), s.getPath(), instruments);
8488
}
8589

8690
void update(String key, Object value){
@@ -121,6 +125,9 @@ void update(String key, Object value){
121125
case "soundCategory":
122126
soundCategory = SoundCategory.valueOf((String) value);
123127
break;
128+
case "song":
129+
song = createSongFromNew((com.xxmicloxx.NoteBlockAPI.model.Song) value);
130+
break;
124131

125132
}
126133
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.xxmicloxx.NoteBlockAPI.event;
2+
3+
import org.bukkit.event.Event;
4+
import org.bukkit.event.HandlerList;
5+
6+
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
7+
8+
public class SongNextEvent extends Event {
9+
10+
private static final HandlerList handlers = new HandlerList();
11+
private SongPlayer song;
12+
13+
public SongNextEvent(SongPlayer song) {
14+
this.song = song;
15+
}
16+
17+
public static HandlerList getHandlerList() {
18+
return handlers;
19+
}
20+
21+
/**
22+
* Returns SongPlayer which is going to play next song in playlist
23+
* @return SongPlayer
24+
*/
25+
public SongPlayer getSongPlayer() {
26+
return song;
27+
}
28+
29+
public HandlerList getHandlers() {
30+
return handlers;
31+
}
32+
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.xxmicloxx.NoteBlockAPI.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class Playlist {
7+
8+
ArrayList<Song> songs = new ArrayList<>();
9+
10+
public Playlist(Song ...songs){
11+
this.songs.addAll(Arrays.asList(songs));
12+
}
13+
14+
public void add(Song ...songs){
15+
this.songs.addAll(Arrays.asList(songs));
16+
}
17+
18+
/**
19+
* Removes songs from playlist
20+
* @param songs
21+
* @throws IllegalArgumentException when you try to remove all {@link Song} from {@link Playlist}
22+
*/
23+
public void remove(Song ...songs){
24+
ArrayList<Song> songsTemp = new ArrayList<>();
25+
songsTemp.addAll(this.songs);
26+
songsTemp.removeAll(Arrays.asList(songs));
27+
if (songsTemp.size() > 0){
28+
this.songs = songsTemp;
29+
} else {
30+
throw new IllegalArgumentException("Cannot remove all songs from playlist");
31+
}
32+
}
33+
34+
public Song get(int songNumber){
35+
return songs.get(songNumber);
36+
}
37+
38+
public int getCount(){
39+
return songs.size();
40+
}
41+
42+
public boolean hasNext(int songNumber){
43+
return songs.size() > (songNumber + 1);
44+
}
45+
46+
public boolean exits(int songNumber){
47+
return songs.size() > songNumber;
48+
}
49+
}

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/NoteBlockSongPlayer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.xxmicloxx.NoteBlockAPI.model.Layer;
1313
import com.xxmicloxx.NoteBlockAPI.model.Note;
1414
import com.xxmicloxx.NoteBlockAPI.model.NotePitch;
15+
import com.xxmicloxx.NoteBlockAPI.model.Playlist;
1516
import com.xxmicloxx.NoteBlockAPI.model.Song;
1617
import com.xxmicloxx.NoteBlockAPI.model.SoundCategory;
1718
import com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils;
@@ -34,8 +35,16 @@ public NoteBlockSongPlayer(Song song, SoundCategory soundCategory) {
3435
super(song, soundCategory);
3536
makeNewClone(com.xxmicloxx.NoteBlockAPI.NoteBlockSongPlayer.class);
3637
}
37-
38-
38+
39+
public NoteBlockSongPlayer(Playlist playlist, SoundCategory soundCategory) {
40+
super(playlist, soundCategory);
41+
makeNewClone(com.xxmicloxx.NoteBlockAPI.NoteBlockSongPlayer.class);
42+
}
43+
44+
public NoteBlockSongPlayer(Playlist playlist) {
45+
super(playlist);
46+
makeNewClone(com.xxmicloxx.NoteBlockAPI.NoteBlockSongPlayer.class);
47+
}
3948

4049
private NoteBlockSongPlayer(SongPlayer songPlayer) {
4150
super(songPlayer);

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/PositionSongPlayer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.xxmicloxx.NoteBlockAPI.model.Layer;
1212
import com.xxmicloxx.NoteBlockAPI.model.Note;
1313
import com.xxmicloxx.NoteBlockAPI.model.NotePitch;
14+
import com.xxmicloxx.NoteBlockAPI.model.Playlist;
1415
import com.xxmicloxx.NoteBlockAPI.model.Song;
1516
import com.xxmicloxx.NoteBlockAPI.model.SoundCategory;
1617
import com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils;
@@ -38,6 +39,16 @@ private PositionSongPlayer(SongPlayer songPlayer) {
3839
super(songPlayer);
3940
}
4041

42+
public PositionSongPlayer(Playlist playlist, SoundCategory soundCategory) {
43+
super(playlist, soundCategory);
44+
makeNewClone(com.xxmicloxx.NoteBlockAPI.PositionSongPlayer.class);
45+
}
46+
47+
public PositionSongPlayer(Playlist playlist) {
48+
super(playlist);
49+
makeNewClone(com.xxmicloxx.NoteBlockAPI.PositionSongPlayer.class);
50+
}
51+
4152
@Override
4253
void update(String key, Object value) {
4354
super.update(key, value);

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/RadioSongPlayer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.xxmicloxx.NoteBlockAPI.model.Layer;
88
import com.xxmicloxx.NoteBlockAPI.model.Note;
99
import com.xxmicloxx.NoteBlockAPI.model.NotePitch;
10+
import com.xxmicloxx.NoteBlockAPI.model.Playlist;
1011
import com.xxmicloxx.NoteBlockAPI.model.Song;
1112
import com.xxmicloxx.NoteBlockAPI.model.SoundCategory;
1213
import com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils;
@@ -27,13 +28,21 @@ public RadioSongPlayer(Song song, SoundCategory soundCategory) {
2728
super(song, soundCategory);
2829
makeNewClone(com.xxmicloxx.NoteBlockAPI.RadioSongPlayer.class);
2930
}
30-
31-
3231

3332
private RadioSongPlayer(com.xxmicloxx.NoteBlockAPI.SongPlayer songPlayer) {
3433
super(songPlayer);
3534
}
3635

36+
public RadioSongPlayer(Playlist playlist, SoundCategory soundCategory) {
37+
super(playlist, soundCategory);
38+
makeNewClone(com.xxmicloxx.NoteBlockAPI.RadioSongPlayer.class);
39+
}
40+
41+
public RadioSongPlayer(Playlist playlist) {
42+
super(playlist);
43+
makeNewClone(com.xxmicloxx.NoteBlockAPI.RadioSongPlayer.class);
44+
}
45+
3746
@Override
3847
public void playTick(Player player, int tick) {
3948
byte playerVolume = NoteBlockAPI.getPlayerVolume(player);

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/RangeSongPlayer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.bukkit.entity.Player;
44

5+
import com.xxmicloxx.NoteBlockAPI.model.Playlist;
56
import com.xxmicloxx.NoteBlockAPI.model.Song;
67
import com.xxmicloxx.NoteBlockAPI.model.SoundCategory;
78

@@ -24,7 +25,14 @@ public RangeSongPlayer(Song song) {
2425
protected RangeSongPlayer(com.xxmicloxx.NoteBlockAPI.SongPlayer songPlayer) {
2526
super(songPlayer);
2627
}
27-
28+
29+
public RangeSongPlayer(Playlist playlist, SoundCategory soundCategory) {
30+
super(playlist, soundCategory);
31+
}
32+
33+
public RangeSongPlayer(Playlist playlist) {
34+
super(playlist);
35+
}
2836

2937
@Override
3038
void update(String key, Object value) {

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/SongPlayer.java

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import com.xxmicloxx.NoteBlockAPI.event.SongDestroyingEvent;
2121
import com.xxmicloxx.NoteBlockAPI.event.SongEndEvent;
2222
import com.xxmicloxx.NoteBlockAPI.event.SongLoopEvent;
23+
import com.xxmicloxx.NoteBlockAPI.event.SongNextEvent;
2324
import com.xxmicloxx.NoteBlockAPI.event.SongStoppedEvent;
2425
import com.xxmicloxx.NoteBlockAPI.model.CustomInstrument;
2526
import com.xxmicloxx.NoteBlockAPI.model.FadeType;
2627
import com.xxmicloxx.NoteBlockAPI.model.Layer;
2728
import com.xxmicloxx.NoteBlockAPI.model.Note;
29+
import com.xxmicloxx.NoteBlockAPI.model.Playlist;
2830
import com.xxmicloxx.NoteBlockAPI.model.Song;
2931
import com.xxmicloxx.NoteBlockAPI.model.SoundCategory;
3032

@@ -36,6 +38,8 @@
3638
public abstract class SongPlayer {
3739

3840
protected Song song;
41+
protected Playlist playlist;
42+
protected int actualSong = 0;
3943

4044
protected boolean playing = false;
4145
protected short tick = -1;
@@ -47,11 +51,6 @@ public abstract class SongPlayer {
4751
protected Thread playerThread;
4852

4953
protected byte volume = 100;
50-
/*protected byte fadeStart = volume;
51-
protected byte fadeTarget = 100;
52-
protected int fadeDuration = 60;
53-
protected int fadeDone = 0;
54-
protected FadeType fadeType = FadeType.LINEAR;*/
5554
protected Fade fadeIn;
5655
protected Fade fadeOut;
5756
protected boolean loop = false;
@@ -65,11 +64,21 @@ public abstract class SongPlayer {
6564
com.xxmicloxx.NoteBlockAPI.SongPlayer oldSongPlayer;
6665

6766
public SongPlayer(Song song) {
68-
this(song, SoundCategory.MASTER);
67+
this(new Playlist(song), SoundCategory.MASTER);
6968
}
7069

7170
public SongPlayer(Song song, SoundCategory soundCategory) {
72-
this.song = song;
71+
//this.song = song;
72+
this(new Playlist(song), soundCategory);
73+
}
74+
75+
public SongPlayer(Playlist playlist){
76+
this(playlist, SoundCategory.MASTER);
77+
}
78+
79+
public SongPlayer(Playlist playlist, SoundCategory soundCategory){
80+
this.playlist = playlist;
81+
this.song = playlist.get(actualSong);
7382
this.soundCategory = soundCategory;
7483
plugin = NoteBlockAPI.getAPI();
7584

@@ -106,6 +115,7 @@ public SongPlayer(Song song, SoundCategory soundCategory) {
106115
instruments[i] = new CustomInstrument(ci.getIndex(), ci.getName(), ci.getSoundfile());
107116
}
108117
song = new Song(s.getSpeed(), layerHashMap, s.getSongHeight(), s.getLength(), s.getTitle(), s.getAuthor(), s.getDescription(), s.getPath(), instruments);
118+
playlist = new Playlist(song);
109119

110120
fadeIn = new Fade(FadeType.NONE, 60);
111121
fadeIn.setFadeStart((byte) 0);
@@ -294,12 +304,26 @@ private void start() {
294304
if (tick > song.getLength()) {
295305
tick = -1;
296306
fadeIn.setFadeDone(0);
307+
CallUpdate("fadeDone", fadeIn.getFadeDone());
297308
fadeOut.setFadeDone(0);
298-
if (loop){
299-
SongLoopEvent event = new SongLoopEvent(this);
309+
if (playlist.hasNext(actualSong)){
310+
actualSong++;
311+
song = playlist.get(actualSong);
312+
CallUpdate("song", song);
313+
SongNextEvent event = new SongNextEvent(this);
300314
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
301-
if (!event.isCancelled()){
302-
continue;
315+
continue;
316+
} else{
317+
actualSong = 0;
318+
song = playlist.get(actualSong);
319+
CallUpdate("song", song);
320+
if (loop){
321+
SongLoopEvent event = new SongLoopEvent(this);
322+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
323+
324+
if (!event.isCancelled()){
325+
continue;
326+
}
303327
}
304328
}
305329
playing = false;
@@ -561,6 +585,36 @@ public void setVolume(byte volume) {
561585
public Song getSong() {
562586
return song;
563587
}
588+
589+
/**
590+
* Gets the Playlist being played by this SongPlayer
591+
* @return
592+
*/
593+
public Playlist getPlaylist() {
594+
return playlist;
595+
}
596+
597+
public int getPlayedSongIndex(){
598+
return actualSong;
599+
}
600+
601+
public void playSong(int index){
602+
lock.lock();
603+
try {
604+
if (playlist.exits(index)){
605+
song = playlist.get(index);
606+
actualSong = index;
607+
tick = -1;
608+
fadeIn.setFadeDone(0);
609+
fadeOut.setFadeDone(0);
610+
CallUpdate("song", song);
611+
CallUpdate("fadeDone", fadeIn.getFadeDone());
612+
CallUpdate("tick", tick);
613+
}
614+
} finally {
615+
lock.unlock();
616+
}
617+
}
564618

565619
/**
566620
* Gets the SoundCategory of this SongPlayer

0 commit comments

Comments
 (0)