77import java .util .concurrent .locks .Lock ;
88import java .util .concurrent .locks .ReentrantLock ;
99
10+ import com .xxmicloxx .NoteBlockAPI .model .*;
1011import org .bukkit .Bukkit ;
1112import org .bukkit .entity .Player ;
1213
1617import com .xxmicloxx .NoteBlockAPI .event .SongLoopEvent ;
1718import com .xxmicloxx .NoteBlockAPI .event .SongNextEvent ;
1819import com .xxmicloxx .NoteBlockAPI .event .SongStoppedEvent ;
19- import com .xxmicloxx .NoteBlockAPI .model .CustomInstrument ;
20- import com .xxmicloxx .NoteBlockAPI .model .FadeType ;
21- import com .xxmicloxx .NoteBlockAPI .model .Layer ;
22- import com .xxmicloxx .NoteBlockAPI .model .Note ;
23- import com .xxmicloxx .NoteBlockAPI .model .Playlist ;
24- import com .xxmicloxx .NoteBlockAPI .model .Song ;
25- import com .xxmicloxx .NoteBlockAPI .model .SoundCategory ;
2620
2721
2822/**
@@ -45,7 +39,7 @@ public abstract class SongPlayer {
4539 protected byte volume = 100 ;
4640 protected Fade fadeIn ;
4741 protected Fade fadeOut ;
48- protected boolean loop = false ;
42+ protected RepeatMode repeat = RepeatMode . NO ;
4943 protected boolean random = false ;
5044
5145 protected Map <Song , Boolean > songQueue = Collections .synchronizedMap (new HashMap <Song , Boolean >()); //True if already played
@@ -319,59 +313,68 @@ private void start() {
319313 fadeIn .setFadeDone (0 );
320314 CallUpdate ("fadeDone" , fadeIn .getFadeDone ());
321315 fadeOut .setFadeDone (0 );
322- if (random ){
323- songQueue .put (song , true );
324- checkPlaylistQueue ();
325- ArrayList <Song > left = new ArrayList <>();
326- for (Song s : songQueue .keySet ()){
327- if (!songQueue .get (s )){
328- left .add (s );
329- }
330- }
316+ if (repeat == RepeatMode .ONE ){
317+ SongLoopEvent event = new SongLoopEvent (this );
318+ plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
331319
332- if (left .size () == 0 ){
333- left .addAll (songQueue .keySet ());
320+ if (!event .isCancelled ()) {
321+ continue ;
322+ }
323+ } else {
324+ if (random ) {
325+ songQueue .put (song , true );
326+ checkPlaylistQueue ();
327+ ArrayList <Song > left = new ArrayList <>();
334328 for (Song s : songQueue .keySet ()) {
335- songQueue .put (s , false );
329+ if (!songQueue .get (s )) {
330+ left .add (s );
331+ }
336332 }
337- song = left .get (rng .nextInt (left .size ()));
338- actualSong = playlist .getIndex (song );
339- CallUpdate ("song" , song );
340- if (loop ) {
341- SongLoopEvent event = new SongLoopEvent (this );
342- plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
343333
344- if (!event .isCancelled ()) {
345- continue ;
334+ if (left .size () == 0 ) {
335+ left .addAll (songQueue .keySet ());
336+ for (Song s : songQueue .keySet ()) {
337+ songQueue .put (s , false );
346338 }
347- }
348- } else {
349- song = left .get (rng .nextInt (left .size ()));
350- actualSong = playlist .getIndex (song );
339+ song = left .get (rng .nextInt (left .size ()));
340+ actualSong = playlist .getIndex (song );
341+ CallUpdate ("song" , song );
342+ if (repeat == RepeatMode .ALL ) {
343+ SongLoopEvent event = new SongLoopEvent (this );
344+ plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
345+
346+ if (!event .isCancelled ()) {
347+ continue ;
348+ }
349+ }
350+ } else {
351+ song = left .get (rng .nextInt (left .size ()));
352+ actualSong = playlist .getIndex (song );
351353
352- CallUpdate ("song" , song );
353- SongNextEvent event = new SongNextEvent (this );
354- plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
355- continue ;
356- }
357- } else {
358- if (playlist .hasNext (actualSong )) {
359- actualSong ++;
360- song = playlist .get (actualSong );
361- CallUpdate ("song" , song );
362- SongNextEvent event = new SongNextEvent (this );
363- plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
364- continue ;
354+ CallUpdate ("song" , song );
355+ SongNextEvent event = new SongNextEvent (this );
356+ plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
357+ continue ;
358+ }
365359 } else {
366- actualSong = 0 ;
367- song = playlist . get ( actualSong ) ;
368- CallUpdate ( " song" , song );
369- if ( loop ) {
370- SongLoopEvent event = new SongLoopEvent (this );
360+ if ( playlist . hasNext ( actualSong )) {
361+ actualSong ++ ;
362+ song = playlist . get ( actualSong );
363+ CallUpdate ( "song" , song );
364+ SongNextEvent event = new SongNextEvent (this );
371365 plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
372-
373- if (!event .isCancelled ()) {
374- continue ;
366+ continue ;
367+ } else {
368+ actualSong = 0 ;
369+ song = playlist .get (actualSong );
370+ CallUpdate ("song" , song );
371+ if (repeat == RepeatMode .ALL ) {
372+ SongLoopEvent event = new SongLoopEvent (this );
373+ plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
374+
375+ if (!event .isCancelled ()) {
376+ continue ;
377+ }
375378 }
376379 }
377380 }
@@ -738,18 +741,36 @@ public void setCategory(SoundCategory soundCategory) {
738741
739742 /**
740743 * Sets whether the SongPlayer will loop
744+ * @deprecated
741745 * @param loop
742746 */
743747 public void setLoop (boolean loop ){
744- this .loop = loop ;
748+ this .repeat = RepeatMode . ALL ;
745749 }
746750
747751 /**
748752 * Gets whether the SongPlayer will loop
753+ * @deprecated
749754 * @return is loop
750755 */
751756 public boolean isLoop (){
752- return loop ;
757+ return repeat == RepeatMode .ALL ;
758+ }
759+
760+ /**
761+ * Sets SongPlayer's {@link RepeatMode}
762+ * @param repeatMode
763+ */
764+ public void setRepeatMode (RepeatMode repeatMode ){
765+ this .repeat = repeatMode ;
766+ }
767+
768+ /**
769+ * Gets SongPlayer's {@link RepeatMode}
770+ * @return
771+ */
772+ public RepeatMode getRepeatMode (){
773+ return repeat ;
753774 }
754775
755776 /**
0 commit comments