Skip to content

Commit

Permalink
Fixed SongView#getLength returning last tick instead of length
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 25, 2024
1 parent d3643b1 commit 48e71fb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/raphimc/noteblocklib/model/SongView.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public int getLength() {
}

public void recalculateLength() {
this.length = this.notes.keySet().stream().mapToInt(i -> i).max().orElse(0);
this.length = this.notes.keySet().stream().mapToInt(i -> i).max().orElse(-1) + 1;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/raphimc/noteblocklib/player/SongPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SongPlayer {
private final SongPlayerCallback callback;

private ScheduledExecutorService scheduler;
private int tick = -1;
private int tick;
private boolean paused;

public SongPlayer(final SongView<?> songView, final SongPlayerCallback callback) {
Expand Down Expand Up @@ -80,6 +80,7 @@ public void stop() {
} catch (InterruptedException ignored) {
}
this.scheduler = null;
this.tick = 0;
this.paused = false;
}

Expand All @@ -88,20 +89,20 @@ private void tick() {
if (this.paused || !this.callback.shouldTick()) {
return;
}
this.tick++;

if (this.tick > this.songView.getLength()) {
if (this.tick >= this.songView.getLength()) {
if (this.callback.shouldLoop()) {
this.tick = -this.callback.getLoopDelay();
} else {
this.callback.onFinished();
this.tick = -1;
this.stop();
return;
}
return;
}

this.callback.playNotes(this.songView.getNotesAtTick(this.tick));

this.tick++;
} catch (Throwable e) {
if (e.getCause() instanceof InterruptedException) return;

Expand Down

0 comments on commit 48e71fb

Please sign in to comment.