Add #getMojangStatus to the Java SDK
All checks were successful
Publish Java SDK / docker (17, 3.8.5) (push) Successful in 33s

This commit is contained in:
Braydon 2024-04-24 15:19:52 -04:00
parent f08ff7602f
commit 77e5daa375
4 changed files with 149 additions and 0 deletions

@ -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);
}
}

@ -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 <T extends MinecraftServer> CompletableFuture<T> 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<Boolean> 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<MojangServerStatus> getMojangStatus() {
return CompletableFuture.supplyAsync(this::sendGetMojangStatusRequest);
}
}

@ -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 extends MinecraftServer> 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();
}
}

@ -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
}
}