From 77e5daa3751a8255bbf3e8cb71c9d44f9b4e9d6b Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Wed, 24 Apr 2024 15:19:52 -0400 Subject: [PATCH] Add #getMojangStatus to the Java SDK --- .../restfulmc/sdk/command/ClientCommands.java | 28 +++++++ .../sdk/command/impl/AsyncClientCommands.java | 23 ++++++ .../sdk/command/impl/SyncClientCommands.java | 23 ++++++ .../sdk/response/MojangServerStatus.java | 75 +++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 Java-SDK/src/main/java/cc/restfulmc/sdk/response/MojangServerStatus.java diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java index 45862b8..370a125 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/ClientCommands.java @@ -26,8 +26,10 @@ package cc.restfulmc.sdk.command; import cc.restfulmc.sdk.client.ClientConfig; import cc.restfulmc.sdk.exception.RESTfulMCAPIException; import cc.restfulmc.sdk.request.APIWebRequest; +import cc.restfulmc.sdk.response.MojangServerStatus; import cc.restfulmc.sdk.response.Player; import cc.restfulmc.sdk.response.server.MinecraftServer; +import com.google.gson.JsonObject; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; @@ -76,4 +78,30 @@ public abstract class ClientCommands { .endpoint(config.getApiEndpoint() + "/server/" + platform.name() + "/" + hostname) .build().execute(platform.getServerClass()); } + + /** + * Check if the server with the + * given hostname is blocked by Mojang. + * + * @param hostname the hostname of the server + * @return whether the server is blocked + * @throws RESTfulMCAPIException if an api error occurs + */ + protected final boolean sendIsServerBlockedRequest(@NonNull String hostname) throws RESTfulMCAPIException { + return APIWebRequest.builder() + .endpoint(config.getApiEndpoint() + "/server/blocked/" + hostname) + .build().execute(JsonObject.class).get("blocked").getAsBoolean(); + } + + /** + * Get the status of Mojang servers. + * + * @return the status of Mojang servers + * @throws RESTfulMCAPIException if an api error occurs + */ + protected final MojangServerStatus sendGetMojangStatusRequest() throws RESTfulMCAPIException { + return APIWebRequest.builder() + .endpoint(config.getApiEndpoint() + "/mojang/status") + .build().execute(MojangServerStatus.class); + } } \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java index f80e157..e1f0ed3 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/AsyncClientCommands.java @@ -27,6 +27,7 @@ import cc.restfulmc.sdk.client.ClientConfig; import cc.restfulmc.sdk.client.RESTfulMCClient; import cc.restfulmc.sdk.command.ClientCommands; import cc.restfulmc.sdk.exception.RESTfulMCAPIException; +import cc.restfulmc.sdk.response.MojangServerStatus; import cc.restfulmc.sdk.response.Player; import cc.restfulmc.sdk.response.server.MinecraftServer; import lombok.NonNull; @@ -68,4 +69,26 @@ public final class AsyncClientCommands extends ClientCommands { public CompletableFuture getMinecraftServer(@NonNull MinecraftServer.Platform platform, @NonNull String hostname) { return CompletableFuture.supplyAsync(() -> sendGetMinecraftServerRequest(platform, hostname)); } + + /** + * Check if the server with the + * given hostname is blocked by Mojang. + * + * @param hostname the hostname of the server + * @return whether the server is blocked + * @throws RESTfulMCAPIException in the future if an api error occurs + */ + public CompletableFuture isMojangBlocked(@NonNull String hostname) { + return CompletableFuture.supplyAsync(() -> sendIsServerBlockedRequest(hostname)); + } + + /** + * Get the status of Mojang servers. + * + * @return the status of Mojang servers + * @throws RESTfulMCAPIException in the future if an api error occurs + */ + public CompletableFuture getMojangStatus() { + return CompletableFuture.supplyAsync(this::sendGetMojangStatusRequest); + } } \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java index b444ddd..5017041 100644 --- a/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/command/impl/SyncClientCommands.java @@ -27,6 +27,7 @@ import cc.restfulmc.sdk.client.ClientConfig; import cc.restfulmc.sdk.client.RESTfulMCClient; import cc.restfulmc.sdk.command.ClientCommands; import cc.restfulmc.sdk.exception.RESTfulMCAPIException; +import cc.restfulmc.sdk.response.MojangServerStatus; import cc.restfulmc.sdk.response.Player; import cc.restfulmc.sdk.response.server.MinecraftServer; import lombok.NonNull; @@ -66,4 +67,26 @@ public final class SyncClientCommands extends ClientCommands { public T getMinecraftServer(@NonNull MinecraftServer.Platform platform, @NonNull String hostname) throws RESTfulMCAPIException { return sendGetMinecraftServerRequest(platform, hostname); } + + /** + * Check if the server with the + * given hostname is blocked by Mojang. + * + * @param hostname the hostname of the server + * @return whether the server is blocked + * @throws RESTfulMCAPIException if an api error occurs + */ + public boolean isMojangBlocked(@NonNull String hostname) throws RESTfulMCAPIException { + return sendIsServerBlockedRequest(hostname); + } + + /** + * Get the status of Mojang servers. + * + * @return the status of Mojang servers + * @throws RESTfulMCAPIException if an api error occurs + */ + public MojangServerStatus getMojangStatus() throws RESTfulMCAPIException { + return sendGetMojangStatusRequest(); + } } \ No newline at end of file diff --git a/Java-SDK/src/main/java/cc/restfulmc/sdk/response/MojangServerStatus.java b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/MojangServerStatus.java new file mode 100644 index 0000000..2b80c41 --- /dev/null +++ b/Java-SDK/src/main/java/cc/restfulmc/sdk/response/MojangServerStatus.java @@ -0,0 +1,75 @@ +/* + * MIT License + * + * Copyright (c) 2024 Braydon (Rainnny). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package cc.restfulmc.sdk.response; + +import lombok.*; + +/** + * @author Braydon + */ +@AllArgsConstructor @Getter @EqualsAndHashCode(onlyExplicitlyIncluded = true) @ToString +public final class MojangServerStatus { + /** + * The servers to show the status of. + */ + @NonNull private final MojangServer[] servers; + + @AllArgsConstructor @Getter @ToString + public static class MojangServer { + /** + * The name of this server. + */ + @NonNull private final String name; + + /** + * The name of this server. + */ + @NonNull private final String endpoint; + + /** + * The status of this server. + */ + @NonNull private final Status status; + } + + /** + * The status of a service. + */ + public enum Status { + /** + * The service is online and accessible. + */ + ONLINE, + + /** + * The service is online, but is experiencing degraded performance. + */ + DEGRADED, + + /** + * The service is offline and inaccessible. + */ + OFFLINE + } +} \ No newline at end of file