add stuff fix stuff improve stuff

This commit is contained in:
Chayapak 2023-04-14 13:34:43 +07:00
parent a96d797766
commit e6ef185e9f
6 changed files with 89 additions and 30 deletions

View file

@ -16,7 +16,10 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class MusicCommand implements Command { public class MusicCommand implements Command {
private Path root; private Path root;
@ -41,6 +44,7 @@ public class MusicCommand implements Command {
usages.add("speed <speed>"); usages.add("speed <speed>");
usages.add("pause"); usages.add("pause");
usages.add("resume"); usages.add("resume");
usages.add("info");
return usages; return usages;
} }
@ -57,9 +61,9 @@ public class MusicCommand implements Command {
} }
public Component execute(CommandContext context, String[] args, String[] fullArgs) { public Component execute(CommandContext context, String[] args, String[] fullArgs) {
root = Path.of(context.bot().music().SONG_DIR.getPath()); root = Path.of(MusicPlayerPlugin.SONG_DIR.getPath());
switch (args[0]) { switch (args[0]) {
case "play" -> { case "play", "playurl", "playnbs", "playnbsurl" -> {
return play(context, args); return play(context, args);
} }
case "stop" -> stop(context); case "stop" -> stop(context);
@ -88,6 +92,9 @@ public class MusicCommand implements Command {
case "pause", "resume" -> { case "pause", "resume" -> {
return pause(context); return pause(context);
} }
case "info" -> {
return info(context);
}
default -> { default -> {
return Component.text("Invalid argument").color(NamedTextColor.RED); return Component.text("Invalid argument").color(NamedTextColor.RED);
} }
@ -378,4 +385,33 @@ public class MusicCommand implements Command {
return Component.text("success"); return Component.text("success");
} }
public Component info (CommandContext context) {
final Bot bot = context.bot();
final Song currentSong = bot.music().currentSong();
final String fileName = bot.music().fileName();
if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED);
// ig very code yup
final String songAuthor = currentSong.songAuthor == null || currentSong.songAuthor.equals("") ? "N/A" : currentSong.songAuthor;
final String songOriginalAuthor = currentSong.songOriginalAuthor == null || currentSong.songOriginalAuthor.equals("") ? "N/A" : currentSong.songOriginalAuthor;
final String songDescription = currentSong.songDescription == null || currentSong.songDescription.equals("") ? "N/A" : currentSong.songDescription;
final Component component = Component.translatable(
"""
Filename: %s
Author: %s
Original author: %s
Description: %s""",
Component.text(fileName).color(NamedTextColor.AQUA),
Component.text(songAuthor).color(NamedTextColor.AQUA),
Component.text(songOriginalAuthor).color(NamedTextColor.AQUA),
Component.text(songDescription).color(NamedTextColor.AQUA)
).color(NamedTextColor.GOLD);
context.sendOutput(component);
return Component.text("success");
}
} }

View file

@ -34,6 +34,8 @@ public class MusicPlayerPlugin extends SessionAdapter {
} }
} }
@Getter private String fileName;
@Getter @Setter private Song currentSong; @Getter @Setter private Song currentSong;
@Getter @Setter private LinkedList<Song> songQueue = new LinkedList<>(); @Getter @Setter private LinkedList<Song> songQueue = new LinkedList<>();
@Getter @Setter private SongLoaderThread loaderThread; @Getter @Setter private SongLoaderThread loaderThread;
@ -57,7 +59,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
public void loadSong (Path location) { public void loadSong (Path location) {
if (loaderThread != null) { if (loaderThread != null) {
bot.chat().tellraw(Component.translatable("Already loading a song, can't load another", NamedTextColor.RED)); bot.chat().tellraw(Component.translatable("Already loading a song", NamedTextColor.RED));
return; return;
} }
@ -97,6 +99,8 @@ public class MusicPlayerPlugin extends SessionAdapter {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED)); bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED));
} else { } else {
songQueue.add(loaderThread.song); songQueue.add(loaderThread.song);
fileName = loaderThread.fileName;
System.out.println(fileName + " " + loaderThread.fileName);
bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD))); bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD)));
} }
loaderThread = null; loaderThread = null;
@ -134,6 +138,8 @@ public class MusicPlayerPlugin extends SessionAdapter {
if (currentSong.finished()) { if (currentSong.finished()) {
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
fileName = null;
if (loop == Loop.CURRENT) { if (loop == Loop.CURRENT) {
currentSong.setTime(0); currentSong.setTime(0);
return; return;
@ -258,20 +264,21 @@ public class MusicPlayerPlugin extends SessionAdapter {
while (currentSong.reachedNextNote()) { while (currentSong.reachedNextNote()) {
final Note note = currentSong.getNextNote(); final Note note = currentSong.getNextNote();
final boolean nbs = currentSong.nbs;
float key = note.pitch; float key = note.pitch;
if (key < 33) key -= 9; if (nbs) {
else if (key > 57) key -= 57; if (key < 33) key -= 9;
else key -= 33; else if (key > 57) key -= 57;
else key -= 33;
}
final double floatingPitch = Math.pow( final double floatingPitch = Math.pow(
2, 2,
(key + (pitch / 10)) / 12 (key + (pitch / 10)) / 12
); );
// if the thing is still out of range just ignore and don't play it!1!1
if (floatingPitch < -1 || floatingPitch > 3) continue;
bot.core().run( bot.core().run(
"minecraft:execute as " + "minecraft:execute as " +
SELECTOR + SELECTOR +

View file

@ -2,12 +2,12 @@ package land.chipmunk.chayapak.chomens_bot.song;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.sound.midi.*; import javax.sound.midi.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
public class MidiConverter { public class MidiConverter {
public static final int SET_INSTRUMENT = 0xC0; public static final int SET_INSTRUMENT = 0xC0;
@ -31,7 +31,7 @@ public class MidiConverter {
} }
public static Song getSong(Sequence sequence, String name, Bot bot) { public static Song getSong(Sequence sequence, String name, Bot bot) {
Song song = new Song(name, bot); Song song = new Song(name, bot, null, null, null, null, false);
long tpq = sequence.getResolution(); long tpq = sequence.getResolution();
@ -40,8 +40,7 @@ public class MidiConverter {
for (int i = 0; i < track.size(); i++) { for (int i = 0; i < track.size(); i++) {
MidiEvent event = track.get(i); MidiEvent event = track.get(i);
MidiMessage message = event.getMessage(); MidiMessage message = event.getMessage();
if (message instanceof MetaMessage) { if (message instanceof MetaMessage mm) {
MetaMessage mm = (MetaMessage) message;
if (mm.getType() == SET_TEMPO) { if (mm.getType() == SET_TEMPO) {
tempoEvents.add(event); tempoEvents.add(event);
} }
@ -49,7 +48,7 @@ public class MidiConverter {
} }
} }
Collections.sort(tempoEvents, (a, b) -> Long.compare(a.getTick(), b.getTick())); tempoEvents.sort(Comparator.comparingLong(MidiEvent::getTick));
for (Track track : sequence.getTracks()) { for (Track track : sequence.getTracks()) {
@ -75,8 +74,7 @@ public class MidiConverter {
tempoEventIdx++; tempoEventIdx++;
} }
if (message instanceof ShortMessage) { if (message instanceof ShortMessage sm) {
ShortMessage sm = (ShortMessage) message;
if (sm.getCommand() == SET_INSTRUMENT) { if (sm.getCommand() == SET_INSTRUMENT) {
ids[sm.getChannel()] = sm.getData1(); ids[sm.getChannel()] = sm.getData1();
} }

View file

@ -145,12 +145,12 @@ public class NBSConverter {
customInstrument.name = getString(buffer, bytes.length); customInstrument.name = getString(buffer, bytes.length);
customInstrument.file = getString(buffer, bytes.length); customInstrument.file = getString(buffer, bytes.length);
customInstrument.pitch = buffer.get(); customInstrument.pitch = buffer.get();
customInstrument.key = buffer.get() == 0 ? false : true; customInstrument.key = buffer.get() != 0;
customInstruments.add(customInstrument); customInstruments.add(customInstrument);
} }
} }
Song song = new Song(songName.trim().length() > 0 ? songName : fileName, bot); Song song = new Song(songName.trim().length() > 0 ? songName : fileName, bot, songName, songAuthor, songOriginalAuthor, songDescription, true);
if (loop > 0) { if (loop > 0) {
song.loopPosition = getMilliTime(loopStartTick, tempo); song.loopPosition = getMilliTime(loopStartTick, tempo);
// song.loopCount = maxLoopCount; // song.loopCount = maxLoopCount;
@ -189,7 +189,7 @@ public class NBSConverter {
if (length > maxSize) { if (length > maxSize) {
throw new IOException("String is too large"); throw new IOException("String is too large");
} }
byte arr[] = new byte[length]; byte[] arr = new byte[length];
buffer.get(arr, 0, length); buffer.get(arr, 0, length);
return new String(arr); return new String(arr);
} }

View file

@ -14,18 +14,31 @@ public class Song {
public long length = 0; // Milliseconds in the song public long length = 0; // Milliseconds in the song
public long time = 0; // Time since start of song public long time = 0; // Time since start of song
public long loopPosition = 200; // Milliseconds into the song to start looping public long loopPosition = 200; // Milliseconds into the song to start looping
public String songName;
public String songAuthor;
public String songOriginalAuthor;
public String songDescription;
public boolean nbs;
// public int loopCount = 0; // Number of times to loop // public int loopCount = 0; // Number of times to loop
// public int currentLoop = 0; // Number of loops so far // public int currentLoop = 0; // Number of loops so far
private Bot bot; private Bot bot;
public Song (Component name, Bot bot) { public Song (Component name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) {
this.bot = bot; this.bot = bot;
this.name = name; this.name = name;
this.songName = songName;
this.songAuthor = songAuthor;
this.songOriginalAuthor = songOriginalAuthor;
this.songDescription = songDescription;
this.nbs = nbs;
} }
public Song (String name, Bot bot) { public Song (String name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) {
this(Component.text(name), bot); this(Component.text(name), bot, songName, songAuthor, songOriginalAuthor, songDescription, nbs);
this.bot = bot; this.bot = bot;
} }

View file

@ -11,26 +11,31 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
public class SongLoaderThread extends Thread { public class SongLoaderThread extends Thread {
private String location; public String fileName;
private File songPath; private File songPath;
private URL songUrl; private URL songUrl;
public SongLoaderException exception; public SongLoaderException exception;
public Song song; public Song song;
private Bot bot; private final Bot bot;
private boolean isUrl = false; private final boolean isUrl;
public SongLoaderThread (URL location, Bot bot) throws SongLoaderException { public SongLoaderThread (URL location, Bot bot) throws SongLoaderException {
this.bot = bot; this.bot = bot;
isUrl = true; isUrl = true;
songUrl = location; songUrl = location;
fileName = location.getFile();
} }
public SongLoaderThread (Path location, Bot bot) throws SongLoaderException { public SongLoaderThread (Path location, Bot bot) throws SongLoaderException {
this.bot = bot; this.bot = bot;
isUrl = false; isUrl = false;
songPath = location.toFile(); songPath = location.toFile();
fileName = location.getFileName().toString();
} }
public void run () { public void run () {