@@ -28,6 +28,7 @@ public abstract class SongPlayer {
2828 protected int actualSong = 0 ;
2929
3030 protected boolean playing = false ;
31+ protected boolean fading = false ;
3132 protected short tick = -1 ;
3233 protected Map <UUID , Boolean > playerList = new ConcurrentHashMap <UUID , Boolean >();
3334
@@ -37,6 +38,7 @@ public abstract class SongPlayer {
3738 protected byte volume = 100 ;
3839 protected Fade fadeIn ;
3940 protected Fade fadeOut ;
41+ protected Fade fadeTemp = null ;
4042 protected RepeatMode repeat = RepeatMode .NO ;
4143 protected boolean random = false ;
4244
@@ -316,8 +318,24 @@ private void start() {
316318 break ;
317319 }
318320
319- if (playing ) {
320- if (tick < fadeIn .getFadeDuration ()){
321+ if (playing || fading ) {
322+ if (fadeTemp != null ){
323+ if (fadeTemp .isDone ()) {
324+ fadeTemp = null ;
325+ fading = false ;
326+ if (!playing ) {
327+ SongStoppedEvent event = new SongStoppedEvent (this );
328+ plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
329+ volume = fadeIn .getFadeTarget ();
330+ continue ;
331+ }
332+ }else {
333+ int fade = fadeTemp .calculateFade ();
334+ if (fade != -1 ){
335+ volume = (byte ) fade ;
336+ }
337+ }
338+ } else if (tick < fadeIn .getFadeDuration ()){
321339 int fade = fadeIn .calculateFade ();
322340 if (fade != -1 ){
323341 volume = (byte ) fade ;
@@ -336,6 +354,7 @@ private void start() {
336354 fadeIn .setFadeDone (0 );
337355 CallUpdate ("fadeDone" , fadeIn .getFadeDone ());
338356 fadeOut .setFadeDone (0 );
357+ volume = fadeIn .getFadeTarget ();
339358 if (repeat == RepeatMode .ONE ){
340359 SongLoopEvent event = new SongLoopEvent (this );
341360 plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
@@ -596,11 +615,32 @@ public boolean isPlaying() {
596615 * @param playing
597616 */
598617 public void setPlaying (boolean playing ) {
618+ setPlaying (playing , null );
619+ }
620+
621+ /**
622+ * Sets whether the SongPlayer is playing and whether it should fade if previous value was different
623+ * @param playing
624+ * @param fade
625+ */
626+ public void setPlaying (boolean playing , boolean fade ) {
627+ setPlaying (playing , fade ? (playing ? fadeIn : fadeOut ) : null );
628+ }
629+
630+ public void setPlaying (boolean playing , Fade fade ) {
631+ if (this .playing == playing ) return ;
632+
599633 this .playing = playing ;
600- if (!playing ) {
601- SongStoppedEvent event = new SongStoppedEvent (this );
602- plugin .doSync (() -> Bukkit .getPluginManager ().callEvent (event ));
634+ if (fade != null && fade .getType () != FadeType .NONE ) {
635+ fadeTemp = new Fade (fade .getType (), fade .getFadeDuration ());
636+ fadeTemp .setFadeStart (playing ? 0 : volume );
637+ fadeTemp .setFadeTarget (playing ? volume : 0 );
638+ fading = true ;
639+ } else {
640+ fading = false ;
641+ volume = fadeIn .getFadeTarget ();
603642 }
643+
604644 CallUpdate ("playing" , playing );
605645 }
606646
@@ -686,6 +726,10 @@ public void setVolume(byte volume) {
686726
687727 fadeIn .setFadeTarget (volume );
688728 fadeOut .setFadeStart (volume );
729+ if (fadeTemp != null ) {
730+ if (playing ) fadeTemp .setFadeTarget (volume );
731+ else fadeTemp .setFadeStart (volume );
732+ }
689733
690734 CallUpdate ("volume" , volume );
691735 }
0 commit comments