ADD speed

This commit is contained in:
Chayapak 2023-09-29 11:51:53 +07:00
parent adf17c27bb
commit 8e449a2423
3 changed files with 35 additions and 3 deletions

View file

@ -54,6 +54,7 @@ public class CommandProcessor {
commands.add(new songItemCommand()); commands.add(new songItemCommand());
commands.add(new testSongCommand()); commands.add(new testSongCommand());
commands.add(new pitchCommand()); commands.add(new pitchCommand());
commands.add(new speedCommand());
for (Command command : commands) { for (Command command : commands) {
commandMap.put(command.getName().toLowerCase(Locale.ROOT), command); commandMap.put(command.getName().toLowerCase(Locale.ROOT), command);
@ -1095,6 +1096,33 @@ public class CommandProcessor {
} }
} }
private static class speedCommand extends Command {
public String getName() { return "speed"; }
public String[] getSyntax() { return new String[] { "<speed>" }; }
public String getDescription() { return "Sets the speed of the song"; }
public boolean processCommand(String args) {
if (args.length() == 0) return false;
try {
final Song currentSong = SongHandler.getInstance().currentSong;
long oldTime = -1;
if (currentSong != null) oldTime = currentSong.time;
SongHandler.getInstance().speed = Float.parseFloat(args);
if (currentSong != null) currentSong.setTime(oldTime);
SongPlayer.addChatMessage("§6Set the speed to §3" + args, true);
} catch (NumberFormatException e) {
return false;
}
return true;
}
}
public static CompletableFuture<Suggestions> handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) { public static CompletableFuture<Suggestions> handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) {
if (!text.contains(" ")) { if (!text.contains(" ")) {
List<String> names = commandCompletions List<String> names = commandCompletions

View file

@ -49,7 +49,9 @@ public class SongHandler {
public boolean wasFlying = false; public boolean wasFlying = false;
public GameMode originalGamemode = GameMode.CREATIVE; public GameMode originalGamemode = GameMode.CREATIVE;
// the purpose of these 2 variables is just because i want to nightcore
public int pitch = 0; public int pitch = 0;
public float speed = 1;
boolean playlistChecked = false; boolean playlistChecked = false;

View file

@ -1,5 +1,7 @@
package com.github.hhhzzzsss.songplayer.song; package com.github.hhhzzzsss.songplayer.song;
import com.github.hhhzzzsss.songplayer.playing.SongHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -71,17 +73,17 @@ public class Song {
} }
public void advanceTime() { public void advanceTime() {
time = System.currentTimeMillis() - startTime; time = (long) ((System.currentTimeMillis() - startTime) * SongHandler.getInstance().speed);
} }
public boolean reachedNextNote() { public boolean reachedNextNote() {
if (position < notes.size()) { if (position < notes.size()) {
return notes.get(position).time <= time; return notes.get(position).time <= time * SongHandler.getInstance().speed;
} else { } else {
if (time > length && shouldLoop()) { if (time > length && shouldLoop()) {
loop(); loop();
if (position < notes.size()) { if (position < notes.size()) {
return notes.get(position).time <= time; return notes.get(position).time <= time * SongHandler.getInstance().speed;
} else { } else {
return false; return false;
} }