Skip to content

Commit cc12811

Browse files
committed
Server status enum
1 parent 7051860 commit cc12811

File tree

7 files changed

+148
-32
lines changed

7 files changed

+148
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
## Java 11
33
This library now requires Java 11 or higher.
44

5+
## ServerStatus
6+
The `ServerStatus` class is now an enum instead of a class with static `int` fields. Each status has a numeric
7+
value (`getValue`), a display name (`getName`) and a brand color (`getColor`).
8+
9+
If a status code is unknown because the API client has not been updated `OFFLINE` will be returned.
10+
511
### SLF4J Implementation
612
This library no longer depends directly on any SLF4J implementation. If you want to see log messages
713
from this library, you must include an SLF4J implementation in your project.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.exaroton.api;
2+
3+
public enum BrandColor {
4+
DARK(0x0f0f0f),
5+
LIGHT(0xffffff),
6+
MAIN(0x19ba19),
7+
DANGER(0xf91c1c),
8+
SUCCESS(MAIN.rgb),
9+
WARN(0xf97f12),
10+
LOADING(0x4c4c4c),
11+
;
12+
13+
private final int rgb;
14+
15+
BrandColor(int rgb) {
16+
this.rgb = rgb;
17+
}
18+
19+
public int getRGB() {
20+
return rgb;
21+
}
22+
}

src/main/java/com/exaroton/api/ExarotonClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ public List<Server> getServers() throws APIException {
215215
List<Server> servers = request.request().getData();
216216
for (Server server: servers) {
217217
server.init(this, gson);
218-
server.fetched = true;
219218
}
220219
return servers;
221220
}

src/main/java/com/exaroton/api/billing/pools/CreditPool.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ public List<Server> getServerList() throws APIException {
219219
List<Server> servers = request.request().getData();
220220
for (Server server: servers) {
221221
server.init(this.client, gson);
222-
server.fetched = true;
223222
}
224223
return servers;
225224
}

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
import com.exaroton.api.ws.WebSocketManager;
77
import com.exaroton.api.ws.subscriber.*;
88
import com.google.gson.Gson;
9+
import org.jetbrains.annotations.ApiStatus;
910
import org.jetbrains.annotations.NotNull;
1011

1112
import java.util.List;
1213
import java.util.Objects;
14+
import java.util.Set;
1315

14-
public class Server {
16+
public final class Server {
1517

1618
/**
1719
* has this server been fetched from the API yet
1820
*/
19-
public boolean fetched;
21+
private boolean fetched;
2022

2123
/**
2224
* Unique server ID
@@ -100,6 +102,15 @@ public Server(@NotNull ExarotonClient client, @NotNull Gson gson, @NotNull Strin
100102
this.id = Objects.requireNonNull(id);
101103
}
102104

105+
/**
106+
* Check if this server has been fetched from the API.
107+
* If not, some methods may not work as expected.
108+
* @return true if the server has been fetched
109+
*/
110+
public boolean isFetched() {
111+
return fetched;
112+
}
113+
103114
/**
104115
* Get the server id
105116
*
@@ -129,26 +140,30 @@ public String getAddress() {
129140

130141
/**
131142
* Get the current server status
132-
* see ServerStatus
133143
*
134-
* @return status code (see ServerStatus)
144+
* @return status or OFFLINE if the status is unknown
135145
*/
136-
public int getStatus() {
137-
return status;
146+
public ServerStatus getStatus() {
147+
return ServerStatus.fromValue(status).orElse(ServerStatus.OFFLINE);
138148
}
139149

140150
/**
141151
* check if the server has this status
142152
*
143-
* @param statusCodes status codes (see {@link ServerStatus})
144-
* @return status match
153+
* @param status status
154+
* @return true if the status matches
145155
*/
146-
public boolean hasStatus(int... statusCodes) {
147-
if (statusCodes == null) throw new IllegalArgumentException("Invalid status code array");
148-
for (int statusCode : statusCodes) {
149-
if (this.status == statusCode) return true;
150-
}
151-
return false;
156+
public boolean hasStatus(ServerStatus... status) {
157+
return hasStatus(Set.of(status));
158+
}
159+
160+
/**
161+
* check if the server has one of the given statuses
162+
* @param status status
163+
* @return true if the status matches
164+
*/
165+
public boolean hasStatus(Set<ServerStatus> status) {
166+
return Objects.requireNonNull(status).contains(this.getStatus());
152167
}
153168

154169
/**
@@ -400,7 +415,7 @@ public Server setFromObject(Server server) {
400415
this.name = server.getName();
401416
this.address = server.getAddress();
402417
this.motd = server.getMotd();
403-
this.status = server.getStatus();
418+
this.status = server.getStatus().getValue();
404419
this.players = server.getPlayerInfo();
405420
this.host = server.getHost();
406421
this.port = server.getPort();
@@ -410,14 +425,16 @@ public Server setFromObject(Server server) {
410425
}
411426

412427
/**
413-
* set the exaroton client used for requests and the gson instance
428+
* Set the exaroton client used for requests and the gson instance. This method assumes the server is already fetched.
414429
*
415430
* @param client exaroton client used for new requests
416431
* @param gson gson instance used for (de-)serialization
417432
*/
433+
@ApiStatus.Internal
418434
public void init(@NotNull ExarotonClient client, @NotNull Gson gson) {
419435
this.client = Objects.requireNonNull(client);
420436
this.gson = Objects.requireNonNull(gson);
437+
this.fetched = true;
421438
}
422439

423440
/**
Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
package com.exaroton.api.server;
22

3-
public class ServerStatus {
4-
public static final int OFFLINE = 0;
5-
public static final int ONLINE = 1;
6-
public static final int STARTING = 2;
7-
public static final int STOPPING = 3;
8-
public static final int RESTARTING = 4;
9-
public static final int SAVING = 5;
10-
public static final int LOADING = 6;
11-
public static final int CRASHED = 7;
12-
public static final int PENDING = 8;
13-
public static final int TRANSFERRING = 9;
14-
public static final int PREPARING = 10;
3+
import com.exaroton.api.BrandColor;
4+
5+
import java.util.Optional;
6+
7+
/**
8+
* Server status
9+
*/
10+
public enum ServerStatus {
11+
OFFLINE(0, "Offline", BrandColor.DANGER),
12+
ONLINE(1, "Online", BrandColor.SUCCESS),
13+
STARTING(2, "Starting", BrandColor.LOADING),
14+
STOPPING(3, "Stopping", BrandColor.LOADING),
15+
RESTARTING(4, "Restarting", BrandColor.LOADING),
16+
SAVING(5, "Saving", BrandColor.LOADING),
17+
LOADING(6, "Loading", BrandColor.LOADING),
18+
CRASHED(7, "Crashed", BrandColor.DANGER),
19+
PENDING(8, "Pending", BrandColor.LOADING),
20+
TRANSFERRING(9, "Transferring", BrandColor.LOADING),
21+
PREPARING(10, "Preparing", BrandColor.LOADING),
22+
;
23+
24+
private final int value;
25+
private final String name;
26+
private final BrandColor color;
27+
28+
ServerStatus(int value, String name, BrandColor color) {
29+
this.value = value;
30+
this.name = name;
31+
this.color = color;
32+
}
33+
34+
/**
35+
* Get the status by its numeric value
36+
* @param value numeric value
37+
* @return Empty if not found
38+
*/
39+
public static Optional<ServerStatus> fromValue(int value) {
40+
for (ServerStatus status : values()) {
41+
if (status.value == value) {
42+
return Optional.of(status);
43+
}
44+
}
45+
return Optional.empty();
46+
}
47+
48+
/**
49+
* Get the numeric value of the status
50+
* @return numeric value
51+
*/
52+
public int getValue() {
53+
return value;
54+
}
55+
56+
/**
57+
* Get the name of the status
58+
* @return name
59+
*/
60+
public String getName() {
61+
return name;
62+
}
63+
64+
/**
65+
* Get the color of the status
66+
* @return color
67+
*/
68+
public BrandColor getColor() {
69+
return color;
70+
}
1571
}

src/main/java/com/exaroton/api/ws/WebSocketManager.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.exaroton.api.ws;
22

33
import com.exaroton.api.server.Server;
4+
import com.exaroton.api.server.ServerStatus;
45
import com.exaroton.api.ws.data.*;
56
import com.exaroton.api.ws.stream.*;
67
import com.exaroton.api.ws.subscriber.*;
@@ -300,8 +301,24 @@ public void close() {
300301
this.client.close();
301302
}
302303

303-
public boolean serverHasStatus(int... status) {
304-
if (!this.server.fetched) {
304+
/**
305+
* check if the server has this status
306+
*
307+
* @param status status
308+
* @return true if the status matches
309+
*/
310+
public boolean serverHasStatus(ServerStatus... status) {
311+
return serverHasStatus(Set.of(status));
312+
}
313+
314+
/**
315+
* check if the server has one of the given statuses
316+
*
317+
* @param status status
318+
* @return true if the status matches
319+
*/
320+
public boolean serverHasStatus(Set<ServerStatus> status) {
321+
if (!this.server.isFetched()) {
305322
try {
306323
this.server.get();
307324
} catch (Exception ignored) {

0 commit comments

Comments
 (0)