Skip to content

Commit

Permalink
Switch Okhttp to java.net.http.HttpClient to reduce plugin binary size
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta committed Jan 31, 2024
1 parent 4169832 commit c3795f5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 62 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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キーを取得します。
Expand Down
33 changes: 2 additions & 31 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.kamesuta</groupId>
<artifactId>BungeePteroPower</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BungeePteroPower</name>
Expand All @@ -16,7 +16,7 @@
efficiently.
</description>
<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand All @@ -42,30 +42,6 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>com.kamesuta.bungeepteropower.lib.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>com.kamesuta.bungeepteropower.lib.okio</shadedPattern>
</relocation>
<relocation>
<pattern>kotlin</pattern>
<shadedPattern>com.kamesuta.bungeepteropower.lib.kotlin</shadedPattern>
</relocation>
<relocation>
<pattern>org.jetbrains.annotations</pattern>
<shadedPattern>com.kamesuta.bungeepteropower.lib.jetbrains.annotations</shadedPattern>
</relocation>
<relocation>
<pattern>org.intellij.lang.annotations</pattern>
<shadedPattern>com.kamesuta.bungeepteropower.lib.intellij.annotations</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
Expand Down Expand Up @@ -96,10 +72,5 @@
<version>1.20-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
</dependencies>
</project>
59 changes: 28 additions & 31 deletions src/main/java/com/kamesuta/bungeepteropower/PterodactylAPI.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -29,42 +29,39 @@ public static CompletableFuture<Void> 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<Void> 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;
}

Expand Down

0 comments on commit c3795f5

Please sign in to comment.