forked from ChomeNS/chomens-bot-java
add stuff fix stuff improve stuff
This commit is contained in:
parent
a96d797766
commit
e6ef185e9f
6 changed files with 89 additions and 30 deletions
|
@ -16,7 +16,10 @@ import java.net.MalformedURLException;
|
|||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
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 {
|
||||
private Path root;
|
||||
|
@ -41,6 +44,7 @@ public class MusicCommand implements Command {
|
|||
usages.add("speed <speed>");
|
||||
usages.add("pause");
|
||||
usages.add("resume");
|
||||
usages.add("info");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
@ -57,9 +61,9 @@ public class MusicCommand implements Command {
|
|||
}
|
||||
|
||||
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]) {
|
||||
case "play" -> {
|
||||
case "play", "playurl", "playnbs", "playnbsurl" -> {
|
||||
return play(context, args);
|
||||
}
|
||||
case "stop" -> stop(context);
|
||||
|
@ -88,6 +92,9 @@ public class MusicCommand implements Command {
|
|||
case "pause", "resume" -> {
|
||||
return pause(context);
|
||||
}
|
||||
case "info" -> {
|
||||
return info(context);
|
||||
}
|
||||
default -> {
|
||||
return Component.text("Invalid argument").color(NamedTextColor.RED);
|
||||
}
|
||||
|
@ -378,4 +385,33 @@ public class MusicCommand implements Command {
|
|||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,8 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
@Getter private String fileName;
|
||||
|
||||
@Getter @Setter private Song currentSong;
|
||||
@Getter @Setter private LinkedList<Song> songQueue = new LinkedList<>();
|
||||
@Getter @Setter private SongLoaderThread loaderThread;
|
||||
|
@ -57,7 +59,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
|
||||
public void loadSong (Path location) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
} else {
|
||||
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)));
|
||||
}
|
||||
loaderThread = null;
|
||||
|
@ -134,6 +138,8 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
if (currentSong.finished()) {
|
||||
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
|
||||
|
||||
fileName = null;
|
||||
|
||||
if (loop == Loop.CURRENT) {
|
||||
currentSong.setTime(0);
|
||||
return;
|
||||
|
@ -258,20 +264,21 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
while (currentSong.reachedNextNote()) {
|
||||
final Note note = currentSong.getNextNote();
|
||||
|
||||
final boolean nbs = currentSong.nbs;
|
||||
|
||||
float key = note.pitch;
|
||||
|
||||
if (key < 33) key -= 9;
|
||||
else if (key > 57) key -= 57;
|
||||
else key -= 33;
|
||||
if (nbs) {
|
||||
if (key < 33) key -= 9;
|
||||
else if (key > 57) key -= 57;
|
||||
else key -= 33;
|
||||
}
|
||||
|
||||
final double floatingPitch = Math.pow(
|
||||
2,
|
||||
(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(
|
||||
"minecraft:execute as " +
|
||||
SELECTOR +
|
||||
|
|
|
@ -2,12 +2,12 @@ package land.chipmunk.chayapak.chomens_bot.song;
|
|||
|
||||
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 java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class MidiConverter {
|
||||
public static final int SET_INSTRUMENT = 0xC0;
|
||||
|
@ -31,7 +31,7 @@ public class MidiConverter {
|
|||
}
|
||||
|
||||
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();
|
||||
|
||||
|
@ -40,8 +40,7 @@ public class MidiConverter {
|
|||
for (int i = 0; i < track.size(); i++) {
|
||||
MidiEvent event = track.get(i);
|
||||
MidiMessage message = event.getMessage();
|
||||
if (message instanceof MetaMessage) {
|
||||
MetaMessage mm = (MetaMessage) message;
|
||||
if (message instanceof MetaMessage mm) {
|
||||
if (mm.getType() == SET_TEMPO) {
|
||||
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()) {
|
||||
|
||||
|
@ -75,8 +74,7 @@ public class MidiConverter {
|
|||
tempoEventIdx++;
|
||||
}
|
||||
|
||||
if (message instanceof ShortMessage) {
|
||||
ShortMessage sm = (ShortMessage) message;
|
||||
if (message instanceof ShortMessage sm) {
|
||||
if (sm.getCommand() == SET_INSTRUMENT) {
|
||||
ids[sm.getChannel()] = sm.getData1();
|
||||
}
|
||||
|
|
|
@ -145,12 +145,12 @@ public class NBSConverter {
|
|||
customInstrument.name = getString(buffer, bytes.length);
|
||||
customInstrument.file = getString(buffer, bytes.length);
|
||||
customInstrument.pitch = buffer.get();
|
||||
customInstrument.key = buffer.get() == 0 ? false : true;
|
||||
customInstrument.key = buffer.get() != 0;
|
||||
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) {
|
||||
song.loopPosition = getMilliTime(loopStartTick, tempo);
|
||||
// song.loopCount = maxLoopCount;
|
||||
|
@ -189,7 +189,7 @@ public class NBSConverter {
|
|||
if (length > maxSize) {
|
||||
throw new IOException("String is too large");
|
||||
}
|
||||
byte arr[] = new byte[length];
|
||||
byte[] arr = new byte[length];
|
||||
buffer.get(arr, 0, length);
|
||||
return new String(arr);
|
||||
}
|
||||
|
|
|
@ -14,18 +14,31 @@ public class Song {
|
|||
public long length = 0; // Milliseconds in the song
|
||||
public long time = 0; // Time since start of song
|
||||
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 currentLoop = 0; // Number of loops so far
|
||||
|
||||
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.name = name;
|
||||
this.songName = songName;
|
||||
this.songAuthor = songAuthor;
|
||||
this.songOriginalAuthor = songOriginalAuthor;
|
||||
this.songDescription = songDescription;
|
||||
this.nbs = nbs;
|
||||
}
|
||||
|
||||
public Song (String name, Bot bot) {
|
||||
this(Component.text(name), bot);
|
||||
public Song (String name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) {
|
||||
this(Component.text(name), bot, songName, songAuthor, songOriginalAuthor, songDescription, nbs);
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,26 +11,31 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Paths;
|
||||
|
||||
public class SongLoaderThread extends Thread {
|
||||
private String location;
|
||||
public String fileName;
|
||||
|
||||
private File songPath;
|
||||
private URL songUrl;
|
||||
public SongLoaderException exception;
|
||||
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 {
|
||||
this.bot = bot;
|
||||
isUrl = true;
|
||||
songUrl = location;
|
||||
|
||||
fileName = location.getFile();
|
||||
}
|
||||
|
||||
public SongLoaderThread (Path location, Bot bot) throws SongLoaderException {
|
||||
this.bot = bot;
|
||||
isUrl = false;
|
||||
songPath = location.toFile();
|
||||
|
||||
fileName = location.getFileName().toString();
|
||||
}
|
||||
|
||||
public void run () {
|
||||
|
|
Loading…
Reference in a new issue