From 3414059055be0f15648fb0a55be65edf114b9424 Mon Sep 17 00:00:00 2001 From: ohAlee Date: Mon, 2 Jan 2023 12:06:57 +0100 Subject: [PATCH] Initial commit --- .idea/misc.xml | 2 +- build.gradle | 3 -- .../java/it/ohalee/minecraftgpt/Main.java | 28 ++++++------------- .../java/it/ohalee/minecraftgpt/OpenAI.java | 2 +- .../minecraftgpt/handler/PlayerHandlers.java | 3 +- 5 files changed, 12 insertions(+), 26 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index f369c56..fae6c8e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 862d0ad..adfbb97 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/src/main/java/it/ohalee/minecraftgpt/Main.java b/src/main/java/it/ohalee/minecraftgpt/Main.java index 32d0ffb..b813308 100644 --- a/src/main/java/it/ohalee/minecraftgpt/Main.java +++ b/src/main/java/it/ohalee/minecraftgpt/Main.java @@ -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 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) 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!"); } diff --git a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java index 30d8391..f33636f 100644 --- a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java +++ b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java @@ -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 getResponse(StringBuilder cached, String message) { cached.append("\nHuman:").append(message).append("\nAI:"); diff --git a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java index 1930db1..40be8d8 100644 --- a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java +++ b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java @@ -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); }); }