ExecutorService because Thread is a retard

This commit is contained in:
Chayapak 2023-06-06 16:31:14 +07:00
parent 7cb3164a3c
commit 399bbb22d0
5 changed files with 16 additions and 7 deletions
src/main/java/land/chipmunk/chayapak/chomens_bot

View file

@ -16,6 +16,7 @@ import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -39,6 +40,7 @@ public class Bot {
@Getter private boolean loggedIn = false;
@Getter private final ExecutorService executorService = Main.executorService;
@Getter private final ScheduledExecutorService executor = Main.executor;
@Getter @Setter private ConsolePlugin console;

View file

@ -12,12 +12,14 @@ import javax.security.auth.login.LoginException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class Main {
public static final List<Bot> bots = new ArrayList<>();
public static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
public static void main(String[] args) throws IOException {

View file

@ -53,7 +53,7 @@ public class TranslateCommand implements Command {
final Gson gson = new Gson();
new Thread(() -> {
bot.executorService().execute(() -> {
try {
final URL url = new URL("https://translate.google.com/translate_a/single?client=at&dt=t&dt=rm&dj=1");
@ -90,7 +90,7 @@ public class TranslateCommand implements Command {
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}
}).start();
});
return null;
}

View file

@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities;
@ -42,11 +43,13 @@ public class UrbanCommand implements Command {
}
public Component execute (CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot();
final String term = String.join(" ", args);
final Gson gson = new Gson();
new Thread(() -> {
bot.executorService().execute(() -> {
try {
final URL url = new URL(
"https://api.urbandictionary.com/v0/define?term=" +
@ -79,7 +82,7 @@ public class UrbanCommand implements Command {
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}
}).start();
});
return null;
}

View file

@ -2,6 +2,7 @@ package land.chipmunk.chayapak.chomens_bot.commands;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities;
@ -41,12 +42,13 @@ public class WikipediaCommand implements Command {
}
public Component execute (CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot();
final String page = String.join(" ", args);
final Gson gson = new Gson();
// TODO: mabe use the executor service thingy instead of threads because threads are b a d
new Thread(() -> {
bot.executorService().execute(() -> {
try {
final URL url = new URL(
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
@ -66,7 +68,7 @@ public class WikipediaCommand implements Command {
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}
}).start();
});
return null;
}