Skip to content

Commit dce2273

Browse files
committed
Add Endpoint to extend automatic server shutdown timer
1 parent 75289e9 commit dce2273

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
## Build System
2-
- Migrate to new Maven Central publishing system.
1+
## New Features
2+
- Add Endpoint to extend automatic server shutdown timer
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.exaroton.api.request.server;
2+
3+
import com.exaroton.api.APIResponse;
4+
import com.exaroton.api.ExarotonClient;
5+
import com.google.gson.Gson;
6+
import com.google.gson.reflect.TypeToken;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.net.http.HttpRequest;
10+
import java.util.HashMap;
11+
12+
public class ExtendServerStopTimeRequest extends ServerRequest<Void> {
13+
private final int time;
14+
15+
public ExtendServerStopTimeRequest(
16+
@NotNull ExarotonClient client,
17+
@NotNull Gson gson,
18+
@NotNull String serverId,
19+
int time
20+
) {
21+
super(client, gson, serverId);
22+
this.time = time;
23+
}
24+
25+
@Override
26+
protected String getEndpoint() {
27+
return "servers/{id}/extend-time";
28+
}
29+
30+
@Override
31+
protected String getMethod() {
32+
return "POST";
33+
}
34+
35+
@Override
36+
protected TypeToken<APIResponse<Void>> getType() {
37+
return new TypeToken<APIResponse<Void>>() {};
38+
}
39+
40+
@Override
41+
protected HttpRequest.BodyPublisher getBodyPublisher(Gson gson, HttpRequest.Builder builder) {
42+
HashMap<String, Integer> body = new HashMap<>();
43+
body.put("time", this.time);
44+
return this.jsonBodyPublisher(gson, builder, body);
45+
}
46+
}

src/main/java/com/exaroton/api/server/Server.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ public CompletableFuture<Void> executeCommand(@NotNull String command) throws IO
427427
return client.request(new ExecuteCommandRequest(this.client, this.gson, this.id, command));
428428
}
429429

430+
/**
431+
* Extend the server stop time by the given amount of minutes. This only works if the server is currently stopping.
432+
* @param time time in minutes to extend the stop time by
433+
* @return completable future that completes once the request has been sent
434+
* @throws IOException connection errors
435+
*/
436+
public CompletableFuture<Void> extendStopTime(int time) throws IOException {
437+
return client.request(new ExtendServerStopTimeRequest(this.client, this.gson, this.id, time));
438+
}
439+
430440
/**
431441
* Get a list of available player lists
432442
*

src/test/java/WebSocketTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void testStartServer() throws IOException, ExecutionException, InterruptedExcept
4747
assertEquals(server.getPort().get(), server.getSocketAddress().get().getPort());
4848
assertEquals(server.getHost().get(), server.getSocketAddress().get().getHostString());
4949

50+
testExtendStopTime();
5051
testHeapSubscriber();
5152
testStatsSubscriber();
5253
testTickSubscriber();
@@ -57,6 +58,10 @@ void testStartServer() throws IOException, ExecutionException, InterruptedExcept
5758
assertTrue(server.getWebSocket().isEmpty(), "Expected websocket to be closed");
5859
}
5960

61+
void testExtendStopTime() throws IOException {
62+
server.extendStopTime(5).join();
63+
}
64+
6065
void testHeapSubscriber() throws ExecutionException, InterruptedException, TimeoutException {
6166
CompletableFuture<HeapUsage> heapFuture = new CompletableFuture<>();
6267
HeapSubscriber heapSubscriber = heapFuture::complete;

0 commit comments

Comments
 (0)