forked from ChomeNS/chomens-bot-java
music load from item
very useful i think i'm the first to do this
This commit is contained in:
parent
18ade1875d
commit
3d53d7a023
3 changed files with 104 additions and 4 deletions
|
@ -1,5 +1,7 @@
|
||||||
package land.chipmunk.chayapak.chomens_bot.commands;
|
package land.chipmunk.chayapak.chomens_bot.commands;
|
||||||
|
|
||||||
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
||||||
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
||||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||||
import land.chipmunk.chayapak.chomens_bot.Main;
|
import land.chipmunk.chayapak.chomens_bot.Main;
|
||||||
import land.chipmunk.chayapak.chomens_bot.command.Command;
|
import land.chipmunk.chayapak.chomens_bot.command.Command;
|
||||||
|
@ -11,14 +13,14 @@ import land.chipmunk.chayapak.chomens_bot.song.Instrument;
|
||||||
import land.chipmunk.chayapak.chomens_bot.song.Loop;
|
import land.chipmunk.chayapak.chomens_bot.song.Loop;
|
||||||
import land.chipmunk.chayapak.chomens_bot.song.Note;
|
import land.chipmunk.chayapak.chomens_bot.song.Note;
|
||||||
import land.chipmunk.chayapak.chomens_bot.song.Song;
|
import land.chipmunk.chayapak.chomens_bot.song.Song;
|
||||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
import land.chipmunk.chayapak.chomens_bot.util.*;
|
||||||
import land.chipmunk.chayapak.chomens_bot.util.PathUtilities;
|
|
||||||
import land.chipmunk.chayapak.chomens_bot.util.TimestampUtilities;
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.JoinConfiguration;
|
import net.kyori.adventure.text.JoinConfiguration;
|
||||||
|
import net.kyori.adventure.text.TranslatableComponent;
|
||||||
import net.kyori.adventure.text.event.ClickEvent;
|
import net.kyori.adventure.text.event.ClickEvent;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import net.kyori.adventure.text.format.TextColor;
|
import net.kyori.adventure.text.format.TextColor;
|
||||||
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
@ -26,7 +28,9 @@ import java.net.URI;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class MusicCommand extends Command {
|
public class MusicCommand extends Command {
|
||||||
|
@ -74,6 +78,7 @@ public class MusicCommand extends Command {
|
||||||
root = MusicPlayerPlugin.SONG_DIR;
|
root = MusicPlayerPlugin.SONG_DIR;
|
||||||
return switch (action) {
|
return switch (action) {
|
||||||
case "play", "playurl", "playnbs", "playnbsurl" -> play(context);
|
case "play", "playurl", "playnbs", "playnbsurl" -> play(context);
|
||||||
|
case "playfromitem", "playitem" -> playFromItem(context);
|
||||||
case "stop" -> stop(context);
|
case "stop" -> stop(context);
|
||||||
case "loop" -> loop(context);
|
case "loop" -> loop(context);
|
||||||
case "list" -> list(context);
|
case "list" -> list(context);
|
||||||
|
@ -190,6 +195,67 @@ public class MusicCommand extends Command {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Component playFromItem (CommandContext context) throws CommandException {
|
||||||
|
// mail command lol
|
||||||
|
|
||||||
|
final Bot bot = context.bot;
|
||||||
|
|
||||||
|
final CompletableFuture<CompoundTag> future = bot.core.runTracked(
|
||||||
|
"minecraft:data get entity " +
|
||||||
|
UUIDUtilities.selector(context.sender.profile.getId()) +
|
||||||
|
" SelectedItem.tag.data"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (future == null) {
|
||||||
|
throw new CommandException(Component.text("There was an error while getting your data"));
|
||||||
|
}
|
||||||
|
|
||||||
|
future.thenApply(tags -> {
|
||||||
|
if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags;
|
||||||
|
|
||||||
|
final StringTag lastOutput = tags.get("LastOutput");
|
||||||
|
|
||||||
|
final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue());
|
||||||
|
|
||||||
|
final List<Component> children = output.children();
|
||||||
|
|
||||||
|
if (
|
||||||
|
!children.isEmpty() &&
|
||||||
|
!children.get(0).children().isEmpty() &&
|
||||||
|
((TranslatableComponent) children.get(0).children().get(0))
|
||||||
|
.key()
|
||||||
|
.equals("arguments.nbtpath.nothing_found")
|
||||||
|
) {
|
||||||
|
context.sendOutput(Component.text("Player has no `data` NBT tag in the selected item").color(NamedTextColor.RED));
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String value = ComponentUtilities.stringify(((TranslatableComponent) children.get(0)).args().get(1));
|
||||||
|
|
||||||
|
if (!value.startsWith("\"") && !value.endsWith("\"") && !value.startsWith("'") && !value.endsWith("'")) {
|
||||||
|
context.sendOutput(Component.text("`data` NBT is not a string").color(NamedTextColor.RED));
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
bot.music.loadSong(
|
||||||
|
Base64.getDecoder().decode(
|
||||||
|
value
|
||||||
|
.substring(1)
|
||||||
|
.substring(0, value.length() - 2)
|
||||||
|
),
|
||||||
|
context.sender
|
||||||
|
);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
context.sendOutput(Component.text("Invalid base64 in the selected item").color(NamedTextColor.RED));
|
||||||
|
}
|
||||||
|
|
||||||
|
return tags;
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public Component stop (CommandContext context) {
|
public Component stop (CommandContext context) {
|
||||||
final Bot bot = context.bot;
|
final Bot bot = context.bot;
|
||||||
bot.music.stopPlaying();
|
bot.music.stopPlaying();
|
||||||
|
|
|
@ -114,6 +114,23 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
||||||
loaderThread.start();
|
loaderThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadSong (byte[] data, PlayerEntry sender) {
|
||||||
|
if (songQueue.size() > 100) return;
|
||||||
|
|
||||||
|
loaderThread = new SongLoaderThread(data, bot, sender.profile.getName());
|
||||||
|
|
||||||
|
bot.chat.tellraw(
|
||||||
|
Component
|
||||||
|
.translatable(
|
||||||
|
"Loading %s",
|
||||||
|
Component.text(sender.profile.getName() + "'s song item", ColorUtilities.getColorByString(bot.config.colorPalette.secondary))
|
||||||
|
)
|
||||||
|
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
|
||||||
|
);
|
||||||
|
|
||||||
|
loaderThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
public void coreReady () {
|
public void coreReady () {
|
||||||
bot.tick.addListener(new TickPlugin.Listener() {
|
bot.tick.addListener(new TickPlugin.Listener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -40,6 +40,10 @@ public class SongLoaderThread extends Thread {
|
||||||
|
|
||||||
private final boolean isUrl;
|
private final boolean isUrl;
|
||||||
|
|
||||||
|
private byte[] data;
|
||||||
|
|
||||||
|
private boolean isItem = false;
|
||||||
|
|
||||||
private boolean isFolder = false;
|
private boolean isFolder = false;
|
||||||
|
|
||||||
public SongLoaderThread(URL location, Bot bot, String requester) {
|
public SongLoaderThread(URL location, Bot bot, String requester) {
|
||||||
|
@ -62,9 +66,19 @@ public class SongLoaderThread extends Thread {
|
||||||
fileName = location.getFileName().toString();
|
fileName = location.getFileName().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SongLoaderThread (byte[] data, Bot bot, String requester) {
|
||||||
|
this.bot = bot;
|
||||||
|
this.requester = requester;
|
||||||
|
this.data = data;
|
||||||
|
this.isItem = true;
|
||||||
|
this.isUrl = false;
|
||||||
|
|
||||||
|
fileName = requester + "'s song item";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run () {
|
public void run () {
|
||||||
if (isFolder && !isUrl) {
|
if (isFolder && !isUrl && !isItem) {
|
||||||
try (Stream<Path> files = Files.list(songPath)) {
|
try (Stream<Path> files = Files.list(songPath)) {
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
files.forEach((file) -> {
|
files.forEach((file) -> {
|
||||||
|
@ -91,6 +105,9 @@ public class SongLoaderThread extends Thread {
|
||||||
final Path fileName = Paths.get(songUrl.toURI().getPath()).getFileName();
|
final Path fileName = Paths.get(songUrl.toURI().getPath()).getFileName();
|
||||||
|
|
||||||
name = fileName == null ? "(root)" : fileName.toString();
|
name = fileName == null ? "(root)" : fileName.toString();
|
||||||
|
} else if (isItem) {
|
||||||
|
bytes = data;
|
||||||
|
name = requester + "'s song item";
|
||||||
} else {
|
} else {
|
||||||
bytes = Files.readAllBytes(songPath);
|
bytes = Files.readAllBytes(songPath);
|
||||||
name = !isFolder ? fileName : songPath.getFileName().toString();
|
name = !isFolder ? fileName : songPath.getFileName().toString();
|
||||||
|
|
Loading…
Reference in a new issue