Skip to content

Commit c3795f5

Browse files
committed
Switch Okhttp to java.net.http.HttpClient to reduce plugin binary size
1 parent 4169832 commit c3795f5

File tree

4 files changed

+40
-62
lines changed

4 files changed

+40
-62
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ https://github.com/Kamesuta/BungeePteroPower/assets/16362824/4f36d65c-ca9f-4dd2-
1717

1818
- You can download it from [GitHub Releases](https://github.com/Kamesuta/BungeePteroPower/releases).
1919

20+
## Requirements
21+
22+
- Java 11 or higher
23+
- uses java.net.http.HttpClient for Pterodactyl API
24+
2025
## Installation
2126

2227
1. Obtain an API key in the Pterodactyl panel.

README_ja.md

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ https://github.com/Kamesuta/BungeePteroPower/assets/16362824/4f36d65c-ca9f-4dd2-
1717

1818
- [GitHub Releases](https://github.com/Kamesuta/BungeePteroPower/releases) からダウンロードできます。
1919

20+
## 必要要件
21+
22+
- Java 11 以上
23+
- PterodactylのAPIを使用するため、java.net.http.HttpClientを使用しています。
24+
2025
## インストール
2126

2227
1. PterodactylパネルでAPIキーを取得します。

pom.xml

+2-31
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kamesuta</groupId>
88
<artifactId>BungeePteroPower</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.1-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>BungeePteroPower</name>
@@ -16,7 +16,7 @@
1616
efficiently.
1717
</description>
1818
<properties>
19-
<java.version>1.8</java.version>
19+
<java.version>11</java.version>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
</properties>
2222

@@ -42,30 +42,6 @@
4242
<goals>
4343
<goal>shade</goal>
4444
</goals>
45-
<configuration>
46-
<relocations>
47-
<relocation>
48-
<pattern>okhttp3</pattern>
49-
<shadedPattern>com.kamesuta.bungeepteropower.lib.okhttp3</shadedPattern>
50-
</relocation>
51-
<relocation>
52-
<pattern>okio</pattern>
53-
<shadedPattern>com.kamesuta.bungeepteropower.lib.okio</shadedPattern>
54-
</relocation>
55-
<relocation>
56-
<pattern>kotlin</pattern>
57-
<shadedPattern>com.kamesuta.bungeepteropower.lib.kotlin</shadedPattern>
58-
</relocation>
59-
<relocation>
60-
<pattern>org.jetbrains.annotations</pattern>
61-
<shadedPattern>com.kamesuta.bungeepteropower.lib.jetbrains.annotations</shadedPattern>
62-
</relocation>
63-
<relocation>
64-
<pattern>org.intellij.lang.annotations</pattern>
65-
<shadedPattern>com.kamesuta.bungeepteropower.lib.intellij.annotations</shadedPattern>
66-
</relocation>
67-
</relocations>
68-
</configuration>
6945
</execution>
7046
</executions>
7147
</plugin>
@@ -96,10 +72,5 @@
9672
<version>1.20-R0.1-SNAPSHOT</version>
9773
<scope>provided</scope>
9874
</dependency>
99-
<dependency>
100-
<groupId>com.squareup.okhttp3</groupId>
101-
<artifactId>okhttp</artifactId>
102-
<version>4.12.0</version>
103-
</dependency>
10475
</dependencies>
10576
</project>

src/main/java/com/kamesuta/bungeepteropower/PterodactylAPI.java

+28-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.kamesuta.bungeepteropower;
22

3-
import okhttp3.*;
4-
import org.jetbrains.annotations.NotNull;
5-
6-
import java.io.IOException;
3+
import java.net.URI;
4+
import java.net.http.HttpClient;
5+
import java.net.http.HttpRequest;
6+
import java.net.http.HttpResponse;
77
import java.util.concurrent.CompletableFuture;
88
import java.util.logging.Level;
99

@@ -29,42 +29,39 @@ public static CompletableFuture<Void> sendPowerSignal(String serverName, String
2929
// Create a path
3030
String path = "/api/client/servers/" + pterodactylServerId + "/power";
3131

32-
OkHttpClient client = new OkHttpClient();
32+
HttpClient client = HttpClient.newHttpClient();
3333

3434
// Create a form body to send power signal
35-
FormBody.Builder formBuilder = new FormBody.Builder();
36-
formBuilder.add("signal", signal.signal);
37-
RequestBody formBody = formBuilder.build();
35+
String formBody = "signal=" + signal.signal;
3836

3937
// Create a request
40-
Request request = new Request.Builder()
41-
.url(plugin.config.pterodactylUrl.resolve(path).toString())
42-
.post(formBody)
43-
.addHeader("Authorization", "Bearer " + plugin.config.pterodactylToken)
38+
HttpRequest request = HttpRequest.newBuilder()
39+
.uri(URI.create(plugin.config.pterodactylUrl.resolve(path).toString()))
40+
.header("Content-Type", "application/x-www-form-urlencoded")
41+
.header("Authorization", "Bearer " + plugin.config.pterodactylToken)
42+
.POST(HttpRequest.BodyPublishers.ofString(formBody))
4443
.build();
4544

4645
// Execute request and register a callback
4746
CompletableFuture<Void> future = new CompletableFuture<>();
48-
client.newCall(request).enqueue(new Callback() {
49-
@Override
50-
public void onFailure(@NotNull Call call, @NotNull IOException e) {
51-
logger.log(Level.WARNING, "Failed to " + signal.signal + " server: " + serverName, e);
52-
future.completeExceptionally(e);
53-
}
47+
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
48+
.thenApply(HttpResponse::statusCode)
49+
.thenAccept(code -> {
50+
if (code >= 200 && code < 300) {
51+
logger.info("Successfully " + signal.signal + " server: " + serverName);
52+
future.complete(null);
53+
} else {
54+
String message = "Failed to " + signal.signal + " server: " + serverName + ". Response code: " + code;
55+
logger.warning(message);
56+
future.completeExceptionally(new RuntimeException(message));
57+
}
58+
})
59+
.exceptionally(e -> {
60+
logger.log(Level.WARNING, "Failed to " + signal.signal + " server: " + serverName, e);
61+
future.completeExceptionally(e);
62+
return null;
63+
});
5464

55-
@Override
56-
public void onResponse(@NotNull Call call, @NotNull Response response) {
57-
if (response.isSuccessful()) {
58-
logger.info("Successfully " + signal.signal + " server: " + serverName);
59-
future.complete(null);
60-
} else {
61-
String message = "Failed to " + signal.signal + " server: " + serverName + ". Response: " + response;
62-
logger.warning(message);
63-
future.completeExceptionally(new RuntimeException(message));
64-
}
65-
response.close();
66-
}
67-
});
6865
return future;
6966
}
7067

0 commit comments

Comments
 (0)