From c3795f559ac1057ede7539988f0d7ca32bceb8d3 Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Thu, 1 Feb 2024 01:38:23 +0900 Subject: [PATCH] Switch Okhttp to java.net.http.HttpClient to reduce plugin binary size --- README.md | 5 ++ README_ja.md | 5 ++ pom.xml | 33 +---------- .../bungeepteropower/PterodactylAPI.java | 59 +++++++++---------- 4 files changed, 40 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 9b04a23..85534a4 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,11 @@ https://github.com/Kamesuta/BungeePteroPower/assets/16362824/4f36d65c-ca9f-4dd2- - You can download it from [GitHub Releases](https://github.com/Kamesuta/BungeePteroPower/releases). +## Requirements + +- Java 11 or higher + - uses java.net.http.HttpClient for Pterodactyl API + ## Installation 1. Obtain an API key in the Pterodactyl panel. diff --git a/README_ja.md b/README_ja.md index b0066d4..0c29308 100644 --- a/README_ja.md +++ b/README_ja.md @@ -17,6 +17,11 @@ https://github.com/Kamesuta/BungeePteroPower/assets/16362824/4f36d65c-ca9f-4dd2- - [GitHub Releases](https://github.com/Kamesuta/BungeePteroPower/releases) からダウンロードできます。 +## 必要要件 + +- Java 11 以上 + - PterodactylのAPIを使用するため、java.net.http.HttpClientを使用しています。 + ## インストール 1. PterodactylパネルでAPIキーを取得します。 diff --git a/pom.xml b/pom.xml index 68959af..7ccbe2f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.kamesuta BungeePteroPower - 1.0-SNAPSHOT + 1.1-SNAPSHOT jar BungeePteroPower @@ -16,7 +16,7 @@ efficiently. - 1.8 + 11 UTF-8 @@ -42,30 +42,6 @@ shade - - - - okhttp3 - com.kamesuta.bungeepteropower.lib.okhttp3 - - - okio - com.kamesuta.bungeepteropower.lib.okio - - - kotlin - com.kamesuta.bungeepteropower.lib.kotlin - - - org.jetbrains.annotations - com.kamesuta.bungeepteropower.lib.jetbrains.annotations - - - org.intellij.lang.annotations - com.kamesuta.bungeepteropower.lib.intellij.annotations - - - @@ -96,10 +72,5 @@ 1.20-R0.1-SNAPSHOT provided - - com.squareup.okhttp3 - okhttp - 4.12.0 - diff --git a/src/main/java/com/kamesuta/bungeepteropower/PterodactylAPI.java b/src/main/java/com/kamesuta/bungeepteropower/PterodactylAPI.java index 68c44da..ac27a20 100644 --- a/src/main/java/com/kamesuta/bungeepteropower/PterodactylAPI.java +++ b/src/main/java/com/kamesuta/bungeepteropower/PterodactylAPI.java @@ -1,9 +1,9 @@ package com.kamesuta.bungeepteropower; -import okhttp3.*; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; @@ -29,42 +29,39 @@ public static CompletableFuture sendPowerSignal(String serverName, String // Create a path String path = "/api/client/servers/" + pterodactylServerId + "/power"; - OkHttpClient client = new OkHttpClient(); + HttpClient client = HttpClient.newHttpClient(); // Create a form body to send power signal - FormBody.Builder formBuilder = new FormBody.Builder(); - formBuilder.add("signal", signal.signal); - RequestBody formBody = formBuilder.build(); + String formBody = "signal=" + signal.signal; // Create a request - Request request = new Request.Builder() - .url(plugin.config.pterodactylUrl.resolve(path).toString()) - .post(formBody) - .addHeader("Authorization", "Bearer " + plugin.config.pterodactylToken) + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(plugin.config.pterodactylUrl.resolve(path).toString())) + .header("Content-Type", "application/x-www-form-urlencoded") + .header("Authorization", "Bearer " + plugin.config.pterodactylToken) + .POST(HttpRequest.BodyPublishers.ofString(formBody)) .build(); // Execute request and register a callback CompletableFuture future = new CompletableFuture<>(); - client.newCall(request).enqueue(new Callback() { - @Override - public void onFailure(@NotNull Call call, @NotNull IOException e) { - logger.log(Level.WARNING, "Failed to " + signal.signal + " server: " + serverName, e); - future.completeExceptionally(e); - } + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::statusCode) + .thenAccept(code -> { + if (code >= 200 && code < 300) { + logger.info("Successfully " + signal.signal + " server: " + serverName); + future.complete(null); + } else { + String message = "Failed to " + signal.signal + " server: " + serverName + ". Response code: " + code; + logger.warning(message); + future.completeExceptionally(new RuntimeException(message)); + } + }) + .exceptionally(e -> { + logger.log(Level.WARNING, "Failed to " + signal.signal + " server: " + serverName, e); + future.completeExceptionally(e); + return null; + }); - @Override - public void onResponse(@NotNull Call call, @NotNull Response response) { - if (response.isSuccessful()) { - logger.info("Successfully " + signal.signal + " server: " + serverName); - future.complete(null); - } else { - String message = "Failed to " + signal.signal + " server: " + serverName + ". Response: " + response; - logger.warning(message); - future.completeExceptionally(new RuntimeException(message)); - } - response.close(); - } - }); return future; }