Initial commit

This commit is contained in:
ohAlee 2023-01-02 12:06:57 +01:00
parent 59a0925b24
commit 3414059055
5 changed files with 12 additions and 26 deletions

View File

@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -29,7 +29,6 @@ dependencies {
compileOnly("io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT")
implementation 'com.theokanning.openai-gpt3-java:client:0.8.1'
api 'com.github.ben-manes.caffeine:caffeine:3.1.1'
compileOnly 'org.checkerframework:checker-qual:3.21.2'
compileOnly 'org.jetbrains:annotations:23.0.0'
@ -43,8 +42,6 @@ dependencies {
shadowJar {
archiveFileName = "MinecraftGPT-${project.ext.fullVersion}.jar"
relocate 'com.github.benmanes.caffeine', 'it.ohalee.minecraftgpt.lib.caffeine'
}
artifacts {

View File

@ -1,38 +1,30 @@
package it.ohalee.minecraftgpt;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.google.common.cache.*;
import it.ohalee.minecraftgpt.command.GPTCommand;
import it.ohalee.minecraftgpt.handler.PlayerHandlers;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main extends JavaPlugin {
private final ExecutorService executor;
@Getter
private Cache<Player, StringBuilder> cache;
public Main() {
executor = Executors.newSingleThreadExecutor();
}
@Override
public void onEnable() {
cache = Caffeine.newBuilder()
cache = CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.MINUTES)
.executor(executor)
.removalListener((key, value, cause) -> {
if (key != null && cause == RemovalCause.EXPIRED) {
((Player) key).sendMessage("ChatGPT has disconnected.");
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
if (notification.getCause() == RemovalCause.EXPIRED && notification.getKey() != null) {
notification.getKey().sendMessage("ChatGPT has disconnected.");
}
})
.build();
}).build();
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
getCommand("chatgpt").setExecutor(new GPTCommand(this));
@ -41,8 +33,6 @@ public class Main extends JavaPlugin {
@Override
public void onDisable() {
executor.shutdown();
getLogger().info("Plugin disabled!");
}

View File

@ -8,7 +8,7 @@ import java.util.concurrent.CompletableFuture;
public class OpenAI {
private static final OpenAiService service = new OpenAiService("");
private static final OpenAiService service = new OpenAiService("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", 0);
public static CompletableFuture<String> getResponse(StringBuilder cached, String message) {
cached.append("\nHuman:").append(message).append("\nAI:");

View File

@ -27,14 +27,13 @@ public class PlayerHandlers implements Listener {
if (this.plugin.getCache().asMap().containsKey(player)) {
e.setCancelled(true);
player.sendMessage(ChatColor.AQUA + "You: " + ChatColor.GRAY + e.getMessage());
OpenAI.getResponse(plugin.getCache().getIfPresent(player), e.getMessage()).whenComplete((response, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
player.sendMessage(ChatColor.RED + "An error occurred while processing your message.");
return;
}
player.sendMessage(ChatColor.AQUA + "You: " + ChatColor.GRAY + e.getMessage());
player.sendMessage(ChatColor.AQUA + "AI: " + ChatColor.GREEN + response);
});
}