Custom API Key, config and permission
This commit is contained in:
parent
3414059055
commit
131a726da7
@ -16,16 +16,19 @@ public class Main extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
saveDefaultConfig();
|
||||||
|
|
||||||
|
OpenAI.init(getConfig().getString("API_KEY"));
|
||||||
|
|
||||||
cache = CacheBuilder.newBuilder()
|
cache = CacheBuilder.newBuilder()
|
||||||
.expireAfterWrite(30, TimeUnit.MINUTES)
|
.expireAfterWrite(30, TimeUnit.MINUTES)
|
||||||
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
|
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
|
||||||
if (notification.getCause() == RemovalCause.EXPIRED && notification.getKey() != null) {
|
if (notification.getCause() == RemovalCause.EXPIRED && notification.getKey() != null) {
|
||||||
notification.getKey().sendMessage("ChatGPT has disconnected.");
|
notification.getKey().sendMessage(getConfig().getString("command.toggle.disabled").replace("&", "§"));
|
||||||
}
|
}
|
||||||
}).build();
|
}).build();
|
||||||
|
|
||||||
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
|
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
|
||||||
|
|
||||||
getCommand("chatgpt").setExecutor(new GPTCommand(this));
|
getCommand("chatgpt").setExecutor(new GPTCommand(this));
|
||||||
|
|
||||||
getLogger().info("Plugin enabled!");
|
getLogger().info("Plugin enabled!");
|
||||||
|
@ -8,7 +8,11 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
|
|
||||||
public class OpenAI {
|
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<String> getResponse(StringBuilder cached, String message) {
|
public static CompletableFuture<String> getResponse(StringBuilder cached, String message) {
|
||||||
cached.append("\nHuman:").append(message).append("\nAI:");
|
cached.append("\nHuman:").append(message).append("\nAI:");
|
||||||
|
@ -21,14 +21,19 @@ public class GPTCommand implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!player.hasPermission("minecraftgpt.command")) {
|
||||||
|
player.sendMessage(plugin.getConfig().getString("command.no-permission").replace("&", "§"));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (plugin.getCache().asMap().containsKey(player)) {
|
if (plugin.getCache().asMap().containsKey(player)) {
|
||||||
plugin.getCache().invalidate(player);
|
plugin.getCache().invalidate(player);
|
||||||
player.sendMessage(ChatColor.RED + "ChatGPT has disconnected.");
|
player.sendMessage(plugin.getConfig().getString("command.toggle.disabled").replace("&", "§"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.getCache().put(player, new StringBuilder());
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import org.bukkit.event.Listener;
|
|||||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PlayerHandlers implements Listener {
|
public class PlayerHandlers implements Listener {
|
||||||
|
|
||||||
@ -27,14 +29,16 @@ public class PlayerHandlers implements Listener {
|
|||||||
if (this.plugin.getCache().asMap().containsKey(player)) {
|
if (this.plugin.getCache().asMap().containsKey(player)) {
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
|
|
||||||
player.sendMessage(ChatColor.AQUA + "You: " + ChatColor.GRAY + e.getMessage());
|
List<String> 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) -> {
|
OpenAI.getResponse(plugin.getCache().getIfPresent(player), e.getMessage()).whenComplete((response, throwable) -> {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throwable.printStackTrace();
|
throwable.printStackTrace();
|
||||||
player.sendMessage(ChatColor.RED + "An error occurred while processing your message.");
|
player.sendMessage(plugin.getConfig().getString("command.error").replace("&", "§"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
player.sendMessage(ChatColor.AQUA + "AI: " + ChatColor.GREEN + response);
|
player.sendMessage(list.get(1).replace("&", "§").replace("%message%", response));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/main/resources/config.yml
Normal file
12
src/main/resources/config.yml
Normal file
@ -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: ""
|
Loading…
Reference in New Issue
Block a user