-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Brettanda/main
Added support for Crafty 4
- Loading branch information
Showing
4 changed files
with
143 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/com/kamesuta/bungeepteropower/power/CraftyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.kamesuta.bungeepteropower.power; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.kamesuta.bungeepteropower.api.PowerController; | ||
import com.kamesuta.bungeepteropower.api.PowerSignal; | ||
|
||
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.concurrent.CompletionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Level; | ||
|
||
import static com.kamesuta.bungeepteropower.BungeePteroPower.logger; | ||
import static com.kamesuta.bungeepteropower.BungeePteroPower.plugin; | ||
|
||
/** | ||
* Crafty API client. | ||
*/ | ||
public class CraftyController implements PowerController { | ||
/** | ||
* Send a power signal to the Crafty server. | ||
* | ||
* @param serverName The name of the server to start | ||
* @param serverId The Crafty server ID | ||
* @param signalType The power signal to send | ||
* @return A future that completes when the request is finished | ||
*/ | ||
@Override | ||
public CompletableFuture<Void> sendPowerSignal(String serverName, String serverId, PowerSignal signalType) { | ||
String signal = signalType.getSignal(); | ||
String action = signalType == PowerSignal.START ? "start_server" : "stop_server"; | ||
logger.info(String.format("%s server: %s (Crafty server ID: %s)", action, serverName, serverId)); | ||
|
||
// Create a path | ||
String path = "/api/v2/servers/" + serverId + "/action/" + action; | ||
|
||
HttpClient client = HttpClient.newHttpClient(); | ||
|
||
// Create a request | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(plugin.config.craftyUrl.resolve(path).toString())) | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", "Bearer " + plugin.config.craftyApiKey) | ||
.POST(HttpRequest.BodyPublishers.ofString("")) | ||
.build(); | ||
|
||
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenApply(status -> { | ||
int code = status.statusCode(); | ||
logger.info(status.toString()); | ||
if (code == 200) { | ||
logger.info("Successfully sent " + signal + " signal to the server: " + serverName); | ||
return (Void) null; | ||
} else { | ||
String message = "Failed to send " + signal + " signal to the server: " + serverName + ". Response code: " + code; | ||
logger.warning(message); | ||
logger.info("Request: " + request + ", Response: " + code + " " + status.body()); | ||
throw new RuntimeException(message); | ||
} | ||
}) | ||
.exceptionally(e -> { | ||
logger.log(Level.WARNING, "Failed to send " + action + " signal to the server: " + serverName, e); | ||
throw new CompletionException(e); | ||
}); | ||
} | ||
|
||
/** | ||
* Restore from a backup. | ||
* Send a stop signal to the server, wait until the server is offline, and then | ||
* restore from a backup. | ||
* | ||
* @param serverName The name of the server | ||
* @param serverId The Crafty server ID | ||
* @param backupName The file name of the backup | ||
* @return A future that completes when the request to restore from the backup | ||
* is sent after the server becomes offline | ||
*/ | ||
@Override | ||
public CompletableFuture<Void> sendRestoreSignal(String serverName, String serverId, String backupName) { | ||
throw new UnsupportedOperationException( | ||
"Feature incomplete at this time. The Crafty 4 Controller API doesn't provide restore function."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters