From e521c6b6717943d0339424a302af5672a437a97a Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 20 Aug 2023 15:33:50 +0700 Subject: [PATCH] make music goto use timestamp + remove debug lines + some music speed thing --- .../chomens_bot/commands/MusicCommand.java | 25 ++++++++++------- .../chomens_bot/plugins/CorePlugin.java | 3 --- .../chomens_bot/util/TimestampUtilities.java | 27 +++++++++++++++++++ 3 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/util/TimestampUtilities.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java index 43d2ba5..258871f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java @@ -9,6 +9,7 @@ import land.chipmunk.chayapak.chomens_bot.plugins.MusicPlayerPlugin; import land.chipmunk.chayapak.chomens_bot.song.Loop; import land.chipmunk.chayapak.chomens_bot.song.Song; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; +import land.chipmunk.chayapak.chomens_bot.util.TimestampUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.event.ClickEvent; @@ -321,19 +322,20 @@ public class MusicCommand extends Command { final Bot bot = context.bot; final Song currentSong = bot.music.currentSong; - final long milliseconds; - try { - milliseconds = Long.parseLong(args[1]) * 1000; - } catch (NumberFormatException e) { - return Component.text("Invalid timestamp").color(NamedTextColor.RED); - } + final String input = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + + final long timestamp = TimestampUtilities.parseTimestamp(input); if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); - if (milliseconds < 0 || milliseconds > currentSong.length) return Component.text("Invalid timestamp").color(NamedTextColor.RED); - currentSong.setTime(milliseconds); + if (timestamp < 0 || timestamp > currentSong.length) return Component.text("Invalid timestamp").color(NamedTextColor.RED); - return null; + currentSong.setTime(timestamp); + + return Component + .text("Set the time to ") + .append(Component.text(input).color(ColorUtilities.getColorByString(bot.config.colorPalette.number))) + .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } public Component pitch (CommandContext context, String[] args) { @@ -356,6 +358,7 @@ public class MusicCommand extends Command { public Component speed (CommandContext context, String[] args) { final Bot bot = context.bot; + final Song currentSong = bot.music.currentSong; float speed; try { @@ -364,8 +367,12 @@ public class MusicCommand extends Command { return Component.text("Invalid speed").color(NamedTextColor.RED); } + final long oldTime = currentSong.time; + bot.music.speed = speed; + currentSong.setTime(oldTime); + return Component.empty() .append(Component.text("Set the speed to ")) .append(Component.text(speed).color(ColorUtilities.getColorByString(bot.config.colorPalette.number))) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java index fb530b1..4a82c89 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java @@ -309,9 +309,6 @@ public class CorePlugin extends PositionPlugin.Listener { toSize.getZ() + bot.position.position.getZ() ); - System.out.println(from); - System.out.println(to); - reset(); refill(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/TimestampUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/TimestampUtilities.java new file mode 100644 index 0000000..b403b84 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/TimestampUtilities.java @@ -0,0 +1,27 @@ +package land.chipmunk.chayapak.chomens_bot.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class TimestampUtilities { + // totallynotskidded™ from SongPlayer (modified a bit) + public static final Pattern TIMESTAMP_PATTERN = Pattern.compile("(?:(\\d+):)?(\\d+):(\\d+)"); + + public static long parseTimestamp (String timestamp) { + final Matcher matcher = TIMESTAMP_PATTERN.matcher(timestamp); + + if (!matcher.matches()) return -1; + + long time = 0; + + final String hourString = matcher.group(1); + final String minuteString = matcher.group(2); + final String secondString = matcher.group(3); + + if (hourString != null) time += (long) Integer.parseInt(hourString) * 60 * 60 * 1000; + time += (long) Integer.parseInt(minuteString) * 60 * 1000; + time += Double.parseDouble(secondString) * 1000.0; + + return time; + } +}