Skip to content

Commit 203b23d

Browse files
committed
Added fade option to setPlaying method #44
1 parent 34c90bb commit 203b23d

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.xxmicloxx</groupId>
77
<artifactId>NoteBlockAPI</artifactId>
8-
<version>1.5.1-SNAPSHOT</version>
8+
<version>1.6.0-SNAPSHOT</version>
99
<name>NoteBlockAPI</name>
1010

1111
<properties>
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>org.spigotmc</groupId>
3131
<artifactId>spigot-api</artifactId>
32-
<version>1.15.2-R0.1-SNAPSHOT</version>
32+
<version>1.16.1-R0.1-SNAPSHOT</version>
3333
<scope>provided</scope>
3434
</dependency>
3535
<dependency>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ protected byte getFadeTarget() {
9292
protected void setFadeDone(int fadeDone){
9393
this.fadeDone = fadeDone;
9494
}
95-
95+
96+
public boolean isDone(){
97+
return fadeDone >= fadeDuration;
98+
}
9699
}

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)