Skip to content

Commit 4f9ea05

Browse files
committed
LagFix, Added ThreadLimit
1 parent 2ade3ee commit 4f9ea05

4 files changed

Lines changed: 42 additions & 14 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>tech.codemein</groupId>
88
<artifactId>AIChatBot</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.0-beta</version>
1010
<packaging>jar</packaging>
1111

1212
<name>AIChat</name>

src/main/java/tech/codemein/aichat/commands/ASKCommand.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@
2020

2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.concurrent.ExecutorService;
24+
import java.util.concurrent.Executors;
25+
26+
import static org.bukkit.Bukkit.getLogger;
2327

2428
public class ASKCommand implements CommandExecutor, TabCompleter {
2529
private final List<ChatCompletionMessageData> messageHistory = new ArrayList<>();
30+
private static AIHandler gpt = new AIHandler();
2631
private static String response;
2732
@Override
2833
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
34+
int maxThreads = FileManager.config.getInt("maxThreads");
2935

3036
Player player = (Player) sender;
3137

@@ -36,22 +42,39 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3642

3743
player.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&7 Question: " + String.join(" ", args)));
3844

39-
AIHandler gpt = new AIHandler();
40-
4145
//The first message that we want to send
4246
String message = String.join(" ", args);
4347

44-
//Response from ChatGPT
45-
player.playSound(player.getLocation(), Sound.BLOCK_LAVA_POP, 1, 2);
46-
try {
47-
response = gpt.exampleBuilders(message);
48-
} catch (Exception e) {
49-
sender.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&c Error: Failed, please look at console and check your API Key if is correct. If you do not know what to do, ask on out discord."));
50-
Main.getInstance().getLogger().warning(e.getMessage());
48+
49+
if (maxThreads == 0) {
50+
player.playSound(player.getLocation(), Sound.BLOCK_LAVA_POP, 1, 2);
51+
try {
52+
response = gpt.exampleBuilders(message);
53+
} catch (Exception e) {
54+
sender.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&c Error: Failed, please look at console and check your API Key if is correct. If you do not know what to do, ask on out discord."));
55+
Main.getInstance().getLogger().warning(e.getMessage());
56+
return true;
57+
}
58+
sender.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&7" + response));
59+
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_PLACE, 1, 2);
5160
return true;
5261
}
53-
sender.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&7" + response));
54-
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_PLACE, 1, 2);
62+
63+
//Response from ChatGPT
64+
ExecutorService executorService = Executors.newFixedThreadPool(maxThreads);
65+
executorService.submit(() -> {
66+
player.playSound(player.getLocation(), Sound.BLOCK_LAVA_POP, 1, 2);
67+
try {
68+
response = gpt.exampleBuilders(message);
69+
} catch (Exception e) {
70+
sender.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&c Error: Failed, please look at console and check your API Key if is correct. If you do not know what to do, ask on out discord."));
71+
Main.getInstance().getLogger().warning(e.getMessage());
72+
return;
73+
}
74+
sender.sendMessage(ColorUtil.translate(FileManager.config.getString("prefix") + "&7" + response));
75+
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_PLACE, 1, 2);
76+
});
77+
5578
return true;
5679
}
5780

src/main/java/tech/codemein/aichat/commands/MainCommand.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import tech.codemein.aichat.managers.FileManager;
1111
import tech.codemein.aichat.utils.ColorUtil;
1212

13-
import java.io.File;
14-
1513
public class MainCommand implements CommandExecutor {
1614
private String SpigotMCUrlForPlugin = null;
1715

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package tech.codemein.aichat.listeners;
2+
3+
import org.bukkit.event.Listener;
4+
5+
public class ChatListener implements Listener {
6+
7+
}

0 commit comments

Comments
 (0)