msuic thread bring back.

This commit is contained in:
Chayapak 2023-10-02 18:39:40 +07:00
parent 857884bcf4
commit 232fa0a2bb
3 changed files with 16 additions and 9 deletions

View file

@ -95,6 +95,8 @@ public class MusicCommand extends Command {
public Component play (CommandContext context) throws CommandException { public Component play (CommandContext context) throws CommandException {
final MusicPlayerPlugin player = context.bot.music; final MusicPlayerPlugin player = context.bot.music;
if (player.loaderThread != null) throw new CommandException(Component.text("Already loading a song"));
String _path; String _path;
Path path; Path path;
try { try {

View file

@ -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.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.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.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.MathUtilities; import land.chipmunk.chayapak.chomens_bot.util.MathUtilities;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -43,7 +43,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
public Song currentSong; public Song currentSong;
public final List<Song> songQueue = new ArrayList<>(); public final List<Song> songQueue = new ArrayList<>();
public SongLoaderRunnable loaderThread; public SongLoaderThread loaderThread = null;
public Loop loop = Loop.OFF; public Loop loop = Loop.OFF;
// sus nightcore stuff,..,.,. // sus nightcore stuff,..,.,.
@ -74,7 +74,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
public void loadSong (Path location, PlayerEntry sender) { public void loadSong (Path location, PlayerEntry sender) {
if (songQueue.size() > 100) return; 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( bot.chat.tellraw(
Component Component
@ -85,7 +85,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
bot.executorService.submit(runnable); loaderThread.start();
} }
public void loadSong (URL location, PlayerEntry sender) { public void loadSong (URL location, PlayerEntry sender) {
@ -98,7 +98,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
return; return;
} }
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot, sender.profile.getName()); loaderThread = new SongLoaderThread(location, bot, sender.profile.getName());
bot.chat.tellraw( bot.chat.tellraw(
Component Component
@ -109,7 +109,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
bot.executorService.submit(runnable); loaderThread.start();
} }
public void coreReady () { public void coreReady () {

View file

@ -17,7 +17,7 @@ import java.util.stream.Stream;
// Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable // Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable
// because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class SongLoaderRunnable implements Runnable { public class SongLoaderThread extends Thread {
// should the converters be here? // should the converters be here?
public static final List<Converter> converters = new ArrayList<>(); public static final List<Converter> converters = new ArrayList<>();
@ -42,7 +42,7 @@ public class SongLoaderRunnable implements Runnable {
private boolean isFolder = false; private boolean isFolder = false;
public SongLoaderRunnable(URL location, Bot bot, String requester) { public SongLoaderThread(URL location, Bot bot, String requester) {
this.bot = bot; this.bot = bot;
this.requester = requester; this.requester = requester;
isUrl = true; isUrl = true;
@ -51,7 +51,7 @@ public class SongLoaderRunnable implements Runnable {
fileName = location.getFile(); fileName = location.getFile();
} }
public SongLoaderRunnable(Path location, Bot bot, String requester) { public SongLoaderThread(Path location, Bot bot, String requester) {
this.bot = bot; this.bot = bot;
this.requester = requester; this.requester = requester;
isUrl = false; isUrl = false;
@ -62,6 +62,7 @@ public class SongLoaderRunnable implements Runnable {
fileName = location.getFileName().toString(); fileName = location.getFileName().toString();
} }
@Override
public void run () { public void run () {
if (isFolder && !isUrl) { if (isFolder && !isUrl) {
try (Stream<Path> files = Files.list(songPath)) { try (Stream<Path> files = Files.list(songPath)) {
@ -80,6 +81,8 @@ public class SongLoaderRunnable implements Runnable {
} }
private void processFile () { private void processFile () {
if (bot.music.songQueue.size() > 100) return;
byte[] bytes; byte[] bytes;
String name; String name;
try { try {
@ -119,6 +122,8 @@ public class SongLoaderRunnable implements Runnable {
if (!isFolder) showAddedToQueue(); if (!isFolder) showAddedToQueue();
} }
bot.music.loaderThread = null;
} }
private void showAddedToQueue () { private void showAddedToQueue () {