更换jodd-http,进一步压缩插件大小

This commit is contained in:
ddaodan 2024-07-12 19:32:44 +08:00
parent 65665d88df
commit 293c9257aa
2 changed files with 17 additions and 27 deletions

View File

@ -20,8 +20,7 @@ repositories {
dependencies { dependencies {
compileOnly "org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT" compileOnly "org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT"
implementation 'org.apache.httpcomponents:httpclient:4.5.14' implementation 'org.jodd:jodd-http:6.3.0'
//implementation 'org.apache.httpcomponents.client5:httpclient5:5.4-beta1'
implementation 'org.json:json:20231013' implementation 'org.json:json:20231013'
} }

View File

@ -3,12 +3,6 @@ package com.ddaodan.MineChatGPT;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -16,6 +10,8 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.List; import java.util.List;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
public class CommandHandler implements CommandExecutor { public class CommandHandler implements CommandExecutor {
private final Main plugin; private final Main plugin;
private final ConfigManager configManager; private final ConfigManager configManager;
@ -97,26 +93,21 @@ public class CommandHandler implements CommandExecutor {
messages.put(message); messages.put(message);
json.put("messages", messages); json.put("messages", messages);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpRequest request = HttpRequest.post(configManager.getBaseUrl() + "/chat/completions")
HttpPost request = new HttpPost(configManager.getBaseUrl() + "/chat/completions"); .header("Content-Type", "application/json")
request.setHeader("Content-Type", "application/json"); .header("Authorization", "Bearer " + configManager.getApiKey())
request.setHeader("Authorization", "Bearer " + configManager.getApiKey()); .body(json.toString());
request.setEntity(new StringEntity(json.toString(), "UTF-8"));
try (CloseableHttpResponse response = httpClient.execute(request)) { HttpResponse response = request.send();
if (response.getStatusLine().getStatusCode() == 200) {
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); if (response.statusCode() == 200) {
JSONObject jsonResponse = new JSONObject(responseBody); String responseBody = response.bodyText();
String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"); JSONObject jsonResponse = new JSONObject(responseBody);
sender.sendMessage(configManager.getChatGPTResponseMessage().replace("%s", answer)); String answer = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
} else { sender.sendMessage(configManager.getChatGPTResponseMessage().replace("%s", answer));
String errorBody = EntityUtils.toString(response.getEntity(), "UTF-8"); } else {
logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody); String errorBody = response.bodyText();
sender.sendMessage(configManager.getChatGPTErrorMessage()); logger.log(Level.SEVERE, "Failed to get a response from ChatGPT: " + errorBody);
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to contact ChatGPT: " + e.getMessage(), e);
sender.sendMessage(configManager.getChatGPTErrorMessage()); sender.sendMessage(configManager.getChatGPTErrorMessage());
} }
} }