refactor: send music loading messages to the context instead of tellraw

This commit is contained in:
ChomeNS
2025-05-10 16:48:55 +07:00
parent f497651043
commit e9cc4f1499
5 changed files with 74 additions and 75 deletions

View File

@@ -1 +1 @@
3181
3185

View File

@@ -121,7 +121,7 @@ public class MusicCommand extends Command implements Listener {
try {
path = Path.of(ROOT.toString(), stringPath);
if (path.toString().contains("http")) player.loadSong(new URI(stringPath).toURL(), context.sender);
if (path.toString().contains("http")) player.loadSong(new URI(stringPath).toURL(), context);
else {
// among us protection!!!11
if (!path.normalize().startsWith(ROOT.toString())) throw new CommandException(Component.text("no"));
@@ -156,7 +156,7 @@ public class MusicCommand extends Command implements Listener {
final String file = matchedArray[0];
player.loadSong(Path.of(realPath.toString(), file), context.sender);
player.loadSong(Path.of(realPath.toString(), file), context);
} catch (final NoSuchFileException e) {
throw new CommandException(Component.translatable("commands.music.error.no_directory"));
}
@@ -178,7 +178,7 @@ public class MusicCommand extends Command implements Listener {
final String file = matchedArray[0];
player.loadSong(Path.of(ROOT.toString(), file), context.sender);
player.loadSong(Path.of(ROOT.toString(), file), context);
} catch (final NoSuchFileException e) {
throw new CommandException(Component.text("this will never happen ok??"));
}
@@ -216,13 +216,13 @@ public class MusicCommand extends Command implements Listener {
try {
bot.music.loadSong(
Base64.getDecoder().decode(output),
context.sender
context
);
} catch (final IllegalArgumentException e) {
try {
bot.music.loadSong(
Ascii85.decode(output),
context.sender
context
);
} catch (final IllegalArgumentException e2) {
context.sendOutput(Component.translatable("commands.music.playitem.invalid_data", NamedTextColor.RED));
@@ -571,8 +571,8 @@ public class MusicCommand extends Command implements Listener {
if (isNotNullAndNotBlank(currentSong.name))
components.add(Component.translatable("commands.music.info.title", keyColor, Component.text(currentSong.name, valueColor)));
if (isNotNullAndNotBlank(currentSong.requester))
components.add(Component.translatable("commands.music.info.requester", keyColor, Component.text(currentSong.requester, valueColor)));
if (currentSong.context != null && isNotNullAndNotBlank(currentSong.context.sender.profile.getName()))
components.add(Component.translatable("commands.music.info.requester", keyColor, Component.text(currentSong.context.sender.profile.getName(), valueColor)));
if (isNotNullAndNotBlank(currentSong.songAuthor))
components.add(Component.translatable("commands.music.info.author", keyColor, Component.text(currentSong.songAuthor, valueColor)));
if (isNotNullAndNotBlank(currentSong.songOriginalAuthor))

View File

@@ -2,14 +2,13 @@ package me.chayapak1.chomens_bot.plugins;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.data.bossbar.BotBossBar;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
import me.chayapak1.chomens_bot.song.Loop;
import me.chayapak1.chomens_bot.song.Note;
import me.chayapak1.chomens_bot.song.Song;
import me.chayapak1.chomens_bot.song.SongLoaderThread;
import me.chayapak1.chomens_bot.util.I18nUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import me.chayapak1.chomens_bot.util.MathUtilities;
import net.kyori.adventure.text.Component;
@@ -88,25 +87,23 @@ public class MusicPlayerPlugin implements Listener {
bot.executor.scheduleAtFixedRate(() -> urlLimit = 0, 0, bot.config.music.urlRatelimit.seconds, TimeUnit.SECONDS);
}
public void loadSong (final Path location, final PlayerEntry sender) {
public void loadSong (final Path location, final CommandContext context) {
startLoadingSong(
location.getFileName().toString(),
new SongLoaderThread(location, bot, sender.profile.getName())
new SongLoaderThread(location, bot, context)
);
}
public void loadSong (final byte[] data, final PlayerEntry sender) {
public void loadSong (final byte[] data, final CommandContext context) {
startLoadingSong(
sender.profile.getName() + "'s song item",
new SongLoaderThread(data, bot, sender.profile.getName())
context.sender.profile.getName() + "'s song item",
new SongLoaderThread(data, bot, context)
);
}
public void loadSong (final URL location, final PlayerEntry sender) {
public void loadSong (final URL location, final CommandContext context) {
if (urlLimit >= bot.config.music.urlRatelimit.limit) {
bot.chat.tellraw(I18nUtilities.render(
Component.translatable("commands.music.error.url_ratelimited", NamedTextColor.RED)
));
context.sendOutput(Component.translatable("commands.music.error.url_ratelimited", NamedTextColor.RED));
return;
}
@@ -114,7 +111,7 @@ public class MusicPlayerPlugin implements Listener {
startLoadingSong(
location.toString(),
new SongLoaderThread(location, bot, sender.profile.getName())
new SongLoaderThread(location, bot, context)
);
}
@@ -123,14 +120,13 @@ public class MusicPlayerPlugin implements Listener {
this.loaderThread = loaderThread;
bot.chat.tellraw(I18nUtilities.render(
Component
.translatable(
"commands.music.loading",
Component.text(songName, bot.colorPalette.secondary)
)
.color(bot.colorPalette.defaultColor)),
BOTH_SELECTOR
loaderThread.context.sendOutput(
Component
.translatable(
"commands.music.loading",
bot.colorPalette.defaultColor,
Component.text(songName, bot.colorPalette.secondary)
)
);
this.loaderThread.start();
@@ -159,13 +155,12 @@ public class MusicPlayerPlugin implements Listener {
addBossBar();
currentSong = songQueue.getFirst(); // songQueue.poll();
bot.chat.tellraw(I18nUtilities.render(
Component.translatable(
"commands.music.nowplaying",
bot.colorPalette.defaultColor,
Component.empty().append(Component.text(currentSong.name)).color(bot.colorPalette.secondary)
)),
BOTH_SELECTOR
currentSong.context.sendOutput(
Component.translatable(
"commands.music.nowplaying",
bot.colorPalette.defaultColor,
Component.empty().append(Component.text(currentSong.name)).color(bot.colorPalette.secondary)
)
);
currentSong.play();
}
@@ -198,13 +193,12 @@ public class MusicPlayerPlugin implements Listener {
return;
}
bot.chat.tellraw(I18nUtilities.render(
Component.translatable(
"commands.music.finished",
bot.colorPalette.defaultColor,
Component.empty().append(Component.text(currentSong.name)).color(bot.colorPalette.secondary)
)),
BOTH_SELECTOR
currentSong.context.sendOutput(
Component.translatable(
"commands.music.finished",
bot.colorPalette.defaultColor,
Component.empty().append(Component.text(currentSong.name)).color(bot.colorPalette.secondary)
)
);
if (loop == Loop.ALL) {

View File

@@ -3,6 +3,7 @@ package me.chayapak1.chomens_bot.song;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.CommandContext;
import java.util.Collections;
import java.util.List;
@@ -17,7 +18,7 @@ public class Song {
public final String originalName;
public String name;
public String requester = "Unknown";
public CommandContext context = null;
public int position = 0; // Current note index
public boolean paused = true;

View File

@@ -2,8 +2,8 @@ package me.chayapak1.chomens_bot.song;
import it.unimi.dsi.fastutil.objects.ObjectList;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.util.DownloadUtilities;
import me.chayapak1.chomens_bot.util.I18nUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@@ -34,7 +34,7 @@ public class SongLoaderThread extends Thread {
private final Bot bot;
private final String requester;
public final CommandContext context;
private final boolean isUrl;
@@ -44,34 +44,44 @@ public class SongLoaderThread extends Thread {
private boolean isFolder = false;
public SongLoaderThread (final URL location, final Bot bot, final String requester) {
public SongLoaderThread (final URL location, final Bot bot, final CommandContext context) {
this.bot = bot;
this.requester = requester;
this.context = context;
isUrl = true;
songUrl = location;
fileName = location.getFile();
updateName();
}
public SongLoaderThread (final Path location, final Bot bot, final String requester) {
public SongLoaderThread (final Path location, final Bot bot, final CommandContext context) {
this.bot = bot;
this.requester = requester;
this.context = context;
isUrl = false;
songPath = location;
isFolder = Files.isDirectory(songPath);
fileName = location.getFileName().toString();
updateName();
}
public SongLoaderThread (final byte[] data, final Bot bot, final String requester) {
public SongLoaderThread (final byte[] data, final Bot bot, final CommandContext context) {
this.bot = bot;
this.requester = requester;
this.context = context;
this.data = data;
this.isItem = true;
this.isUrl = false;
fileName = requester + "'s song item";
fileName = context.sender.profile.getName() + "'s song item";
updateName();
}
private void updateName () {
setName("SongLoaderThread for " + fileName);
}
@Override
@@ -103,7 +113,7 @@ public class SongLoaderThread extends Thread {
name = fileName == null ? "(root)" : fileName.toString();
} else if (isItem) {
bytes = data;
name = requester + "'s song item";
name = context.sender.profile.getName() + "'s song item";
} else {
bytes = Files.readAllBytes(songPath);
name = !isFolder ? fileName : songPath.getFileName().toString();
@@ -129,7 +139,7 @@ public class SongLoaderThread extends Thread {
failed();
} else {
song.requester = requester;
song.context = context;
bot.music.songQueue.add(song);
@@ -141,36 +151,30 @@ public class SongLoaderThread extends Thread {
private void showAddedToQueue () {
if (isFolder) {
bot.chat.tellraw(
I18nUtilities.render(
Component.translatable(
"commands.music.loading.added_folder_to_queue",
bot.colorPalette.defaultColor
)
context.sendOutput(
Component.translatable(
"commands.music.loading.added_folder_to_queue",
bot.colorPalette.defaultColor
)
);
} else {
bot.chat.tellraw(
I18nUtilities.render(
Component.translatable(
"commands.music.loading.added_song_to_queue",
bot.colorPalette.defaultColor,
Component.empty()
.append(Component.text(song.name, bot.colorPalette.secondary))
)
context.sendOutput(
Component.translatable(
"commands.music.loading.added_song_to_queue",
bot.colorPalette.defaultColor,
Component.empty()
.append(Component.text(song.name, bot.colorPalette.secondary))
)
);
}
}
private void failed () {
bot.chat.tellraw(
I18nUtilities.render(
Component.translatable(
"commands.music.error.loading_failed",
NamedTextColor.RED,
exception.message
)
context.sendOutput(
Component.translatable(
"commands.music.error.loading_failed",
NamedTextColor.RED,
exception.message
)
);
bot.music.loaderThread = null;