From e938e5a4b630b1521be13f46ecac0e261f1004e7 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 18 Aug 2025 19:20:28 +0700 Subject: [PATCH] fix: some improvements and bugfixes in music --- build-number.txt | 2 +- .../chomens_bot/commands/MusicCommand.java | 34 ++-- .../plugins/MusicPlayerPlugin.java | 150 +++++++++--------- 3 files changed, 96 insertions(+), 90 deletions(-) diff --git a/build-number.txt b/build-number.txt index 01488fde..b6873d79 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -3584 \ No newline at end of file +3586 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java index 8e456295..aea45759 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java @@ -397,24 +397,26 @@ public class MusicCommand extends Command implements Listener { final Bot bot = context.bot; final List queue = bot.music.songQueue; - final List queueWithNames = new ObjectArrayList<>(); - int i = 0; - for (final Song song : queue) { - queueWithNames.add( - Component.text( - song.name, - (i++ & 1) == 0 - ? bot.colorPalette.primary - : bot.colorPalette.secondary - ) + synchronized (queue) { + final List queueWithNames = new ObjectArrayList<>(); + int i = 0; + for (final Song song : queue) { + queueWithNames.add( + Component.text( + song.name, + (i++ & 1) == 0 + ? bot.colorPalette.primary + : bot.colorPalette.secondary + ) + ); + } + + return Component.translatable( + "commands.music.queue", + NamedTextColor.GREEN, + Component.join(JoinConfiguration.commas(true), queueWithNames) ); } - - return Component.translatable( - "commands.music.queue", - NamedTextColor.GREEN, - Component.join(JoinConfiguration.commas(true), queueWithNames) - ); } // lazy fix for java using "goto" as keyword real diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java index 7afb869e..dacdf00e 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/MusicPlayerPlugin.java @@ -57,6 +57,8 @@ public class MusicPlayerPlugin implements Listener { public SongLoaderThread loaderThread = null; public Loop loop = Loop.OFF; + private final Object tickLock = new Object(); + // sus nightcore stuff,..,.,. public double pitch = 0; public double speed = 1; @@ -84,7 +86,17 @@ public class MusicPlayerPlugin implements Listener { bot.listener.addListener(this); - bot.executor.scheduleAtFixedRate(this::onMusicTick, 0, 50, TimeUnit.MILLISECONDS); + bot.executor.scheduleAtFixedRate(() -> { + if (!bot.loggedIn) return; + + synchronized (tickLock) { + try { + onMusicTick(); + } catch (final Exception e) { + bot.logger.error(e); + } + } + }, 0, 50, TimeUnit.MILLISECONDS); bot.executor.scheduleAtFixedRate(() -> urlLimit = 0, 0, bot.config.music.urlRatelimit.seconds, TimeUnit.SECONDS); } @@ -147,81 +159,75 @@ public class MusicPlayerPlugin implements Listener { // this needs a separate ticker because we need // the song to be playing without lag private void onMusicTick () { - try { - if (!bot.loggedIn) return; + if (currentSong == null) { + if (songQueue.isEmpty()) return; // this line - if (currentSong == null) { - if (songQueue.isEmpty()) return; // this line + currentSong = songQueue.getFirst(); // songQueue.poll(); + currentSong.context.sendOutput( + Component.translatable( + "commands.music.nowplaying", + bot.colorPalette.defaultColor, + Component.empty().append(Component.text(currentSong.name, bot.colorPalette.secondary)) + ) + ); + currentSong.play(); - addBossBar(); + addBossBar(); + } - currentSong = songQueue.getFirst(); // songQueue.poll(); - currentSong.context.sendOutput( - Component.translatable( - "commands.music.nowplaying", - bot.colorPalette.defaultColor, - Component.empty().append(Component.text(currentSong.name, bot.colorPalette.secondary)) - ) - ); + if (isStopping) { + currentSong = null; + isStopping = false; + } else if (!currentSong.finished()) { + handleLyrics(); + + BotBossBar bossBar = bot.bossbar.get(BOSS_BAR_NAME); + + if (bossBar == null) bossBar = addBossBar(); + + if (bossBar != null && bot.options.useCore) { + bossBar.setTitle(generateBossBar()); + bossBar.setColor(bossBarColor); + bossBar.setValue((int) Math.floor(((currentSong.time / speed) / 1000))); + bossBar.setMax((long) (currentSong.length / speed) / 1000); + } + + if (currentSong.paused || bot.core.isRateLimited()) return; + + handlePlaying(); + } else { + currentLyrics = ""; + + if (loop == Loop.CURRENT) { + currentSong.loop(); + return; + } + + currentSong.context.sendOutput( + Component.translatable( + "commands.music.finished", + bot.colorPalette.defaultColor, + Component.empty().append(Component.text(currentSong.name, bot.colorPalette.secondary)) + ) + ); + + if (loop == Loop.ALL) { + skip(); + return; + } + + songQueue.removeFirst(); + + if (songQueue.isEmpty()) { + stopPlaying(); + return; + } + + if (currentSong.size() > 0) { + currentSong = songQueue.getFirst(); + currentSong.setTime(0); currentSong.play(); } - - if (isStopping) { - currentSong = null; - isStopping = false; - } else if (!currentSong.finished()) { - handleLyrics(); - - BotBossBar bossBar = bot.bossbar.get(BOSS_BAR_NAME); - - if (bossBar == null) bossBar = addBossBar(); - - if (bossBar != null && bot.options.useCore) { - bossBar.setTitle(generateBossBar()); - bossBar.setColor(bossBarColor); - bossBar.setValue((int) Math.floor(((currentSong.time / speed) / 1000))); - bossBar.setMax((long) (currentSong.length / speed) / 1000); - } - - if (currentSong.paused || bot.core.isRateLimited()) return; - - handlePlaying(); - } else { - currentLyrics = ""; - - if (loop == Loop.CURRENT) { - currentSong.loop(); - return; - } - - currentSong.context.sendOutput( - Component.translatable( - "commands.music.finished", - bot.colorPalette.defaultColor, - Component.empty().append(Component.text(currentSong.name, bot.colorPalette.secondary)) - ) - ); - - if (loop == Loop.ALL) { - skip(); - return; - } - - songQueue.removeFirst(); - - if (songQueue.isEmpty()) { - stopPlaying(); - return; - } - - if (currentSong.size() > 0) { - currentSong = songQueue.getFirst(); - currentSong.setTime(0); - currentSong.play(); - } - } - } catch (final Exception e) { - bot.logger.error(e); } } @@ -241,8 +247,6 @@ public class MusicPlayerPlugin implements Listener { } public BotBossBar addBossBar () { - if (currentSong == null) return null; - rainbow = false; final BotBossBar bossBar = new BotBossBar(