From 232fa0a2bb817a0f85935667eb5f3ad6873be447 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:39:40 +0700 Subject: [PATCH] msuic thread bring back. --- .../chayapak/chomens_bot/commands/MusicCommand.java | 2 ++ .../chomens_bot/plugins/MusicPlayerPlugin.java | 12 ++++++------ ...SongLoaderRunnable.java => SongLoaderThread.java} | 11 ++++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) rename src/main/java/land/chipmunk/chayapak/chomens_bot/song/{SongLoaderRunnable.java => SongLoaderThread.java} (93%) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java index ea6749e..21bfd80 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java @@ -95,6 +95,8 @@ public class MusicCommand extends Command { public Component play (CommandContext context) throws CommandException { final MusicPlayerPlugin player = context.bot.music; + if (player.loaderThread != null) throw new CommandException(Component.text("Already loading a song")); + String _path; Path path; try { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 6f9b57b..cf3c91d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -9,7 +9,7 @@ import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; import land.chipmunk.chayapak.chomens_bot.song.Loop; import land.chipmunk.chayapak.chomens_bot.song.Note; import land.chipmunk.chayapak.chomens_bot.song.Song; -import land.chipmunk.chayapak.chomens_bot.song.SongLoaderRunnable; +import land.chipmunk.chayapak.chomens_bot.song.SongLoaderThread; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import land.chipmunk.chayapak.chomens_bot.util.MathUtilities; import net.kyori.adventure.text.Component; @@ -43,7 +43,7 @@ public class MusicPlayerPlugin extends Bot.Listener { public Song currentSong; public final List songQueue = new ArrayList<>(); - public SongLoaderRunnable loaderThread; + public SongLoaderThread loaderThread = null; public Loop loop = Loop.OFF; // sus nightcore stuff,..,.,. @@ -74,7 +74,7 @@ public class MusicPlayerPlugin extends Bot.Listener { public void loadSong (Path location, PlayerEntry sender) { if (songQueue.size() > 100) return; - final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot, sender.profile.getName()); + loaderThread = new SongLoaderThread(location, bot, sender.profile.getName()); bot.chat.tellraw( Component @@ -85,7 +85,7 @@ public class MusicPlayerPlugin extends Bot.Listener { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); - bot.executorService.submit(runnable); + loaderThread.start(); } public void loadSong (URL location, PlayerEntry sender) { @@ -98,7 +98,7 @@ public class MusicPlayerPlugin extends Bot.Listener { return; } - final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot, sender.profile.getName()); + loaderThread = new SongLoaderThread(location, bot, sender.profile.getName()); bot.chat.tellraw( Component @@ -109,7 +109,7 @@ public class MusicPlayerPlugin extends Bot.Listener { .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); - bot.executorService.submit(runnable); + loaderThread.start(); } public void coreReady () { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderThread.java similarity index 93% rename from src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java rename to src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderThread.java index 2ed34d4..70d26e1 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderThread.java @@ -17,7 +17,7 @@ import java.util.stream.Stream; // Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable // because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -public class SongLoaderRunnable implements Runnable { +public class SongLoaderThread extends Thread { // should the converters be here? public static final List converters = new ArrayList<>(); @@ -42,7 +42,7 @@ public class SongLoaderRunnable implements Runnable { private boolean isFolder = false; - public SongLoaderRunnable(URL location, Bot bot, String requester) { + public SongLoaderThread(URL location, Bot bot, String requester) { this.bot = bot; this.requester = requester; isUrl = true; @@ -51,7 +51,7 @@ public class SongLoaderRunnable implements Runnable { fileName = location.getFile(); } - public SongLoaderRunnable(Path location, Bot bot, String requester) { + public SongLoaderThread(Path location, Bot bot, String requester) { this.bot = bot; this.requester = requester; isUrl = false; @@ -62,6 +62,7 @@ public class SongLoaderRunnable implements Runnable { fileName = location.getFileName().toString(); } + @Override public void run () { if (isFolder && !isUrl) { try (Stream files = Files.list(songPath)) { @@ -80,6 +81,8 @@ public class SongLoaderRunnable implements Runnable { } private void processFile () { + if (bot.music.songQueue.size() > 100) return; + byte[] bytes; String name; try { @@ -119,6 +122,8 @@ public class SongLoaderRunnable implements Runnable { if (!isFolder) showAddedToQueue(); } + + bot.music.loaderThread = null; } private void showAddedToQueue () {