add speed

This commit is contained in:
Chayapak 2023-04-11 16:30:30 +07:00
parent a1c5a23ac0
commit d0af62319a
3 changed files with 32 additions and 6 deletions

View file

@ -38,6 +38,7 @@ public class MusicCommand implements Command {
usages.add("queue");
usages.add("goto <timestamp>");
usages.add("pitch <pitch>");
usages.add("speed <speed>");
usages.add("pause");
usages.add("resume");
@ -81,6 +82,9 @@ public class MusicCommand implements Command {
case "pitch" -> {
return pitch(context, args);
}
case "speed" -> {
return speed(context, args);
}
case "pause", "resume" -> {
return pause(context);
}
@ -337,6 +341,27 @@ public class MusicCommand implements Command {
return Component.text("success");
}
public Component speed (CommandContext context, String[] args) {
final Bot bot = context.bot();
float speed;
try {
speed = Float.parseFloat(args[1]);
} catch (IllegalArgumentException ignored) {
return Component.text("Invalid speed").color(NamedTextColor.RED);
}
bot.music().speed(speed);
context.sendOutput(
Component.empty()
.append(Component.text("Set the speed to "))
.append(Component.text(speed).color(NamedTextColor.GOLD))
);
return Component.text("success");
}
public Component pause (CommandContext context) {
final Bot bot = context.bot();
final Song currentSong = bot.music().currentSong();

View file

@ -41,6 +41,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
// sus nightcore stuff,..,.,.
@Getter @Setter private float pitch = 0;
@Getter @Setter private float speed = 1;
private int ticksUntilPausedBossbar = 20;
@ -123,7 +124,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW);
bossBar.visible(true);
bossBar.style(BossBarStyle.PROGRESS);
bossBar.value((int) Math.floor(currentSong.time));
bossBar.value((int) Math.floor(currentSong.time * speed));
bossBar.max(currentSong.length);
if (currentSong.paused) return;
@ -202,7 +203,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
Component component = Component.empty()
.append(Component.empty().append(currentSong.name).color(pitch > 0 ? NamedTextColor.LIGHT_PURPLE : NamedTextColor.GREEN))
.append(Component.text(" | ").color(NamedTextColor.DARK_GRAY))
.append(Component.translatable("%s / %s", formatTime(currentSong.time).color(NamedTextColor.GRAY), formatTime(currentSong.length).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY))
.append(Component.translatable("%s / %s", formatTime((long) (currentSong.time * speed)).color(NamedTextColor.GRAY), formatTime(currentSong.length).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY))
.append(Component.text(" | ").color(NamedTextColor.DARK_GRAY))
.append(
Component.translatable(

View file

@ -72,16 +72,16 @@ public class Song {
}
public void advanceTime () {
time = System.currentTimeMillis() - startTime;
time = (long) ((System.currentTimeMillis() - startTime) * bot.music().speed());
}
public boolean reachedNextNote () {
if (position < notes.size()) {
return notes.get(position).time <= time;
return notes.get(position).time <= time * bot.music().speed();
} else {
if (finished() && bot.music().loop() != Loop.OFF) {
if (position < notes.size()) {
return notes.get(position).time <= time;
return notes.get(position).time <= time * bot.music().speed();
} else {
return false;
}
@ -99,7 +99,7 @@ public class Song {
}
public boolean finished () {
return time > length;
return time > length || position >= size();
}
public int size () {