1.0:插件发布

This commit is contained in:
2024-07-12 15:54:24 +08:00
commit 2b79f0e1e8
16 changed files with 926 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
package com.ddaodan.MineChatGPT;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.ChatColor;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List;
public class CommandHandler implements CommandExecutor {
private final Main plugin;
private final ConfigManager configManager;
private static final Logger logger = Logger.getLogger(CommandHandler.class.getName());
public CommandHandler(Main plugin, ConfigManager configManager) {
this.plugin = plugin;
this.configManager = configManager;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("chatgpt")) {
if (args.length == 0) {
sendHelpMessage(sender);
return true;
}
String subCommand = args[0];
if (subCommand.equalsIgnoreCase("reload")) {
if (!sender.hasPermission("minechatgpt.reload")) {
sender.sendMessage(ChatColor.RED + String.format(configManager.getNoPermissionMessage(), "minechatgpt.reload"));
return true;
}
configManager.reloadConfig();
sender.sendMessage(ChatColor.GREEN + configManager.getReloadMessage());
return true;
} else if (subCommand.equalsIgnoreCase("model")) {
if (!sender.hasPermission("minechatgpt.model")) {
sender.sendMessage(ChatColor.RED + String.format(configManager.getNoPermissionMessage(), "minechatgpt.model"));
return true;
}
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + configManager.getUsageMessage());
return true;
}
String model = args[1];
List<String> models = configManager.getModels();
if (models.contains(model)) {
// Logic to switch model
sender.sendMessage(ChatColor.GREEN + String.format(configManager.getModelSwitchMessage(), model));
} else {
sender.sendMessage(ChatColor.RED + configManager.getInvalidModelMessage());
}
return true;
} else if (subCommand.equalsIgnoreCase("modellist")) {
if (!sender.hasPermission("minechatgpt.modellist")) {
sender.sendMessage(ChatColor.RED + String.format(configManager.getNoPermissionMessage(), "minechatgpt.modellist"));
return true;
}
List<String> models = configManager.getModels();
sender.sendMessage(ChatColor.YELLOW + configManager.getAvailableModelsMessage());
for (String model : models) {
sender.sendMessage(ChatColor.YELLOW + "- " + model);
}
return true;
} else {
if (!sender.hasPermission("minechatgpt.use")) {
sender.sendMessage(ChatColor.RED + String.format(configManager.getNoPermissionMessage(), "minechatgpt.use"));
return true;
}
String question = String.join(" ", args);
// Logic to send question to ChatGPT
sender.sendMessage(ChatColor.AQUA + String.format(configManager.getQuestionMessage(), question));
askChatGPT(sender, question);
return true;
}
}
return false;
}
private void askChatGPT(CommandSender sender, String question) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
JSONObject json = new JSONObject();
json.put("model", configManager.getDefaultModel());
JSONArray messages = new JSONArray();
JSONObject message = new JSONObject();
message.put("role", "user");
message.put("content", question);
messages.put(message);
json.put("messages", messages);
RequestBody body = RequestBody.create(mediaType, json.toString());
Request request = new Request.Builder()
.url(configManager.getBaseUrl() + "/chat/completions")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + configManager.getApiKey())
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
logger.log(Level.SEVERE, "Failed to contact ChatGPT: " + e.getMessage(), e);
sender.sendMessage(ChatColor.RED + configManager.getChatGPTErrorMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseBody = response.body().string();
JSONObject jsonResponse = new JSONObject(responseBody);
String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
sender.sendMessage(ChatColor.AQUA + String.format(configManager.getChatGPTResponseMessage(), answer));
} else {
String errorBody = response.body().string();
logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody);
sender.sendMessage(ChatColor.RED + configManager.getChatGPTErrorMessage());
}
response.close();
}
});
}
private void sendHelpMessage(CommandSender sender) {
sender.sendMessage(ChatColor.YELLOW + configManager.getHelpMessage());
sender.sendMessage(ChatColor.YELLOW + configManager.getHelpAskMessage());
sender.sendMessage(ChatColor.YELLOW + configManager.getHelpReloadMessage());
sender.sendMessage(ChatColor.YELLOW + configManager.getHelpModelMessage());
sender.sendMessage(ChatColor.YELLOW + configManager.getHelpModelListMessage());
}
}

View File

@@ -0,0 +1,91 @@
package com.ddaodan.MineChatGPT;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.List;
public class ConfigManager {
private final Main plugin;
private FileConfiguration config;
public ConfigManager(Main plugin) {
this.plugin = plugin;
reloadConfig();
}
public void reloadConfig() {
plugin.reloadConfig();
config = plugin.getConfig();
}
public String getApiKey() {
return config.getString("api.key");
}
public String getBaseUrl() {
return config.getString("api.base_url");
}
public String getDefaultModel() {
return config.getString("default_model");
}
public String getReloadMessage() {
return config.getString("messages.reload");
}
public List<String> getModels() {
return config.getStringList("models");
}
public String getHelpMessage() {
return config.getString("messages.help");
}
public String getHelpAskMessage() {
return config.getString("messages.help_ask");
}
public String getHelpModelListMessage() {
return config.getString("messages.help_modellist");
}
public String getHelpReloadMessage() {
return config.getString("messages.help_reload");
}
public String getHelpModelMessage() {
return config.getString("messages.help_model");
}
public String getUsageMessage() {
return config.getString("messages.usage");
}
public String getModelSwitchMessage() {
return config.getString("messages.model_switch");
}
public String getChatGPTErrorMessage() {
return config.getString("messages.chatgpt_error");
}
public String getChatGPTResponseMessage() {
return config.getString("messages.chatgpt_response");
}
public String getQuestionMessage() {
return config.getString("messages.question");
}
public String getInvalidModelMessage() {
return config.getString("messages.invalid_model");
}
public String getAvailableModelsMessage() {
return config.getString("messages.available_models");
}
public String getNoPermissionMessage() {
return config.getString("messages.no_permission");
}
}

View File

@@ -0,0 +1,23 @@
package com.ddaodan.MineChatGPT;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects;
public final class Main extends JavaPlugin {
private ConfigManager configManager;
private CommandHandler commandHandler;
@Override
public void onEnable() {
saveDefaultConfig();
configManager = new ConfigManager(this);
commandHandler = new CommandHandler(this, configManager);
Objects.requireNonNull(getCommand("chatgpt")).setExecutor(commandHandler);
}
@Override
public void onDisable() {
saveConfig();
}
}

View File

@@ -0,0 +1,30 @@
# OpenAI API key setting
api:
# Your OpenAI API key, used for authentication
# To obtain an API key, visit https://platform.openai.com/account/api-keys and create a new API key
key: "sk-your_openai_api_key"
# The base URL for the OpenAI API, used to construct requests
base_url: "https://api.openai.com/v1"
# List of supported models
models:
# OpenAI ChatGPT
- "gpt-3.5-turbo"
- "gpt-4"
# The default model to use
default_model: "gpt-3.5-turbo"
# Message settings
messages:
reload: "Configuration reloaded successfully!"
help: "===== MineChatGPT Help ====="
help_ask: "/chatgpt <text> - Ask ChatGPT a question."
help_reload: "/chatgpt reload - Reload the configuration file."
help_model: "/chatgpt model <model_name> - Switch to a different model."
help_modellist: "/chatgpt modellist - List available models."
usage: "Usage: /chatgpt model <model_name>"
model_switch: "Model switched to %s"
chatgpt_error: "Failed to contact ChatGPT."
chatgpt_response: "ChatGPT: %s"
question: "Question: %s"
invalid_model: "Invalid model. Use /chatgpt modellist to see available models."
available_models: "Available models:"
no_permission: "You do not have permission to use this command. Required permission: %s"

View File

@@ -0,0 +1,30 @@
# API 相关设置
api:
# 你的 OpenAI API key用于身份验证
# 获取 API key 的方法:访问 //platform.openai.com/account/api-keys 并创建一个新的 API key
key: "your_openai_api_key"
# OpenAI API 的基础 URL用于构建请求
base_url: "https://api.openai.com/v1"
# 支持的模型列表
models:
# OpenAI ChatGPT
- "gpt-3.5-turbo"
- "gpt-4"
# 默认使用的模型
default_model: "gpt-3.5-turbo"
# 消息相关设置
messages:
reload: "已重新加载配置文件!"
help: "===== MineChatGPT 帮助 ====="
help_ask: "/chatgpt <text> - 向ChatGPT提问"
help_reload: "/chatgpt reload - 重新加载配置文件"
help_model: "/chatgpt model <model_name> - 切换至其他模型"
help_modellist: "/chatgpt modellist - 可用的模型列表"
usage: "输入: /chatgpt model <model_name>"
model_switch: "已切换至模型 %s"
chatgpt_error: "无法联系ChatGPT。"
chatgpt_response: "ChatGPT: %s"
question: "你: %s"
invalid_model: "模型无效。使用 /chatgpt modellist 查看可用模型。"
available_models: "可用模型列表:"
no_permission: "你没有权限使用这个指令。需要的权限:%s"

View File

@@ -0,0 +1,23 @@
name: MineChatGPT
version: '${version}'
main: com.ddaodan.MineChatGPT.Main
author: ddaodan
description: A Spigot plugin for interacting with ChatGPT
commands:
chatgpt:
description: Interact with ChatGPT
usage: /chatgpt <question>
permissions:
minechatgpt.use:
description: Allows using the /chatgpt command to interact with AI
default: true
minechatgpt.reload:
description: Allows reloading the configuration file
default: op
minechatgpt.model:
description: Allows switching models
default: op
minechatgpt.modellist:
description: Allows listing available models
default: op