diff --git a/src/main/java/it/ohalee/minecraftgpt/Main.java b/src/main/java/it/ohalee/minecraftgpt/Main.java index 1f4cc09..3819bc4 100644 --- a/src/main/java/it/ohalee/minecraftgpt/Main.java +++ b/src/main/java/it/ohalee/minecraftgpt/Main.java @@ -13,28 +13,30 @@ import java.util.concurrent.TimeUnit; public class Main extends JavaPlugin { public static Cache CACHE; - public static Cache USER_TYPE; + public static Cache USER_TYPE = CacheBuilder.newBuilder().build(); @Override public void onEnable() { saveDefaultConfig(); - OpenAI.init(getConfig().getString("API_KEY")); + OpenAI.init(getConfig().getString("API_KEY")).exceptionallyAsync(throwable -> { + getLogger().severe("Error while initializing OpenAI service! Is your API key valid?"); + throwable.printStackTrace(); + return null; + }); CACHE = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.MINUTES) .removalListener((RemovalListener) notification -> { - if (notification.getKey() == null) { - return; - } + if (notification.getKey() == null) return; USER_TYPE.invalidate(notification.getKey()); if (notification.getCause() == RemovalCause.EXPIRED) { notification.getKey().sendMessage(Messages.format(getConfig().getString("command.toggle.disabled"))); } }).build(); - USER_TYPE = CacheBuilder.newBuilder().build(); getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this); + ChatCommand command = new ChatCommand(this); PluginCommand chatgpt = getCommand("chatgpt"); chatgpt.setExecutor(command); diff --git a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java index a87e80c..1f77011 100644 --- a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java +++ b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java @@ -11,8 +11,8 @@ public class OpenAI { private static OpenAiService service; - public static void init(String key) { - service = new OpenAiService(key, 0); + public static CompletableFuture init(String key) { + return CompletableFuture.runAsync(() -> service = new OpenAiService(key, 5)); } public static CompletableFuture getResponse(ConfigurationSection section, StringBuilder cached, String message) { diff --git a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java index 7a03812..1686b5b 100644 --- a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java +++ b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java @@ -37,7 +37,7 @@ public class PlayerHandlers implements Listener { return; } - Collection recipients = switch (Main.USER_TYPE.asMap().getOrDefault(player, hasFull ? Type.FULL : Type.SINGLE) ) { + Collection recipients = switch (Main.USER_TYPE.asMap().getOrDefault(player, hasFull ? Type.FULL : Type.SINGLE)) { case SINGLE -> Collections.singletonList(player); case FULL, BROADCAST -> e.getRecipients(); };