forked from ChomeNS/chomens-bot-java
use ExecutorService s instead of threads.,.,
This commit is contained in:
parent
19581fedeb
commit
3dd65b15cf
6 changed files with 54 additions and 78 deletions
src/main/java/land/chipmunk/chayapak/chomens_bot
|
@ -50,15 +50,13 @@ public class GrepLogCommand implements Command {
|
||||||
boolean regex = false;
|
boolean regex = false;
|
||||||
|
|
||||||
if (_args[0].equals("stop")) {
|
if (_args[0].equals("stop")) {
|
||||||
if (bot.grepLog().thread() == null) return Component.text("No query is running").color(NamedTextColor.RED);
|
bot.grepLog().future().cancel(true);
|
||||||
|
bot.grepLog().future(null);
|
||||||
bot.grepLog().thread().interrupt();
|
|
||||||
bot.grepLog().thread(null);
|
|
||||||
|
|
||||||
return Component.text("Log query stopped");
|
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
|
// this is a mess
|
||||||
if (_args[0].equals("-ignorecase")) {
|
if (_args[0].equals("-ignorecase")) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||||
|
|
||||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||||
import land.chipmunk.chayapak.chomens_bot.Configuration;
|
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.command.ConsoleCommandContext;
|
||||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
@ -46,7 +47,7 @@ public class ConsolePlugin {
|
||||||
|
|
||||||
final String prompt = "> ";
|
final String prompt = "> ";
|
||||||
|
|
||||||
new Thread(() -> {
|
Main.executorService.submit(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
String line = null;
|
String line = null;
|
||||||
try {
|
try {
|
||||||
|
@ -57,7 +58,7 @@ public class ConsolePlugin {
|
||||||
|
|
||||||
handleLine(line);
|
handleLine(line);
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
|
||||||
for (Listener listener : listeners) { listener.ready(); }
|
for (Listener listener : listeners) { listener.ready(); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package land.chipmunk.chayapak.chomens_bot.plugins;
|
package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||||
|
|
||||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
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.ColorUtilities;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import net.dv8tion.jda.api.entities.TextChannel;
|
import net.dv8tion.jda.api.entities.TextChannel;
|
||||||
|
@ -13,25 +13,26 @@ import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
public class GrepLogPlugin {
|
public class GrepLogPlugin {
|
||||||
private final Bot bot;
|
private final Bot bot;
|
||||||
|
|
||||||
@Getter @Setter private GrepLogThread thread = null;
|
@Getter @Setter private Future<?> future = null;
|
||||||
|
|
||||||
public GrepLogPlugin (Bot bot) {
|
public GrepLogPlugin (Bot bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void query (String query, boolean regex, boolean ignoreCase) {
|
public void query (String query, boolean regex, boolean ignoreCase) {
|
||||||
thread = new GrepLogThread(query, regex, ignoreCase);
|
final Runnable runnable = new GrepLogRunnable(query, regex, ignoreCase);
|
||||||
thread.start();
|
future = bot.executorService().submit(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
// should i move this to another file or keep it here
|
// 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 String query;
|
||||||
private final boolean regex;
|
private final boolean regex;
|
||||||
private final boolean ignoreCase;
|
private final boolean ignoreCase;
|
||||||
|
@ -43,7 +44,7 @@ public class GrepLogPlugin {
|
||||||
private int matches = 0;
|
private int matches = 0;
|
||||||
private final StringBuilder results = new StringBuilder();
|
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.regex = regex;
|
||||||
this.ignoreCase = ignoreCase;
|
this.ignoreCase = ignoreCase;
|
||||||
|
|
||||||
|
@ -160,19 +161,20 @@ public class GrepLogPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void finish () {
|
private void finish () {
|
||||||
thread = null;
|
|
||||||
|
|
||||||
if (results.toString().split("\n").length < 100) { // ig lazy fix for removing \n lol
|
if (results.toString().split("\n").length < 100) { // ig lazy fix for removing \n lol
|
||||||
bot.chat().tellraw(
|
bot.chat().tellraw(
|
||||||
Component.empty()
|
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.newline())
|
||||||
.append(Component.text(results.toString()))
|
.append(Component.text(results.toString()))
|
||||||
);
|
);
|
||||||
} else if (bot.config().discord().enabled()) {
|
} else if (bot.config().discord().enabled()) {
|
||||||
bot.chat().tellraw(
|
bot.chat().tellraw(
|
||||||
Component.translatable(
|
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()))
|
Component.text(matches).color(ColorUtilities.getColorByString(bot.config().colorPalette().number()))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
||||||
|
|
||||||
@Getter @Setter private Song currentSong;
|
@Getter @Setter private Song currentSong;
|
||||||
@Getter @Setter private List<Song> songQueue = new ArrayList<>();
|
@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;
|
@Getter @Setter private Loop loop = Loop.OFF;
|
||||||
|
|
||||||
// sus nightcore stuff,..,.,.
|
// sus nightcore stuff,..,.,.
|
||||||
|
@ -54,13 +54,9 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadSong (Path location) {
|
public void loadSong (Path location) {
|
||||||
if (loaderThread != null) {
|
|
||||||
bot.chat().tellraw(Component.translatable("Already loading a song", NamedTextColor.RED));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final SongLoaderThread _loaderThread = new SongLoaderThread(location, bot);
|
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
|
||||||
|
|
||||||
bot.chat().tellraw(
|
bot.chat().tellraw(
|
||||||
Component
|
Component
|
||||||
.translatable(
|
.translatable(
|
||||||
|
@ -69,8 +65,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
||||||
)
|
)
|
||||||
.color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
.color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
||||||
);
|
);
|
||||||
_loaderThread.start();
|
|
||||||
loaderThread = _loaderThread;
|
bot.executorService().submit(runnable);
|
||||||
} catch (SongLoaderException e) {
|
} catch (SongLoaderException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
|
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) {
|
public void loadSong (URL location) {
|
||||||
if (loaderThread != null) {
|
|
||||||
bot.chat().tellraw(Component.translatable("Already loading a song", NamedTextColor.RED));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final SongLoaderThread _loaderThread = new SongLoaderThread(location, bot);
|
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
|
||||||
|
|
||||||
bot.chat().tellraw(
|
bot.chat().tellraw(
|
||||||
Component
|
Component
|
||||||
.translatable(
|
.translatable(
|
||||||
|
@ -94,8 +86,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
||||||
)
|
)
|
||||||
.color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
.color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
||||||
);
|
);
|
||||||
_loaderThread.start();
|
|
||||||
loaderThread = _loaderThread;
|
bot.executorService().submit(runnable);
|
||||||
} catch (SongLoaderException e) {
|
} catch (SongLoaderException e) {
|
||||||
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
|
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
|
||||||
loaderThread = null;
|
loaderThread = null;
|
||||||
|
@ -106,23 +98,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
||||||
bot.tick().addListener(new TickPlugin.Listener() {
|
bot.tick().addListener(new TickPlugin.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTick() {
|
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 (currentSong == null) {
|
||||||
if (songQueue.size() == 0) return;
|
if (songQueue.size() == 0) return; // this line
|
||||||
|
|
||||||
addBossBar();
|
addBossBar();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
package land.chipmunk.chayapak.chomens_bot.song;
|
package land.chipmunk.chayapak.chomens_bot.song;
|
||||||
|
|
||||||
import net.kyori.adventure.text.Component;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
import net.kyori.adventure.text.Component;
|
||||||
|
|
||||||
// Author: _ChipMC_ or hhhzzzsss?
|
// Author: _ChipMC_ or hhhzzzsss?
|
||||||
public class SongLoaderException extends Exception {
|
public class SongLoaderException extends Exception {
|
||||||
|
@ -17,9 +16,4 @@ public class SongLoaderException extends Exception {
|
||||||
super(null, cause);
|
super(null, cause);
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage () {
|
|
||||||
return ComponentUtilities.stringify(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,20 @@ package land.chipmunk.chayapak.chomens_bot.song;
|
||||||
|
|
||||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||||
import land.chipmunk.chayapak.chomens_bot.plugins.MusicPlayerPlugin;
|
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 land.chipmunk.chayapak.chomens_bot.util.DownloadUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
// Author: _ChipMC_ or hhhzzzsss?
|
// Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable
|
||||||
public class SongLoaderThread extends Thread {
|
// because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
public class SongLoaderRunnable implements Runnable {
|
||||||
public String fileName;
|
public String fileName;
|
||||||
|
|
||||||
private File songPath;
|
private File songPath;
|
||||||
|
@ -23,7 +27,7 @@ public class SongLoaderThread extends Thread {
|
||||||
|
|
||||||
private final boolean isUrl;
|
private final boolean isUrl;
|
||||||
|
|
||||||
public SongLoaderThread (URL location, Bot bot) throws SongLoaderException {
|
public SongLoaderRunnable(URL location, Bot bot) throws SongLoaderException {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
isUrl = true;
|
isUrl = true;
|
||||||
songUrl = location;
|
songUrl = location;
|
||||||
|
@ -31,7 +35,7 @@ public class SongLoaderThread extends Thread {
|
||||||
fileName = location.getFile();
|
fileName = location.getFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SongLoaderThread (Path location, Bot bot) throws SongLoaderException {
|
public SongLoaderRunnable(Path location, Bot bot) throws SongLoaderException {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
isUrl = false;
|
isUrl = false;
|
||||||
songPath = location.toFile();
|
songPath = location.toFile();
|
||||||
|
@ -52,26 +56,12 @@ public class SongLoaderThread extends Thread {
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
exception = new SongLoaderException(Component.text(e.getMessage()), e);
|
|
||||||
|
showFailedMessage();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
if (name.endsWith(".mid") || name.endsWith(".midi")) {
|
|
||||||
song = MidiConverter.getSongFromBytes(bytes, name, bot);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name.endsWith(".nbs")) {
|
|
||||||
song = NBSConverter.getSongFromBytes(bytes, name, bot);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
exception = new SongLoaderException(Component.translatable("Invalid format"));
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
song = MidiConverter.getSongFromBytes(bytes, name, bot);
|
song = MidiConverter.getSongFromBytes(bytes, name, bot);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -88,9 +78,23 @@ public class SongLoaderThread extends Thread {
|
||||||
|
|
||||||
if (song == null) {
|
if (song == null) {
|
||||||
exception = new SongLoaderException(Component.translatable("Invalid format"));
|
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) {
|
private File getSongFile (String name) {
|
||||||
return new File(MusicPlayerPlugin.SONG_DIR, name);
|
return new File(MusicPlayerPlugin.SONG_DIR, name);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue