goto subcommand for music

This commit is contained in:
Chipmunk 2023-02-17 10:07:32 -05:00
parent eb122b21e6
commit 8d4274883b
2 changed files with 64 additions and 0 deletions

View file

@ -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<Long> {
private static Collection<String> EXAMPLES = Arrays.<String>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<String> getExamples () { return EXAMPLES; }
}

View file

@ -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<CommandSource> 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;
}
}