improve music info + add song requester

This commit is contained in:
Chayapak 2023-09-25 16:37:02 +07:00
parent ee27f874c0
commit 5856429708
4 changed files with 29 additions and 25 deletions

View file

@ -18,6 +18,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.JoinConfiguration;
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 java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -102,7 +103,7 @@ public class MusicCommand extends Command {
path = Path.of(root.toString(), _path); path = Path.of(root.toString(), _path);
if (path.toString().contains("http")) player.loadSong(new URL(_path)); if (path.toString().contains("http")) player.loadSong(new URL(_path), context.sender);
else { else {
// among us protection!!!11 // among us protection!!!11
if (!path.normalize().startsWith(root.toString())) throw new CommandException(Component.text("no")); if (!path.normalize().startsWith(root.toString())) throw new CommandException(Component.text("no"));
@ -137,7 +138,7 @@ public class MusicCommand extends Command {
final String file = matchedArray[0]; final String file = matchedArray[0];
player.loadSong(Path.of(realPath.toString(), file)); player.loadSong(Path.of(realPath.toString(), file), context.sender);
} catch (CommandException e) { } catch (CommandException e) {
throw e; throw e;
} catch (NoSuchFileException e) { } catch (NoSuchFileException e) {
@ -163,7 +164,7 @@ public class MusicCommand extends Command {
final String file = matchedArray[0]; final String file = matchedArray[0];
player.loadSong(Path.of(root.toString(), file)); player.loadSong(Path.of(root.toString(), file), context.sender);
} catch (CommandException e) { } catch (CommandException e) {
throw e; throw e;
} catch (NoSuchFileException e) { } catch (NoSuchFileException e) {
@ -433,23 +434,18 @@ public class MusicCommand extends Command {
if (currentSong == null) throw new CommandException(Component.text("No song is currently playing")); if (currentSong == null) throw new CommandException(Component.text("No song is currently playing"));
// ig very code yup final List<Component> components = new ArrayList<>();
final String title = currentSong.name;
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;
return Component.translatable( final TextColor keyColor = ColorUtilities.getColorByString(bot.config.colorPalette.secondary);
""" final TextColor valueColor = ColorUtilities.getColorByString(bot.config.colorPalette.string);
Title/Filename: %s
Author: %s if (currentSong.name != null) components.add(Component.translatable("Title/Filename: %s", Component.text(currentSong.name).color(valueColor)).color(keyColor));
Original author: %s if (currentSong.requester != null) components.add(Component.translatable("Requester: %s", Component.text(currentSong.requester).color(valueColor)).color(keyColor));
Description: %s""", if (currentSong.songAuthor != null && !currentSong.songAuthor.isBlank()) components.add(Component.translatable("Author: %s", Component.text(currentSong.songAuthor).color(valueColor)).color(keyColor));
Component.text(title).color(NamedTextColor.AQUA), if (currentSong.songOriginalAuthor != null && !currentSong.songOriginalAuthor.isBlank()) components.add(Component.translatable("Original author: %s", Component.text(currentSong.songOriginalAuthor).color(valueColor)).color(keyColor));
Component.text(songAuthor).color(NamedTextColor.AQUA), if (currentSong.songDescription != null && !currentSong.songDescription.isBlank()) components.add(Component.translatable("Description: %s", Component.text(currentSong.songDescription).color(valueColor)).color(keyColor));
Component.text(songOriginalAuthor).color(NamedTextColor.AQUA),
Component.text(songDescription).color(NamedTextColor.AQUA) return Component.join(JoinConfiguration.newlines(), components);
).color(NamedTextColor.GOLD);
} }
public Component testSong (CommandContext context) { public Component testSong (CommandContext context) {

View file

@ -5,6 +5,7 @@ import com.github.steveice10.mc.protocol.data.game.BossBarDivision;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.BotBossBar; import land.chipmunk.chayapak.chomens_bot.data.BotBossBar;
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry;
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;
@ -67,10 +68,10 @@ public class MusicPlayerPlugin extends Bot.Listener {
bot.executor.scheduleAtFixedRate(() -> limit = 0, 0, bot.config.music.urlRatelimit.seconds, TimeUnit.SECONDS); bot.executor.scheduleAtFixedRate(() -> limit = 0, 0, bot.config.music.urlRatelimit.seconds, TimeUnit.SECONDS);
} }
public void loadSong (Path location) { public void loadSong (Path location, PlayerEntry sender) {
if (songQueue.size() > 100) return; if (songQueue.size() > 100) return;
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot); final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot, sender.profile.getName());
bot.chat.tellraw( bot.chat.tellraw(
Component Component
@ -84,7 +85,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
bot.executorService.submit(runnable); bot.executorService.submit(runnable);
} }
public void loadSong (URL location) { public void loadSong (URL location, PlayerEntry sender) {
if (songQueue.size() > 100) return; if (songQueue.size() > 100) return;
limit++; limit++;
@ -94,7 +95,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
return; return;
} }
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot); final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot, sender.profile.getName());
bot.chat.tellraw( bot.chat.tellraw(
Component Component

View file

@ -10,6 +10,7 @@ public class Song {
public final ArrayList<Note> notes = new ArrayList<>(); public final ArrayList<Note> notes = new ArrayList<>();
public final String originalName; public final String originalName;
public final String name; public final String name;
public String requester = "Unknown";
public int position = 0; // Current note index public int position = 0; // Current note index
public boolean paused = true; public boolean paused = true;
public long startTime = 0; // Start time in millis since unix epoch public long startTime = 0; // Start time in millis since unix epoch

View file

@ -36,20 +36,24 @@ public class SongLoaderRunnable implements Runnable {
private final Bot bot; private final Bot bot;
private final String requester;
private final boolean isUrl; private final boolean isUrl;
private boolean isFolder = false; private boolean isFolder = false;
public SongLoaderRunnable(URL location, Bot bot) { public SongLoaderRunnable(URL location, Bot bot, String requester) {
this.bot = bot; this.bot = bot;
this.requester = requester;
isUrl = true; isUrl = true;
songUrl = location; songUrl = location;
fileName = location.getFile(); fileName = location.getFile();
} }
public SongLoaderRunnable(Path location, Bot bot) { public SongLoaderRunnable(Path location, Bot bot, String requester) {
this.bot = bot; this.bot = bot;
this.requester = requester;
isUrl = false; isUrl = false;
songPath = location; songPath = location;
@ -109,6 +113,8 @@ public class SongLoaderRunnable implements Runnable {
failed(); failed();
} else { } else {
song.requester = requester;
bot.music.songQueue.add(song); bot.music.songQueue.add(song);
if (!isFolder) showAddedToQueue(); if (!isFolder) showAddedToQueue();