From 8d4274883b1b5b101f8f52c06c350e2cabd92b90 Mon Sep 17 00:00:00 2001 From: Chipmunk <65827213+ChipmunkMC@users.noreply.github.com> Date: Fri, 17 Feb 2023 10:07:32 -0500 Subject: [PATCH] `goto` subcommand for `music` --- .../arguments/TimestampArgumentType.java | 36 +++++++++++++++++++ .../chipmunkbot/commands/MusicCommand.java | 28 +++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/land/chipmunk/chipmunkbot/command/arguments/TimestampArgumentType.java diff --git a/src/main/java/land/chipmunk/chipmunkbot/command/arguments/TimestampArgumentType.java b/src/main/java/land/chipmunk/chipmunkbot/command/arguments/TimestampArgumentType.java new file mode 100644 index 0000000..69d1615 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/command/arguments/TimestampArgumentType.java @@ -0,0 +1,36 @@ +package land.chipmunk.chipmunkbot.command.arguments; + +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import java.util.Collection; +import java.util.Arrays; + +public class TimestampArgumentType implements ArgumentType { + private static Collection EXAMPLES = Arrays.asList("0:01", "1:23", "6:09"); + + private TimestampArgumentType () { + } + + public static TimestampArgumentType timestamp () { return new TimestampArgumentType(); } + + @Override + public Long parse (StringReader reader) throws CommandSyntaxException { + long seconds = 0L; + long minutes = 0L; + + seconds = reader.readLong(); + if (reader.canRead() && reader.peek() == ':') { + reader.skip(); + minutes = seconds; + seconds = reader.readLong(); + } + + return (seconds * 1000) + (minutes * 1000 * 60); + } + + // ? Should I create a getter method? Seems like reinventing the wheel since LongArgumentType#getLong is already a thing. + + @Override + public Collection getExamples () { return EXAMPLES; } +} diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java index 72f8d53..5bdedd0 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/MusicCommand.java @@ -9,6 +9,8 @@ import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger; +import static land.chipmunk.chipmunkbot.command.arguments.TimestampArgumentType.timestamp; +import static com.mojang.brigadier.arguments.LongArgumentType.getLong; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; @@ -20,6 +22,7 @@ import java.util.ArrayList; public class MusicCommand extends Command { private static SimpleCommandExceptionType NO_SONG_IS_CURRENTLY_PLAYING = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("No song is currently playing"))); + private static SimpleCommandExceptionType OOB_TIMESTAMP = new SimpleCommandExceptionType(ComponentMessage.wrap(Component.translatable("Invalid timestamp for the current song"))); public MusicCommand () { super(); @@ -46,6 +49,14 @@ public class MusicCommand extends Command { .executes(this::loop) ) ) + + .then( + literal("goto") + .then( + argument("timestamp", timestamp()) + .executes(this::gotoCommand) + ) + ) ); } @@ -132,4 +143,21 @@ public class MusicCommand extends Command { return 1; } + + public int gotoCommand (CommandContext context) throws CommandSyntaxException { + final CommandSource source = context.getSource(); + final SongPlayer songPlayer = source.client().songPlayer(); + final Song currentSong = songPlayer.currentSong(); + final long millis = getLong(context, "timestamp"); + + if (currentSong == null) throw NO_SONG_IS_CURRENTLY_PLAYING.create(); + + if (millis < 0 || millis > currentSong.length) throw OOB_TIMESTAMP.create(); + + currentSong.setTime(millis); + + source.sendOutput(Component.translatable("Set the current time of the song to %s", songPlayer.formatTime(millis))); + + return 1; + } }