use ExecutorService s instead of threads.,.,

This commit is contained in:
Chayapak 2023-06-12 18:43:10 +07:00
parent 19581fedeb
commit 3dd65b15cf
6 changed files with 54 additions and 78 deletions

View file

@ -50,15 +50,13 @@ public class GrepLogCommand implements Command {
boolean regex = false;
if (_args[0].equals("stop")) {
if (bot.grepLog().thread() == null) return Component.text("No query is running").color(NamedTextColor.RED);
bot.grepLog().thread().interrupt();
bot.grepLog().thread(null);
bot.grepLog().future().cancel(true);
bot.grepLog().future(null);
return Component.text("Log query stopped");
}
if (bot.grepLog().thread() != null) return Component.text("Another query is already running").color(NamedTextColor.RED);
if (bot.grepLog().future() != null) return Component.text("Another query is already running").color(NamedTextColor.RED);
// this is a mess
if (_args[0].equals("-ignorecase")) {

View file

@ -2,6 +2,7 @@ package land.chipmunk.chayapak.chomens_bot.plugins;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.Configuration;
import land.chipmunk.chayapak.chomens_bot.Main;
import land.chipmunk.chayapak.chomens_bot.command.ConsoleCommandContext;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import lombok.Getter;
@ -46,7 +47,7 @@ public class ConsolePlugin {
final String prompt = "> ";
new Thread(() -> {
Main.executorService.submit(() -> {
while (true) {
String line = null;
try {
@ -57,7 +58,7 @@ public class ConsolePlugin {
handleLine(line);
}
}).start();
});
for (Listener listener : listeners) { listener.ready(); }
}

View file

@ -1,8 +1,8 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.entities.TextChannel;
@ -13,25 +13,26 @@ import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
public class GrepLogPlugin {
private final Bot bot;
@Getter @Setter private GrepLogThread thread = null;
@Getter @Setter private Future<?> future = null;
public GrepLogPlugin (Bot bot) {
this.bot = bot;
}
public void query (String query, boolean regex, boolean ignoreCase) {
thread = new GrepLogThread(query, regex, ignoreCase);
thread.start();
final Runnable runnable = new GrepLogRunnable(query, regex, ignoreCase);
future = bot.executorService().submit(runnable);
}
// should i move this to another file or keep it here
public class GrepLogThread extends Thread {
public class GrepLogRunnable implements Runnable {
private String query;
private final boolean regex;
private final boolean ignoreCase;
@ -43,7 +44,7 @@ public class GrepLogPlugin {
private int matches = 0;
private final StringBuilder results = new StringBuilder();
public GrepLogThread(String query, boolean regex, boolean ignoreCase) {
public GrepLogRunnable(String query, boolean regex, boolean ignoreCase) {
this.regex = regex;
this.ignoreCase = ignoreCase;
@ -160,19 +161,20 @@ public class GrepLogPlugin {
}
private void finish () {
thread = null;
if (results.toString().split("\n").length < 100) { // ig lazy fix for removing \n lol
bot.chat().tellraw(
Component.empty()
.append(Component.text("Log query output:"))
.append(Component.text("Log query output for \""))
.append(Component.text(query))
.append(Component.text("\":"))
.append(Component.newline())
.append(Component.text(results.toString()))
);
} else if (bot.config().discord().enabled()) {
bot.chat().tellraw(
Component.translatable(
"Log query finished, found %s matches. Results were sent in Discord",
"Log query for \"%s\" finished, found %s matches. Results were sent in Discord",
Component.text(query).color(ColorUtilities.getColorByString(bot.config().colorPalette().string())),
Component.text(matches).color(ColorUtilities.getColorByString(bot.config().colorPalette().number()))
)
);

View file

@ -34,7 +34,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
@Getter @Setter private Song currentSong;
@Getter @Setter private List<Song> songQueue = new ArrayList<>();
@Getter @Setter private SongLoaderThread loaderThread;
@Getter @Setter private SongLoaderRunnable loaderThread;
@Getter @Setter private Loop loop = Loop.OFF;
// sus nightcore stuff,..,.,.
@ -54,13 +54,9 @@ public class MusicPlayerPlugin extends Bot.Listener {
}
public void loadSong (Path location) {
if (loaderThread != null) {
bot.chat().tellraw(Component.translatable("Already loading a song", NamedTextColor.RED));
return;
}
try {
final SongLoaderThread _loaderThread = new SongLoaderThread(location, bot);
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
bot.chat().tellraw(
Component
.translatable(
@ -69,8 +65,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
)
.color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
);
_loaderThread.start();
loaderThread = _loaderThread;
bot.executorService().submit(runnable);
} catch (SongLoaderException e) {
e.printStackTrace();
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
@ -79,13 +75,9 @@ public class MusicPlayerPlugin extends Bot.Listener {
}
public void loadSong (URL location) {
if (loaderThread != null) {
bot.chat().tellraw(Component.translatable("Already loading a song", NamedTextColor.RED));
return;
}
try {
final SongLoaderThread _loaderThread = new SongLoaderThread(location, bot);
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
bot.chat().tellraw(
Component
.translatable(
@ -94,8 +86,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
)
.color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
);
_loaderThread.start();
loaderThread = _loaderThread;
bot.executorService().submit(runnable);
} catch (SongLoaderException e) {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
loaderThread = null;
@ -106,23 +98,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
if (loaderThread != null && !loaderThread.isAlive()) {
if (loaderThread.exception != null) {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED));
} else {
songQueue.add(loaderThread.song);
bot.chat().tellraw(
Component.translatable(
"Added %s to the song queue",
Component.empty().append(loaderThread.song.name).color(ColorUtilities.getColorByString(bot.config().colorPalette().secondary()))
).color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
);
}
loaderThread = null;
}
if (currentSong == null) {
if (songQueue.size() == 0) return;
if (songQueue.size() == 0) return; // this line
addBossBar();

View file

@ -1,8 +1,7 @@
package land.chipmunk.chayapak.chomens_bot.song;
import net.kyori.adventure.text.Component;
import lombok.Getter;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import net.kyori.adventure.text.Component;
// Author: _ChipMC_ or hhhzzzsss?
public class SongLoaderException extends Exception {
@ -17,9 +16,4 @@ public class SongLoaderException extends Exception {
super(null, cause);
this.message = message;
}
@Override
public String getMessage () {
return ComponentUtilities.stringify(message);
}
}

View file

@ -2,16 +2,20 @@ package land.chipmunk.chayapak.chomens_bot.song;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.plugins.MusicPlayerPlugin;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.DownloadUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;
// Author: _ChipMC_ or hhhzzzsss?
public class SongLoaderThread extends Thread {
// Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable
// because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class SongLoaderRunnable implements Runnable {
public String fileName;
private File songPath;
@ -23,7 +27,7 @@ public class SongLoaderThread extends Thread {
private final boolean isUrl;
public SongLoaderThread (URL location, Bot bot) throws SongLoaderException {
public SongLoaderRunnable(URL location, Bot bot) throws SongLoaderException {
this.bot = bot;
isUrl = true;
songUrl = location;
@ -31,7 +35,7 @@ public class SongLoaderThread extends Thread {
fileName = location.getFile();
}
public SongLoaderThread (Path location, Bot bot) throws SongLoaderException {
public SongLoaderRunnable(Path location, Bot bot) throws SongLoaderException {
this.bot = bot;
isUrl = false;
songPath = location.toFile();
@ -52,25 +56,11 @@ public class SongLoaderThread extends Thread {
}
} catch (Exception e) {
e.printStackTrace();
exception = new SongLoaderException(Component.text(e.getMessage()), e);
return;
}
try {
if (name.endsWith(".mid") || name.endsWith(".midi")) {
song = MidiConverter.getSongFromBytes(bytes, name, bot);
return;
}
showFailedMessage();
if (name.endsWith(".nbs")) {
song = NBSConverter.getSongFromBytes(bytes, name, bot);
return;
}
} catch (Exception e) {
e.printStackTrace();
exception = new SongLoaderException(Component.translatable("Invalid format"));
}
try {
song = MidiConverter.getSongFromBytes(bytes, name, bot);
@ -88,9 +78,23 @@ public class SongLoaderThread extends Thread {
if (song == null) {
exception = new SongLoaderException(Component.translatable("Invalid format"));
showFailedMessage();
} else {
bot.music().songQueue().add(song);
bot.chat().tellraw(
Component.translatable(
"Added %s to the song queue",
Component.empty().append(song.name).color(ColorUtilities.getColorByString(bot.config().colorPalette().secondary()))
).color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
);
}
}
private void showFailedMessage () {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", exception.message()).color(NamedTextColor.RED));
}
private File getSongFile (String name) {
return new File(MusicPlayerPlugin.SONG_DIR, name);
}