From 131a726da73abf2f579157cb686dd52bfcbaa735 Mon Sep 17 00:00:00 2001 From: ohAlee Date: Mon, 2 Jan 2023 12:41:23 +0100 Subject: [PATCH] Custom API Key, config and permission --- src/main/java/it/ohalee/minecraftgpt/Main.java | 7 +++++-- src/main/java/it/ohalee/minecraftgpt/OpenAI.java | 6 +++++- .../it/ohalee/minecraftgpt/command/GPTCommand.java | 9 +++++++-- .../ohalee/minecraftgpt/handler/PlayerHandlers.java | 10 +++++++--- src/main/resources/config.yml | 12 ++++++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/it/ohalee/minecraftgpt/Main.java b/src/main/java/it/ohalee/minecraftgpt/Main.java index b813308..e67f976 100644 --- a/src/main/java/it/ohalee/minecraftgpt/Main.java +++ b/src/main/java/it/ohalee/minecraftgpt/Main.java @@ -16,16 +16,19 @@ public class Main extends JavaPlugin { @Override public void onEnable() { + saveDefaultConfig(); + + OpenAI.init(getConfig().getString("API_KEY")); + cache = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.MINUTES) .removalListener((RemovalListener) notification -> { if (notification.getCause() == RemovalCause.EXPIRED && notification.getKey() != null) { - notification.getKey().sendMessage("ChatGPT has disconnected."); + notification.getKey().sendMessage(getConfig().getString("command.toggle.disabled").replace("&", "§")); } }).build(); getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this); - getCommand("chatgpt").setExecutor(new GPTCommand(this)); getLogger().info("Plugin enabled!"); diff --git a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java index f33636f..5f6b712 100644 --- a/src/main/java/it/ohalee/minecraftgpt/OpenAI.java +++ b/src/main/java/it/ohalee/minecraftgpt/OpenAI.java @@ -8,7 +8,11 @@ import java.util.concurrent.CompletableFuture; public class OpenAI { - private static final OpenAiService service = new OpenAiService("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", 0); + private static OpenAiService service; + + public static void init(String key) { + service = new OpenAiService(key, 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/command/GPTCommand.java b/src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java index 4fc9e2a..348a9af 100644 --- a/src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java +++ b/src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java @@ -21,14 +21,19 @@ public class GPTCommand implements CommandExecutor { return true; } + if (!player.hasPermission("minecraftgpt.command")) { + player.sendMessage(plugin.getConfig().getString("command.no-permission").replace("&", "§")); + return true; + } + if (plugin.getCache().asMap().containsKey(player)) { plugin.getCache().invalidate(player); - player.sendMessage(ChatColor.RED + "ChatGPT has disconnected."); + player.sendMessage(plugin.getConfig().getString("command.toggle.disabled").replace("&", "§")); return true; } plugin.getCache().put(player, new StringBuilder()); - player.sendMessage(ChatColor.GREEN + "ChatGPT has connected. Say Hi!"); + player.sendMessage(plugin.getConfig().getString("command.toggle.enabled").replace("&", "§")); return true; } diff --git a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java index 40be8d8..6557aa6 100644 --- a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java +++ b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java @@ -11,6 +11,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerQuitEvent; +import java.util.List; + @RequiredArgsConstructor public class PlayerHandlers implements Listener { @@ -27,14 +29,16 @@ public class PlayerHandlers implements Listener { if (this.plugin.getCache().asMap().containsKey(player)) { e.setCancelled(true); - player.sendMessage(ChatColor.AQUA + "You: " + ChatColor.GRAY + e.getMessage()); + List list = plugin.getConfig().getStringList("command.format"); + + player.sendMessage(list.get(0).replace("&", "§").replace("%message%", 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."); + player.sendMessage(plugin.getConfig().getString("command.error").replace("&", "§")); return; } - player.sendMessage(ChatColor.AQUA + "AI: " + ChatColor.GREEN + response); + player.sendMessage(list.get(1).replace("&", "§").replace("%message%", response)); }); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..b2104b9 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,12 @@ +command: + no-permission: "&cYou do not have permission to use this command." + toggle: + enabled: "&aChatGPT has connected. Say Hi!" + disabled: "&cChatGPT has disconnected." + format: + - "&bYou: &7%message%" + - "&bAI: &a%message%" + error: "&cAn error occurred while processing your message." + +# https://beta.openai.com/account/api-keys +API_KEY: "" \ No newline at end of file