From 8e449a24239e4c73f949c99f36805340eef7b557 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 29 Sep 2023 11:51:53 +0700 Subject: [PATCH] ADD speed --- .../songplayer/CommandProcessor.java | 28 +++++++++++++++++++ .../songplayer/playing/SongHandler.java | 2 ++ .../hhhzzzsss/songplayer/song/Song.java | 8 ++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java index f462de7..622e6f5 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java @@ -54,6 +54,7 @@ public class CommandProcessor { commands.add(new songItemCommand()); commands.add(new testSongCommand()); commands.add(new pitchCommand()); + commands.add(new speedCommand()); for (Command command : commands) { 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[] { "" }; } + 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 handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) { if (!text.contains(" ")) { List names = commandCompletions diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java index ff45981..c7eaf1e 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java @@ -49,7 +49,9 @@ public class SongHandler { public boolean wasFlying = false; public GameMode originalGamemode = GameMode.CREATIVE; + // the purpose of these 2 variables is just because i want to nightcore public int pitch = 0; + public float speed = 1; boolean playlistChecked = false; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/Song.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/Song.java index 634142d..adc88f5 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/Song.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/Song.java @@ -1,5 +1,7 @@ package com.github.hhhzzzsss.songplayer.song; +import com.github.hhhzzzsss.songplayer.playing.SongHandler; + import java.util.ArrayList; import java.util.Collections; @@ -71,17 +73,17 @@ public class Song { } public void advanceTime() { - time = System.currentTimeMillis() - startTime; + time = (long) ((System.currentTimeMillis() - startTime) * SongHandler.getInstance().speed); } public boolean reachedNextNote() { if (position < notes.size()) { - return notes.get(position).time <= time; + return notes.get(position).time <= time * SongHandler.getInstance().speed; } else { if (time > length && shouldLoop()) { loop(); if (position < notes.size()) { - return notes.get(position).time <= time; + return notes.get(position).time <= time * SongHandler.getInstance().speed; } else { return false; }